Commit a98e0740cd95196854fbeea5591317b711a2f7a7
1 parent
36e2f308
Use general purpose timer from trbase and drop events when they are not handled at all
Showing
7 changed files
with
21 additions
and
108 deletions
| @@ -24,6 +24,7 @@ | @@ -24,6 +24,7 @@ | ||
| 24 | #define __TR_EVENT_DISPATCHER_H__ | 24 | #define __TR_EVENT_DISPATCHER_H__ |
| 25 | 25 | ||
| 26 | #include <time.h> | 26 | #include <time.h> |
| 27 | +#include <stdint.h> | ||
| 27 | 28 | ||
| 28 | #include "trbase.h" | 29 | #include "trbase.h" |
| 29 | #include "trdata.h" | 30 | #include "trdata.h" |
| @@ -51,9 +52,8 @@ TR_CLASS(TR_EventDispatcher) { | @@ -51,9 +52,8 @@ TR_CLASS(TR_EventDispatcher) { | ||
| 51 | TR_Hash handler; | 52 | TR_Hash handler; |
| 52 | TR_EventHandler default_handler; | 53 | TR_EventHandler default_handler; |
| 53 | int running; | 54 | int running; |
| 54 | - int heartbeat; // milliseconds | ||
| 55 | - int nextbeat; // milliseconds | ||
| 56 | - size_t n_beats; | 55 | + TR_Timer heartbeat; |
| 56 | + unsigned long n_beats; | ||
| 57 | TR_EventDispatcherMode mode; | 57 | TR_EventDispatcherMode mode; |
| 58 | }; | 58 | }; |
| 59 | TR_INSTANCE_INIT(TR_EventDispatcher); | 59 | TR_INSTANCE_INIT(TR_EventDispatcher); |
| @@ -67,10 +67,14 @@ TR_CLASSVARS_DECL(TR_EventDispatcher) { | @@ -67,10 +67,14 @@ TR_CLASSVARS_DECL(TR_EventDispatcher) { | ||
| 67 | #define TR_DISPATCHER_EVENT_SHUTDOWN 3 | 67 | #define TR_DISPATCHER_EVENT_SHUTDOWN 3 |
| 68 | #define TR_DISPATCHER_EVENT_MAX ((size_t)TR_DISPATCHER_EVENT_SHUTDOWN) | 68 | #define TR_DISPATCHER_EVENT_MAX ((size_t)TR_DISPATCHER_EVENT_SHUTDOWN) |
| 69 | 69 | ||
| 70 | +#define TR_DISPATCHER_MODE_TO_EVENTID(mode) \ | ||
| 71 | + ((mode) == TR_EVD_CLIENT \ | ||
| 72 | + ? TR_DISPATCHER_EVENT_USER_WAIT \ | ||
| 73 | + : TR_DISPATCHER_EVENT_DATA_WAIT) | ||
| 74 | + | ||
| 70 | void TR_eventDispatcherRegisterHandler(TR_EventDispatcher, TR_EventHandler); | 75 | void TR_eventDispatcherRegisterHandler(TR_EventDispatcher, TR_EventHandler); |
| 71 | -void TR_eventDispatcherSetHeartbeat(TR_EventDispatcher, int); | ||
| 72 | -int TR_eventDispatcherGetBeatTime(TR_EventDispatcher); | ||
| 73 | -int TR_eventDispatcherGetDataWaitTime(TR_EventDispatcher); | 76 | +unsigned long |
| 77 | +TR_eventDispatcherGetDataWaitTime(TR_EventDispatcher); | ||
| 74 | void TR_eventDispatcherStart(TR_EventDispatcher); | 78 | void TR_eventDispatcherStart(TR_EventDispatcher); |
| 75 | void TR_eventDispatcherShutdown(TR_EventDispatcher); | 79 | void TR_eventDispatcherShutdown(TR_EventDispatcher); |
| 76 | 80 | ||
| @@ -78,6 +82,10 @@ void TR_eventDispatcherShutdown(TR_EventDispatcher); | @@ -78,6 +82,10 @@ void TR_eventDispatcherShutdown(TR_EventDispatcher); | ||
| 78 | (TR_queuePut((disp)->events, (ev))) | 82 | (TR_queuePut((disp)->events, (ev))) |
| 79 | #define TR_eventDispatcherStop(disp) \ | 83 | #define TR_eventDispatcherStop(disp) \ |
| 80 | (((TR_EventDispatcher)disp)->running = 0) | 84 | (((TR_EventDispatcher)disp)->running = 0) |
| 85 | +#define TR_eventDispatcherSetHeartbeat(disp, beat) \ | ||
| 86 | + (TR_timerSetMil((disp)->heartbeat, (beat))) | ||
| 87 | +#define TR_eventDispatcherGetBeatTime(disp) \ | ||
| 88 | + (TR_timerGet((disp)->heartbeat, &((disp)->n_beats))) | ||
| 81 | 89 | ||
| 82 | #endif // __TR_EVENT_DISPATCHER_H__ | 90 | #endif // __TR_EVENT_DISPATCHER_H__ |
| 83 | 91 |
| @@ -7,8 +7,6 @@ TREVENT = event.c \ | @@ -7,8 +7,6 @@ TREVENT = event.c \ | ||
| 7 | get_event_string.c \ | 7 | get_event_string.c \ |
| 8 | event_dispatcher.c \ | 8 | event_dispatcher.c \ |
| 9 | event_dispatcher_register_handler.c \ | 9 | event_dispatcher_register_handler.c \ |
| 10 | - event_dispatcher_set_hearbeat.c \ | ||
| 11 | - event_dispatcher_get_beat_time.c \ | ||
| 12 | event_dispatcher_get_data_wait_time.c \ | 10 | event_dispatcher_get_data_wait_time.c \ |
| 13 | event_dispatcher_start.c \ | 11 | event_dispatcher_start.c \ |
| 14 | event_dispatcher_shutdown.c \ | 12 | event_dispatcher_shutdown.c \ |
| @@ -67,11 +67,10 @@ eventDispatcherCtor(void * _this, va_list * params) { | @@ -67,11 +67,10 @@ eventDispatcherCtor(void * _this, va_list * params) { | ||
| 67 | 67 | ||
| 68 | this->events = TR_new(TR_Queue); | 68 | this->events = TR_new(TR_Queue); |
| 69 | this->handler = TR_new(TR_Hash); | 69 | this->handler = TR_new(TR_Hash); |
| 70 | + this->heartbeat = TR_new(TR_Timer, TR_TBASE_MIL, 1000); | ||
| 70 | this->mode = va_arg(*params, TR_EventDispatcherMode); | 71 | this->mode = va_arg(*params, TR_EventDispatcherMode); |
| 71 | this->default_handler = va_arg(*params, TR_EventHandler); | 72 | this->default_handler = va_arg(*params, TR_EventHandler); |
| 72 | this->running = 0; | 73 | this->running = 0; |
| 73 | - this->heartbeat = 0; | ||
| 74 | - this->nextbeat = 0; | ||
| 75 | 74 | ||
| 76 | if (! _TR_controlDispatcher) { | 75 | if (! _TR_controlDispatcher) { |
| 77 | _TR_controlDispatcher = this; | 76 | _TR_controlDispatcher = this; |
| @@ -96,6 +95,7 @@ eventDispatcherDtor(void * _this) { | @@ -96,6 +95,7 @@ eventDispatcherDtor(void * _this) { | ||
| 96 | TR_hashEach(this->handler, NULL, releaseHandlerQueues); | 95 | TR_hashEach(this->handler, NULL, releaseHandlerQueues); |
| 97 | 96 | ||
| 98 | TR_hashCleanup(this->handler); | 97 | TR_hashCleanup(this->handler); |
| 98 | + TR_delete(this->heartbeat); | ||
| 99 | TR_delete(this->handler); | 99 | TR_delete(this->handler); |
| 100 | TR_delete(this->events); | 100 | TR_delete(this->events); |
| 101 | } | 101 | } |
src/event_dispatcher_get_beat_time.c
deleted
100644 → 0
| 1 | -/** | ||
| 2 | - * \file | ||
| 3 | - * | ||
| 4 | - * \author Georg Hopp | ||
| 5 | - * | ||
| 6 | - * \copyright | ||
| 7 | - * Copyright © 2014 Georg Hopp | ||
| 8 | - * | ||
| 9 | - * This program is free software: you can redistribute it and/or modify | ||
| 10 | - * it under the terms of the GNU General Public License as published by | ||
| 11 | - * the Free Software Foundation, either version 3 of the License, or | ||
| 12 | - * (at your option) any later version. | ||
| 13 | - * | ||
| 14 | - * This program is distributed in the hope that it will be useful, | ||
| 15 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 16 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 17 | - * GNU General Public License for more details. | ||
| 18 | - * | ||
| 19 | - * You should have received a copy of the GNU General Public License | ||
| 20 | - * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 21 | - */ | ||
| 22 | - | ||
| 23 | -#include <time.h> | ||
| 24 | - | ||
| 25 | -#include "trbase.h" | ||
| 26 | - | ||
| 27 | -#include "tr/event_dispatcher.h" | ||
| 28 | - | ||
| 29 | -int | ||
| 30 | -TR_eventDispatcherGetBeatTime(TR_EventDispatcher this) | ||
| 31 | -{ | ||
| 32 | - struct timespec tp; | ||
| 33 | - int now; // milliseconds | ||
| 34 | - | ||
| 35 | - clock_gettime(CLOCK_REALTIME, &tp); | ||
| 36 | - now = tp.tv_sec * 1000 + tp.tv_nsec / 1000000; | ||
| 37 | - | ||
| 38 | - return this->nextbeat - now; | ||
| 39 | -} | ||
| 40 | - | ||
| 41 | -// vim: set ts=4 sw=4: |
| @@ -24,7 +24,7 @@ | @@ -24,7 +24,7 @@ | ||
| 24 | 24 | ||
| 25 | #include "tr/event_dispatcher.h" | 25 | #include "tr/event_dispatcher.h" |
| 26 | 26 | ||
| 27 | -int | 27 | +unsigned long |
| 28 | TR_eventDispatcherGetDataWaitTime(TR_EventDispatcher this) | 28 | TR_eventDispatcherGetDataWaitTime(TR_EventDispatcher this) |
| 29 | { | 29 | { |
| 30 | if (TR_EVD_SERVER == this->mode) { | 30 | if (TR_EVD_SERVER == this->mode) { |
src/event_dispatcher_set_hearbeat.c
deleted
100644 → 0
| 1 | -/** | ||
| 2 | - * \file | ||
| 3 | - * | ||
| 4 | - * \author Georg Hopp | ||
| 5 | - * | ||
| 6 | - * \copyright | ||
| 7 | - * Copyright © 2014 Georg Hopp | ||
| 8 | - * | ||
| 9 | - * This program is free software: you can redistribute it and/or modify | ||
| 10 | - * it under the terms of the GNU General Public License as published by | ||
| 11 | - * the Free Software Foundation, either version 3 of the License, or | ||
| 12 | - * (at your option) any later version. | ||
| 13 | - * | ||
| 14 | - * This program is distributed in the hope that it will be useful, | ||
| 15 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 16 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 17 | - * GNU General Public License for more details. | ||
| 18 | - * | ||
| 19 | - * You should have received a copy of the GNU General Public License | ||
| 20 | - * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 21 | - */ | ||
| 22 | - | ||
| 23 | -#include <time.h> | ||
| 24 | - | ||
| 25 | -#include "trbase.h" | ||
| 26 | - | ||
| 27 | -#include "tr/event_dispatcher.h" | ||
| 28 | - | ||
| 29 | -void | ||
| 30 | -TR_eventDispatcherSetHeartbeat(TR_EventDispatcher this, int heartbeat) | ||
| 31 | -{ | ||
| 32 | - struct timespec tp; | ||
| 33 | - int now; // milliseconds | ||
| 34 | - | ||
| 35 | - clock_gettime(CLOCK_REALTIME, &tp); | ||
| 36 | - now = tp.tv_sec * 1000 + tp.tv_nsec / 1000000; | ||
| 37 | - | ||
| 38 | - this->heartbeat = heartbeat; | ||
| 39 | - | ||
| 40 | - if (this->heartbeat) { | ||
| 41 | - this->nextbeat = now + this->heartbeat; | ||
| 42 | - } else { | ||
| 43 | - this->nextbeat = 0; | ||
| 44 | - } | ||
| 45 | -} | ||
| 46 | - | ||
| 47 | -// vim: set ts=4 sw=4: |
| @@ -20,8 +20,6 @@ | @@ -20,8 +20,6 @@ | ||
| 20 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | 20 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 21 | */ | 21 | */ |
| 22 | 22 | ||
| 23 | -#include <time.h> | ||
| 24 | - | ||
| 25 | #include "trbase.h" | 23 | #include "trbase.h" |
| 26 | #include "trdata.h" | 24 | #include "trdata.h" |
| 27 | #include "trhash.h" | 25 | #include "trhash.h" |
| @@ -38,18 +36,13 @@ TR_eventDispatcherStart(TR_EventDispatcher this) | @@ -38,18 +36,13 @@ TR_eventDispatcherStart(TR_EventDispatcher this) | ||
| 38 | this->running = 1; | 36 | this->running = 1; |
| 39 | 37 | ||
| 40 | while (this->running || (! TR_queueEmpty(this->events))) { | 38 | while (this->running || (! TR_queueEmpty(this->events))) { |
| 41 | - struct timespec tp; | ||
| 42 | - int now; // milliseconds | ||
| 43 | TR_Event event; | 39 | TR_Event event; |
| 44 | TR_Queue handler_queue; | 40 | TR_Queue handler_queue; |
| 45 | TR_HashValue handler_queue_hv; | 41 | TR_HashValue handler_queue_hv; |
| 46 | 42 | ||
| 47 | - clock_gettime(CLOCK_REALTIME, &tp); | ||
| 48 | - now = tp.tv_sec * 1000 + tp.tv_nsec / 1000000; | 43 | + TR_eventDispatcherGetBeatTime(this); |
| 49 | 44 | ||
| 50 | - if (this->nextbeat && this->nextbeat <= now) { | ||
| 51 | - this->n_beats = ((now - this->nextbeat) / this->heartbeat) + 1; | ||
| 52 | - this->nextbeat += this->n_beats * this->heartbeat; | 45 | + if (this->n_beats) { |
| 53 | event = TR_eventSubjectEmit( | 46 | event = TR_eventSubjectEmit( |
| 54 | (TR_EventSubject)this, | 47 | (TR_EventSubject)this, |
| 55 | TR_DISPATCHER_EVENT_HEARTBEAT, | 48 | TR_DISPATCHER_EVENT_HEARTBEAT, |
| @@ -93,6 +86,8 @@ TR_eventDispatcherStart(TR_EventDispatcher this) | @@ -93,6 +86,8 @@ TR_eventDispatcherStart(TR_EventDispatcher this) | ||
| 93 | } else { | 86 | } else { |
| 94 | TR_eventDispatcherEnqueueEvent(this, event); | 87 | TR_eventDispatcherEnqueueEvent(this, event); |
| 95 | } | 88 | } |
| 89 | + } else { | ||
| 90 | + TR_delete(event); | ||
| 96 | } | 91 | } |
| 97 | } | 92 | } |
| 98 | } | 93 | } |
Please
register
or
login
to post a comment