Commit ac2246841d8bea402ff8d1f05bce4ade0d7363b2

Authored by Georg Hopp
1 parent e5534f93

Some fixes and additions. Dispatcher and handler now work.

... ... @@ -59,6 +59,7 @@ TR_CLASSVARS_DECL(TR_EventDispatcher) {
59 59 #define TR_DISPATCHER_EVENT_USER_WAIT 1
60 60 #define TR_DISPATCHER_EVENT_DATA_WAIT 2
61 61 #define TR_DISPATCHER_EVENT_SHUTDOWN 3
  62 +#define TR_DISPATCHER_EVENT_MAX ((size_t)TR_DISPATCHER_EVENT_SHUTDOWN)
62 63
63 64 void TR_eventDispatcherRegisterHandler(TR_EventDispatcher, TR_EventHandler);
64 65 void TR_eventDispatcherSetHeartbeat(TR_EventDispatcher, time_t);
... ...
... ... @@ -35,20 +35,36 @@ TR_CLASS(TR_EventHandler);
35 35
36 36 #include "tr/event_dispatcher.h"
37 37
38   -typedef int (* TR_EventMethod_fptr)(TR_Event);
  38 +typedef int (* TR_EventMethod_fptr)(TR_EventHandler, TR_Event);
39 39
40 40 TR_CLASS(TR_EventHandler) {
41 41 TR_EventDispatcher dispatcher[10];
42 42 size_t ndispatcher;
43   - TR_Hash event_methods;
44 43 };
45 44 TR_INSTANCE_INIT(TR_EventHandler);
46   -TR_CLASSVARS_DECL(TR_EventHandler) {};
  45 +TR_CLASSVARS_DECL(TR_EventHandler) {
  46 + TR_Hash event_methods;
  47 +};
47 48
48 49 void TR_eventHandlerSetDispatcher(TR_EventHandler, TR_EventDispatcher);
49 50 void TR_eventHandlerIssueEvent(TR_EventHandler, TR_EventSubject, int, void *);
50 51 int TR_eventHandlerHandleEvent(TR_EventHandler, TR_Event);
51 52
  53 +#define TR_EVENT_HANDLER_SET_METHOD(cls, subject, id, method) \
  54 + do { \
  55 + intptr_t key = TR_eventSubjectId(subject, id); \
  56 + TR_EventMethod_fptr event_func = (method); \
  57 + TR_hashAdd( \
  58 + TR_CLASSVARS(TR_EventHandler, cls)->event_methods, \
  59 + TR_new( \
  60 + TR_HashValue, \
  61 + &key, \
  62 + sizeof(intptr_t), \
  63 + &event_func, \
  64 + sizeof(TR_EventMethod_fptr))); \
  65 + } while(0)
  66 +
  67 +
52 68 #endif // __TR_EVENT_HANDLER_H__
53 69
54 70 // vim: set ts=4 sw=4:
... ...
1   -#ifndef __TR_EVENT_H__
2   -#define __TR_EVENT_H__
  1 +#ifndef __TR_EVENTLIB_H__
  2 +#define __TR_EVENTLIB_H__
3 3
4 4 #include "tr/event.h"
5 5 #include "tr/event_handler.h"
... ...
... ... @@ -64,11 +64,17 @@ eventDispatcherCvInit(TR_class_ptr cls)
64 64 TR_EVENT_CREATE(cls, TR_DISPATCHER_EVENT_SHUTDOWN);
65 65 }
66 66
  67 +intptr_t dispatcher_events[TR_DISPATCHER_EVENT_MAX + 1];
67 68 TR_INIT_IFACE(TR_Class, eventDispatcherCtor, eventDispatcherDtor, NULL);
68 69 TR_CREATE_CLASS(
69 70 TR_EventDispatcher,
70 71 TR_EventSubject,
71 72 eventDispatcherCvInit,
72   - TR_IF(TR_Class));
  73 + TR_IF(TR_Class)) = {
  74 + {
  75 + TR_DISPATCHER_EVENT_MAX + 1,
  76 + dispatcher_events
  77 + }
  78 +};
73 79
74 80 // vim: set ts=4 sw=4:
... ...
... ... @@ -45,7 +45,7 @@ doRegister(const void * _node, const void * data)
45 45 handler_queue->free_msgs = 0;
46 46 TR_hashAdd(
47 47 dispatcher->handler,
48   - TR_new(TR_HashValue, node->key, node->nkey, handler_queue, sizeof(TR_Queue)));
  48 + TR_new(TR_HashValue, node->key, node->nkey, &handler_queue, sizeof(TR_Queue)));
49 49 }
50 50
51 51 TR_queuePut(handler_queue, current_handler);
... ... @@ -56,7 +56,10 @@ TR_eventDispatcherRegisterHandler(TR_EventDispatcher this, TR_EventHandler handl
56 56 {
57 57 void * cb_data[] = { this, handler };
58 58
59   - TR_hashEach(handler->event_methods, cb_data, doRegister);
  59 + TR_hashEach(
  60 + TR_CLASSVARS(TR_EventHandler, TR_GET_CLASS(handler))->event_methods,
  61 + cb_data,
  62 + doRegister);
60 63 TR_eventHandlerSetDispatcher(handler, this);
61 64 }
62 65
... ...
... ... @@ -66,12 +66,18 @@ TR_eventDispatcherStart(TR_EventDispatcher this)
66 66 }
67 67
68 68 if (current) {
  69 + TR_Queue handler_queue;
69 70 TR_HashValue handler_queue_hv = TR_hashGetByVal(
70 71 this->handler,
71   - TR_sdbm((unsigned char *)current->id, sizeof(current->id)));
72   - TR_Queue handler_queue = handler_queue_hv->value;
  72 + TR_sdbm(
  73 + (unsigned char *)&(current->id),
  74 + sizeof(current->id)));
73 75
74   - if (! TR_queueEmpty(handler_queue)) {
  76 + handler_queue = handler_queue_hv
  77 + ? *(TR_Queue *)handler_queue_hv->value
  78 + : NULL;
  79 +
  80 + if (handler_queue && ! TR_queueEmpty(handler_queue)) {
75 81 TR_Queue queue_node = handler_queue->first;
76 82
77 83 while (queue_node) {
... ...
... ... @@ -32,22 +32,19 @@ int
32 32 eventHandlerCtor(void * _this, va_list * params) {
33 33 TR_EventHandler this = _this;
34 34
35   - this->ndispatcher = 0;
36   - this->event_methods = TR_new(TR_Hash);
  35 + this->ndispatcher = 0;
37 36
38 37 return 0;
39 38 }
40 39
41 40 static
42 41 void
43   -eventHandlerDtor(void * _this) {
44   - TR_EventHandler this = _this;
45   -
46   - TR_hashCleanup(this->event_methods);
47   - TR_delete(this->event_methods);
48   -}
  42 +eventHandlerDtor(void * _this) {}
49 43
  44 +TR_INSTANCE(TR_Hash, _event_methods);
50 45 TR_INIT_IFACE(TR_Class, eventHandlerCtor, eventHandlerDtor, NULL);
51   -TR_CREATE_CLASS(TR_EventHandler, NULL, NULL, TR_IF(TR_Class));
  46 +TR_CREATE_CLASS(TR_EventHandler, NULL, NULL, TR_IF(TR_Class)) = {
  47 + &(__event_methods.data)
  48 +};
52 49
53 50 // vim: set ts=4 sw=4:
... ...
... ... @@ -30,15 +30,18 @@
30 30 int
31 31 TR_eventHandlerHandleEvent(TR_EventHandler this, TR_Event event)
32 32 {
33   - TR_HashValue handle_func_hv = TR_hashGetByVal(
34   - this->event_methods,
35   - TR_sdbm((unsigned char *)event->id, sizeof(event->id)));
  33 + TR_EventMethod_fptr event_func = NULL;
  34 + TR_HashValue handle_func_hv = TR_hashGetByVal(
  35 + TR_CLASSVARS(TR_EventHandler, TR_GET_CLASS(this))->event_methods,
  36 + TR_sdbm((unsigned char *)&event->id, sizeof(event->id)));
36 37
37 38 if (! handle_func_hv) {
38 39 return 0;
39 40 }
40 41
41   - return ((TR_EventMethod_fptr)handle_func_hv->value)(event);
  42 + event_func = *(TR_EventMethod_fptr *)handle_func_hv->value;
  43 +
  44 + return event_func(this, event);
42 45 }
43 46
44 47 // vim: set ts=4 sw=4:
... ...
Please register or login to post a comment