From: Michal Voců Date: Thu, 15 Mar 2007 17:33:18 +0000 (+0000) Subject: * singletons X-Git-Tag: glite-lb-server-bones_R_2_2_4_1~8 X-Git-Url: http://scientific.zcu.cz/git/?a=commitdiff_plain;h=98d62cc92cfd303e1d1b40b3f2a9b182f3d272b5;p=jra1mw.git * singletons --- diff --git a/org.glite.lb.logger/src-nt/EventManager.H b/org.glite.lb.logger/src-nt/EventManager.H new file mode 100644 index 0000000..1fa4cab --- /dev/null +++ b/org.glite.lb.logger/src-nt/EventManager.H @@ -0,0 +1,59 @@ +#ifndef _EVENT_MANAGER_H +#define _EVENT_MANAGER_H + + +class EventManager { +public: + // type for return code of event handler + typedef enum { + NOT_HANDLED, // the event was not handled at all + HANDLED, // the event was handled succesfully + HANDLED_FINAL // the event was handled, + // no other handlers should be called + } eventstatus_t; + + + static EventManager* getEventManager() { return &theEventManager; }; + + class Event { + public: + }; + + template + class EventHandler { + public: + + virtual eventstatus_t handleEvent(T *&e); + + eventstatus_t dispatchEvent(Event *&e) { + T *event = dynamic_cast(e); + + if(event) + return(handleEvent(event)); + else + return(NOT_HANDLED); + } + }; + + + void postEvent(Event &); + + template + bool registerHandler(EventHandler *); + + template + bool registerHandlerFirst(EventHandler *); + +private: + + // the event manager + static EventManager theEventManager; + + // private default constructor for singleton instance + EventManager() + {}; + +}; + + +#endif diff --git a/org.glite.lb.logger/src-nt/Exception.H b/org.glite.lb.logger/src-nt/Exception.H new file mode 100644 index 0000000..0fbac3e --- /dev/null +++ b/org.glite.lb.logger/src-nt/Exception.H @@ -0,0 +1,7 @@ +#ifndef _EXCEPTION_H +#define _EXCEPTION_H + +class Exception { +}; + +#endif diff --git a/org.glite.lb.logger/src-nt/HTTPTransport.cpp b/org.glite.lb.logger/src-nt/HTTPTransport.cpp index 343943a..8f495c3 100644 --- a/org.glite.lb.logger/src-nt/HTTPTransport.cpp +++ b/org.glite.lb.logger/src-nt/HTTPTransport.cpp @@ -146,7 +146,7 @@ HTTPTransport::onReady() } if(state != NONE) - ThreadPool::theThreadPool.queueWorkRead(this); + ThreadPool::instance()->queueWorkRead(this); else { std::cout << request << std::endl << headers << std::endl; std::cout.write(body, content_length); diff --git a/org.glite.lb.logger/src-nt/Makefile b/org.glite.lb.logger/src-nt/Makefile index abd2193..2ff9420 100644 --- a/org.glite.lb.logger/src-nt/Makefile +++ b/org.glite.lb.logger/src-nt/Makefile @@ -6,14 +6,33 @@ CFLAGS = -g -Wall COMPILEPP = $(CXX) $(CXXFLAGS) $(CFLAGS) COMPILE = $(CC) $(CFLAGS) -LINK = c++ $(LDFLAGS) +LINK = libtool --mode=link g++ $(LDFLAGS) THREAD_LIB = -lpthread +CPPUNIT_ROOT = /afs/ruk.cuni.cz/home/michal/egee/repository/externals/cppunit/1.10.2/slc3_ia32_gcc323 +CPPUNIT_LIB = -L$(CPPUNIT_ROOT)/lib -lcppunit -ldl +CPPUNIT_INCLUDE = -I$(CPPUNIT_ROOT)/include -plain: SocketInput.o PlainConnection.o HTTPTransport.o PluginManager.o ThreadPool.o main.o +TEST_OBJS= \ + test/ThreadPoolTest.o \ + test/PluginManagerTest.o \ + test/EventManagerTest.o \ + test/SingletonTest.o \ + test/test_main.o + +plain: SocketInput.o PlainConnection.o HTTPTransport.o ThreadPool.o main.o $(LINK) -o $@ $+ $(THREAD_LIB) +utest: ThreadPool.o PluginManager.o EventManager.o $(TEST_OBJS) + $(LINK) -o $@ $+ $(CPPUNIT_LIB) $(THREAD_LIB) + +stest: test/SingletonTest.o test/test_main.o + $(LINK) -o $@ $+ $(CPPUNIT_LIB) $(THREAD_LIB) + +$(TEST_OBJS): %.o: %.cpp + $(COMPILEPP) -I. $(CPPUNIT_INCLUDE) -o $@ -c $< + %.o: %.cpp $(COMPILEPP) -o $@ -c $< diff --git a/org.glite.lb.logger/src-nt/PluginManager.H b/org.glite.lb.logger/src-nt/PluginManager.H index 29556d5..7b5aa74 100644 --- a/org.glite.lb.logger/src-nt/PluginManager.H +++ b/org.glite.lb.logger/src-nt/PluginManager.H @@ -2,14 +2,13 @@ #define _PLUGIN_MANAGER_H #include - #include -class PluginManager { +#include "Singleton.H" + +class PluginManager: public Singleton { + friend class Singleton; public: - // the plugin manager instance - static PluginManager thePluginManager; - // base class for plugins class Plugin { @@ -17,7 +16,7 @@ public: const char *name; Plugin(const char *aname) : name(aname) { - PluginManager::thePluginManager.registerPlugin(this, aname); + PluginManager::instance()->registerPlugin(this, aname); } virtual bool initialize() = 0; @@ -53,13 +52,15 @@ public: } +protected: + // default constructor + PluginManager() : pluginList() + {}; + private: // list of registered plugins std::list pluginList; - // singleton class with private default constructor - PluginManager() : pluginList() - {}; }; diff --git a/org.glite.lb.logger/src-nt/PluginManager.cpp b/org.glite.lb.logger/src-nt/PluginManager.cpp index de9f981..df53043 100644 --- a/org.glite.lb.logger/src-nt/PluginManager.cpp +++ b/org.glite.lb.logger/src-nt/PluginManager.cpp @@ -1,4 +1,3 @@ #include "PluginManager.H" -PluginManager PluginManager::thePluginManager; diff --git a/org.glite.lb.logger/src-nt/Singleton.H b/org.glite.lb.logger/src-nt/Singleton.H new file mode 100644 index 0000000..ce03525 --- /dev/null +++ b/org.glite.lb.logger/src-nt/Singleton.H @@ -0,0 +1,39 @@ +#ifndef _SINGLETON_H +#define _SINGLETON_H + +#include + +#include "Exception.H" + +template +class Singleton { +public: + // obtain the singleton object + static T* instance() { + // XXX - is this really thread safe? + static pthread_mutex_t instance_lock = PTHREAD_MUTEX_INITIALIZER; + + pthread_mutex_lock(&instance_lock); + if(theInstance == NULL) { + theInstance = new T; + } + pthread_mutex_unlock(&instance_lock); + return(theInstance); + } + +protected: + // prevent other's from messing with the instance + Singleton() {} + Singleton(const Singleton &) {} + Singleton& operator=(const Singleton &) {} + ~Singleton() {} + +private: + static T *theInstance; +}; + +template +T *Singleton::theInstance = NULL; + + +#endif diff --git a/org.glite.lb.logger/src-nt/SocketInput.cpp b/org.glite.lb.logger/src-nt/SocketInput.cpp index 420ff22..f38cabb 100644 --- a/org.glite.lb.logger/src-nt/SocketInput.cpp +++ b/org.glite.lb.logger/src-nt/SocketInput.cpp @@ -31,7 +31,7 @@ SocketInput::SocketInput(const char *path, } bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)); listen(fd, SOCK_QUEUE_MAX); - ThreadPool::theThreadPool.setWorkAccept(this); + ThreadPool::instance()->setWorkAccept(this); } @@ -49,7 +49,7 @@ SocketInput::onReady() { Connection *conn = cFactory->accept(fd); Transport *trans = tFactory->newTransport(conn); - ThreadPool::theThreadPool.queueWorkRead(trans); + ThreadPool::instance()->queueWorkRead(trans); } diff --git a/org.glite.lb.logger/src-nt/ThreadPool.H b/org.glite.lb.logger/src-nt/ThreadPool.H index 9c17de9..b03d7e5 100644 --- a/org.glite.lb.logger/src-nt/ThreadPool.H +++ b/org.glite.lb.logger/src-nt/ThreadPool.H @@ -7,7 +7,10 @@ #include -class ThreadPool { +#include "Singleton.H" + +class ThreadPool : public Singleton { + friend class Singleton; public: const static int default_timeout = 5; @@ -35,11 +38,6 @@ public: }; public: - static ThreadPool theThreadPool; - - static ThreadPool *getThreadPool() - { return &theThreadPool; } - void startWorkers(unsigned int n); void stopWorkers(); diff --git a/org.glite.lb.logger/src-nt/ThreadPool.cpp b/org.glite.lb.logger/src-nt/ThreadPool.cpp index c57998d..763e930 100644 --- a/org.glite.lb.logger/src-nt/ThreadPool.cpp +++ b/org.glite.lb.logger/src-nt/ThreadPool.cpp @@ -10,7 +10,6 @@ #include "ThreadPool.H" #include "Exception.H" -ThreadPool ThreadPool::theThreadPool; static inline void @@ -231,7 +230,7 @@ ThreadPool::getWork() void ThreadPool::threadCleanup(void *data) { - ThreadPool *pool = ThreadPool::getThreadPool(); + ThreadPool *pool = ThreadPool::instance(); pthread_mutex_unlock(&(pool->work_queue_mutex)); } @@ -240,7 +239,7 @@ ThreadPool::threadCleanup(void *data) void * ThreadPool::threadMain(void *data) { - ThreadPool *pool = ThreadPool::getThreadPool(); + ThreadPool *pool = ThreadPool::instance(); WorkDescription *work_unit; pthread_cleanup_push(ThreadPool::threadCleanup, NULL); diff --git a/org.glite.lb.logger/src-nt/main.cpp b/org.glite.lb.logger/src-nt/main.cpp index 63dfb92..aa7ac45 100644 --- a/org.glite.lb.logger/src-nt/main.cpp +++ b/org.glite.lb.logger/src-nt/main.cpp @@ -12,7 +12,7 @@ int main(int argc, char *argv[]) SocketInput *input; // initialize plugins - PluginManager::thePluginManager.initialize(); + PluginManager::instance()->initialize(); // create unix socket with plain IO and HTTP transport input = new SocketInput(sock_path, @@ -20,14 +20,14 @@ int main(int argc, char *argv[]) &HTTPTransport::theFactory); // start worker threads - ThreadPool::theThreadPool.startWorkers(num_threads); + ThreadPool::instance()->startWorkers(num_threads); // run the main loop - ThreadPool::theThreadPool.run(); + ThreadPool::instance()->run(); // cleanup & exit delete input; - PluginManager::thePluginManager.cleanup(); + PluginManager::instance()->cleanup(); return 0; }