Browse Source

Some fixes and additions. Dispatcher and handler now work.

1.0.0
Georg Hopp 12 years ago
parent
commit
ac2246841d
  1. 1
      include/tr/event_dispatcher.h
  2. 22
      include/tr/event_handler.h
  3. 4
      include/trevent.h
  4. 8
      src/event_dispatcher.c
  5. 7
      src/event_dispatcher_register_handler.c
  6. 12
      src/event_dispatcher_start.c
  7. 15
      src/event_handler.c
  8. 11
      src/event_handler_handle_event.c

1
include/tr/event_dispatcher.h

@ -59,6 +59,7 @@ TR_CLASSVARS_DECL(TR_EventDispatcher) {
#define TR_DISPATCHER_EVENT_USER_WAIT 1
#define TR_DISPATCHER_EVENT_DATA_WAIT 2
#define TR_DISPATCHER_EVENT_SHUTDOWN 3
#define TR_DISPATCHER_EVENT_MAX ((size_t)TR_DISPATCHER_EVENT_SHUTDOWN)
void TR_eventDispatcherRegisterHandler(TR_EventDispatcher, TR_EventHandler);
void TR_eventDispatcherSetHeartbeat(TR_EventDispatcher, time_t);

22
include/tr/event_handler.h

@ -35,20 +35,36 @@ TR_CLASS(TR_EventHandler);
#include "tr/event_dispatcher.h"
typedef int (* TR_EventMethod_fptr)(TR_Event);
typedef int (* TR_EventMethod_fptr)(TR_EventHandler, TR_Event);
TR_CLASS(TR_EventHandler) {
TR_EventDispatcher dispatcher[10];
size_t ndispatcher;
TR_Hash event_methods;
};
TR_INSTANCE_INIT(TR_EventHandler);
TR_CLASSVARS_DECL(TR_EventHandler) {};
TR_CLASSVARS_DECL(TR_EventHandler) {
TR_Hash event_methods;
};
void TR_eventHandlerSetDispatcher(TR_EventHandler, TR_EventDispatcher);
void TR_eventHandlerIssueEvent(TR_EventHandler, TR_EventSubject, int, void *);
int TR_eventHandlerHandleEvent(TR_EventHandler, TR_Event);
#define TR_EVENT_HANDLER_SET_METHOD(cls, subject, id, method) \
do { \
intptr_t key = TR_eventSubjectId(subject, id); \
TR_EventMethod_fptr event_func = (method); \
TR_hashAdd( \
TR_CLASSVARS(TR_EventHandler, cls)->event_methods, \
TR_new( \
TR_HashValue, \
&key, \
sizeof(intptr_t), \
&event_func, \
sizeof(TR_EventMethod_fptr))); \
} while(0)
#endif // __TR_EVENT_HANDLER_H__
// vim: set ts=4 sw=4:

4
include/trevent.h

@ -1,5 +1,5 @@
#ifndef __TR_EVENT_H__
#define __TR_EVENT_H__
#ifndef __TR_EVENTLIB_H__
#define __TR_EVENTLIB_H__
#include "tr/event.h"
#include "tr/event_handler.h"

8
src/event_dispatcher.c

@ -64,11 +64,17 @@ eventDispatcherCvInit(TR_class_ptr cls)
TR_EVENT_CREATE(cls, TR_DISPATCHER_EVENT_SHUTDOWN);
}
intptr_t dispatcher_events[TR_DISPATCHER_EVENT_MAX + 1];
TR_INIT_IFACE(TR_Class, eventDispatcherCtor, eventDispatcherDtor, NULL);
TR_CREATE_CLASS(
TR_EventDispatcher,
TR_EventSubject,
eventDispatcherCvInit,
TR_IF(TR_Class));
TR_IF(TR_Class)) = {
{
TR_DISPATCHER_EVENT_MAX + 1,
dispatcher_events
}
};
// vim: set ts=4 sw=4:

7
src/event_dispatcher_register_handler.c

@ -45,7 +45,7 @@ doRegister(const void * _node, const void * data)
handler_queue->free_msgs = 0;
TR_hashAdd(
dispatcher->handler,
TR_new(TR_HashValue, node->key, node->nkey, handler_queue, sizeof(TR_Queue)));
TR_new(TR_HashValue, node->key, node->nkey, &handler_queue, sizeof(TR_Queue)));
}
TR_queuePut(handler_queue, current_handler);
@ -56,7 +56,10 @@ TR_eventDispatcherRegisterHandler(TR_EventDispatcher this, TR_EventHandler handl
{
void * cb_data[] = { this, handler };
TR_hashEach(handler->event_methods, cb_data, doRegister);
TR_hashEach(
TR_CLASSVARS(TR_EventHandler, TR_GET_CLASS(handler))->event_methods,
cb_data,
doRegister);
TR_eventHandlerSetDispatcher(handler, this);
}

12
src/event_dispatcher_start.c

@ -66,12 +66,18 @@ TR_eventDispatcherStart(TR_EventDispatcher this)
}
if (current) {
TR_Queue handler_queue;
TR_HashValue handler_queue_hv = TR_hashGetByVal(
this->handler,
TR_sdbm((unsigned char *)current->id, sizeof(current->id)));
TR_Queue handler_queue = handler_queue_hv->value;
TR_sdbm(
(unsigned char *)&(current->id),
sizeof(current->id)));
if (! TR_queueEmpty(handler_queue)) {
handler_queue = handler_queue_hv
? *(TR_Queue *)handler_queue_hv->value
: NULL;
if (handler_queue && ! TR_queueEmpty(handler_queue)) {
TR_Queue queue_node = handler_queue->first;
while (queue_node) {

15
src/event_handler.c

@ -32,22 +32,19 @@ int
eventHandlerCtor(void * _this, va_list * params) {
TR_EventHandler this = _this;
this->ndispatcher = 0;
this->event_methods = TR_new(TR_Hash);
this->ndispatcher = 0;
return 0;
}
static
void
eventHandlerDtor(void * _this) {
TR_EventHandler this = _this;
TR_hashCleanup(this->event_methods);
TR_delete(this->event_methods);
}
eventHandlerDtor(void * _this) {}
TR_INSTANCE(TR_Hash, _event_methods);
TR_INIT_IFACE(TR_Class, eventHandlerCtor, eventHandlerDtor, NULL);
TR_CREATE_CLASS(TR_EventHandler, NULL, NULL, TR_IF(TR_Class));
TR_CREATE_CLASS(TR_EventHandler, NULL, NULL, TR_IF(TR_Class)) = {
&(__event_methods.data)
};
// vim: set ts=4 sw=4:

11
src/event_handler_handle_event.c

@ -30,15 +30,18 @@
int
TR_eventHandlerHandleEvent(TR_EventHandler this, TR_Event event)
{
TR_HashValue handle_func_hv = TR_hashGetByVal(
this->event_methods,
TR_sdbm((unsigned char *)event->id, sizeof(event->id)));
TR_EventMethod_fptr event_func = NULL;
TR_HashValue handle_func_hv = TR_hashGetByVal(
TR_CLASSVARS(TR_EventHandler, TR_GET_CLASS(this))->event_methods,
TR_sdbm((unsigned char *)&event->id, sizeof(event->id)));
if (! handle_func_hv) {
return 0;
}
return ((TR_EventMethod_fptr)handle_func_hv->value)(event);
event_func = *(TR_EventMethod_fptr *)handle_func_hv->value;
return event_func(this, event);
}
// vim: set ts=4 sw=4:
Loading…
Cancel
Save