summaryrefslogtreecommitdiff
path: root/Final/cpp/lib/common/sys/apr
diff options
context:
space:
mode:
authorRajith Muditha Attapattu <rajith@apache.org>2007-11-19 16:54:44 +0000
committerRajith Muditha Attapattu <rajith@apache.org>2007-11-19 16:54:44 +0000
commitd66aca6eb24b89052e7e2ab05ade9fcd5398bb95 (patch)
treeb576cec8a33157707585a5ca63b730765276ef69 /Final/cpp/lib/common/sys/apr
parent96420dfa6bb12a4b9fce082d50e49910e3dc8779 (diff)
downloadqpid-python-d66aca6eb24b89052e7e2ab05ade9fcd5398bb95.tar.gz
Undoing the accidental move instead of a copy
git-svn-id: https://svn.apache.org/repos/asf/incubator/qpid/tags/M2@596363 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'Final/cpp/lib/common/sys/apr')
-rw-r--r--Final/cpp/lib/common/sys/apr/APRAcceptor.cpp130
-rw-r--r--Final/cpp/lib/common/sys/apr/APRBase.cpp90
-rw-r--r--Final/cpp/lib/common/sys/apr/APRBase.h78
-rw-r--r--Final/cpp/lib/common/sys/apr/APRPool.cpp78
-rw-r--r--Final/cpp/lib/common/sys/apr/APRPool.h61
-rw-r--r--Final/cpp/lib/common/sys/apr/APRSocket.cpp77
-rw-r--r--Final/cpp/lib/common/sys/apr/APRSocket.h48
-rw-r--r--Final/cpp/lib/common/sys/apr/LFProcessor.cpp181
-rw-r--r--Final/cpp/lib/common/sys/apr/LFProcessor.h122
-rw-r--r--Final/cpp/lib/common/sys/apr/LFSessionContext.cpp181
-rw-r--r--Final/cpp/lib/common/sys/apr/LFSessionContext.h90
-rw-r--r--Final/cpp/lib/common/sys/apr/Socket.cpp94
-rw-r--r--Final/cpp/lib/common/sys/apr/Thread.cpp37
13 files changed, 0 insertions, 1267 deletions
diff --git a/Final/cpp/lib/common/sys/apr/APRAcceptor.cpp b/Final/cpp/lib/common/sys/apr/APRAcceptor.cpp
deleted file mode 100644
index a427542fc3..0000000000
--- a/Final/cpp/lib/common/sys/apr/APRAcceptor.cpp
+++ /dev/null
@@ -1,130 +0,0 @@
-/*
- *
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- *
- */
-#include <sys/Acceptor.h>
-#include <sys/SessionHandlerFactory.h>
-#include <apr_pools.h>
-#include "LFProcessor.h"
-#include "LFSessionContext.h"
-#include "APRBase.h"
-#include "APRPool.h"
-
-namespace qpid {
-namespace sys {
-
-class APRAcceptor : public Acceptor
-{
- public:
- APRAcceptor(int16_t port, int backlog, int threads, bool trace);
- ~APRAcceptor();
- virtual int16_t getPort() const;
- virtual void run(qpid::sys::SessionHandlerFactory* factory);
- virtual void shutdown();
-
- private:
- void shutdownImpl();
-
- private:
- int16_t port;
- bool trace;
- LFProcessor processor;
- apr_socket_t* socket;
- volatile bool running;
- Mutex shutdownLock;
- apr_pool_t* pool;
-};
-
-// Define generic Acceptor::create() to return APRAcceptor.
-Acceptor::shared_ptr Acceptor::create(int16_t port, int backlog, int threads, bool trace)
-{
- return Acceptor::shared_ptr(new APRAcceptor(port, backlog, threads, trace));
-}
-// Must define Acceptor virtual dtor.
-Acceptor::~Acceptor() {
-}
-
-APRAcceptor::~APRAcceptor() {
- APRPool::free(pool);
-}
-
-APRAcceptor::APRAcceptor(int16_t port_, int backlog, int threads, bool trace_) :
- port(port_),
- trace(trace_),
- processor(threads, 1000, 5000000)
-{
- pool = APRPool::get();
- apr_sockaddr_t* address;
- CHECK_APR_SUCCESS(apr_sockaddr_info_get(&address, APR_ANYADDR, APR_UNSPEC, port, APR_IPV4_ADDR_OK, pool));
- CHECK_APR_SUCCESS(apr_socket_create(&socket, APR_INET, SOCK_STREAM, APR_PROTO_TCP, pool));
- CHECK_APR_SUCCESS(apr_socket_opt_set(socket, APR_SO_REUSEADDR, 1));
- CHECK_APR_SUCCESS(apr_socket_bind(socket, address));
- CHECK_APR_SUCCESS(apr_socket_listen(socket, backlog));
-}
-
-int16_t APRAcceptor::getPort() const {
- apr_sockaddr_t* address;
- CHECK_APR_SUCCESS(apr_socket_addr_get(&address, APR_LOCAL, socket));
- return address->port;
-}
-
-void APRAcceptor::run(SessionHandlerFactory* factory) {
- running = true;
- processor.start();
- std::cout << "Listening on port " << getPort() << "..." << std::endl;
- while(running){
- apr_socket_t* client;
- apr_status_t status = apr_socket_accept(&client, socket, pool);
- if(status == APR_SUCCESS){
- //make this socket non-blocking:
- CHECK_APR_SUCCESS(apr_socket_timeout_set(client, 0));
- CHECK_APR_SUCCESS(apr_socket_opt_set(client, APR_SO_NONBLOCK, 1));
- CHECK_APR_SUCCESS(apr_socket_opt_set(client, APR_TCP_NODELAY, 1));
- CHECK_APR_SUCCESS(apr_socket_opt_set(client, APR_SO_SNDBUF, 32768));
- CHECK_APR_SUCCESS(apr_socket_opt_set(client, APR_SO_RCVBUF, 32768));
- LFSessionContext* session = new LFSessionContext(client, &processor, trace);
- session->init(factory->create(session));
- }else{
- Mutex::ScopedLock locker(shutdownLock);
- if(running) {
- if(status != APR_EINTR){
- std::cout << "ERROR: " << get_desc(status) << std::endl;
- }
- shutdownImpl();
- }
- }
- }
-}
-
-void APRAcceptor::shutdown() {
- Mutex::ScopedLock locker(shutdownLock);
- if (running) {
- shutdownImpl();
- }
-}
-
-void APRAcceptor::shutdownImpl() {
- Mutex::ScopedLock locker(shutdownLock);
- running = false;
- processor.stop();
- CHECK_APR_SUCCESS(apr_socket_close(socket));
-}
-
-
-}}
diff --git a/Final/cpp/lib/common/sys/apr/APRBase.cpp b/Final/cpp/lib/common/sys/apr/APRBase.cpp
deleted file mode 100644
index 861071499f..0000000000
--- a/Final/cpp/lib/common/sys/apr/APRBase.cpp
+++ /dev/null
@@ -1,90 +0,0 @@
-/*
- *
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- *
- */
-#include <iostream>
-#include <QpidError.h>
-#include "APRBase.h"
-
-using namespace qpid::sys;
-
-APRBase* APRBase::instance = 0;
-
-APRBase* APRBase::getInstance(){
- if(instance == 0){
- instance = new APRBase();
- }
- return instance;
-}
-
-
-APRBase::APRBase() : count(0){
- apr_initialize();
- CHECK_APR_SUCCESS(apr_pool_create(&pool, 0));
- CHECK_APR_SUCCESS(apr_thread_mutex_create(&mutex, APR_THREAD_MUTEX_NESTED, pool));
-}
-
-APRBase::~APRBase(){
- CHECK_APR_SUCCESS(apr_thread_mutex_destroy(mutex));
- apr_pool_destroy(pool);
- apr_terminate();
-}
-
-bool APRBase::_increment(){
- bool deleted(false);
- CHECK_APR_SUCCESS(apr_thread_mutex_lock(mutex));
- if(this == instance){
- count++;
- }else{
- deleted = true;
- }
- CHECK_APR_SUCCESS(apr_thread_mutex_unlock(mutex));
- return !deleted;
-}
-
-void APRBase::_decrement(){
- APRBase* copy = 0;
- CHECK_APR_SUCCESS(apr_thread_mutex_lock(mutex));
- if(--count == 0){
- copy = instance;
- instance = 0;
- }
- CHECK_APR_SUCCESS(apr_thread_mutex_unlock(mutex));
- if(copy != 0){
- delete copy;
- }
-}
-
-void APRBase::increment(){
- int count = 0;
- while(count++ < 2 && !getInstance()->_increment()){
- std::cout << "WARNING: APR initialization triggered concurrently with termination." << std::endl;
- }
-}
-
-void APRBase::decrement(){
- getInstance()->_decrement();
-}
-
-std::string qpid::sys::get_desc(apr_status_t status){
- const int size = 50;
- char tmp[size];
- return std::string(apr_strerror(status, tmp, size));
-}
-
diff --git a/Final/cpp/lib/common/sys/apr/APRBase.h b/Final/cpp/lib/common/sys/apr/APRBase.h
deleted file mode 100644
index 6a866a554a..0000000000
--- a/Final/cpp/lib/common/sys/apr/APRBase.h
+++ /dev/null
@@ -1,78 +0,0 @@
-/*
- *
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- *
- */
-#ifndef _APRBase_
-#define _APRBase_
-
-#include <string>
-#include <apr_thread_mutex.h>
-#include <apr_errno.h>
-#include <QpidError.h>
-
-namespace qpid {
-namespace sys {
-
- /**
- * Use of APR libraries necessitates explicit init and terminate
- * calls. Any class using APR libs should obtain the reference to
- * this singleton and increment on construction, decrement on
- * destruction. This class can then correctly initialise apr
- * before the first use and terminate after the last use.
- */
- class APRBase{
- static APRBase* instance;
- apr_pool_t* pool;
- apr_thread_mutex_t* mutex;
- int count;
-
- APRBase();
- ~APRBase();
- static APRBase* getInstance();
- bool _increment();
- void _decrement();
- public:
- static void increment();
- static void decrement();
- };
-
- //this is also a convenient place for a helper function for error checking:
- void check(apr_status_t status, const char* file, const int line);
- std::string get_desc(apr_status_t status);
-
-#define CHECK_APR_SUCCESS(A) qpid::sys::check(A, __FILE__, __LINE__);
-
-}
-}
-
-// Inlined as it is called *a lot*
-void inline qpid::sys::check(apr_status_t status, const char* file, const int line){
- if (status != APR_SUCCESS){
- const int size = 50;
- char tmp[size];
- std::string msg(apr_strerror(status, tmp, size));
- throw qpid::QpidError(APR_ERROR + ((int) status), msg,
- qpid::SrcLine(file, line));
- }
-}
-
-
-
-
-#endif
diff --git a/Final/cpp/lib/common/sys/apr/APRPool.cpp b/Final/cpp/lib/common/sys/apr/APRPool.cpp
deleted file mode 100644
index 91481faf09..0000000000
--- a/Final/cpp/lib/common/sys/apr/APRPool.cpp
+++ /dev/null
@@ -1,78 +0,0 @@
-/*
- *
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- *
- */
-
-#include "APRPool.h"
-#include "APRBase.h"
-#include <boost/pool/detail/singleton.hpp>
-#include <iostream>
-#include <sstream>
-
-
-using namespace qpid::sys;
-
-APRPool::APRPool(){
- APRBase::increment();
- allocated_pools = new std::stack<apr_pool_t*>();
- CHECK_APR_SUCCESS(apr_pool_create(&pool, NULL));
- CHECK_APR_SUCCESS(apr_thread_mutex_create(&poolGuard, APR_THREAD_MUTEX_NESTED, pool));
-}
-
-APRPool::~APRPool(){
- while(allocated_pools->size() > 0) {
- apr_pool_t* pool = allocated_pools->top();
- allocated_pools->pop();
- apr_pool_destroy(pool);
- }
- apr_pool_destroy(pool);
- apr_thread_mutex_destroy(poolGuard);
- delete allocated_pools;
- APRBase::decrement();
-}
-
-void APRPool::free_pool(apr_pool_t* pool) {
- CHECK_APR_SUCCESS(apr_thread_mutex_lock(poolGuard));
- allocated_pools->push(pool);
- CHECK_APR_SUCCESS(apr_thread_mutex_unlock(poolGuard));
-}
-
-apr_pool_t* APRPool::allocate_pool() {
- CHECK_APR_SUCCESS(apr_thread_mutex_lock(poolGuard));
- apr_pool_t* retval;
- if (allocated_pools->size() == 0) {
- CHECK_APR_SUCCESS(apr_pool_create(&retval, pool));
- }
- else {
- retval = allocated_pools->top();
- allocated_pools->pop();
- }
- CHECK_APR_SUCCESS(apr_thread_mutex_unlock(poolGuard));
- return retval;
-}
-
-apr_pool_t* APRPool::get() {
- return
- boost::details::pool::singleton_default<APRPool>::instance().allocate_pool();
-}
-
-void APRPool::free(apr_pool_t* pool) {
- boost::details::pool::singleton_default<APRPool>::instance().free_pool(pool);
-}
-
diff --git a/Final/cpp/lib/common/sys/apr/APRPool.h b/Final/cpp/lib/common/sys/apr/APRPool.h
deleted file mode 100644
index c22338599e..0000000000
--- a/Final/cpp/lib/common/sys/apr/APRPool.h
+++ /dev/null
@@ -1,61 +0,0 @@
-#ifndef _APRPool_
-#define _APRPool_
-
-/*
- *
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- *
- */
-#include <boost/noncopyable.hpp>
-#include <apr_pools.h>
-#include <apr_thread_mutex.h>
-#include <stack>
-
-namespace qpid {
-namespace sys {
-/**
- * Singleton APR memory pool.
- */
-class APRPool : private boost::noncopyable {
- public:
- APRPool();
- ~APRPool();
-
- apr_pool_t* allocate_pool();
-
- void free_pool(apr_pool_t* pool);
-
- /** Allocate pool */
- static apr_pool_t* get();
-
- /** Free pool */
- static void free(apr_pool_t* pool);
-
- private:
- apr_pool_t* pool;
- apr_thread_mutex_t* poolGuard;
- std::stack<apr_pool_t*>* allocated_pools;
-};
-
-}}
-
-
-
-
-
-#endif /*!_APRPool_*/
diff --git a/Final/cpp/lib/common/sys/apr/APRSocket.cpp b/Final/cpp/lib/common/sys/apr/APRSocket.cpp
deleted file mode 100644
index f68d51d8e4..0000000000
--- a/Final/cpp/lib/common/sys/apr/APRSocket.cpp
+++ /dev/null
@@ -1,77 +0,0 @@
-/*
- *
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- *
- */
-#include "APRBase.h"
-#include "APRSocket.h"
-#include <assert.h>
-#include <iostream>
-
-using namespace qpid::sys;
-using namespace qpid::framing;
-
-APRSocket::APRSocket(apr_socket_t* _socket) : socket(_socket), closed(false){
-
-}
-
-void APRSocket::read(qpid::framing::Buffer& buffer){
- apr_size_t bytes;
- bytes = buffer.available();
- apr_status_t s = apr_socket_recv(socket, buffer.start(), &bytes);
- buffer.move(bytes);
- if(APR_STATUS_IS_TIMEUP(s)){
- //timed out
- }else if(APR_STATUS_IS_EOF(s)){
- close();
- }
-}
-
-void APRSocket::write(qpid::framing::Buffer& buffer){
- apr_size_t bytes;
- do{
- bytes = buffer.available();
- apr_socket_send(socket, buffer.start(), &bytes);
- buffer.move(bytes);
- }while(bytes > 0);
-}
-
-void APRSocket::close(){
- if(!closed){
- CHECK_APR_SUCCESS(apr_socket_close(socket));
- closed = true;
- }
-}
-
-bool APRSocket::isOpen(){
- return !closed;
-}
-
-u_int8_t APRSocket::read(){
- char data[1];
- apr_size_t bytes = 1;
- apr_status_t s = apr_socket_recv(socket, data, &bytes);
- if(APR_STATUS_IS_EOF(s) || bytes == 0){
- return 0;
- }else{
- return *data;
- }
-}
-
-APRSocket::~APRSocket(){
-}
diff --git a/Final/cpp/lib/common/sys/apr/APRSocket.h b/Final/cpp/lib/common/sys/apr/APRSocket.h
deleted file mode 100644
index 53f1055c6a..0000000000
--- a/Final/cpp/lib/common/sys/apr/APRSocket.h
+++ /dev/null
@@ -1,48 +0,0 @@
-/*
- *
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- *
- */
-#ifndef _APRSocket_
-#define _APRSocket_
-
-#include <apr_network_io.h>
-#include <Buffer.h>
-
-namespace qpid {
-namespace sys {
-
- class APRSocket
- {
- apr_socket_t* const socket;
- volatile bool closed;
- public:
- APRSocket(apr_socket_t* socket);
- void read(qpid::framing::Buffer& b);
- void write(qpid::framing::Buffer& b);
- void close();
- bool isOpen();
- u_int8_t read();
- ~APRSocket();
- };
-
-}
-}
-
-
-#endif
diff --git a/Final/cpp/lib/common/sys/apr/LFProcessor.cpp b/Final/cpp/lib/common/sys/apr/LFProcessor.cpp
deleted file mode 100644
index 22b601e688..0000000000
--- a/Final/cpp/lib/common/sys/apr/LFProcessor.cpp
+++ /dev/null
@@ -1,181 +0,0 @@
-/*
- *
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- *
- */
-#include <sstream>
-#include <QpidError.h>
-#include "LFProcessor.h"
-#include "APRBase.h"
-#include "APRPool.h"
-#include "LFSessionContext.h"
-
-using namespace qpid::sys;
-using qpid::QpidError;
-
-// TODO aconway 2006-10-12: stopped is read outside locks.
-//
-
-LFProcessor::LFProcessor(int _workers, int _size, int _timeout) :
- size(_size),
- timeout(_timeout),
- signalledCount(0),
- current(0),
- count(0),
- workerCount(_workers),
- hasLeader(false),
- workers(new Thread[_workers]),
- stopped(false)
-{
- pool = APRPool::get();
- CHECK_APR_SUCCESS(apr_pollset_create(&pollset, size, pool, APR_POLLSET_THREADSAFE));
-}
-
-
-LFProcessor::~LFProcessor(){
- if (!stopped) stop();
- delete[] workers;
- CHECK_APR_SUCCESS(apr_pollset_destroy(pollset));
- APRPool::free(pool);
-}
-
-void LFProcessor::start(){
- for(int i = 0; i < workerCount; i++){
- workers[i] = Thread(this);
- }
-}
-
-void LFProcessor::add(const apr_pollfd_t* const fd){
- CHECK_APR_SUCCESS(apr_pollset_add(pollset, fd));
- Monitor::ScopedLock l(countLock);
- sessions.push_back(reinterpret_cast<LFSessionContext*>(fd->client_data));
- count++;
-}
-
-void LFProcessor::remove(const apr_pollfd_t* const fd){
- CHECK_APR_SUCCESS(apr_pollset_remove(pollset, fd));
- Monitor::ScopedLock l(countLock);
- sessions.erase(find(sessions.begin(), sessions.end(), reinterpret_cast<LFSessionContext*>(fd->client_data)));
- count--;
-}
-
-void LFProcessor::reactivate(const apr_pollfd_t* const fd){
- CHECK_APR_SUCCESS(apr_pollset_add(pollset, fd));
-}
-
-void LFProcessor::deactivate(const apr_pollfd_t* const fd){
- CHECK_APR_SUCCESS(apr_pollset_remove(pollset, fd));
-}
-
-void LFProcessor::update(const apr_pollfd_t* const fd){
- CHECK_APR_SUCCESS(apr_pollset_remove(pollset, fd));
- CHECK_APR_SUCCESS(apr_pollset_add(pollset, fd));
-}
-
-bool LFProcessor::full(){
- Mutex::ScopedLock locker(countLock);
- return count == size;
-}
-
-bool LFProcessor::empty(){
- Mutex::ScopedLock locker(countLock);
- return count == 0;
-}
-
-void LFProcessor::poll() {
- apr_status_t status = APR_EGENERAL;
- do{
- current = 0;
- if(!stopped){
- status = apr_pollset_poll(pollset, timeout, &signalledCount, &signalledFDs);
- }
- }while(status != APR_SUCCESS && !stopped);
-}
-
-void LFProcessor::run(){
- try{
- while(!stopped){
- const apr_pollfd_t* event = 0;
- LFSessionContext* session = 0;
- {
- Monitor::ScopedLock l(leadLock);
- waitToLead();
- event = getNextEvent();
- if(!event) return;
- session = reinterpret_cast<LFSessionContext*>(
- event->client_data);
- session->startProcessing();
- relinquishLead();
- }
-
- //process event:
- if(event->rtnevents & APR_POLLIN) session->read();
- if(event->rtnevents & APR_POLLOUT) session->write();
-
- if(session->isClosed()){
- session->handleClose();
- Monitor::ScopedLock l(countLock);
- sessions.erase(find(sessions.begin(),sessions.end(), session));
- count--;
- }else{
- session->stopProcessing();
- }
- }
- }catch(std::exception e){
- std::cout << e.what() << std::endl;
- }
-}
-
-void LFProcessor::waitToLead(){
- while(hasLeader && !stopped) leadLock.wait();
- hasLeader = !stopped;
-}
-
-void LFProcessor::relinquishLead(){
- hasLeader = false;
- leadLock.notify();
-}
-
-const apr_pollfd_t* LFProcessor::getNextEvent(){
- while(true){
- if(stopped){
- return 0;
- }else if(current < signalledCount){
- //use result of previous poll if one is available
- return signalledFDs + (current++);
- }else{
- //else poll to get new events
- poll();
- }
- }
-}
-
-void LFProcessor::stop(){
- stopped = true;
- {
- Monitor::ScopedLock l(leadLock);
- leadLock.notifyAll();
- }
- for(int i = 0; i < workerCount; i++){
- workers[i].join();
- }
- for(iterator i = sessions.begin(); i < sessions.end(); i++){
- (*i)->shutdown();
- }
-}
-
diff --git a/Final/cpp/lib/common/sys/apr/LFProcessor.h b/Final/cpp/lib/common/sys/apr/LFProcessor.h
deleted file mode 100644
index 0f4850ee08..0000000000
--- a/Final/cpp/lib/common/sys/apr/LFProcessor.h
+++ /dev/null
@@ -1,122 +0,0 @@
-/*
- *
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- *
- */
-#ifndef _LFProcessor_
-#define _LFProcessor_
-
-#include <apr_poll.h>
-#include <iostream>
-#include <vector>
-#include <sys/Monitor.h>
-#include <sys/Runnable.h>
-#include <sys/Thread.h>
-
-namespace qpid {
-namespace sys {
-
- class LFSessionContext;
-
- /**
- * This class processes a poll set using the leaders-followers
- * pattern for thread synchronization: the leader will poll and on
- * the poll returning, it will remove a session, promote a
- * follower to leadership, then process the session.
- */
- class LFProcessor : private virtual qpid::sys::Runnable
- {
- typedef std::vector<LFSessionContext*>::iterator iterator;
-
- const int size;
- const apr_interval_time_t timeout;
- apr_pollset_t* pollset;
- int signalledCount;
- int current;
- const apr_pollfd_t* signalledFDs;
- int count;
- const int workerCount;
- bool hasLeader;
- qpid::sys::Thread* workers;
- qpid::sys::Monitor leadLock;
- qpid::sys::Mutex countLock;
- std::vector<LFSessionContext*> sessions;
- volatile bool stopped;
- apr_pool_t* pool;
-
- const apr_pollfd_t* getNextEvent();
- void waitToLead();
- void relinquishLead();
- void poll();
- virtual void run();
-
- public:
- LFProcessor(int workers, int size, int timeout);
- /**
- * Add the fd to the poll set. Relies on the client_data being
- * an instance of LFSessionContext.
- */
- void add(const apr_pollfd_t* const fd);
- /**
- * Remove the fd from the poll set.
- */
- void remove(const apr_pollfd_t* const fd);
- /**
- * Signal that the fd passed in, already part of the pollset,
- * has had its flags altered.
- */
- void update(const apr_pollfd_t* const fd);
- /**
- * Add an fd back to the poll set after deactivation.
- */
- void reactivate(const apr_pollfd_t* const fd);
- /**
- * Temporarily remove the fd from the poll set. Called when processing
- * is about to begin.
- */
- void deactivate(const apr_pollfd_t* const fd);
- /**
- * Indicates whether the capacity of this processor has been
- * reached (or whether it can still handle further fd's).
- */
- bool full();
- /**
- * Indicates whether there are any fd's registered.
- */
- bool empty();
- /**
- * Stop processing.
- */
- void stop();
- /**
- * Start processing.
- */
- void start();
- /**
- * Is processing stopped?
- */
- bool isStopped();
-
- ~LFProcessor();
- };
-
-}
-}
-
-
-#endif
diff --git a/Final/cpp/lib/common/sys/apr/LFSessionContext.cpp b/Final/cpp/lib/common/sys/apr/LFSessionContext.cpp
deleted file mode 100644
index 8a7ce18136..0000000000
--- a/Final/cpp/lib/common/sys/apr/LFSessionContext.cpp
+++ /dev/null
@@ -1,181 +0,0 @@
-/*
- *
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- *
- */
-#include "LFSessionContext.h"
-#include "APRBase.h"
-#include "APRPool.h"
-#include <QpidError.h>
-#include <assert.h>
-
-using namespace qpid::sys;
-using namespace qpid::sys;
-using namespace qpid::framing;
-
-LFSessionContext::LFSessionContext(apr_socket_t* _socket,
- LFProcessor* const _processor,
- bool _debug) :
- debug(_debug),
- socket(_socket),
- initiated(false),
- in(65536),
- out(65536),
- processor(_processor),
- processing(false),
- closing(false)
-{
-
- fd.p = APRPool::get();
- fd.desc_type = APR_POLL_SOCKET;
- fd.reqevents = APR_POLLIN;
- fd.client_data = this;
- fd.desc.s = _socket;
-
- out.flip();
-}
-
-LFSessionContext::~LFSessionContext(){
-
-}
-
-void LFSessionContext::read(){
- socket.read(in);
- in.flip();
- if(initiated){
- AMQFrame frame;
- try{
- while(frame.decode(in)){
- if(debug) log("RECV", &frame);
- handler->received(&frame);
- }
- }catch(QpidError error){
- std::cout << "Error [" << error.code << "] " << error.msg
- << " (" << error.location.file << ":" << error.location.line
- << ")" << std::endl;
- }
- }else{
- ProtocolInitiation protocolInit;
- if(protocolInit.decode(in)){
- handler->initiated(&protocolInit);
- initiated = true;
- if(debug) std::cout << "INIT [" << &socket << "]" << std::endl;
- }
- }
- in.compact();
-}
-
-void LFSessionContext::write(){
- bool done = isClosed();
- while(!done){
- if(out.available() > 0){
- socket.write(out);
- if(out.available() > 0){
-
- //incomplete write, leave flags to receive notification of readiness to write
- done = true;//finished processing for now, but write is still in progress
- }
- }else{
- //do we have any frames to write?
- Mutex::ScopedLock l(writeLock);
- if(!framesToWrite.empty()){
- out.clear();
- bool encoded(false);
- AMQFrame* frame = framesToWrite.front();
- while(frame && out.available() >= frame->size()){
- encoded = true;
- frame->encode(out);
- if(debug) log("SENT", frame);
- delete frame;
- framesToWrite.pop();
- frame = framesToWrite.empty() ? 0 : framesToWrite.front();
- }
- if(!encoded) THROW_QPID_ERROR(FRAMING_ERROR, "Could not write frame, too large for buffer.");
- out.flip();
- }else{
- //reset flags, don't care about writability anymore
- fd.reqevents = APR_POLLIN;
- done = true;
-
- if(closing){
- socket.close();
- }
- }
- }
- }
-}
-
-void LFSessionContext::send(AMQFrame* frame){
- Mutex::ScopedLock l(writeLock);
- if(!closing){
- framesToWrite.push(frame);
- if(!(fd.reqevents & APR_POLLOUT)){
- fd.reqevents |= APR_POLLOUT;
- if(!processing){
- processor->update(&fd);
- }
- }
- }
-}
-
-void LFSessionContext::startProcessing(){
- Mutex::ScopedLock l(writeLock);
- processing = true;
- processor->deactivate(&fd);
-}
-
-void LFSessionContext::stopProcessing(){
- Mutex::ScopedLock l(writeLock);
- processor->reactivate(&fd);
- processing = false;
-}
-
-void LFSessionContext::close(){
- Mutex::ScopedLock l(writeLock);
- closing = true;
- if(!processing){
- //allow pending frames to be written to socket
- fd.reqevents = APR_POLLOUT;
- processor->update(&fd);
- }
-}
-
-void LFSessionContext::handleClose(){
- handler->closed();
- APRPool::free(fd.p);
- if (debug) std::cout << "Session closed [" << &socket << "]" << std::endl;
- delete handler;
- delete this;
-}
-
-void LFSessionContext::shutdown(){
- socket.close();
- handleClose();
-}
-
-void LFSessionContext::init(SessionHandler* _handler){
- handler = _handler;
- processor->add(&fd);
-}
-
-void LFSessionContext::log(const std::string& desc, AMQFrame* const frame){
- Mutex::ScopedLock l(logLock);
- std::cout << desc << " [" << &socket << "]: " << *frame << std::endl;
-}
-
-Mutex LFSessionContext::logLock;
diff --git a/Final/cpp/lib/common/sys/apr/LFSessionContext.h b/Final/cpp/lib/common/sys/apr/LFSessionContext.h
deleted file mode 100644
index eeb8279d9a..0000000000
--- a/Final/cpp/lib/common/sys/apr/LFSessionContext.h
+++ /dev/null
@@ -1,90 +0,0 @@
-/*
- *
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- *
- */
-#ifndef _LFSessionContext_
-#define _LFSessionContext_
-
-#include <queue>
-
-#include <apr_network_io.h>
-#include <apr_poll.h>
-#include <apr_time.h>
-
-#include <AMQFrame.h>
-#include <Buffer.h>
-#include <sys/Monitor.h>
-#include <sys/SessionContext.h>
-#include <sys/SessionHandler.h>
-
-#include "APRSocket.h"
-#include "LFProcessor.h"
-
-namespace qpid {
-namespace sys {
-
-
-class LFSessionContext : public virtual qpid::sys::SessionContext
-{
- const bool debug;
- APRSocket socket;
- bool initiated;
-
- qpid::framing::Buffer in;
- qpid::framing::Buffer out;
-
- qpid::sys::SessionHandler* handler;
- LFProcessor* const processor;
-
- apr_pollfd_t fd;
-
- std::queue<qpid::framing::AMQFrame*> framesToWrite;
- qpid::sys::Mutex writeLock;
-
- bool processing;
- bool closing;
-
- static qpid::sys::Mutex logLock;
- void log(const std::string& desc,
- qpid::framing::AMQFrame* const frame);
-
-
- public:
- LFSessionContext(apr_socket_t* socket,
- LFProcessor* const processor,
- bool debug = false);
- virtual ~LFSessionContext();
- virtual void send(qpid::framing::AMQFrame* frame);
- virtual void close();
- void read();
- void write();
- void init(qpid::sys::SessionHandler* handler);
- void startProcessing();
- void stopProcessing();
- void handleClose();
- void shutdown();
- inline apr_pollfd_t* const getFd(){ return &fd; }
- inline bool isClosed(){ return !socket.isOpen(); }
-};
-
-}
-}
-
-
-#endif
diff --git a/Final/cpp/lib/common/sys/apr/Socket.cpp b/Final/cpp/lib/common/sys/apr/Socket.cpp
deleted file mode 100644
index c2abf50c5f..0000000000
--- a/Final/cpp/lib/common/sys/apr/Socket.cpp
+++ /dev/null
@@ -1,94 +0,0 @@
-/*
- *
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- *
- */
-
-
-#include <sys/Socket.h>
-#include <apr/APRBase.h>
-#include <apr/APRPool.h>
-
-#include <iostream>
-
-using namespace qpid::sys;
-
-Socket Socket::createTcp() {
- Socket s;
- apr_pool_t* pool = APRPool::get();
- CHECK_APR_SUCCESS(
- apr_socket_create(
- &s.socket, APR_INET, SOCK_STREAM, APR_PROTO_TCP,
- pool));
- APRPool::free(pool);
- return s;
-}
-
-Socket::Socket(apr_socket_t* s) {
- socket = s;
-}
-
-void Socket::setTimeout(Time interval) {
- apr_socket_timeout_set(socket, interval/TIME_USEC);
-}
-
-void Socket::connect(const std::string& host, int port) {
- apr_sockaddr_t* address;
- apr_pool_t* pool = APRPool::get();
- CHECK_APR_SUCCESS(
- apr_sockaddr_info_get(
- &address, host.c_str(), APR_UNSPEC, port, APR_IPV4_ADDR_OK,
- pool));
- CHECK_APR_SUCCESS(apr_socket_connect(socket, address));
- APRPool::free(pool);
-}
-
-void Socket::close() {
- if (socket == 0) return;
- CHECK_APR_SUCCESS(apr_socket_shutdown(socket, APR_SHUTDOWN_READWRITE));
- CHECK_APR_SUCCESS(apr_socket_close(socket));
- socket = 0;
-}
-
-ssize_t Socket::send(const void* data, size_t size)
-{
- apr_size_t sent = size;
- apr_status_t status =
- apr_socket_send(socket, reinterpret_cast<const char*>(data), &sent);
- if (APR_STATUS_IS_TIMEUP(status)) return SOCKET_TIMEOUT;
- if (APR_STATUS_IS_EOF(status)) return SOCKET_EOF;
- CHECK_APR_SUCCESS(status);
- return sent;
-}
-
-ssize_t Socket::recv(void* data, size_t size)
-{
- apr_size_t received = size;
- apr_status_t status =
- apr_socket_recv(socket, reinterpret_cast<char*>(data), &received);
- if (APR_STATUS_IS_TIMEUP(status)) return SOCKET_TIMEOUT;
- if (APR_STATUS_IS_EOF(status)) return SOCKET_EOF;
- CHECK_APR_SUCCESS(status);
- return received;
-}
-
-void Socket::setTcpNoDelay(bool on)
-{
- CHECK_APR_SUCCESS(apr_socket_opt_set(socket, APR_TCP_NODELAY, on ? 1 : 0));
-}
-
diff --git a/Final/cpp/lib/common/sys/apr/Thread.cpp b/Final/cpp/lib/common/sys/apr/Thread.cpp
deleted file mode 100644
index 997ff03ab3..0000000000
--- a/Final/cpp/lib/common/sys/apr/Thread.cpp
+++ /dev/null
@@ -1,37 +0,0 @@
-/*
- *
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- *
- */
-
-#include <sys/Thread.h>
-#include "APRPool.h"
-
-using namespace qpid::sys;
-using qpid::sys::Runnable;
-
-void* APR_THREAD_FUNC Thread::runRunnable(apr_thread_t* thread, void *data) {
- reinterpret_cast<Runnable*>(data)->run();
- CHECK_APR_SUCCESS(apr_thread_exit(thread, APR_SUCCESS));
- return NULL;
-}
-
-Thread::~Thread() {
-}
-
-