Commit c0b33ec777e8d674c782bbe94a72e6cdec6aa320

Authored by Georg Hopp
1 parent c6dd2aa0

try for a threaded server, but not very much luck now.

@@ -40,5 +40,8 @@ test-driver @@ -40,5 +40,8 @@ test-driver
40 /assets/html/_documentation.html 40 /assets/html/_documentation.html
41 tags 41 tags
42 /trcomm.h* 42 /trcomm.h*
43 -/testers/testserver*  
44 -/testers/testclient* 43 +/testers/testserver
  44 +/testers/testserver2
  45 +/testers/testserver_thread
  46 +/testers/testudp
  47 +/testers/testtcp
@@ -18,5 +18,5 @@ nobase_include_HEADERS = trcomm.h \ @@ -18,5 +18,5 @@ nobase_include_HEADERS = trcomm.h \
18 tr/simple_client.h \ 18 tr/simple_client.h \
19 tr/interface/comm_end_point.h \ 19 tr/interface/comm_end_point.h \
20 tr/interface/comm_manager.h \ 20 tr/interface/comm_manager.h \
21 - tr/interface/protocol.h  
22 - 21 + tr/interface/protocol.h \
  22 + tr/threaded_server.h
  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 +
@@ -21,6 +21,7 @@ @@ -21,6 +21,7 @@
21 #include "tr/interface/comm_end_point.h" 21 #include "tr/interface/comm_end_point.h"
22 #include "tr/interface/comm_manager.h" 22 #include "tr/interface/comm_manager.h"
23 #include "tr/interface/protocol.h" 23 #include "tr/interface/protocol.h"
  24 +#include "tr/threaded_server.h"
24 25
25 #endif // __TR_COMM_H__ 26 #endif // __TR_COMM_H__
26 27
@@ -25,6 +25,8 @@ TRCOMM = cet_accept.c \ @@ -25,6 +25,8 @@ TRCOMM = cet_accept.c \
25 protocol_message_raw.c \ 25 protocol_message_raw.c \
26 protocol_raw.c \ 26 protocol_raw.c \
27 server.c \ 27 server.c \
  28 + threaded_server.c \
  29 + threaded_server_start.c \
28 server_bind_tcp.c \ 30 server_bind_tcp.c \
29 server_bind_udp.c \ 31 server_bind_udp.c \
30 server_start.c \ 32 server_start.c \
@@ -103,6 +103,8 @@ static @@ -103,6 +103,8 @@ static
103 void 103 void
104 commManagerCvInit(TR_class_ptr cls) 104 commManagerCvInit(TR_class_ptr cls)
105 { 105 {
  106 + TR_CLASSVARS(TR_EventHandler, cls)->event_methods->tree = TR_new(TR_Tree);
  107 +
106 TR_EVENT_HANDLER_SET_METHOD( 108 TR_EVENT_HANDLER_SET_METHOD(
107 cls, TR_EventDispatcher, 109 cls, TR_EventDispatcher,
108 TR_DISPATCHER_EVENT_DATA_WAIT, 110 TR_DISPATCHER_EVENT_DATA_WAIT,
@@ -92,11 +92,13 @@ static @@ -92,11 +92,13 @@ static
92 void 92 void
93 connectorCvInit(TR_class_ptr cls) 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 TR_INSTANCE(TR_Hash, connectorEventMethods); 104 TR_INSTANCE(TR_Hash, connectorEventMethods);
@@ -158,6 +158,8 @@ static @@ -158,6 +158,8 @@ static
158 void 158 void
159 ioHandlerCvInit(TR_class_ptr cls) 159 ioHandlerCvInit(TR_class_ptr cls)
160 { 160 {
  161 + TR_CLASSVARS(TR_EventHandler, cls)->event_methods->tree = TR_new(TR_Tree);
  162 +
161 TR_EVENT_HANDLER_SET_METHOD( 163 TR_EVENT_HANDLER_SET_METHOD(
162 cls, 164 cls,
163 TR_CommEndPoint, 165 TR_CommEndPoint,
@@ -125,6 +125,8 @@ static @@ -125,6 +125,8 @@ static
125 void 125 void
126 protocolHandlerCvInit(TR_class_ptr cls) 126 protocolHandlerCvInit(TR_class_ptr cls)
127 { 127 {
  128 + TR_CLASSVARS(TR_EventHandler, cls)->event_methods->tree = TR_new(TR_Tree);
  129 +
128 TR_EVENT_HANDLER_SET_METHOD( 130 TR_EVENT_HANDLER_SET_METHOD(
129 cls, 131 cls,
130 TR_CommEndPoint, 132 TR_CommEndPoint,
@@ -135,6 +135,8 @@ static @@ -135,6 +135,8 @@ static
135 void 135 void
136 simpleClientCvInit(TR_class_ptr cls) 136 simpleClientCvInit(TR_class_ptr cls)
137 { 137 {
  138 + TR_CLASSVARS(TR_EventHandler, cls)->event_methods->tree = TR_new(TR_Tree);
  139 +
138 TR_EVENT_HANDLER_SET_METHOD( 140 TR_EVENT_HANDLER_SET_METHOD(
139 cls, 141 cls,
140 TR_EventDispatcher, 142 TR_EventDispatcher,
  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:
  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,3 +7,4 @@ gcc ${CFLAGS} -I/usr/local/include -L/usr/local/lib ${LIBS} -o testserver testse
7 gcc ${CFLAGS} -I/usr/local/include -L/usr/local/lib ${LIBS} -o testserver2 testserver2.c test_handler.o ${TRLIBS} 7 gcc ${CFLAGS} -I/usr/local/include -L/usr/local/lib ${LIBS} -o testserver2 testserver2.c test_handler.o ${TRLIBS}
8 gcc ${CFLAGS} -I/usr/local/include -L/usr/local/lib ${LIBS} -o testtcp testclient.c ${TRLIBS} 8 gcc ${CFLAGS} -I/usr/local/include -L/usr/local/lib ${LIBS} -o testtcp testclient.c ${TRLIBS}
9 gcc ${CFLAGS} -I/usr/local/include -L/usr/local/lib ${LIBS} -DUDP=1 -o testudp testclient.c ${TRLIBS} 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}
@@ -98,6 +98,8 @@ static @@ -98,6 +98,8 @@ static
98 void 98 void
99 testHandlerCvInit(TR_class_ptr class) 99 testHandlerCvInit(TR_class_ptr class)
100 { 100 {
  101 + TR_CLASSVARS(TR_EventHandler, class)->event_methods->tree = TR_new(TR_Tree);
  102 +
101 TR_EVENT_HANDLER_SET_METHOD( 103 TR_EVENT_HANDLER_SET_METHOD(
102 class, 104 class,
103 TR_EventDispatcher, 105 TR_EventDispatcher,
@@ -31,6 +31,7 @@ main (int argc, char * argv[]) @@ -31,6 +31,7 @@ main (int argc, char * argv[])
31 int i, j=0; 31 int i, j=0;
32 32
33 TR_logger = TR_INSTANCE_CAST(TR_Logger, mylogger2); 33 TR_logger = TR_INSTANCE_CAST(TR_Logger, mylogger2);
  34 +
34 protocol = TR_new(TR_ProtocolRaw); 35 protocol = TR_new(TR_ProtocolRaw);
35 #if UDP 36 #if UDP
36 socket = TR_new(TR_UdpSocket, TR_logger, "127.0.0.1", 5678, 0); 37 socket = TR_new(TR_UdpSocket, TR_logger, "127.0.0.1", 5678, 0);
@@ -56,7 +57,7 @@ main (int argc, char * argv[]) @@ -56,7 +57,7 @@ main (int argc, char * argv[])
56 message = (TR_ProtoMessageRaw)TR_simpleClientIssue( 57 message = (TR_ProtoMessageRaw)TR_simpleClientIssue(
57 client, 58 client,
58 (TR_ProtoMessage)message, 59 (TR_ProtoMessage)message,
59 - 100); 60 + 100000000000000000);
60 61
61 if (! message) break; 62 if (! message) break;
62 #if 0 63 #if 0
  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