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
00023
00024
00025 #include <SharedObject.h>
00026 #include <ExceptionHolder.h>
00027 #include <boost/function.hpp>
00028 #include <memory>
00029
00030 namespace qpid {
00031 namespace sys {
00032
00033 class Event;
00034 class EventHandler;
00035 class EventChannel;
00036
00040 class Event
00041 {
00042 public:
00044 typedef boost::function0<void> Callback;
00045
00052 Event(Callback cb = 0) : callback(cb) {}
00053
00054 virtual ~Event();
00055
00057 void dispatch();
00058
00060 bool hasError() const;
00061
00063 void throwIfError() throw(Exception);
00064
00065 protected:
00066 virtual void prepare(EventHandler&);
00067 virtual Event* complete(EventHandler&);
00068 void setError(const ExceptionHolder& e);
00069
00070 Callback callback;
00071 ExceptionHolder error;
00072
00073 friend class EventChannel;
00074 friend class EventHandler;
00075 };
00076
00077 template <class BufT>
00078 class IOEvent : public Event {
00079 public:
00080 void getDescriptor() const { return descriptor; }
00081 size_t getSize() const { return size; }
00082 BufT getBuffer() const { return buffer; }
00083
00084 protected:
00085 IOEvent(int fd, Callback cb, size_t sz, BufT buf) :
00086 Event(cb), descriptor(fd), buffer(buf), size(sz) {}
00087
00088 int descriptor;
00089 BufT buffer;
00090 size_t size;
00091 };
00092
00094 class ReadEvent : public IOEvent<void*>
00095 {
00096 public:
00097 explicit ReadEvent(int fd=-1, void* buf=0, size_t sz=0, Callback cb=0) :
00098 IOEvent<void*>(fd, cb, sz, buf), received(0) {}
00099
00100 private:
00101 void prepare(EventHandler&);
00102 Event* complete(EventHandler&);
00103 ssize_t doRead();
00104
00105 size_t received;
00106 };
00107
00109 class WriteEvent : public IOEvent<const void*>
00110 {
00111 public:
00112 explicit WriteEvent(int fd=-1, const void* buf=0, size_t sz=0,
00113 Callback cb=0) :
00114 IOEvent<const void*>(fd, cb, sz, buf), written(0) {}
00115
00116 protected:
00117 void prepare(EventHandler&);
00118 Event* complete(EventHandler&);
00119
00120 private:
00121 ssize_t doWrite();
00122 size_t written;
00123 };
00124
00126 class AcceptEvent : public Event
00127 {
00128 public:
00130 explicit AcceptEvent(int fd=-1, Callback cb=0) :
00131 Event(cb), descriptor(fd), accepted(0) {}
00132
00134 int getAcceptedDesscriptor() const { return accepted; }
00135
00136 private:
00137 void prepare(EventHandler&);
00138 Event* complete(EventHandler&);
00139
00140 int descriptor;
00141 int accepted;
00142 };
00143
00144
00145 class QueueSet;
00146
00150 class EventChannel : public qpid::SharedObject<EventChannel>
00151 {
00152 public:
00153 static shared_ptr create();
00154
00155 ~EventChannel();
00156
00158 void postEvent(Event& event);
00159
00161 void postEvent(Event* event) { postEvent(*event); }
00162
00167 Event* getEvent();
00168
00169 private:
00170 EventChannel();
00171 boost::shared_ptr<EventHandler> handler;
00172 };
00173
00174
00175 }}
00176
00177
00178
00179 #endif