Commit 079be38153796e5effdb202736df69c599af3ceb
1 parent
2c3acbb3
fix close handling and introduce server class
Showing
13 changed files
with
415 additions
and
150 deletions
@@ -12,6 +12,7 @@ nobase_include_HEADERS = trcomm.h \ | @@ -12,6 +12,7 @@ nobase_include_HEADERS = trcomm.h \ | ||
12 | tr/protocol/raw.h \ | 12 | tr/protocol/raw.h \ |
13 | tr/protocol/message_raw.h \ | 13 | tr/protocol/message_raw.h \ |
14 | tr/protocol_handler.h \ | 14 | tr/protocol_handler.h \ |
15 | + tr/server.h \ | ||
15 | tr/interface/comm_end_point.h \ | 16 | tr/interface/comm_end_point.h \ |
16 | tr/interface/comm_manager.h \ | 17 | tr/interface/comm_manager.h \ |
17 | tr/interface/protocol.h | 18 | tr/interface/protocol.h |
include/tr/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_SERVER_H__ | ||
24 | +#define __TR_SERVER_H__ | ||
25 | + | ||
26 | +#include <sys/types.h> | ||
27 | + | ||
28 | +#include "trbase.h" | ||
29 | +#include "trevent.h" | ||
30 | + | ||
31 | +#include "tr/comm_manager.h" | ||
32 | +#include "tr/connector.h" | ||
33 | +#include "tr/io_handler.h" | ||
34 | +#include "tr/protocol_handler.h" | ||
35 | + | ||
36 | +TR_CLASS(TR_Server) { | ||
37 | + TR_CommManager comm_manager; | ||
38 | + TR_EventDispatcher dispatcher; | ||
39 | + TR_Connector connector; | ||
40 | + TR_IoHandler io_handler; | ||
41 | + TR_ProtocolHandler protocol_handler; | ||
42 | +}; | ||
43 | +TR_INSTANCE_INIT(TR_Server); | ||
44 | +TR_CLASSVARS_DECL(TR_Server) {}; | ||
45 | + | ||
46 | +#define TR_serverAddEndpoint(srv, ep) \ | ||
47 | + TR_commManagerAddEndpoint((srv)->comm_manager, (ep)) | ||
48 | + | ||
49 | +#define TR_serverAddHandler(srv, handler) \ | ||
50 | + TR_eventDispatcherRegisterHandler((srv)->dispatcher, (handler)) | ||
51 | + | ||
52 | +void TR_serverStart(TR_Server, int); | ||
53 | + | ||
54 | +#define TR_serverClassCleanup() \ | ||
55 | + TR_eventHandlerClassCleanup(TR_ProtocolHandler); \ | ||
56 | + TR_eventHandlerClassCleanup(TR_IoHandler); \ | ||
57 | + TR_eventHandlerClassCleanup(TR_Connector); \ | ||
58 | + TR_eventHandlerClassCleanup(TR_CommManagerPoll) | ||
59 | + | ||
60 | +#endif // __TR_SERVER_H__ | ||
61 | + | ||
62 | +// vim: set ts=4 sw=4: | ||
63 | + |
@@ -14,6 +14,7 @@ | @@ -14,6 +14,7 @@ | ||
14 | #include "tr/protocol/raw.h" | 14 | #include "tr/protocol/raw.h" |
15 | #include "tr/protocol/message_raw.h" | 15 | #include "tr/protocol/message_raw.h" |
16 | #include "tr/protocol_handler.h" | 16 | #include "tr/protocol_handler.h" |
17 | +#include "tr/server.h" | ||
17 | #include "tr/interface/comm_end_point.h" | 18 | #include "tr/interface/comm_end_point.h" |
18 | #include "tr/interface/comm_manager.h" | 19 | #include "tr/interface/comm_manager.h" |
19 | #include "tr/interface/protocol.h" | 20 | #include "tr/interface/protocol.h" |
@@ -24,6 +24,8 @@ TRCOMM = cep_append_read_data.c \ | @@ -24,6 +24,8 @@ TRCOMM = cep_append_read_data.c \ | ||
24 | protocol_handler.c \ | 24 | protocol_handler.c \ |
25 | protocol_message_raw.c \ | 25 | protocol_message_raw.c \ |
26 | protocol_raw.c \ | 26 | protocol_raw.c \ |
27 | + server.c \ | ||
28 | + server_start.c \ | ||
27 | i_comm_end_point.c \ | 29 | i_comm_end_point.c \ |
28 | i_comm_manager.c \ | 30 | i_comm_manager.c \ |
29 | i_protocol.c | 31 | i_protocol.c |
@@ -112,7 +112,7 @@ TR_commManagerShutdownRead(void * _this, TR_Event event) | @@ -112,7 +112,7 @@ TR_commManagerShutdownRead(void * _this, TR_Event event) | ||
112 | { | 112 | { |
113 | TR_CALL(_this, TR_CommManager, shutdownRead, event); | 113 | TR_CALL(_this, TR_CommManager, shutdownRead, event); |
114 | 114 | ||
115 | - if (TR_socketFinRd(((TR_CommEndPoint)event->subject)->transport)) { | 115 | + if (TR_socketFinRdWr(((TR_CommEndPoint)event->subject)->transport)) { |
116 | // close | 116 | // close |
117 | TR_eventHandlerIssueEvent( | 117 | TR_eventHandlerIssueEvent( |
118 | (TR_EventHandler)_this, | 118 | (TR_EventHandler)_this, |
@@ -120,7 +120,7 @@ TR_commManagerShutdownRead(void * _this, TR_Event event) | @@ -120,7 +120,7 @@ TR_commManagerShutdownRead(void * _this, TR_Event event) | ||
120 | event->subject, | 120 | event->subject, |
121 | TR_CEP_EVENT_CLOSE, | 121 | TR_CEP_EVENT_CLOSE, |
122 | NULL)); | 122 | NULL)); |
123 | - } else if (TR_cepHasPendingData((TR_CommEndPoint)event->subject)) { | 123 | + } else if (! TR_cepHasPendingData((TR_CommEndPoint)event->subject)) { |
124 | // handle pending data... close is issued from disableWrite | 124 | // handle pending data... close is issued from disableWrite |
125 | TR_eventHandlerIssueEvent( | 125 | TR_eventHandlerIssueEvent( |
126 | (TR_EventHandler)_this, | 126 | (TR_EventHandler)_this, |
@@ -87,21 +87,29 @@ ioHandlerWrite(void * _this, TR_Event event) | @@ -87,21 +87,29 @@ ioHandlerWrite(void * _this, TR_Event event) | ||
87 | TR_CommEndPoint endpoint = (TR_CommEndPoint)event->subject; | 87 | TR_CommEndPoint endpoint = (TR_CommEndPoint)event->subject; |
88 | 88 | ||
89 | if (TR_cepWriteBuffered(endpoint)) { | 89 | if (TR_cepWriteBuffered(endpoint)) { |
90 | - TR_Event new_event; | ||
91 | - | ||
92 | if (TR_cepHasPendingData(endpoint)) { | 90 | if (TR_cepHasPendingData(endpoint)) { |
93 | - new_event = TR_eventSubjectEmit( | ||
94 | - event->subject, | ||
95 | - TR_CEP_EVENT_PENDING_DATA, | ||
96 | - NULL); | 91 | + TR_eventHandlerIssueEvent( |
92 | + (TR_EventHandler)_this, | ||
93 | + TR_eventSubjectEmit( | ||
94 | + event->subject, | ||
95 | + TR_CEP_EVENT_PENDING_DATA, | ||
96 | + NULL)); | ||
97 | } else { | 97 | } else { |
98 | - new_event = TR_eventSubjectEmit( | ||
99 | - event->subject, | ||
100 | - TR_CEP_EVENT_END_DATA, | ||
101 | - NULL); | 98 | + TR_eventHandlerIssueEvent( |
99 | + (TR_EventHandler)_this, | ||
100 | + TR_eventSubjectEmit( | ||
101 | + event->subject, | ||
102 | + TR_CEP_EVENT_END_DATA, | ||
103 | + NULL)); | ||
104 | + if (TRUE == endpoint->do_close) { | ||
105 | + TR_eventHandlerIssueEvent( | ||
106 | + (TR_EventHandler)_this, | ||
107 | + TR_eventSubjectEmit( | ||
108 | + event->subject, | ||
109 | + TR_CEP_EVENT_CLOSE, | ||
110 | + NULL)); | ||
111 | + } | ||
102 | } | 112 | } |
103 | - | ||
104 | - TR_eventHandlerIssueEvent((TR_EventHandler)_this, new_event); | ||
105 | } | 113 | } |
106 | 114 | ||
107 | return TR_EVENT_DONE; | 115 | return TR_EVENT_DONE; |
src/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/server.h" | ||
32 | +#include "tr/comm_manager.h" | ||
33 | +#include "tr/comm_manager_poll.h" | ||
34 | +#include "tr/connector.h" | ||
35 | +#include "tr/io_handler.h" | ||
36 | +#include "tr/protocol_handler.h" | ||
37 | + | ||
38 | + | ||
39 | +static | ||
40 | +int | ||
41 | +serverCtor(void * _this, va_list * params) | ||
42 | +{ | ||
43 | + TR_Server this = _this; | ||
44 | + | ||
45 | + this->comm_manager = (TR_CommManager)TR_new(TR_CommManagerPoll); | ||
46 | + this->dispatcher = TR_new(TR_EventDispatcher); | ||
47 | + this->connector = TR_new(TR_Connector); | ||
48 | + this->io_handler = TR_new(TR_IoHandler); | ||
49 | + this->protocol_handler = TR_new(TR_ProtocolHandler); | ||
50 | + | ||
51 | + TR_eventDispatcherRegisterHandler( | ||
52 | + this->dispatcher, | ||
53 | + (TR_EventHandler)this->comm_manager); | ||
54 | + TR_eventDispatcherRegisterHandler( | ||
55 | + this->dispatcher, | ||
56 | + (TR_EventHandler)this->connector); | ||
57 | + TR_eventDispatcherRegisterHandler( | ||
58 | + this->dispatcher, | ||
59 | + (TR_EventHandler)this->io_handler); | ||
60 | + TR_eventDispatcherRegisterHandler( | ||
61 | + this->dispatcher, | ||
62 | + (TR_EventHandler)this->protocol_handler); | ||
63 | + | ||
64 | + return 0; | ||
65 | +} | ||
66 | + | ||
67 | +static | ||
68 | +void | ||
69 | +serverDtor(void * _this) | ||
70 | +{ | ||
71 | + TR_Server this = _this; | ||
72 | + | ||
73 | + TR_delete(this->protocol_handler); | ||
74 | + TR_delete(this->io_handler); | ||
75 | + TR_delete(this->connector); | ||
76 | + TR_delete(this->dispatcher); | ||
77 | + TR_delete(this->comm_manager); | ||
78 | +} | ||
79 | + | ||
80 | +TR_INIT_IFACE(TR_Class, serverCtor, serverDtor, NULL); | ||
81 | +TR_CREATE_CLASS(TR_Server, NULL, NULL, TR_IF(TR_Class)); | ||
82 | + | ||
83 | +// vim: set ts=4 sw=4: |
src/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/server.h" | ||
27 | + | ||
28 | +void | ||
29 | +TR_serverStart(TR_Server this, int heartbeat) | ||
30 | +{ | ||
31 | + TR_eventDispatcherSetHeartbeat(this->dispatcher, 1000); | ||
32 | + TR_eventDispatcherStart(this->dispatcher); | ||
33 | +} | ||
34 | + | ||
35 | +// vim: set ts=4 sw=4: |
1 | #!/bin/bash | 1 | #!/bin/bash |
2 | TRLIBS="-ltrbase -ltrhashing -ltrio -ltrdata -ltrevent -ltrcomm" | 2 | TRLIBS="-ltrbase -ltrhashing -ltrio -ltrdata -ltrevent -ltrcomm" |
3 | LIBS="-lcrypto -lssl -lrt -luuid" | 3 | LIBS="-lcrypto -lssl -lrt -luuid" |
4 | -gcc ${CFLAGS} -I/usr/local/include -L/usr/local/lib ${TRLIBS} ${LIBS} -o testserver testserver.c | 4 | +gcc ${CFLAGS} -c -o test_handler.o test_handler.c |
5 | +gcc ${CFLAGS} -I/usr/local/include -L/usr/local/lib ${TRLIBS} ${LIBS} -o testserver testserver.c test_handler.o | ||
6 | +gcc ${CFLAGS} -I/usr/local/include -L/usr/local/lib ${TRLIBS} ${LIBS} -o testserver2 testserver2.c test_handler.o |
testers/test_handler.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 "trevent.h" | ||
8 | +#include "test_handler.h" | ||
9 | + | ||
10 | +static | ||
11 | +TR_EventDone | ||
12 | +testHandlerHeartbeat(TR_EventHandler this, TR_Event event) | ||
13 | +{ | ||
14 | + printf("handled: %llu/s\n", ((TestHandler)this)->handled); | ||
15 | + ((TestHandler)this)->handled = 0; | ||
16 | + | ||
17 | + return TR_EVENT_DONE; | ||
18 | +} | ||
19 | + | ||
20 | +static | ||
21 | +TR_EventDone | ||
22 | +testHandlerNewMessage(TR_EventHandler this, TR_Event event) | ||
23 | +{ | ||
24 | + TR_ProtoMessageRaw msg = event->data; | ||
25 | + TR_SizedData data = (TR_SizedData)msg->data; | ||
26 | + char buf[data->size + 1]; | ||
27 | + int i; | ||
28 | + | ||
29 | + ((TestHandler)this)->handled++; | ||
30 | + | ||
31 | + memcpy(buf, data->data, data->size); | ||
32 | + buf[data->size] = 0; | ||
33 | + for (i = 0; buf[i]; i++) { | ||
34 | + if (! isprint(buf[i])) buf[i] = '.'; | ||
35 | + } | ||
36 | +// printf("echo message: %s(%zd)\n", buf, data->size); | ||
37 | + | ||
38 | + TR_eventHandlerIssueEvent( | ||
39 | + (TR_EventHandler)this, | ||
40 | + TR_eventSubjectEmit( | ||
41 | + event->subject, | ||
42 | + TR_CEP_EVENT_SEND_MSG, | ||
43 | + event->data)); | ||
44 | + | ||
45 | + return TR_EVENT_DONE; | ||
46 | +} | ||
47 | + | ||
48 | +static | ||
49 | +TR_EventDone | ||
50 | +testHandlerClose(TR_EventHandler this, TR_Event event) | ||
51 | +{ | ||
52 | + puts("close"); | ||
53 | + | ||
54 | + return TR_EVENT_PENDING; | ||
55 | +} | ||
56 | + | ||
57 | +static | ||
58 | +TR_EventDone | ||
59 | +testHandlerUpgrade(TR_EventHandler this, TR_Event event) | ||
60 | +{ | ||
61 | + printf("upgrade: %"PRIdPTR"\n", event->id); | ||
62 | + | ||
63 | + return TR_EVENT_PENDING; | ||
64 | +} | ||
65 | + | ||
66 | +static | ||
67 | +int | ||
68 | +testHandlerCtor(void * _this, va_list * params) | ||
69 | +{ | ||
70 | + TR_PARENTCALL(TestHandler, _this, TR_Class, ctor, params); | ||
71 | + ((TestHandler)_this)->handled = 0; | ||
72 | + | ||
73 | + return 0; | ||
74 | +} | ||
75 | + | ||
76 | +static | ||
77 | +void | ||
78 | +testHandlerDtor(void * _this, va_list * params) | ||
79 | +{ | ||
80 | + TR_PARENTCALL(TestHandler, _this, TR_Class, dtor); | ||
81 | +} | ||
82 | + | ||
83 | +static | ||
84 | +void | ||
85 | +testHandlerCvInit(TR_class_ptr class) | ||
86 | +{ | ||
87 | + TR_EVENT_HANDLER_SET_METHOD( | ||
88 | + class, | ||
89 | + TR_EventDispatcher, | ||
90 | + TR_DISPATCHER_EVENT_HEARTBEAT, | ||
91 | + testHandlerHeartbeat); | ||
92 | + TR_EVENT_HANDLER_SET_METHOD( | ||
93 | + class, | ||
94 | + TR_CommEndPoint, | ||
95 | + TR_CEP_EVENT_NEW_MSG, | ||
96 | + testHandlerNewMessage); | ||
97 | + TR_EVENT_HANDLER_SET_METHOD( | ||
98 | + class, | ||
99 | + TR_CommEndPoint, | ||
100 | + TR_CEP_EVENT_CLOSE, | ||
101 | + testHandlerClose); | ||
102 | + TR_EVENT_HANDLER_SET_METHOD( | ||
103 | + class, | ||
104 | + TR_CommEndPoint, | ||
105 | + TR_CEP_EVENT_UPGRADE, | ||
106 | + testHandlerUpgrade); | ||
107 | +} | ||
108 | + | ||
109 | +TR_INSTANCE(TR_Hash, testHandlerEventMethods); | ||
110 | +TR_INIT_IFACE(TR_Class, testHandlerCtor, testHandlerDtor, NULL); | ||
111 | +TR_CREATE_CLASS( | ||
112 | + TestHandler, | ||
113 | + TR_EventHandler, | ||
114 | + testHandlerCvInit, | ||
115 | + TR_IF(TR_Class)) = { | ||
116 | + { &(_testHandlerEventMethods.data) } | ||
117 | +}; | ||
118 | + | ||
119 | +// vim: set ts=4 sw=4: |
testers/test_handler.h
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 "trevent.h" | ||
8 | + | ||
9 | +TR_CLASS(TestHandler) { | ||
10 | + TR_EXTENDS(TR_EventHandler); | ||
11 | + unsigned long long handled; | ||
12 | +}; | ||
13 | +TR_INSTANCE_INIT(TestHandler); | ||
14 | +TR_CLASSVARS_DECL(TestHandler) { | ||
15 | + TR_CV_EXTENDS(TR_EventHandler); | ||
16 | +}; | ||
17 | + | ||
18 | +// vim: set ts=4 sw=4: |
@@ -6,123 +6,7 @@ | @@ -6,123 +6,7 @@ | ||
6 | #include "trcomm.h" | 6 | #include "trcomm.h" |
7 | #include "trevent.h" | 7 | #include "trevent.h" |
8 | 8 | ||
9 | -TR_CLASS(TestHandler) { | ||
10 | - TR_EXTENDS(TR_EventHandler); | ||
11 | - unsigned long long handled; | ||
12 | -}; | ||
13 | -TR_INSTANCE_INIT(TestHandler); | ||
14 | -TR_CLASSVARS_DECL(TestHandler) { | ||
15 | - TR_CV_EXTENDS(TR_EventHandler); | ||
16 | -}; | ||
17 | - | ||
18 | -static | ||
19 | -TR_EventDone | ||
20 | -testHandlerHeartbeat(TR_EventHandler this, TR_Event event) | ||
21 | -{ | ||
22 | - printf("handled: %llu/s\n", ((TestHandler)this)->handled); | ||
23 | - ((TestHandler)this)->handled = 0; | ||
24 | - | ||
25 | - return TR_EVENT_DONE; | ||
26 | -} | ||
27 | - | ||
28 | -static | ||
29 | -TR_EventDone | ||
30 | -testHandlerNewMessage(TR_EventHandler this, TR_Event event) | ||
31 | -{ | ||
32 | - TR_ProtoMessageRaw msg = event->data; | ||
33 | - TR_SizedData data = (TR_SizedData)msg->data; | ||
34 | - char buf[data->size + 1]; | ||
35 | - int i; | ||
36 | - | ||
37 | - ((TestHandler)this)->handled++; | ||
38 | - | ||
39 | - memcpy(buf, data->data, data->size); | ||
40 | - buf[data->size] = 0; | ||
41 | - for (i = 0; buf[i]; i++) { | ||
42 | - if (! isprint(buf[i])) buf[i] = '.'; | ||
43 | - } | ||
44 | - printf("echo message: %s(%zd)\n", buf, data->size); | ||
45 | - | ||
46 | - TR_eventHandlerIssueEvent( | ||
47 | - (TR_EventHandler)this, | ||
48 | - TR_eventSubjectEmit( | ||
49 | - event->subject, | ||
50 | - TR_CEP_EVENT_SEND_MSG, | ||
51 | - event->data)); | ||
52 | - | ||
53 | - return TR_EVENT_DONE; | ||
54 | -} | ||
55 | - | ||
56 | -static | ||
57 | -TR_EventDone | ||
58 | -testHandlerClose(TR_EventHandler this, TR_Event event) | ||
59 | -{ | ||
60 | - puts("close"); | ||
61 | - | ||
62 | - return TR_EVENT_PENDING; | ||
63 | -} | ||
64 | - | ||
65 | -static | ||
66 | -TR_EventDone | ||
67 | -testHandlerUpgrade(TR_EventHandler this, TR_Event event) | ||
68 | -{ | ||
69 | - printf("upgrade: %"PRIdPTR"\n", event->id); | ||
70 | - | ||
71 | - return TR_EVENT_PENDING; | ||
72 | -} | ||
73 | - | ||
74 | -static | ||
75 | -int | ||
76 | -testHandlerCtor(void * _this, va_list * params) | ||
77 | -{ | ||
78 | - TR_PARENTCALL(TestHandler, _this, TR_Class, ctor, params); | ||
79 | - ((TestHandler)_this)->handled = 0; | ||
80 | - | ||
81 | - return 0; | ||
82 | -} | ||
83 | - | ||
84 | -static | ||
85 | -void | ||
86 | -testHandlerDtor(void * _this, va_list * params) | ||
87 | -{ | ||
88 | - TR_PARENTCALL(TestHandler, _this, TR_Class, dtor); | ||
89 | -} | ||
90 | - | ||
91 | -static | ||
92 | -void | ||
93 | -testHandlerCvInit(TR_class_ptr class) | ||
94 | -{ | ||
95 | - TR_EVENT_HANDLER_SET_METHOD( | ||
96 | - class, | ||
97 | - TR_EventDispatcher, | ||
98 | - TR_DISPATCHER_EVENT_HEARTBEAT, | ||
99 | - testHandlerHeartbeat); | ||
100 | - TR_EVENT_HANDLER_SET_METHOD( | ||
101 | - class, | ||
102 | - TR_CommEndPoint, | ||
103 | - TR_CEP_EVENT_NEW_MSG, | ||
104 | - testHandlerNewMessage); | ||
105 | - TR_EVENT_HANDLER_SET_METHOD( | ||
106 | - class, | ||
107 | - TR_CommEndPoint, | ||
108 | - TR_CEP_EVENT_CLOSE, | ||
109 | - testHandlerClose); | ||
110 | - TR_EVENT_HANDLER_SET_METHOD( | ||
111 | - class, | ||
112 | - TR_CommEndPoint, | ||
113 | - TR_CEP_EVENT_UPGRADE, | ||
114 | - testHandlerUpgrade); | ||
115 | -} | ||
116 | - | ||
117 | -TR_INSTANCE(TR_Hash, testHandlerEventMethods); | ||
118 | -TR_INIT_IFACE(TR_Class, testHandlerCtor, testHandlerDtor, NULL); | ||
119 | -TR_CREATE_CLASS( | ||
120 | - TestHandler, | ||
121 | - TR_EventHandler, | ||
122 | - testHandlerCvInit, | ||
123 | - TR_IF(TR_Class)) = { | ||
124 | - { &(_testHandlerEventMethods.data) } | ||
125 | -}; | 9 | +#include "test_handler.h" |
126 | 10 | ||
127 | TR_INSTANCE(TR_LoggerSyslog, mylogger, {TR_LOGGER_DEBUG}); | 11 | TR_INSTANCE(TR_LoggerSyslog, mylogger, {TR_LOGGER_DEBUG}); |
128 | 12 | ||
@@ -135,13 +19,10 @@ main (int argc, char * argv[]) | @@ -135,13 +19,10 @@ main (int argc, char * argv[]) | ||
135 | TR_IoHandler io_handler = TR_new(TR_IoHandler); | 19 | TR_IoHandler io_handler = TR_new(TR_IoHandler); |
136 | TR_ProtocolHandler protocol_handler = TR_new(TR_ProtocolHandler); | 20 | TR_ProtocolHandler protocol_handler = TR_new(TR_ProtocolHandler); |
137 | TestHandler test_handler = TR_new(TestHandler); | 21 | TestHandler test_handler = TR_new(TestHandler); |
138 | -#if 0 | ||
139 | - TR_ConnEntryPoint ep; | ||
140 | - TR_TcpSocket ep_sock; | ||
141 | -#else | ||
142 | - TR_DatagramService ep; | ||
143 | - TR_UdpSocket ep_sock; | ||
144 | -#endif | 22 | + TR_ConnEntryPoint tcp_ep; |
23 | + TR_TcpSocket tcp_ep_sock; | ||
24 | + TR_DatagramService udp_ep; | ||
25 | + TR_UdpSocket udp_ep_sock; | ||
145 | TR_Protocol protocol; | 26 | TR_Protocol protocol; |
146 | 27 | ||
147 | TR_logger = TR_INSTANCE_CAST(TR_Logger, mylogger); | 28 | TR_logger = TR_INSTANCE_CAST(TR_Logger, mylogger); |
@@ -157,17 +38,15 @@ main (int argc, char * argv[]) | @@ -157,17 +38,15 @@ main (int argc, char * argv[]) | ||
157 | (TR_EventHandler)test_handler); | 38 | (TR_EventHandler)test_handler); |
158 | 39 | ||
159 | protocol = TR_new(TR_ProtocolRaw); | 40 | protocol = TR_new(TR_ProtocolRaw); |
160 | -#if 0 | ||
161 | - ep_sock = TR_new(TR_TcpSocket, TR_logger, "0.0.0.0", 5678, 0); | ||
162 | - ep = TR_new(TR_ConnEntryPoint, ep_sock, protocol); | ||
163 | -#else | ||
164 | - ep_sock = TR_new(TR_UdpSocket, TR_logger, "0.0.0.0", 5678, 0); | ||
165 | - TR_socketBind((TR_Socket)ep_sock); | ||
166 | - TR_socketNonblock((TR_Socket)ep_sock); | ||
167 | - ep = TR_new(TR_DatagramService, ep_sock, protocol); | ||
168 | -#endif | ||
169 | - | ||
170 | - TR_commManagerAddEndpoint(cmgr, (TR_CommEndPoint)ep); | 41 | + tcp_ep_sock = TR_new(TR_TcpSocket, TR_logger, "0.0.0.0", 5678, 0); |
42 | + tcp_ep = TR_new(TR_ConnEntryPoint, tcp_ep_sock, protocol); | ||
43 | + udp_ep_sock = TR_new(TR_UdpSocket, TR_logger, "0.0.0.0", 5678, 0); | ||
44 | + TR_socketBind((TR_Socket)udp_ep_sock); | ||
45 | + TR_socketNonblock((TR_Socket)udp_ep_sock); | ||
46 | + udp_ep = TR_new(TR_DatagramService, udp_ep_sock, protocol); | ||
47 | + | ||
48 | + TR_commManagerAddEndpoint(cmgr, (TR_CommEndPoint)tcp_ep); | ||
49 | + TR_commManagerAddEndpoint(cmgr, (TR_CommEndPoint)udp_ep); | ||
171 | 50 | ||
172 | TR_eventDispatcherSetHeartbeat(dispatcher, 1000); | 51 | TR_eventDispatcherSetHeartbeat(dispatcher, 1000); |
173 | TR_eventDispatcherStart(dispatcher); | 52 | TR_eventDispatcherStart(dispatcher); |
testers/testserver2.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_DEBUG}); | ||
13 | + | ||
14 | +int | ||
15 | +main (int argc, char * argv[]) | ||
16 | +{ | ||
17 | + TR_Server server = TR_new(TR_Server); | ||
18 | + TR_Protocol protocol = TR_new(TR_ProtocolRaw); | ||
19 | + TestHandler test_handler = TR_new(TestHandler); | ||
20 | + TR_Socket socket; | ||
21 | + | ||
22 | + TR_logger = TR_INSTANCE_CAST(TR_Logger, mylogger); | ||
23 | + | ||
24 | + TR_serverAddHandler(server, (TR_EventHandler)test_handler); | ||
25 | + socket = (TR_Socket)TR_new(TR_TcpSocket, TR_logger, "0.0.0.0", 5678, 0); | ||
26 | + TR_serverAddEndpoint( | ||
27 | + server, | ||
28 | + TR_new(TR_ConnEntryPoint, socket, protocol)); | ||
29 | + | ||
30 | + socket = TR_new(TR_UdpSocket, TR_logger, "0.0.0.0", 5678, 0); | ||
31 | + TR_socketBind((TR_Socket)socket); | ||
32 | + TR_socketNonblock((TR_Socket)socket); | ||
33 | + TR_serverAddEndpoint( | ||
34 | + server, | ||
35 | + TR_new(TR_DatagramService, socket, protocol)); | ||
36 | + | ||
37 | + TR_serverStart(server, 1000); | ||
38 | + | ||
39 | + puts("cleanup..."); | ||
40 | + | ||
41 | + TR_delete(server); | ||
42 | + TR_delete(test_handler); | ||
43 | + TR_delete(protocol); | ||
44 | + //TR_delete(ep); | ||
45 | + | ||
46 | + TR_eventHandlerClassCleanup(TestHandler); | ||
47 | + TR_serverClassCleanup(); | ||
48 | + | ||
49 | + TR_cleanup(); | ||
50 | + | ||
51 | + return 0; | ||
52 | +} | ||
53 | + | ||
54 | +// vim: set ts=4 sw=4: |
Please
register
or
login
to post a comment