From f1d2bae0dc11aeabf61075be3e76db37b8c6c279 Mon Sep 17 00:00:00 2001 From: Georg Hopp Date: Tue, 5 Aug 2014 19:05:08 +0100 Subject: [PATCH] add forgotten new files...oops --- src/event_dispatcher_get_beat_time.c | 35 +++++++++ src/event_dispatcher_get_data_wait_time.c | 39 ++++++++++ src/event_dispatcher_register_handler.c | 63 ++++++++++++++++ src/event_dispatcher_set_hearbeat.c | 41 +++++++++++ src/event_dispatcher_shutdown.c | 45 ++++++++++++ src/event_dispatcher_start.c | 87 +++++++++++++++++++++++ src/event_handler_handle_event.c | 44 ++++++++++++ src/event_handler_issue_event.c | 44 ++++++++++++ src/event_handler_set_dispatcher.c | 40 +++++++++++ 9 files changed, 438 insertions(+) create mode 100644 src/event_dispatcher_get_beat_time.c create mode 100644 src/event_dispatcher_get_data_wait_time.c create mode 100644 src/event_dispatcher_register_handler.c create mode 100644 src/event_dispatcher_set_hearbeat.c create mode 100644 src/event_dispatcher_shutdown.c create mode 100644 src/event_dispatcher_start.c create mode 100644 src/event_handler_handle_event.c create mode 100644 src/event_handler_issue_event.c create mode 100644 src/event_handler_set_dispatcher.c diff --git a/src/event_dispatcher_get_beat_time.c b/src/event_dispatcher_get_beat_time.c new file mode 100644 index 0000000..9fa054f --- /dev/null +++ b/src/event_dispatcher_get_beat_time.c @@ -0,0 +1,35 @@ +/** + * \file + * + * \author Georg Hopp + * + * \copyright + * Copyright © 2012 Georg Hopp + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include + +#include "trbase.h" + +#include "tr/event_dispatcher.h" + +time_t +TR_eventDispatcherGetBeatTime(TR_EventDispatcher this) +{ + return this->nextbeat - time(NULL); +} + +// vim: set ts=4 sw=4: diff --git a/src/event_dispatcher_get_data_wait_time.c b/src/event_dispatcher_get_data_wait_time.c new file mode 100644 index 0000000..77d7285 --- /dev/null +++ b/src/event_dispatcher_get_data_wait_time.c @@ -0,0 +1,39 @@ +/** + * \file + * + * \author Georg Hopp + * + * \copyright + * Copyright © 2012 Georg Hopp + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include + +#include "trbase.h" + +#include "tr/event_dispatcher.h" + +time_t +TR_eventDispatcherGetDataWaitTime(TR_EventDispatcher this) +{ + if (TR_EVD_SERVER == this->mode) { + return TR_eventDispatcherGetBeatTime(this); + } + + return 0; +} + +// vim: set ts=4 sw=4: diff --git a/src/event_dispatcher_register_handler.c b/src/event_dispatcher_register_handler.c new file mode 100644 index 0000000..fbd3983 --- /dev/null +++ b/src/event_dispatcher_register_handler.c @@ -0,0 +1,63 @@ +/** + * \file + * + * \author Georg Hopp + * + * \copyright + * Copyright © 2012 Georg Hopp + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "trbase.h" +#include "trdata.h" + +#include "tr/event_dispatcher.h" +#include "tr/event_handler.h" + +static +void +doRegister(const void * _node, const void * data) +{ + TR_HashValue node = (TR_HashValue)_node; + TR_EventDispatcher dispatcher = ((void **)data)[0]; + TR_EventHandler current_handler = ((void **)data)[1]; + TR_HashValue handler_queue_hv; + TR_Queue handler_queue; + + handler_queue_hv = TR_hashGetByVal(dispatcher->handler, node->hash); + + if (handler_queue_hv) { + handler_queue = (TR_Queue)handler_queue_hv->value; + } else { + handler_queue = TR_new(TR_Queue); + handler_queue->free_msgs = 0; + TR_hashAdd( + dispatcher->handler, + TR_new(TR_HashValue, node->key, node->nkey, handler_queue, sizeof(TR_Queue))); + } + + TR_queuePut(handler_queue, current_handler); +} + +void +TR_eventDispatcherRegisterHandler(TR_EventDispatcher this, TR_EventHandler handler) +{ + void * cb_data[] = { this, handler }; + + TR_hashEach(handler->event_methods, cb_data, doRegister); + TR_eventHandlerSetDispatcher(handler, this); +} + +// vim: set ts=4 sw=4: diff --git a/src/event_dispatcher_set_hearbeat.c b/src/event_dispatcher_set_hearbeat.c new file mode 100644 index 0000000..69a6d38 --- /dev/null +++ b/src/event_dispatcher_set_hearbeat.c @@ -0,0 +1,41 @@ +/** + * \file + * + * \author Georg Hopp + * + * \copyright + * Copyright © 2012 Georg Hopp + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include + +#include "trbase.h" + +#include "tr/event_dispatcher.h" + +void +TR_eventDispatcherSetHeartbeat(TR_EventDispatcher this, time_t heartbeat) +{ + this->heartbeat = heartbeat; + + if (this->heartbeat) { + this->nextbeat = time(NULL) + this->heartbeat; + } else { + this->nextbeat = 0; + } +} + +// vim: set ts=4 sw=4: diff --git a/src/event_dispatcher_shutdown.c b/src/event_dispatcher_shutdown.c new file mode 100644 index 0000000..3283c17 --- /dev/null +++ b/src/event_dispatcher_shutdown.c @@ -0,0 +1,45 @@ +/** + * \file + * + * \author Georg Hopp + * + * \copyright + * Copyright © 2012 Georg Hopp + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include + +#include "trbase.h" +#include "trdata.h" +#include "trhash.h" + +#include "tr/event.h" +#include "tr/event_subject.h" +#include "tr/event_dispatcher.h" + +void +TR_eventDispatcherShutdown(TR_EventDispatcher this) +{ + TR_eventDispatcherEnqueueEvent( + this, + TR_eventSubjectEmit( + (TR_EventSubject)this, + TR_DISPATCHER_EVENT_SHUTDOWN, + NULL)); + TR_eventDispatcherStop(this); +} + +// vim: set ts=4 sw=4: diff --git a/src/event_dispatcher_start.c b/src/event_dispatcher_start.c new file mode 100644 index 0000000..870f3ea --- /dev/null +++ b/src/event_dispatcher_start.c @@ -0,0 +1,87 @@ +/** + * \file + * + * \author Georg Hopp + * + * \copyright + * Copyright © 2012 Georg Hopp + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include + +#include "trbase.h" +#include "trdata.h" +#include "trhash.h" + +#include "tr/event.h" +#include "tr/event_subject.h" +#include "tr/event_dispatcher.h" + +void +TR_eventDispatcherStart(TR_EventDispatcher this) +{ + this->running = 1; + + while (this->running || (! TR_queueEmpty(this->events))) { + time_t now = time(NULL); + TR_Event current = NULL; + + if (this->nextbeat && this->nextbeat < now) { + this->nextbeat += this->heartbeat; + TR_eventDispatcherEnqueueEvent( + this, + TR_eventSubjectEmit( + (TR_EventSubject)this, + TR_DISPATCHER_EVENT_HEARTBEAT, + NULL)); + } + + if (TR_queueEmpty(this->events)) { + if (TR_EVD_CLIENT == this->mode) { + current = TR_eventSubjectEmit( + (TR_EventSubject)this, + TR_DISPATCHER_EVENT_USER_WAIT, + NULL); + } else { + current = TR_eventSubjectEmit( + (TR_EventSubject)this, + TR_DISPATCHER_EVENT_DATA_WAIT, + NULL); + } + } else { + current = TR_queueGet(this->events); + } + + if (current) { + 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; + + if (! TR_queueEmpty(handler_queue)) { + TR_Queue queue_node = handler_queue->first; + + while (queue_node) { + TR_EventHandler handler = queue_node->msg; + if (TR_eventHandlerHandleEvent(handler, current)) break; + queue_node = queue_node->next; + } + } + } + } +} + +// vim: set ts=4 sw=4: diff --git a/src/event_handler_handle_event.c b/src/event_handler_handle_event.c new file mode 100644 index 0000000..9bc1cdf --- /dev/null +++ b/src/event_handler_handle_event.c @@ -0,0 +1,44 @@ +/** + * \file + * + * \author Georg Hopp + * + * \copyright + * Copyright © 2012 Georg Hopp + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "trbase.h" +#include "trdata.h" +#include "trhash.h" + +#include "tr/event.h" +#include "tr/event_handler.h" + +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))); + + if (! handle_func_hv) { + return 0; + } + + return ((TR_EventMethod_fptr)handle_func_hv->value)(event); +} + +// vim: set ts=4 sw=4: diff --git a/src/event_handler_issue_event.c b/src/event_handler_issue_event.c new file mode 100644 index 0000000..b4101de --- /dev/null +++ b/src/event_handler_issue_event.c @@ -0,0 +1,44 @@ +/** + * \file + * + * \author Georg Hopp + * + * \copyright + * Copyright © 2012 Georg Hopp + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "trbase.h" + +#include "tr/event.h" +#include "tr/event_handler.h" +#include "tr/event_subject.h" + +void +TR_eventHandlerIssueEvent( + TR_EventHandler this, + TR_EventSubject subject, + int idx, + void * data) +{ + TR_Event event = TR_eventSubjectEmit(subject, idx, data); + int i; + + for (i=0; indispatcher; i++) { + TR_eventDispatcherEnqueueEvent(this->dispatcher[i], event); + } +} + +// vim: set ts=4 sw=4: diff --git a/src/event_handler_set_dispatcher.c b/src/event_handler_set_dispatcher.c new file mode 100644 index 0000000..2e42819 --- /dev/null +++ b/src/event_handler_set_dispatcher.c @@ -0,0 +1,40 @@ +/** + * \file + * + * \author Georg Hopp + * + * \copyright + * Copyright © 2012 Georg Hopp + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "trbase.h" + +#include "tr/event_handler.h" +#include "tr/event_dispatcher.h" + +void +TR_eventHandlerSetDispatcher(TR_EventHandler this, TR_EventDispatcher disp) +{ + /** + * FIXME All dispatchers after the tenth one will be silently + * dropped... for now its ok, but this should be handled better. + */ + if (10 > this->ndispatcher) { + this->dispatcher[this->ndispatcher++] = disp; + } +} + +// vim: set ts=4 sw=4: