00001 #ifndef _sys_EventChannel_h
00002 #define _sys_EventChannel_h
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "qpid/SharedObject.h"
00023 #include "qpid/ExceptionHolder.h"
00024 #include <boost/function.hpp>
00025 #include <memory>
00026
00027 namespace qpid {
00028 namespace sys {
00029
00030 class Event;
00031 class EventHandler;
00032 class EventChannel;
00033
00037 class Event
00038 {
00039 public:
00041 typedef boost::function0<void> Callback;
00042
00049 Event(Callback cb = 0) : callback(cb) {}
00050
00051 virtual ~Event();
00052
00054 void dispatch();
00055
00057 bool hasError() const;
00058
00060 void throwIfError() throw(Exception);
00061
00062 protected:
00063 virtual void prepare(EventHandler&);
00064 virtual Event* complete(EventHandler&);
00065 void setError(const ExceptionHolder& e);
00066
00067 Callback callback;
00068 ExceptionHolder error;
00069
00070 friend class EventChannel;
00071 friend class EventHandler;
00072 };
00073
00074 template <class BufT>
00075 class IOEvent : public Event {
00076 public:
00077 void getDescriptor() const { return descriptor; }
00078 size_t getSize() const { return size; }
00079 BufT getBuffer() const { return buffer; }
00080
00081 protected:
00082 IOEvent(int fd, Callback cb, size_t sz, BufT buf) :
00083 Event(cb), descriptor(fd), buffer(buf), size(sz) {}
00084
00085 int descriptor;
00086 BufT buffer;
00087 size_t size;
00088 };
00089
00091 class ReadEvent : public IOEvent<void*>
00092 {
00093 public:
00094 explicit ReadEvent(int fd=-1, void* buf=0, size_t sz=0, Callback cb=0) :
00095 IOEvent<void*>(fd, cb, sz, buf), received(0) {}
00096
00097 private:
00098 void prepare(EventHandler&);
00099 Event* complete(EventHandler&);
00100 ssize_t doRead();
00101
00102 size_t received;
00103 };
00104
00106 class WriteEvent : public IOEvent<const void*>
00107 {
00108 public:
00109 explicit WriteEvent(int fd=-1, const void* buf=0, size_t sz=0,
00110 Callback cb=0) :
00111 IOEvent<const void*>(fd, cb, sz, buf), written(0) {}
00112
00113 protected:
00114 void prepare(EventHandler&);
00115 Event* complete(EventHandler&);
00116
00117 private:
00118 ssize_t doWrite();
00119 size_t written;
00120 };
00121
00123 class AcceptEvent : public Event
00124 {
00125 public:
00127 explicit AcceptEvent(int fd=-1, Callback cb=0) :
00128 Event(cb), descriptor(fd), accepted(0) {}
00129
00131 int getAcceptedDesscriptor() const { return accepted; }
00132
00133 private:
00134 void prepare(EventHandler&);
00135 Event* complete(EventHandler&);
00136
00137 int descriptor;
00138 int accepted;
00139 };
00140
00141
00142 class QueueSet;
00143
00147 class EventChannel : public qpid::SharedObject<EventChannel>
00148 {
00149 public:
00150 static shared_ptr create();
00151
00152 ~EventChannel();
00153
00155 void postEvent(Event& event);
00156
00158 void postEvent(Event* event) { postEvent(*event); }
00159
00164 Event* getEvent();
00165
00166 private:
00167 EventChannel();
00168 boost::shared_ptr<EventHandler> handler;
00169 };
00170
00171
00172 }}
00173
00174
00175
00176 #endif