--- /dev/null
+#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
--- /dev/null
+#ifndef _EXCEPTION_H
+#define _EXCEPTION_H
+
+class Exception {
+};
+
+#endif
}
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);
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 $<
#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 {
const char *name;
Plugin(const char *aname) : name(aname) {
- PluginManager::thePluginManager.registerPlugin(this, aname);
+ PluginManager::instance()->registerPlugin(this, aname);
}
virtual bool initialize() = 0;
}
+protected:
+ // default constructor
+ PluginManager() : pluginList()
+ {};
+
private:
// list of registered plugins
std::list<Plugin *> pluginList;
- // singleton class with private default constructor
- PluginManager() : pluginList()
- {};
};
#include "PluginManager.H"
-PluginManager PluginManager::thePluginManager;
--- /dev/null
+#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
}
bind(fd, (struct sockaddr *)&saddr, sizeof(saddr));
listen(fd, SOCK_QUEUE_MAX);
- ThreadPool::theThreadPool.setWorkAccept(this);
+ ThreadPool::instance()->setWorkAccept(this);
}
{
Connection *conn = cFactory->accept(fd);
Transport *trans = tFactory->newTransport(conn);
- ThreadPool::theThreadPool.queueWorkRead(trans);
+ ThreadPool::instance()->queueWorkRead(trans);
}
#include <list>
-class ThreadPool {
+#include "Singleton.H"
+
+class ThreadPool : public Singleton<ThreadPool> {
+ friend class Singleton<ThreadPool>;
public:
const static int default_timeout = 5;
};
public:
- static ThreadPool theThreadPool;
-
- static ThreadPool *getThreadPool()
- { return &theThreadPool; }
-
void startWorkers(unsigned int n);
void stopWorkers();
#include "ThreadPool.H"
#include "Exception.H"
-ThreadPool ThreadPool::theThreadPool;
static inline
void
void
ThreadPool::threadCleanup(void *data)
{
- ThreadPool *pool = ThreadPool::getThreadPool();
+ ThreadPool *pool = ThreadPool::instance();
pthread_mutex_unlock(&(pool->work_queue_mutex));
}
void *
ThreadPool::threadMain(void *data)
{
- ThreadPool *pool = ThreadPool::getThreadPool();
+ ThreadPool *pool = ThreadPool::instance();
WorkDescription *work_unit;
pthread_cleanup_push(ThreadPool::threadCleanup, NULL);
SocketInput *input;
// initialize plugins
- PluginManager::thePluginManager.initialize();
+ PluginManager::instance()->initialize();
// create unix socket with plain IO and HTTP transport
input = new SocketInput(sock_path,
&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;
}