From a98e0740cd95196854fbeea5591317b711a2f7a7 Mon Sep 17 00:00:00 2001 From: Georg Hopp Date: Sun, 21 Sep 2014 10:17:26 +0100 Subject: [PATCH] Use general purpose timer from trbase and drop events when they are not handled at all --- include/tr/event_dispatcher.h | 20 +++++++--- src/Makefile.am | 2 - src/event_dispatcher.c | 4 +- src/event_dispatcher_get_beat_time.c | 41 -------------------- src/event_dispatcher_get_data_wait_time.c | 2 +- src/event_dispatcher_set_hearbeat.c | 47 ----------------------- src/event_dispatcher_start.c | 13 ++----- 7 files changed, 21 insertions(+), 108 deletions(-) delete mode 100644 src/event_dispatcher_get_beat_time.c delete mode 100644 src/event_dispatcher_set_hearbeat.c diff --git a/include/tr/event_dispatcher.h b/include/tr/event_dispatcher.h index e36006a..c198303 100644 --- a/include/tr/event_dispatcher.h +++ b/include/tr/event_dispatcher.h @@ -24,6 +24,7 @@ #define __TR_EVENT_DISPATCHER_H__ #include +#include #include "trbase.h" #include "trdata.h" @@ -51,9 +52,8 @@ TR_CLASS(TR_EventDispatcher) { TR_Hash handler; TR_EventHandler default_handler; int running; - int heartbeat; // milliseconds - int nextbeat; // milliseconds - size_t n_beats; + TR_Timer heartbeat; + unsigned long n_beats; TR_EventDispatcherMode mode; }; TR_INSTANCE_INIT(TR_EventDispatcher); @@ -67,10 +67,14 @@ TR_CLASSVARS_DECL(TR_EventDispatcher) { #define TR_DISPATCHER_EVENT_SHUTDOWN 3 #define TR_DISPATCHER_EVENT_MAX ((size_t)TR_DISPATCHER_EVENT_SHUTDOWN) +#define TR_DISPATCHER_MODE_TO_EVENTID(mode) \ + ((mode) == TR_EVD_CLIENT \ + ? TR_DISPATCHER_EVENT_USER_WAIT \ + : TR_DISPATCHER_EVENT_DATA_WAIT) + void TR_eventDispatcherRegisterHandler(TR_EventDispatcher, TR_EventHandler); -void TR_eventDispatcherSetHeartbeat(TR_EventDispatcher, int); -int TR_eventDispatcherGetBeatTime(TR_EventDispatcher); -int TR_eventDispatcherGetDataWaitTime(TR_EventDispatcher); +unsigned long +TR_eventDispatcherGetDataWaitTime(TR_EventDispatcher); void TR_eventDispatcherStart(TR_EventDispatcher); void TR_eventDispatcherShutdown(TR_EventDispatcher); @@ -78,6 +82,10 @@ void TR_eventDispatcherShutdown(TR_EventDispatcher); (TR_queuePut((disp)->events, (ev))) #define TR_eventDispatcherStop(disp) \ (((TR_EventDispatcher)disp)->running = 0) +#define TR_eventDispatcherSetHeartbeat(disp, beat) \ + (TR_timerSetMil((disp)->heartbeat, (beat))) +#define TR_eventDispatcherGetBeatTime(disp) \ + (TR_timerGet((disp)->heartbeat, &((disp)->n_beats))) #endif // __TR_EVENT_DISPATCHER_H__ diff --git a/src/Makefile.am b/src/Makefile.am index 16e318f..2210eb8 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -7,8 +7,6 @@ TREVENT = event.c \ get_event_string.c \ event_dispatcher.c \ event_dispatcher_register_handler.c \ - event_dispatcher_set_hearbeat.c \ - event_dispatcher_get_beat_time.c \ event_dispatcher_get_data_wait_time.c \ event_dispatcher_start.c \ event_dispatcher_shutdown.c \ diff --git a/src/event_dispatcher.c b/src/event_dispatcher.c index 15f5a2b..e1efd29 100644 --- a/src/event_dispatcher.c +++ b/src/event_dispatcher.c @@ -67,11 +67,10 @@ eventDispatcherCtor(void * _this, va_list * params) { this->events = TR_new(TR_Queue); this->handler = TR_new(TR_Hash); + this->heartbeat = TR_new(TR_Timer, TR_TBASE_MIL, 1000); this->mode = va_arg(*params, TR_EventDispatcherMode); this->default_handler = va_arg(*params, TR_EventHandler); this->running = 0; - this->heartbeat = 0; - this->nextbeat = 0; if (! _TR_controlDispatcher) { _TR_controlDispatcher = this; @@ -96,6 +95,7 @@ eventDispatcherDtor(void * _this) { TR_hashEach(this->handler, NULL, releaseHandlerQueues); TR_hashCleanup(this->handler); + TR_delete(this->heartbeat); TR_delete(this->handler); TR_delete(this->events); } diff --git a/src/event_dispatcher_get_beat_time.c b/src/event_dispatcher_get_beat_time.c deleted file mode 100644 index 8f597b0..0000000 --- a/src/event_dispatcher_get_beat_time.c +++ /dev/null @@ -1,41 +0,0 @@ -/** - * \file - * - * \author Georg Hopp - * - * \copyright - * Copyright © 2014 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" - -int -TR_eventDispatcherGetBeatTime(TR_EventDispatcher this) -{ - struct timespec tp; - int now; // milliseconds - - clock_gettime(CLOCK_REALTIME, &tp); - now = tp.tv_sec * 1000 + tp.tv_nsec / 1000000; - - return this->nextbeat - now; -} - -// 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 index 87dae3f..12778ae 100644 --- a/src/event_dispatcher_get_data_wait_time.c +++ b/src/event_dispatcher_get_data_wait_time.c @@ -24,7 +24,7 @@ #include "tr/event_dispatcher.h" -int +unsigned long TR_eventDispatcherGetDataWaitTime(TR_EventDispatcher this) { if (TR_EVD_SERVER == this->mode) { diff --git a/src/event_dispatcher_set_hearbeat.c b/src/event_dispatcher_set_hearbeat.c deleted file mode 100644 index e4e3b3f..0000000 --- a/src/event_dispatcher_set_hearbeat.c +++ /dev/null @@ -1,47 +0,0 @@ -/** - * \file - * - * \author Georg Hopp - * - * \copyright - * Copyright © 2014 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, int heartbeat) -{ - struct timespec tp; - int now; // milliseconds - - clock_gettime(CLOCK_REALTIME, &tp); - now = tp.tv_sec * 1000 + tp.tv_nsec / 1000000; - - this->heartbeat = heartbeat; - - if (this->heartbeat) { - this->nextbeat = now + this->heartbeat; - } else { - this->nextbeat = 0; - } -} - -// vim: set ts=4 sw=4: diff --git a/src/event_dispatcher_start.c b/src/event_dispatcher_start.c index 9974950..1f2b3b5 100644 --- a/src/event_dispatcher_start.c +++ b/src/event_dispatcher_start.c @@ -20,8 +20,6 @@ * along with this program. If not, see . */ -#include - #include "trbase.h" #include "trdata.h" #include "trhash.h" @@ -38,18 +36,13 @@ TR_eventDispatcherStart(TR_EventDispatcher this) this->running = 1; while (this->running || (! TR_queueEmpty(this->events))) { - struct timespec tp; - int now; // milliseconds TR_Event event; TR_Queue handler_queue; TR_HashValue handler_queue_hv; - clock_gettime(CLOCK_REALTIME, &tp); - now = tp.tv_sec * 1000 + tp.tv_nsec / 1000000; + TR_eventDispatcherGetBeatTime(this); - if (this->nextbeat && this->nextbeat <= now) { - this->n_beats = ((now - this->nextbeat) / this->heartbeat) + 1; - this->nextbeat += this->n_beats * this->heartbeat; + if (this->n_beats) { event = TR_eventSubjectEmit( (TR_EventSubject)this, TR_DISPATCHER_EVENT_HEARTBEAT, @@ -93,6 +86,8 @@ TR_eventDispatcherStart(TR_EventDispatcher this) } else { TR_eventDispatcherEnqueueEvent(this, event); } + } else { + TR_delete(event); } } }