diff options
| author | Alan Conway <aconway@apache.org> | 2006-11-09 21:55:34 +0000 |
|---|---|---|
| committer | Alan Conway <aconway@apache.org> | 2006-11-09 21:55:34 +0000 |
| commit | c33f3d8550b9b4455ad6ca8a2327a7bd9d6f7db1 (patch) | |
| tree | bb5d68281986eb1664c227d15f303664a65d5e03 /cpp/src/qpid/apr | |
| parent | 76fb78a8495b6cd48c633e8b6219b29761133d82 (diff) | |
| download | qpid-python-c33f3d8550b9b4455ad6ca8a2327a7bd9d6f7db1.tar.gz | |
Added POSIX equivalents to APR classes used by clients, inlined trivial calls.
git-svn-id: https://svn.apache.org/repos/asf/incubator/qpid/trunk/qpid@473087 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'cpp/src/qpid/apr')
| -rw-r--r-- | cpp/src/qpid/apr/APRAcceptor.cpp (renamed from cpp/src/qpid/apr/Acceptor.cpp) | 40 | ||||
| -rw-r--r-- | cpp/src/qpid/apr/APRBase.h | 4 | ||||
| -rw-r--r-- | cpp/src/qpid/apr/APRSocket.h | 4 | ||||
| -rw-r--r-- | cpp/src/qpid/apr/Acceptor.h | 55 | ||||
| -rw-r--r-- | cpp/src/qpid/apr/LFProcessor.h | 6 | ||||
| -rw-r--r-- | cpp/src/qpid/apr/LFSessionContext.cpp | 2 | ||||
| -rw-r--r-- | cpp/src/qpid/apr/LFSessionContext.h | 16 | ||||
| -rw-r--r-- | cpp/src/qpid/apr/Monitor.h | 124 | ||||
| -rw-r--r-- | cpp/src/qpid/apr/Socket.cpp (renamed from cpp/src/qpid/apr/Socket.h) | 57 | ||||
| -rw-r--r-- | cpp/src/qpid/apr/Thread.cpp | 29 | ||||
| -rw-r--r-- | cpp/src/qpid/apr/Thread.h | 45 | ||||
| -rw-r--r-- | cpp/src/qpid/apr/Time.cpp | 39 |
12 files changed, 65 insertions, 356 deletions
diff --git a/cpp/src/qpid/apr/Acceptor.cpp b/cpp/src/qpid/apr/APRAcceptor.cpp index cbeea9902b..3aa4e6ede7 100644 --- a/cpp/src/qpid/apr/Acceptor.cpp +++ b/cpp/src/qpid/apr/APRAcceptor.cpp @@ -15,13 +15,40 @@ * limitations under the License. * */ -#include "Acceptor.h" +#include <qpid/sys/Acceptor.h> +#include <qpid/sys/SessionHandlerFactory.h> +#include "LFProcessor.h" +#include "LFSessionContext.h" #include "APRBase.h" #include "APRPool.h" -using namespace qpid::sys; +namespace qpid { +namespace sys { -Acceptor::Acceptor(int16_t port_, int backlog, int threads) : +class APRAcceptor : public Acceptor +{ + public: + APRAcceptor(int16_t port, int backlog, int threads); + virtual int16_t getPort() const; + virtual void run(qpid::sys::SessionHandlerFactory* factory); + virtual void shutdown(); + + private: + int16_t port; + LFProcessor processor; + apr_socket_t* socket; + volatile bool running; +}; + +// Define generic Acceptor::create() to return APRAcceptor. +Acceptor::shared_ptr Acceptor::create(int16_t port, int backlog, int threads) +{ + return Acceptor::shared_ptr(new APRAcceptor(port, backlog, threads)); +} +// Must define Acceptor virtual dtor. +Acceptor::~Acceptor() {} + +APRAcceptor::APRAcceptor(int16_t port_, int backlog, int threads) : port(port_), processor(APRPool::get(), threads, 1000, 5000000) { @@ -33,13 +60,13 @@ Acceptor::Acceptor(int16_t port_, int backlog, int threads) : CHECK_APR_SUCCESS(apr_socket_listen(socket, backlog)); } -int16_t Acceptor::getPort() const { +int16_t APRAcceptor::getPort() const { apr_sockaddr_t* address; CHECK_APR_SUCCESS(apr_socket_addr_get(&address, APR_LOCAL, socket)); return address->port; } -void Acceptor::run(SessionHandlerFactory* factory) { +void APRAcceptor::run(SessionHandlerFactory* factory) { running = true; processor.start(); std::cout << "Listening on port " << getPort() << "..." << std::endl; @@ -65,7 +92,7 @@ void Acceptor::run(SessionHandlerFactory* factory) { shutdown(); } -void Acceptor::shutdown() { +void APRAcceptor::shutdown() { // TODO aconway 2006-10-12: Cleanup, this is not thread safe. if (running) { running = false; @@ -75,3 +102,4 @@ void Acceptor::shutdown() { } +}} diff --git a/cpp/src/qpid/apr/APRBase.h b/cpp/src/qpid/apr/APRBase.h index b84e9860df..ee6ba51417 100644 --- a/cpp/src/qpid/apr/APRBase.h +++ b/cpp/src/qpid/apr/APRBase.h @@ -19,8 +19,8 @@ #define _APRBase_ #include <string> -#include "apr-1/apr_thread_mutex.h" -#include "apr-1/apr_errno.h" +#include <apr-1/apr_thread_mutex.h> +#include <apr-1/apr_errno.h> namespace qpid { namespace sys { diff --git a/cpp/src/qpid/apr/APRSocket.h b/cpp/src/qpid/apr/APRSocket.h index f7e7ad107b..28e0c7e6de 100644 --- a/cpp/src/qpid/apr/APRSocket.h +++ b/cpp/src/qpid/apr/APRSocket.h @@ -18,8 +18,8 @@ #ifndef _APRSocket_ #define _APRSocket_ -#include "apr-1/apr_network_io.h" -#include "qpid/framing/Buffer.h" +#include <apr-1/apr_network_io.h> +#include <qpid/framing/Buffer.h> namespace qpid { namespace sys { diff --git a/cpp/src/qpid/apr/Acceptor.h b/cpp/src/qpid/apr/Acceptor.h deleted file mode 100644 index 1813b391c1..0000000000 --- a/cpp/src/qpid/apr/Acceptor.h +++ /dev/null @@ -1,55 +0,0 @@ -/* - * - * Copyright (c) 2006 The Apache Software Foundation - * - * Licensed 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 _LFAcceptor_ -#define _LFAcceptor_ - -#include "LFProcessor.h" -#include "LFSessionContext.h" -#include "apr-1/apr_network_io.h" -#include "apr-1/apr_poll.h" -#include "apr-1/apr_time.h" -#include "Monitor.h" -#include "qpid/sys/Runnable.h" -#include "qpid/sys/SessionContext.h" -#include "qpid/sys/SessionHandlerFactory.h" -#include <qpid/SharedObject.h> - -namespace qpid { -namespace sys { - -/** APR Acceptor. */ -class Acceptor : public qpid::SharedObject<Acceptor> -{ - public: - Acceptor(int16_t port, int backlog, int threads); - virtual int16_t getPort() const; - virtual void run(qpid::sys::SessionHandlerFactory* factory); - virtual void shutdown(); - - private: - int16_t port; - LFProcessor processor; - apr_socket_t* socket; - volatile bool running; -}; - -} -} - - -#endif diff --git a/cpp/src/qpid/apr/LFProcessor.h b/cpp/src/qpid/apr/LFProcessor.h index dd85ad9e84..3eeecc7325 100644 --- a/cpp/src/qpid/apr/LFProcessor.h +++ b/cpp/src/qpid/apr/LFProcessor.h @@ -18,12 +18,12 @@ #ifndef _LFProcessor_ #define _LFProcessor_ -#include "apr-1/apr_poll.h" +#include <apr-1/apr_poll.h> #include <iostream> #include <vector> #include <qpid/sys/Monitor.h> -#include "qpid/sys/Runnable.h" -#include "qpid/sys/Thread.h" +#include <qpid/sys/Runnable.h> +#include <qpid/sys/Thread.h> namespace qpid { namespace sys { diff --git a/cpp/src/qpid/apr/LFSessionContext.cpp b/cpp/src/qpid/apr/LFSessionContext.cpp index 4a704013a8..ebb1fc9fb2 100644 --- a/cpp/src/qpid/apr/LFSessionContext.cpp +++ b/cpp/src/qpid/apr/LFSessionContext.cpp @@ -17,7 +17,7 @@ */ #include "LFSessionContext.h" #include "APRBase.h" -#include "qpid/QpidError.h" +#include <qpid/QpidError.h> #include <assert.h> using namespace qpid::sys; diff --git a/cpp/src/qpid/apr/LFSessionContext.h b/cpp/src/qpid/apr/LFSessionContext.h index 9b3104b085..83387c934f 100644 --- a/cpp/src/qpid/apr/LFSessionContext.h +++ b/cpp/src/qpid/apr/LFSessionContext.h @@ -20,15 +20,15 @@ #include <queue> -#include "apr-1/apr_network_io.h" -#include "apr-1/apr_poll.h" -#include "apr-1/apr_time.h" +#include <apr-1/apr_network_io.h> +#include <apr-1/apr_poll.h> +#include <apr-1/apr_time.h> -#include "qpid/framing/AMQFrame.h" -#include "qpid/framing/Buffer.h" -#include "qpid/sys/Monitor.h" -#include "qpid/sys/SessionContext.h" -#include "qpid/sys/SessionHandler.h" +#include <qpid/framing/AMQFrame.h> +#include <qpid/framing/Buffer.h> +#include <qpid/sys/Monitor.h> +#include <qpid/sys/SessionContext.h> +#include <qpid/sys/SessionHandler.h> #include "APRSocket.h" #include "LFProcessor.h" diff --git a/cpp/src/qpid/apr/Monitor.h b/cpp/src/qpid/apr/Monitor.h deleted file mode 100644 index 10bc20820e..0000000000 --- a/cpp/src/qpid/apr/Monitor.h +++ /dev/null @@ -1,124 +0,0 @@ -/* - * - * Copyright (c) 2006 The Apache Software Foundation - * - * Licensed 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 _Monitor_ -#define _Monitor_ - -#include <boost/noncopyable.hpp> -#include <qpid/sys/Time.h> -#include "apr-1/apr_thread_mutex.h" -#include "apr-1/apr_thread_cond.h" -#include "APRBase.h" -#include "APRPool.h" - -namespace qpid { -namespace sys { - -template <class L> -class ScopedLock -{ - public: - ScopedLock(L& l) : mutex(l) { l.lock(); } - ~ScopedLock() { mutex.unlock(); } - private: - L& mutex; -}; - - -class Mutex : private boost::noncopyable -{ - public: - typedef ScopedLock<Mutex> ScopedLock; - - inline Mutex(); - inline ~Mutex(); - inline void lock(); - inline void unlock(); - inline void trylock(); - - protected: - apr_thread_mutex_t* mutex; -}; - -/** A condition variable and a mutex */ -class Monitor : public Mutex -{ - public: - inline Monitor(); - inline ~Monitor(); - inline void wait(); - inline void wait(int64_t nsecs); - inline void notify(); - inline void notifyAll(); - - private: - apr_thread_cond_t* condition; -}; - - - -Mutex::Mutex() { - CHECK_APR_SUCCESS(apr_thread_mutex_create(&mutex, APR_THREAD_MUTEX_NESTED, APRPool::get())); -} - -Mutex::~Mutex(){ - CHECK_APR_SUCCESS(apr_thread_mutex_destroy(mutex)); -} - -void Mutex::lock() { - CHECK_APR_SUCCESS(apr_thread_mutex_lock(mutex)); -} -void Mutex::unlock() { - CHECK_APR_SUCCESS(apr_thread_mutex_unlock(mutex)); -} - -void Mutex::trylock() { - CHECK_APR_SUCCESS(apr_thread_mutex_trylock(mutex)); -} - -Monitor::Monitor() { - CHECK_APR_SUCCESS(apr_thread_cond_create(&condition, APRPool::get())); -} - -Monitor::~Monitor() { - CHECK_APR_SUCCESS(apr_thread_cond_destroy(condition)); -} - -void Monitor::wait() { - CHECK_APR_SUCCESS(apr_thread_cond_wait(condition, mutex)); -} - -void Monitor::wait(int64_t nsecs){ - // APR uses microseconds. - apr_status_t status = apr_thread_cond_timedwait( - condition, mutex, nsecs/1000); - if(!status == APR_TIMEUP) CHECK_APR_SUCCESS(status); -} - -void Monitor::notify(){ - CHECK_APR_SUCCESS(apr_thread_cond_signal(condition)); -} - -void Monitor::notifyAll(){ - CHECK_APR_SUCCESS(apr_thread_cond_broadcast(condition)); -} - - -}} - - -#endif diff --git a/cpp/src/qpid/apr/Socket.h b/cpp/src/qpid/apr/Socket.cpp index 9a519e7391..b6cb63b215 100644 --- a/cpp/src/qpid/apr/Socket.h +++ b/cpp/src/qpid/apr/Socket.cpp @@ -1,6 +1,3 @@ -#ifndef _apr_Socket_h -#define _apr_Socket_h - /* * * Copyright (c) 2006 The Apache Software Foundation @@ -19,32 +16,14 @@ * */ -#include <apr-1/apr_network_io.h> -#include "APRBase.h" -#include "APRPool.h" - -namespace qpid { -namespace sys { - -class Socket -{ - public: - inline Socket(); - inline ~Socket(); - inline void setTimeout(long msecs); - inline void connect(const std::string& host, int port); - inline void close(); - enum { SOCKET_TIMEOUT=-2, SOCKET_EOF=-3 }; +#include <qpid/sys/Socket.h> +#include <qpid/apr/APRBase.h> +#include <qpid/apr/APRPool.h> - inline ssize_t send(const char* data, size_t size); - inline ssize_t recv(char* data, size_t size); - private: - apr_socket_t* socket; -}; +using namespace qpid::sys; -inline Socket::Socket() { CHECK_APR_SUCCESS( @@ -53,17 +32,12 @@ Socket::Socket() APRPool::get())); } -inline -Socket::~Socket() { } - -inline void -Socket::setTimeout(long msecs) +void Socket::setTimeout(long msecs) { apr_socket_timeout_set(socket, msecs*1000); } -inline void -Socket::connect(const std::string& host, int port) +void Socket::connect(const std::string& host, int port) { apr_sockaddr_t* address; CHECK_APR_SUCCESS( @@ -73,35 +47,30 @@ Socket::connect(const std::string& host, int port) CHECK_APR_SUCCESS(apr_socket_connect(socket, address)); } -inline void -Socket::close() +void Socket::close() { + if (socket == 0) return; CHECK_APR_SUCCESS(apr_socket_close(socket)); socket = 0; } -inline ssize_t -Socket::send(const char* data, size_t size) +ssize_t Socket::send(const char* data, size_t size) { apr_size_t sent = size; apr_status_t status = apr_socket_send(socket, data, &sent); - if (!APR_STATUS_IS_TIMEUP(status)) return SOCKET_TIMEOUT; - if (!APR_STATUS_IS_EOF(status)) return SOCKET_EOF; + if (APR_STATUS_IS_TIMEUP(status)) return SOCKET_TIMEOUT; + if (APR_STATUS_IS_EOF(status)) return SOCKET_EOF; CHECK_APR_SUCCESS(status); return sent; } -inline ssize_t -Socket::recv(char* data, size_t size) +ssize_t Socket::recv(char* data, size_t size) { apr_size_t received = size; apr_status_t status = apr_socket_recv(socket, data, &received); - if (!APR_STATUS_IS_TIMEUP(status)) return SOCKET_TIMEOUT; + if (APR_STATUS_IS_TIMEUP(status)) return SOCKET_TIMEOUT; CHECK_APR_SUCCESS(status); return received; } -}} - -#endif /*!_apr_Socket_h*/ diff --git a/cpp/src/qpid/apr/Thread.cpp b/cpp/src/qpid/apr/Thread.cpp index 6d5cadb009..b26583d4c5 100644 --- a/cpp/src/qpid/apr/Thread.cpp +++ b/cpp/src/qpid/apr/Thread.cpp @@ -16,40 +16,15 @@ * */ -#include "Thread.h" -#include "APRPool.h" -#include "APRBase.h" -#include <apr-1/apr_portable.h> +#include <qpid/sys/Thread.h> using namespace qpid::sys; using qpid::sys::Runnable; -namespace { -void* APR_THREAD_FUNC run(apr_thread_t* thread, void *data) { +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() : thread(0) {} -Thread::Thread(Runnable* runnable) { - CHECK_APR_SUCCESS( - apr_thread_create(&thread, NULL, run, runnable, APRPool::get())); -} - -void Thread::join(){ - apr_status_t status; - if (thread != 0) - CHECK_APR_SUCCESS(apr_thread_join(&status, thread)); -} - -Thread::Thread(apr_thread_t* t) : thread(t) {} - -Thread Thread::current(){ - apr_thread_t* thr; - apr_os_thread_t osthr = apr_os_thread_current(); - CHECK_APR_SUCCESS(apr_os_thread_put(&thr, &osthr, APRPool::get())); - return Thread(thr); -} diff --git a/cpp/src/qpid/apr/Thread.h b/cpp/src/qpid/apr/Thread.h deleted file mode 100644 index 0c717dea70..0000000000 --- a/cpp/src/qpid/apr/Thread.h +++ /dev/null @@ -1,45 +0,0 @@ -#ifndef _apr_Thread_h -#define _apr_Thread_h - -/* - * - * Copyright (c) 2006 The Apache Software Foundation - * - * Licensed 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 <apr-1/apr_thread_proc.h> -#include <qpid/sys/Runnable.h> - -namespace qpid { -namespace sys { - -class Thread -{ - - public: - Thread(); - explicit Thread(qpid::sys::Runnable*); - void join(); - static Thread current(); - - private: - Thread(apr_thread_t*); - - apr_thread_t* thread; -}; - -}} - -#endif /*!_apr_Thread_h*/ diff --git a/cpp/src/qpid/apr/Time.cpp b/cpp/src/qpid/apr/Time.cpp deleted file mode 100644 index 8b5590481d..0000000000 --- a/cpp/src/qpid/apr/Time.cpp +++ /dev/null @@ -1,39 +0,0 @@ -/* - * - * Copyright (c) 2006 The Apache Software Foundation - * - * Licensed 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 <qpid/sys/Time.h> -#include "apr-1/apr_time.h" - -namespace qpid { -namespace sys { - -int64_t getTimeNsecs() -{ - // APR returns microseconds. - return apr_time_now() * 1000; -} - -int64_t getTimeMsecs() -{ - // APR returns microseconds. - return apr_time_now() / 1000; -} - - -}} - |
