Browse Source

Use general purpose timer from trbase and drop events when they are not handled at all

1.0.0
Georg Hopp 11 years ago
parent
commit
a98e0740cd
  1. 20
      include/tr/event_dispatcher.h
  2. 2
      src/Makefile.am
  3. 4
      src/event_dispatcher.c
  4. 41
      src/event_dispatcher_get_beat_time.c
  5. 2
      src/event_dispatcher_get_data_wait_time.c
  6. 47
      src/event_dispatcher_set_hearbeat.c
  7. 13
      src/event_dispatcher_start.c

20
include/tr/event_dispatcher.h

@ -24,6 +24,7 @@
#define __TR_EVENT_DISPATCHER_H__
#include <time.h>
#include <stdint.h>
#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__

2
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 \

4
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);
}

41
src/event_dispatcher_get_beat_time.c

@ -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 <http://www.gnu.org/licenses/>.
*/
#include <time.h>
#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:

2
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) {

47
src/event_dispatcher_set_hearbeat.c

@ -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 <http://www.gnu.org/licenses/>.
*/
#include <time.h>
#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:

13
src/event_dispatcher_start.c

@ -20,8 +20,6 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <time.h>
#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);
}
}
}

Loading…
Cancel
Save