00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef _GLASS_PLUGIN_EVENT_HH_
00022 #define _GLASS_PLUGIN_EVENT_HH_
00023
00024 #include <map>
00025 #include <queue>
00026 #include <set>
00027 #include "types.h"
00028 #include "plugin.h"
00029 #include "exception.h"
00030 #include "packager.h"
00031 #include <boost/thread.hpp>
00032 #include "serial.h"
00033
00034 namespace libglass {
00035
00036 class _Event;
00037
00043 class EventBase : public PluginBase {
00047 PLUGIN(EventBase);
00048 private:
00049
00050 std::map<const string *, _Event *,
00051 StringPointerCompare> events;
00052 std::map<string, std::set<nodeId> > owners;
00053 boost::mutex events_lock;
00055 protected:
00056 friend class _Event;
00057
00068 bool registerEvent(_Event *e);
00069
00080 bool unregisterEvent(_Event *e);
00081
00082
00083
00084
00085
00086
00087
00088
00089 #if defined(_MSC_VER) && (_MSC_VER < 1300) // earlier than .NET compiler (VC 7.0)
00090 public:
00091 #else
00092 protected:
00093 #endif
00094
00098 ~EventBase();
00099
00100 public:
00101 };
00102
00103 class LIBGLASS_API _Event : public PluginInterface {
00104 private:
00105 EventBase *eb;
00106 string name;
00108 protected:
00109 friend class EventBase;
00110 friend class EventPacket;
00111
00115 _Event(const char *name) throw(Exception);
00116
00120 virtual ~_Event();
00121
00132 bool _enqueueEvent(chain &s, nodeId target);
00133
00140 bool _enqueueEvent(chain &s, std::vector<nodeId> &targets);
00141
00150 virtual bool pushEvent(chain &s, nodeId sender) = 0;
00151 public:
00152 };
00153
00157 template<typename T,
00158 Serializer(*packf, T) = toString<T>,
00159 Unserializer(*unpackf, T) = fromString<T> >
00160 class Event : public _Event {
00161 private:
00162 struct _EventData {
00163 T data;
00164 nodeId sender;
00165 unsigned int used;
00166 };
00167 std::queue<_EventData> q;
00168 void (*handler)(T &);
00170 protected:
00171
00178 bool pushEvent(chain &c, nodeId sender) {
00179 _EventData d;
00180 unsigned int initial = 0;
00181
00182
00183
00184 if (unpackf(c, initial, d.data, d.used, 1, NULL) == false) {
00185 return false;
00186 }
00187 d.sender = sender;
00188
00189 if (handler) {
00190 handler(d.data);
00191 }
00192 else {
00193 q.push(d);
00194 }
00195 return true;
00196 }
00197 public:
00198
00199 Event(const char *name, void (*func)(T &data) = NULL) throw(Exception) :
00200 _Event(name), handler(func) {
00201 }
00202
00203 ~Event() {
00204 }
00205
00209 void clear(void) {
00210 while (!q.empty()) {
00211 _EventData d(q.front());
00212 q.pop();
00213
00214 }
00215 }
00216
00227 bool enqueueEvent(const T data, nodeId target = everybodyId, unsigned int len = 1) {
00228 chain c;
00229 if (packf(c, data, len, NULL) == false) {
00230 return false;
00231 }
00232 return _enqueueEvent(c, target);
00233 }
00234
00235 #ifdef TODO
00236
00243 bool enqueueEvent(T data, std::vector<nodeId> &targets) {
00244
00245 return false;
00246 }
00247 #endif
00248
00254 T getEvent(nodeId id = everybodyId, unsigned int *used = 0) throw(EmptyException) {
00255 if (q.empty()) {
00256 throw EmptyException();
00257 }
00258
00259 if (id == everybodyId) {
00260 _EventData d(q.front());
00261 q.pop();
00262 if (used) *used = d.used;
00263 return d.data;
00264 }
00265 else {
00266
00267 throw EmptyException();
00268 }
00269 }
00270
00277 inline bool isEmpty(void) {
00278 return q.empty();
00279 }
00280
00292 bool setHandler(void (*func)(T &data)) {
00293 this->handler = func;
00294
00295 if (!func) {
00296 return true;
00297 }
00298
00299 while (!q.empty()) {
00300 handler(q.front());
00301 q.pop();
00302 }
00303
00304 return true;
00305 }
00306
00311 unsigned int queueSize(void) {
00312 return q.size();
00313 }
00314
00315 };
00316
00317 class EventSerializable : public _Event {
00318 private:
00319 std::queue<SerializableObject *> q;
00320 void (*handler)(SerializableObject *o);
00321
00322 protected:
00323 bool pushEvent(chain &s, nodeId sender);
00324
00325 public:
00326
00327 EventSerializable(const char *name,
00328 void (*func)(SerializableObject *o) = NULL) throw(Exception);
00329
00330 ~EventSerializable();
00331
00335 void clear(void);
00336
00345 bool enqueueEvent(SerializableObject &o, nodeId target = everybodyId);
00346
00347 #ifdef TODO
00348 bool enqueueEvent(SerializableObject &o, std::vector<nodeId> &targets);
00349 #endif
00350
00358 SerializableObject *getEvent(void) throw(EmptyException);
00359
00366 bool isEmpty(void);
00367
00379 bool setHandler(void (*func)(SerializableObject *o));
00380
00385 unsigned int queueSize(void);
00386 };
00387
00388 }
00389
00390
00391 #endif // _GLASS_PLUGIN_EVENT_HH_