Commit c0b33ec777e8d674c782bbe94a72e6cdec6aa320
1 parent
c6dd2aa0
try for a threaded server, but not very much luck now.
Showing
16 changed files
with
236 additions
and
10 deletions
include/tr/threaded_server.h
0 → 100644
| 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 | +#ifndef __TR_THREADED_SERVER_H__ | |
| 24 | +#define __TR_THREADED_SERVER_H__ | |
| 25 | + | |
| 26 | +#include <sys/types.h> | |
| 27 | + | |
| 28 | +#include "trbase.h" | |
| 29 | +#include "trevent.h" | |
| 30 | + | |
| 31 | +#include "tr/server.h" | |
| 32 | + | |
| 33 | +TR_CLASS(TR_ThreadedServer) { | |
| 34 | + TR_EXTENDS(TR_Server); | |
| 35 | + | |
| 36 | + TR_EventThread * threads; | |
| 37 | + size_t n_threads; | |
| 38 | +}; | |
| 39 | +TR_INSTANCE_INIT(TR_ThreadedServer); | |
| 40 | +TR_CLASSVARS_DECL(TR_ThreadedServer) {}; | |
| 41 | + | |
| 42 | +void TR_threadedServerStart(TR_ThreadedServer, unsigned long); | |
| 43 | + | |
| 44 | +#endif // __TR_THREADED_SERVER_H__ | |
| 45 | + | |
| 46 | +// vim: set ts=4 sw=4: | |
| 47 | + | ... | ... |
| ... | ... | @@ -103,6 +103,8 @@ static |
| 103 | 103 | void |
| 104 | 104 | commManagerCvInit(TR_class_ptr cls) |
| 105 | 105 | { |
| 106 | + TR_CLASSVARS(TR_EventHandler, cls)->event_methods->tree = TR_new(TR_Tree); | |
| 107 | + | |
| 106 | 108 | TR_EVENT_HANDLER_SET_METHOD( |
| 107 | 109 | cls, TR_EventDispatcher, |
| 108 | 110 | TR_DISPATCHER_EVENT_DATA_WAIT, | ... | ... |
| ... | ... | @@ -92,11 +92,13 @@ static |
| 92 | 92 | void |
| 93 | 93 | connectorCvInit(TR_class_ptr cls) |
| 94 | 94 | { |
| 95 | - TR_EVENT_HANDLER_SET_METHOD( | |
| 96 | - cls, | |
| 97 | - TR_ConnEntryPoint, | |
| 98 | - TR_CET_EVENT_ACC_READY, | |
| 99 | - connectorAccept); | |
| 95 | + TR_CLASSVARS(TR_EventHandler, cls)->event_methods->tree = TR_new(TR_Tree); | |
| 96 | + | |
| 97 | + TR_EVENT_HANDLER_SET_METHOD( | |
| 98 | + cls, | |
| 99 | + TR_ConnEntryPoint, | |
| 100 | + TR_CET_EVENT_ACC_READY, | |
| 101 | + connectorAccept); | |
| 100 | 102 | } |
| 101 | 103 | |
| 102 | 104 | TR_INSTANCE(TR_Hash, connectorEventMethods); | ... | ... |
src/threaded_server.c
0 → 100644
| 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 <stdarg.h> | |
| 24 | + | |
| 25 | +#include <sys/types.h> | |
| 26 | + | |
| 27 | +#include "trbase.h" | |
| 28 | +#include "trio.h" | |
| 29 | +#include "trevent.h" | |
| 30 | + | |
| 31 | +#include "tr/threaded_server.h" | |
| 32 | + | |
| 33 | +static | |
| 34 | +int | |
| 35 | +threadedServerCtor(void * _this, va_list * params) | |
| 36 | +{ | |
| 37 | + TR_ThreadedServer this = _this; | |
| 38 | + int i; | |
| 39 | + | |
| 40 | + TR_PARENTCALL(TR_ThreadedServer, _this, TR_Class, ctor, params); | |
| 41 | + | |
| 42 | + this->n_threads = va_arg(*params, size_t); | |
| 43 | + this->threads = TR_malloc(sizeof(TR_EventThread) * this->n_threads); | |
| 44 | + | |
| 45 | + for (i=0; i<this->n_threads; i++) { | |
| 46 | + this->threads[i] = TR_new( | |
| 47 | + TR_EventThread, | |
| 48 | + ((TR_Server)this)->dispatcher); | |
| 49 | + } | |
| 50 | + | |
| 51 | + return 0; | |
| 52 | +} | |
| 53 | + | |
| 54 | +static | |
| 55 | +void | |
| 56 | +threadedServerDtor(void * _this) | |
| 57 | +{ | |
| 58 | + TR_ThreadedServer this = _this; | |
| 59 | + int i; | |
| 60 | + | |
| 61 | + for (i=0; i<this->n_threads; i++) { | |
| 62 | + TR_delete(this->threads[i]); | |
| 63 | + } | |
| 64 | + | |
| 65 | + TR_PARENTCALL(TR_ThreadedServer, _this, TR_Class, dtor); | |
| 66 | +} | |
| 67 | + | |
| 68 | +TR_INIT_IFACE(TR_Class, threadedServerCtor, threadedServerDtor, NULL); | |
| 69 | +TR_CREATE_CLASS(TR_ThreadedServer, TR_Server, NULL, TR_IF(TR_Class)); | |
| 70 | + | |
| 71 | +// vim: set ts=4 sw=4: | ... | ... |
src/threaded_server_start.c
0 → 100644
| 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 "trbase.h" | |
| 24 | +#include "trevent.h" | |
| 25 | + | |
| 26 | +#include "tr/threaded_server.h" | |
| 27 | + | |
| 28 | +void | |
| 29 | +TR_threadedServerStart(TR_ThreadedServer this, unsigned long heartbeat) | |
| 30 | +{ | |
| 31 | + int i; | |
| 32 | + | |
| 33 | + TR_eventDispatcherSetHeartbeat(((TR_Server)this)->dispatcher, heartbeat); | |
| 34 | + | |
| 35 | + for (i=0; i<this->n_threads; i++) { | |
| 36 | + TR_eventThreadStart(this->threads[i]); | |
| 37 | + } | |
| 38 | + | |
| 39 | + for (i=0; i<this->n_threads; i++) { | |
| 40 | + TR_eventThreadJoin(this->threads[i]); | |
| 41 | + } | |
| 42 | +} | |
| 43 | + | |
| 44 | +// vim: set ts=4 sw=4: | ... | ... |
| ... | ... | @@ -7,3 +7,4 @@ gcc ${CFLAGS} -I/usr/local/include -L/usr/local/lib ${LIBS} -o testserver testse |
| 7 | 7 | gcc ${CFLAGS} -I/usr/local/include -L/usr/local/lib ${LIBS} -o testserver2 testserver2.c test_handler.o ${TRLIBS} |
| 8 | 8 | gcc ${CFLAGS} -I/usr/local/include -L/usr/local/lib ${LIBS} -o testtcp testclient.c ${TRLIBS} |
| 9 | 9 | gcc ${CFLAGS} -I/usr/local/include -L/usr/local/lib ${LIBS} -DUDP=1 -o testudp testclient.c ${TRLIBS} |
| 10 | +gcc ${CFLAGS} -I/usr/local/include -L/usr/local/lib ${LIBS} -lpthread -o testserver_thread testserver_thread.c test_handler.o ${TRLIBS} | ... | ... |
| ... | ... | @@ -31,6 +31,7 @@ main (int argc, char * argv[]) |
| 31 | 31 | int i, j=0; |
| 32 | 32 | |
| 33 | 33 | TR_logger = TR_INSTANCE_CAST(TR_Logger, mylogger2); |
| 34 | + | |
| 34 | 35 | protocol = TR_new(TR_ProtocolRaw); |
| 35 | 36 | #if UDP |
| 36 | 37 | socket = TR_new(TR_UdpSocket, TR_logger, "127.0.0.1", 5678, 0); |
| ... | ... | @@ -56,7 +57,7 @@ main (int argc, char * argv[]) |
| 56 | 57 | message = (TR_ProtoMessageRaw)TR_simpleClientIssue( |
| 57 | 58 | client, |
| 58 | 59 | (TR_ProtoMessage)message, |
| 59 | - 100); | |
| 60 | + 100000000000000000); | |
| 60 | 61 | |
| 61 | 62 | if (! message) break; |
| 62 | 63 | #if 0 | ... | ... |
testers/testserver_thread.c
0 → 100644
| 1 | +#include <stdio.h> | |
| 2 | +#include <string.h> | |
| 3 | +#include <inttypes.h> | |
| 4 | + | |
| 5 | +#include "trbase.h" | |
| 6 | +#include "trcomm.h" | |
| 7 | +#include "trio.h" | |
| 8 | +#include "trevent.h" | |
| 9 | + | |
| 10 | +#include "test_handler.h" | |
| 11 | + | |
| 12 | +TR_INSTANCE(TR_LoggerSyslog, mylogger, {TR_LOGGER_INFO}); | |
| 13 | +TR_INSTANCE(TR_LoggerStderr, mylogger2, {TR_LOGGER_INFO}); | |
| 14 | + | |
| 15 | +int | |
| 16 | +main (int argc, char * argv[]) | |
| 17 | +{ | |
| 18 | + TR_ThreadedServer server = TR_new(TR_ThreadedServer, 2); | |
| 19 | + TR_Protocol protocol = TR_new(TR_ProtocolRaw); | |
| 20 | + TestHandler test_handler = TR_new(TestHandler); | |
| 21 | + | |
| 22 | + //TR_logger = TR_INSTANCE_CAST(TR_Logger, mylogger2); | |
| 23 | + | |
| 24 | + TR_serverAddHandler((TR_Server)server, (TR_EventHandler)test_handler); | |
| 25 | + TR_serverBindTcp((TR_Server)server, "0.0.0.0", 5678, protocol); | |
| 26 | + TR_serverBindUdp((TR_Server)server, "0.0.0.0", 5678, protocol); | |
| 27 | + | |
| 28 | + TR_threadedServerStart(server, 1000); | |
| 29 | + | |
| 30 | + puts("cleanup..."); | |
| 31 | + | |
| 32 | + TR_delete(server); | |
| 33 | + TR_delete(test_handler); | |
| 34 | + TR_delete(protocol); | |
| 35 | + | |
| 36 | + TR_eventHandlerClassCleanup(TestHandler); | |
| 37 | + TR_serverClassCleanup(); | |
| 38 | + | |
| 39 | + TR_cleanup(); | |
| 40 | + | |
| 41 | + return 0; | |
| 42 | +} | |
| 43 | + | |
| 44 | +// vim: set ts=4 sw=4: | ... | ... |
Please
register
or
login
to post a comment