Browse Source

Change handler storage to TR_List and use TR_iterableForeach to iterate over them

1.0.0
Georg Hopp 10 years ago
parent
commit
67253158d6
  1. 27
      src/event_dispatcher_register_handler.c
  2. 19
      src/event_dispatcher_start.c

27
src/event_dispatcher_register_handler.c

@ -33,31 +33,32 @@ doRegister(const void * _node, const void * data)
TR_HashValue node = (TR_HashValue)_node; TR_HashValue node = (TR_HashValue)_node;
TR_EventDispatcher dispatcher = ((void **)data)[0]; TR_EventDispatcher dispatcher = ((void **)data)[0];
TR_EventHandler current_handler = ((void **)data)[1]; TR_EventHandler current_handler = ((void **)data)[1];
TR_HashValue handler_queue_hv;
TR_Queue handler_queue;
TR_HashValue handler_list_hv;
TR_List handler_list;
handler_queue_hv = TR_hashGetByVal(dispatcher->handler, node->hash);
handler_list_hv = TR_hashGetByVal(dispatcher->handler, node->hash);
if (handler_queue_hv) {
handler_queue = *(TR_Queue *)handler_queue_hv->value;
if (handler_list_hv) {
handler_list = *(TR_List *)handler_list_hv->value;
} else { } else {
handler_queue = TR_new(TR_Queue);
((TR_List)handler_queue)->free_msgs = 0;
handler_list = TR_new(TR_Queue);
handler_list->free_msgs = 0;
// TODO change TR_Queue to TR_Dynarray as this is no queue. // TODO change TR_Queue to TR_Dynarray as this is no queue.
handler_queue_hv = TR_new(
handler_list_hv = TR_new(
TR_HashValue, TR_HashValue,
node->key, node->key,
node->nkey, node->nkey,
&handler_queue,
sizeof(TR_Queue));
TR_hashAdd(dispatcher->handler, handler_queue_hv);
&handler_list,
sizeof(TR_List));
TR_hashAdd(dispatcher->handler, handler_list_hv);
} }
TR_queuePut(handler_queue, current_handler);
TR_listPut(handler_list, current_handler);
} }
void void
TR_eventDispatcherRegisterHandler(TR_EventDispatcher this, TR_EventHandler handler)
TR_eventDispatcherRegisterHandler(
TR_EventDispatcher this, TR_EventHandler handler)
{ {
void * cb_data[] = { this, handler }; void * cb_data[] = { this, handler };

19
src/event_dispatcher_start.c

@ -37,8 +37,8 @@ TR_eventDispatcherStart(TR_EventDispatcher this)
while (this->running || (! TR_queueEmpty(this->events))) { while (this->running || (! TR_queueEmpty(this->events))) {
TR_Event event; TR_Event event;
TR_Queue handler_queue;
TR_HashValue handler_queue_hv;
TR_List handler_list;
TR_HashValue handler_list_hv;
TR_eventDispatcherGetBeatTime(this); TR_eventDispatcherGetBeatTime(this);
@ -59,28 +59,25 @@ TR_eventDispatcherStart(TR_EventDispatcher this)
} }
} }
handler_queue_hv = TR_hashGetByVal(
handler_list_hv = TR_hashGetByVal(
this->handler, this->handler,
TR_sdbm( TR_sdbm(
(unsigned char *)&(event->id), (unsigned char *)&(event->id),
sizeof(event->id))); sizeof(event->id)));
handler_queue = handler_queue_hv
? *(TR_Queue *)handler_queue_hv->value
handler_list = handler_list_hv
? *(TR_List *)handler_list_hv->value
: NULL; : NULL;
if (handler_queue) {
size_t idx = TR_listFirst((TR_List)handler_queue);
if (handler_list) {
TR_EventDone done = TR_EVENT_PENDING; TR_EventDone done = TR_EVENT_PENDING;
while (idx != TR_listLast((TR_List)handler_queue) + 1) {
TR_EventHandler handler = ((TR_List)handler_queue)->data[idx];
TR_iterableForeach(handler_list) {
TR_EventHandler handler = TR_iterableCurrent(handler_list);
TR_EventDone this_done; TR_EventDone this_done;
this_done = TR_eventHandlerHandleEvent(handler, event); this_done = TR_eventHandlerHandleEvent(handler, event);
done = TR_EVENT_DONE == done ? done : this_done; done = TR_EVENT_DONE == done ? done : this_done;
idx = idx + 1 == TR_queueSize((TR_List)handler_queue) ? 0 : idx + 1;
} }
if (TR_EVENT_DONE == done) { if (TR_EVENT_DONE == done) {

Loading…
Cancel
Save