Showing
9 changed files
with
438 additions
and
0 deletions
src/event_dispatcher_get_beat_time.c
0 → 100644
| 1 | +/** | |
| 2 | + * \file | |
| 3 | + * | |
| 4 | + * \author Georg Hopp | |
| 5 | + * | |
| 6 | + * \copyright | |
| 7 | + * Copyright © 2012 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 | +time_t | |
| 30 | +TR_eventDispatcherGetBeatTime(TR_EventDispatcher this) | |
| 31 | +{ | |
| 32 | + return this->nextbeat - time(NULL); | |
| 33 | +} | |
| 34 | + | |
| 35 | +// vim: set ts=4 sw=4: | ... | ... |
src/event_dispatcher_get_data_wait_time.c
0 → 100644
| 1 | +/** | |
| 2 | + * \file | |
| 3 | + * | |
| 4 | + * \author Georg Hopp | |
| 5 | + * | |
| 6 | + * \copyright | |
| 7 | + * Copyright © 2012 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 | +time_t | |
| 30 | +TR_eventDispatcherGetDataWaitTime(TR_EventDispatcher this) | |
| 31 | +{ | |
| 32 | + if (TR_EVD_SERVER == this->mode) { | |
| 33 | + return TR_eventDispatcherGetBeatTime(this); | |
| 34 | + } | |
| 35 | + | |
| 36 | + return 0; | |
| 37 | +} | |
| 38 | + | |
| 39 | +// vim: set ts=4 sw=4: | ... | ... |
src/event_dispatcher_register_handler.c
0 → 100644
| 1 | +/** | |
| 2 | + * \file | |
| 3 | + * | |
| 4 | + * \author Georg Hopp | |
| 5 | + * | |
| 6 | + * \copyright | |
| 7 | + * Copyright © 2012 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 "trbase.h" | |
| 24 | +#include "trdata.h" | |
| 25 | + | |
| 26 | +#include "tr/event_dispatcher.h" | |
| 27 | +#include "tr/event_handler.h" | |
| 28 | + | |
| 29 | +static | |
| 30 | +void | |
| 31 | +doRegister(const void * _node, const void * data) | |
| 32 | +{ | |
| 33 | + TR_HashValue node = (TR_HashValue)_node; | |
| 34 | + TR_EventDispatcher dispatcher = ((void **)data)[0]; | |
| 35 | + TR_EventHandler current_handler = ((void **)data)[1]; | |
| 36 | + TR_HashValue handler_queue_hv; | |
| 37 | + TR_Queue handler_queue; | |
| 38 | + | |
| 39 | + handler_queue_hv = TR_hashGetByVal(dispatcher->handler, node->hash); | |
| 40 | + | |
| 41 | + if (handler_queue_hv) { | |
| 42 | + handler_queue = (TR_Queue)handler_queue_hv->value; | |
| 43 | + } else { | |
| 44 | + handler_queue = TR_new(TR_Queue); | |
| 45 | + handler_queue->free_msgs = 0; | |
| 46 | + TR_hashAdd( | |
| 47 | + dispatcher->handler, | |
| 48 | + TR_new(TR_HashValue, node->key, node->nkey, handler_queue, sizeof(TR_Queue))); | |
| 49 | + } | |
| 50 | + | |
| 51 | + TR_queuePut(handler_queue, current_handler); | |
| 52 | +} | |
| 53 | + | |
| 54 | +void | |
| 55 | +TR_eventDispatcherRegisterHandler(TR_EventDispatcher this, TR_EventHandler handler) | |
| 56 | +{ | |
| 57 | + void * cb_data[] = { this, handler }; | |
| 58 | + | |
| 59 | + TR_hashEach(handler->event_methods, cb_data, doRegister); | |
| 60 | + TR_eventHandlerSetDispatcher(handler, this); | |
| 61 | +} | |
| 62 | + | |
| 63 | +// vim: set ts=4 sw=4: | ... | ... |
src/event_dispatcher_set_hearbeat.c
0 → 100644
| 1 | +/** | |
| 2 | + * \file | |
| 3 | + * | |
| 4 | + * \author Georg Hopp | |
| 5 | + * | |
| 6 | + * \copyright | |
| 7 | + * Copyright © 2012 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, time_t heartbeat) | |
| 31 | +{ | |
| 32 | + this->heartbeat = heartbeat; | |
| 33 | + | |
| 34 | + if (this->heartbeat) { | |
| 35 | + this->nextbeat = time(NULL) + this->heartbeat; | |
| 36 | + } else { | |
| 37 | + this->nextbeat = 0; | |
| 38 | + } | |
| 39 | +} | |
| 40 | + | |
| 41 | +// vim: set ts=4 sw=4: | ... | ... |
src/event_dispatcher_shutdown.c
0 → 100644
| 1 | +/** | |
| 2 | + * \file | |
| 3 | + * | |
| 4 | + * \author Georg Hopp | |
| 5 | + * | |
| 6 | + * \copyright | |
| 7 | + * Copyright © 2012 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 | +#include "trdata.h" | |
| 27 | +#include "trhash.h" | |
| 28 | + | |
| 29 | +#include "tr/event.h" | |
| 30 | +#include "tr/event_subject.h" | |
| 31 | +#include "tr/event_dispatcher.h" | |
| 32 | + | |
| 33 | +void | |
| 34 | +TR_eventDispatcherShutdown(TR_EventDispatcher this) | |
| 35 | +{ | |
| 36 | + TR_eventDispatcherEnqueueEvent( | |
| 37 | + this, | |
| 38 | + TR_eventSubjectEmit( | |
| 39 | + (TR_EventSubject)this, | |
| 40 | + TR_DISPATCHER_EVENT_SHUTDOWN, | |
| 41 | + NULL)); | |
| 42 | + TR_eventDispatcherStop(this); | |
| 43 | +} | |
| 44 | + | |
| 45 | +// vim: set ts=4 sw=4: | ... | ... |
src/event_dispatcher_start.c
0 → 100644
| 1 | +/** | |
| 2 | + * \file | |
| 3 | + * | |
| 4 | + * \author Georg Hopp | |
| 5 | + * | |
| 6 | + * \copyright | |
| 7 | + * Copyright © 2012 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 | +#include "trdata.h" | |
| 27 | +#include "trhash.h" | |
| 28 | + | |
| 29 | +#include "tr/event.h" | |
| 30 | +#include "tr/event_subject.h" | |
| 31 | +#include "tr/event_dispatcher.h" | |
| 32 | + | |
| 33 | +void | |
| 34 | +TR_eventDispatcherStart(TR_EventDispatcher this) | |
| 35 | +{ | |
| 36 | + this->running = 1; | |
| 37 | + | |
| 38 | + while (this->running || (! TR_queueEmpty(this->events))) { | |
| 39 | + time_t now = time(NULL); | |
| 40 | + TR_Event current = NULL; | |
| 41 | + | |
| 42 | + if (this->nextbeat && this->nextbeat < now) { | |
| 43 | + this->nextbeat += this->heartbeat; | |
| 44 | + TR_eventDispatcherEnqueueEvent( | |
| 45 | + this, | |
| 46 | + TR_eventSubjectEmit( | |
| 47 | + (TR_EventSubject)this, | |
| 48 | + TR_DISPATCHER_EVENT_HEARTBEAT, | |
| 49 | + NULL)); | |
| 50 | + } | |
| 51 | + | |
| 52 | + if (TR_queueEmpty(this->events)) { | |
| 53 | + if (TR_EVD_CLIENT == this->mode) { | |
| 54 | + current = TR_eventSubjectEmit( | |
| 55 | + (TR_EventSubject)this, | |
| 56 | + TR_DISPATCHER_EVENT_USER_WAIT, | |
| 57 | + NULL); | |
| 58 | + } else { | |
| 59 | + current = TR_eventSubjectEmit( | |
| 60 | + (TR_EventSubject)this, | |
| 61 | + TR_DISPATCHER_EVENT_DATA_WAIT, | |
| 62 | + NULL); | |
| 63 | + } | |
| 64 | + } else { | |
| 65 | + current = TR_queueGet(this->events); | |
| 66 | + } | |
| 67 | + | |
| 68 | + if (current) { | |
| 69 | + TR_HashValue handler_queue_hv = TR_hashGetByVal( | |
| 70 | + this->handler, | |
| 71 | + TR_sdbm((unsigned char *)current->id, sizeof(current->id))); | |
| 72 | + TR_Queue handler_queue = handler_queue_hv->value; | |
| 73 | + | |
| 74 | + if (! TR_queueEmpty(handler_queue)) { | |
| 75 | + TR_Queue queue_node = handler_queue->first; | |
| 76 | + | |
| 77 | + while (queue_node) { | |
| 78 | + TR_EventHandler handler = queue_node->msg; | |
| 79 | + if (TR_eventHandlerHandleEvent(handler, current)) break; | |
| 80 | + queue_node = queue_node->next; | |
| 81 | + } | |
| 82 | + } | |
| 83 | + } | |
| 84 | + } | |
| 85 | +} | |
| 86 | + | |
| 87 | +// vim: set ts=4 sw=4: | ... | ... |
src/event_handler_handle_event.c
0 → 100644
| 1 | +/** | |
| 2 | + * \file | |
| 3 | + * | |
| 4 | + * \author Georg Hopp | |
| 5 | + * | |
| 6 | + * \copyright | |
| 7 | + * Copyright © 2012 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 "trbase.h" | |
| 24 | +#include "trdata.h" | |
| 25 | +#include "trhash.h" | |
| 26 | + | |
| 27 | +#include "tr/event.h" | |
| 28 | +#include "tr/event_handler.h" | |
| 29 | + | |
| 30 | +int | |
| 31 | +TR_eventHandlerHandleEvent(TR_EventHandler this, TR_Event event) | |
| 32 | +{ | |
| 33 | + TR_HashValue handle_func_hv = TR_hashGetByVal( | |
| 34 | + this->event_methods, | |
| 35 | + TR_sdbm((unsigned char *)event->id, sizeof(event->id))); | |
| 36 | + | |
| 37 | + if (! handle_func_hv) { | |
| 38 | + return 0; | |
| 39 | + } | |
| 40 | + | |
| 41 | + return ((TR_EventMethod_fptr)handle_func_hv->value)(event); | |
| 42 | +} | |
| 43 | + | |
| 44 | +// vim: set ts=4 sw=4: | ... | ... |
src/event_handler_issue_event.c
0 → 100644
| 1 | +/** | |
| 2 | + * \file | |
| 3 | + * | |
| 4 | + * \author Georg Hopp | |
| 5 | + * | |
| 6 | + * \copyright | |
| 7 | + * Copyright © 2012 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 "trbase.h" | |
| 24 | + | |
| 25 | +#include "tr/event.h" | |
| 26 | +#include "tr/event_handler.h" | |
| 27 | +#include "tr/event_subject.h" | |
| 28 | + | |
| 29 | +void | |
| 30 | +TR_eventHandlerIssueEvent( | |
| 31 | + TR_EventHandler this, | |
| 32 | + TR_EventSubject subject, | |
| 33 | + int idx, | |
| 34 | + void * data) | |
| 35 | +{ | |
| 36 | + TR_Event event = TR_eventSubjectEmit(subject, idx, data); | |
| 37 | + int i; | |
| 38 | + | |
| 39 | + for (i=0; i<this->ndispatcher; i++) { | |
| 40 | + TR_eventDispatcherEnqueueEvent(this->dispatcher[i], event); | |
| 41 | + } | |
| 42 | +} | |
| 43 | + | |
| 44 | +// vim: set ts=4 sw=4: | ... | ... |
src/event_handler_set_dispatcher.c
0 → 100644
| 1 | +/** | |
| 2 | + * \file | |
| 3 | + * | |
| 4 | + * \author Georg Hopp | |
| 5 | + * | |
| 6 | + * \copyright | |
| 7 | + * Copyright © 2012 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 "trbase.h" | |
| 24 | + | |
| 25 | +#include "tr/event_handler.h" | |
| 26 | +#include "tr/event_dispatcher.h" | |
| 27 | + | |
| 28 | +void | |
| 29 | +TR_eventHandlerSetDispatcher(TR_EventHandler this, TR_EventDispatcher disp) | |
| 30 | +{ | |
| 31 | + /** | |
| 32 | + * FIXME All dispatchers after the tenth one will be silently | |
| 33 | + * dropped... for now its ok, but this should be handled better. | |
| 34 | + */ | |
| 35 | + if (10 > this->ndispatcher) { | |
| 36 | + this->dispatcher[this->ndispatcher++] = disp; | |
| 37 | + } | |
| 38 | +} | |
| 39 | + | |
| 40 | +// vim: set ts=4 sw=4: | ... | ... |
Please
register
or
login
to post a comment