Commit a17bd09dfed67e5ef9f08ca90a9053f5db3382d3

Authored by Georg Hopp
1 parent 83ee50f9

create explicit event done type and reissue event is no handler has returned EVENT_DONE.

@@ -32,6 +32,11 @@ @@ -32,6 +32,11 @@
32 32
33 TR_CLASS(TR_EventDispatcher); 33 TR_CLASS(TR_EventDispatcher);
34 34
  35 +typedef enum TR_EventDone_e {
  36 + TR_EVENT_DONE = 0,
  37 + TR_EVENT_PENDING
  38 +} TR_EventDone;
  39 +
35 #include "tr/event_handler.h" 40 #include "tr/event_handler.h"
36 41
37 typedef enum TR_e_EventDispatcherMode { 42 typedef enum TR_e_EventDispatcherMode {
@@ -35,7 +35,7 @@ TR_CLASS(TR_EventHandler); @@ -35,7 +35,7 @@ TR_CLASS(TR_EventHandler);
35 35
36 #include "tr/event_dispatcher.h" 36 #include "tr/event_dispatcher.h"
37 37
38 -typedef int (* TR_EventMethod_fptr)(TR_EventHandler, TR_Event); 38 +typedef TR_EventDone (* TR_EventMethod_fptr)(TR_EventHandler, TR_Event);
39 39
40 TR_CLASS(TR_EventHandler) { 40 TR_CLASS(TR_EventHandler) {
41 TR_EventDispatcher dispatcher[10]; 41 TR_EventDispatcher dispatcher[10];
@@ -46,9 +46,9 @@ TR_CLASSVARS_DECL(TR_EventHandler) { @@ -46,9 +46,9 @@ TR_CLASSVARS_DECL(TR_EventHandler) {
46 TR_Hash event_methods; 46 TR_Hash event_methods;
47 }; 47 };
48 48
49 -void TR_eventHandlerSetDispatcher(TR_EventHandler, TR_EventDispatcher);  
50 -void TR_eventHandlerIssueEvent(TR_EventHandler, TR_Event);  
51 -int TR_eventHandlerHandleEvent(TR_EventHandler, TR_Event); 49 +void TR_eventHandlerSetDispatcher(TR_EventHandler, TR_EventDispatcher);
  50 +void TR_eventHandlerIssueEvent(TR_EventHandler, TR_Event);
  51 +TR_EventDone TR_eventHandlerHandleEvent(TR_EventHandler, TR_Event);
52 52
53 #define TR_eventHandlerClassCleanup(cname) \ 53 #define TR_eventHandlerClassCleanup(cname) \
54 (TR__eventHandlerClassCleanup(TR_CLASS_BY_NAME(cname))) 54 (TR__eventHandlerClassCleanup(TR_CLASS_BY_NAME(cname)))
@@ -68,7 +68,6 @@ void TR__eventHandlerClassCleanup(TR_class_ptr); @@ -68,7 +68,6 @@ void TR__eventHandlerClassCleanup(TR_class_ptr);
68 sizeof(TR_EventMethod_fptr))); \ 68 sizeof(TR_EventMethod_fptr))); \
69 } while(0) 69 } while(0)
70 70
71 -  
72 #endif // __TR_EVENT_HANDLER_H__ 71 #endif // __TR_EVENT_HANDLER_H__
73 72
74 // vim: set ts=4 sw=4: 73 // vim: set ts=4 sw=4:
@@ -38,7 +38,9 @@ TR_eventDispatcherStart(TR_EventDispatcher this) @@ -38,7 +38,9 @@ TR_eventDispatcherStart(TR_EventDispatcher this)
38 while (this->running || (! TR_queueEmpty(this->events))) { 38 while (this->running || (! TR_queueEmpty(this->events))) {
39 struct timespec tp; 39 struct timespec tp;
40 int now; // milliseconds 40 int now; // milliseconds
41 - TR_Event current = NULL; 41 + TR_Event event;
  42 + TR_Queue handler_queue;
  43 + TR_HashValue handler_queue_hv;
42 44
43 clock_gettime(CLOCK_REALTIME, &tp); 45 clock_gettime(CLOCK_REALTIME, &tp);
44 now = tp.tv_sec * 1000 + tp.tv_nsec / 1000000; 46 now = tp.tv_sec * 1000 + tp.tv_nsec / 1000000;
@@ -55,43 +57,49 @@ TR_eventDispatcherStart(TR_EventDispatcher this) @@ -55,43 +57,49 @@ TR_eventDispatcherStart(TR_EventDispatcher this)
55 57
56 if (TR_queueEmpty(this->events)) { 58 if (TR_queueEmpty(this->events)) {
57 if (TR_EVD_CLIENT == this->mode) { 59 if (TR_EVD_CLIENT == this->mode) {
58 - current = TR_eventSubjectEmit( 60 + event = TR_eventSubjectEmit(
59 (TR_EventSubject)this, 61 (TR_EventSubject)this,
60 TR_DISPATCHER_EVENT_USER_WAIT, 62 TR_DISPATCHER_EVENT_USER_WAIT,
61 NULL); 63 NULL);
62 } else { 64 } else {
63 - current = TR_eventSubjectEmit( 65 + event = TR_eventSubjectEmit(
64 (TR_EventSubject)this, 66 (TR_EventSubject)this,
65 TR_DISPATCHER_EVENT_DATA_WAIT, 67 TR_DISPATCHER_EVENT_DATA_WAIT,
66 NULL); 68 NULL);
67 } 69 }
68 } else { 70 } else {
69 - current = TR_queueGet(this->events); 71 + event = TR_queueGet(this->events);
70 } 72 }
71 73
72 - if (current) {  
73 - TR_Queue handler_queue;  
74 - TR_HashValue handler_queue_hv = TR_hashGetByVal(  
75 - this->handler,  
76 - TR_sdbm(  
77 - (unsigned char *)&(current->id),  
78 - sizeof(current->id)));  
79 -  
80 - handler_queue = handler_queue_hv  
81 - ? *(TR_Queue *)handler_queue_hv->value  
82 - : NULL;  
83 -  
84 - if (handler_queue && ! TR_queueEmpty(handler_queue)) {  
85 - TR_Queue queue_node = handler_queue->first;  
86 -  
87 - while (queue_node) {  
88 - TR_EventHandler handler = queue_node->msg;  
89 - if (TR_eventHandlerHandleEvent(handler, current)) break;  
90 - queue_node = queue_node->next;  
91 - } 74 + handler_queue_hv = TR_hashGetByVal(
  75 + this->handler,
  76 + TR_sdbm(
  77 + (unsigned char *)&(event->id),
  78 + sizeof(event->id)));
  79 +
  80 + handler_queue = handler_queue_hv
  81 + ? *(TR_Queue *)handler_queue_hv->value
  82 + : NULL;
  83 +
  84 + if (handler_queue && ! TR_queueEmpty(handler_queue)) {
  85 + TR_Queue queue_node = handler_queue->first;
  86 + TR_EventDone done;
  87 +
  88 + while (queue_node) {
  89 + TR_EventHandler handler = queue_node->msg;
  90 + TR_EventDone this_done;
  91 +
  92 + this_done = TR_eventHandlerHandleEvent(handler, event);
  93 + done = TR_EVENT_DONE == done ? done : this_done;
  94 +
  95 + queue_node = queue_node->next;
92 } 96 }
93 97
94 - TR_delete(current); 98 + if (TR_EVENT_DONE == done) {
  99 + TR_delete(event);
  100 + } else {
  101 + TR_eventDispatcherEnqueueEvent(this, event);
  102 + }
95 } 103 }
96 } 104 }
97 } 105 }
@@ -27,7 +27,7 @@ @@ -27,7 +27,7 @@
27 #include "tr/event.h" 27 #include "tr/event.h"
28 #include "tr/event_handler.h" 28 #include "tr/event_handler.h"
29 29
30 -int 30 +TR_EventDone
31 TR_eventHandlerHandleEvent(TR_EventHandler this, TR_Event event) 31 TR_eventHandlerHandleEvent(TR_EventHandler this, TR_Event event)
32 { 32 {
33 TR_EventMethod_fptr event_func = NULL; 33 TR_EventMethod_fptr event_func = NULL;
Please register or login to post a comment