* singletons
authorMichal Voců <michal@ruk.cuni.cz>
Thu, 15 Mar 2007 17:33:18 +0000 (17:33 +0000)
committerMichal Voců <michal@ruk.cuni.cz>
Thu, 15 Mar 2007 17:33:18 +0000 (17:33 +0000)
org.glite.lb.logger/src-nt/EventManager.H [new file with mode: 0644]
org.glite.lb.logger/src-nt/Exception.H [new file with mode: 0644]
org.glite.lb.logger/src-nt/HTTPTransport.cpp
org.glite.lb.logger/src-nt/Makefile
org.glite.lb.logger/src-nt/PluginManager.H
org.glite.lb.logger/src-nt/PluginManager.cpp
org.glite.lb.logger/src-nt/Singleton.H [new file with mode: 0644]
org.glite.lb.logger/src-nt/SocketInput.cpp
org.glite.lb.logger/src-nt/ThreadPool.H
org.glite.lb.logger/src-nt/ThreadPool.cpp
org.glite.lb.logger/src-nt/main.cpp

diff --git a/org.glite.lb.logger/src-nt/EventManager.H b/org.glite.lb.logger/src-nt/EventManager.H
new file mode 100644 (file)
index 0000000..1fa4cab
--- /dev/null
@@ -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 T>
+       class EventHandler {
+       public:
+
+               virtual eventstatus_t handleEvent(T *&e);
+
+               eventstatus_t dispatchEvent(Event *&e) {
+                       T *event = dynamic_cast<T*>(e);
+                       
+                       if(event) 
+                               return(handleEvent(event));
+                       else
+                               return(NOT_HANDLED);
+               }
+       };
+
+
+       void postEvent(Event &);
+
+       template<class T>
+       bool registerHandler(EventHandler<T> *);
+
+       template<class T>
+       bool registerHandlerFirst(EventHandler<T> *);
+
+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 (file)
index 0000000..0fbac3e
--- /dev/null
@@ -0,0 +1,7 @@
+#ifndef _EXCEPTION_H
+#define _EXCEPTION_H
+
+class Exception {
+};
+
+#endif
index 343943a..8f495c3 100644 (file)
@@ -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);
index abd2193..2ff9420 100644 (file)
@@ -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 $<
 
index 29556d5..7b5aa74 100644 (file)
@@ -2,14 +2,13 @@
 #define _PLUGIN_MANAGER_H
 
 #include <list>
-
 #include <iostream>
 
-class PluginManager {
+#include "Singleton.H"
+
+class PluginManager: public Singleton<PluginManager> {
+       friend class Singleton<PluginManager>;
 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<Plugin *> pluginList;
 
-       // singleton class with private default constructor
-       PluginManager() : pluginList()
-               {};
 };
 
 
index de9f981..df53043 100644 (file)
@@ -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 (file)
index 0000000..ce03525
--- /dev/null
@@ -0,0 +1,39 @@
+#ifndef _SINGLETON_H
+#define _SINGLETON_H
+
+#include <pthread.h>
+
+#include "Exception.H"
+
+template <class T>
+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<class T>
+T *Singleton<T>::theInstance = NULL;
+
+
+#endif
index 420ff22..f38cabb 100644 (file)
@@ -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);
 }
 
 
index 9c17de9..b03d7e5 100644 (file)
@@ -7,7 +7,10 @@
 
 #include <list>
 
-class ThreadPool {
+#include "Singleton.H"
+
+class ThreadPool : public Singleton<ThreadPool> {
+       friend class Singleton<ThreadPool>;
 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();
 
index c57998d..763e930 100644 (file)
@@ -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); 
index 63dfb92..aa7ac45 100644 (file)
@@ -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;
 }