9 changed files with 438 additions and 0 deletions
-
35src/event_dispatcher_get_beat_time.c
-
39src/event_dispatcher_get_data_wait_time.c
-
63src/event_dispatcher_register_handler.c
-
41src/event_dispatcher_set_hearbeat.c
-
45src/event_dispatcher_shutdown.c
-
87src/event_dispatcher_start.c
-
44src/event_handler_handle_event.c
-
44src/event_handler_issue_event.c
-
40src/event_handler_set_dispatcher.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 <http://www.gnu.org/licenses/>. |
||||
|
*/ |
||||
|
|
||||
|
#include <time.h> |
||||
|
|
||||
|
#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: |
||||
@ -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 <http://www.gnu.org/licenses/>. |
||||
|
*/ |
||||
|
|
||||
|
#include <time.h> |
||||
|
|
||||
|
#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: |
||||
@ -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 <http://www.gnu.org/licenses/>. |
||||
|
*/ |
||||
|
|
||||
|
#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: |
||||
@ -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 <http://www.gnu.org/licenses/>. |
||||
|
*/ |
||||
|
|
||||
|
#include <time.h> |
||||
|
|
||||
|
#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: |
||||
@ -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 <http://www.gnu.org/licenses/>. |
||||
|
*/ |
||||
|
|
||||
|
#include <time.h> |
||||
|
|
||||
|
#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: |
||||
@ -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 <http://www.gnu.org/licenses/>. |
||||
|
*/ |
||||
|
|
||||
|
#include <time.h> |
||||
|
|
||||
|
#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: |
||||
@ -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 <http://www.gnu.org/licenses/>. |
||||
|
*/ |
||||
|
|
||||
|
#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: |
||||
@ -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 <http://www.gnu.org/licenses/>. |
||||
|
*/ |
||||
|
|
||||
|
#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; i<this->ndispatcher; i++) { |
||||
|
TR_eventDispatcherEnqueueEvent(this->dispatcher[i], event); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// vim: set ts=4 sw=4: |
||||
@ -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 <http://www.gnu.org/licenses/>. |
||||
|
*/ |
||||
|
|
||||
|
#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: |
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue