Commit 42d139f31e1d9e5136452a4e60c44f94e12a2033
1 parent
93ccec2f
Some fixes and adaptations on changes trevent code. Add a simple echo client.
Showing
17 changed files
with
369 additions
and
18 deletions
... | ... | @@ -31,7 +31,7 @@ |
31 | 31 | #include "tr/comm_end_point.h" |
32 | 32 | |
33 | 33 | typedef TR_EventDone (* fptr_TR_commManagerAddEndpoint)(void *, TR_CommEndPoint); |
34 | -typedef TR_EventDone (* fptr_TR_commManagerSelect)(void *, TR_Event, int); | |
34 | +typedef TR_EventDone (* fptr_TR_commManagerSelect)(void *, TR_Event, unsigned long); | |
35 | 35 | typedef TR_EventDone (* fptr_TR_commManagerPollWrite)(void *, TR_Event); |
36 | 36 | typedef TR_EventDone (* fptr_TR_commManagerPollRead)(void *, TR_Event); |
37 | 37 | typedef TR_EventDone (* fptr_TR_commManagerDisableWrite)(void *, TR_Event); | ... | ... |
... | ... | @@ -53,7 +53,7 @@ TR_CLASSVARS_DECL(TR_Server) {}; |
53 | 53 | |
54 | 54 | void TR_serverBindTcp(TR_Server, const char *, int, TR_Protocol); |
55 | 55 | void TR_serverBindUdp(TR_Server, const char *, int, TR_Protocol); |
56 | -void TR_serverStart(TR_Server, int); | |
56 | +void TR_serverStart(TR_Server, unsigned long); | |
57 | 57 | |
58 | 58 | #define TR_serverClassCleanup() \ |
59 | 59 | TR_eventHandlerClassCleanup(TR_ProtocolHandler); \ | ... | ... |
include/tr/simple_client.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_SIMPLE_CLIENT_H__ | |
24 | +#define __TR_SIMPLE_CLIENT_H__ | |
25 | + | |
26 | +#include <sys/types.h> | |
27 | +#include <stdint.h> | |
28 | + | |
29 | +#include "trbase.h" | |
30 | +#include "trevent.h" | |
31 | + | |
32 | +#include "tr/comm_manager.h" | |
33 | +#include "tr/comm_end_point.h" | |
34 | +#include "tr/io_handler.h" | |
35 | +#include "tr/protocol_handler.h" | |
36 | +#include "tr/protocol.h" | |
37 | +#include "tr/proto_message.h" | |
38 | +#include "tr/interface/comm_manager.h" | |
39 | + | |
40 | +TR_CLASS(TR_SimpleClient) { | |
41 | + TR_EXTENDS(TR_EventHandler); | |
42 | + | |
43 | + TR_CommEndPoint endpoint; | |
44 | + TR_CommManager comm_manager; | |
45 | + TR_ProtoMessage request; | |
46 | + TR_ProtoMessage response; | |
47 | + TR_IoHandler io_handler; | |
48 | + TR_ProtocolHandler protocol_handler; | |
49 | + TR_EventDispatcher dispatcher; | |
50 | + int send_issued; | |
51 | + TR_Timer timer; | |
52 | +}; | |
53 | +TR_INSTANCE_INIT(TR_SimpleClient); | |
54 | +TR_CLASSVARS_DECL(TR_SimpleClient) { | |
55 | + TR_CV_EXTENDS(TR_EventHandler); | |
56 | +}; | |
57 | + | |
58 | +TR_ProtoMessage TR_simpleClientIssue( | |
59 | + TR_SimpleClient, TR_ProtoMessage, unsigned long); | |
60 | + | |
61 | +#define TR_simpleClientClassCleanup() \ | |
62 | + TR_eventHandlerClassCleanup(TR_ProtocolHandler); \ | |
63 | + TR_eventHandlerClassCleanup(TR_IoHandler); \ | |
64 | + TR_eventHandlerClassCleanup(TR_CommManagerEpoll); \ | |
65 | + TR_eventHandlerClassCleanup(TR_SimpleClient) | |
66 | + | |
67 | +#endif // __TR_SIMPLE_CLIENT_H__ | |
68 | + | |
69 | +// vim: set ts=4 sw=4: | |
70 | + | ... | ... |
... | ... | @@ -17,6 +17,7 @@ |
17 | 17 | #include "tr/protocol/message_raw.h" |
18 | 18 | #include "tr/protocol_handler.h" |
19 | 19 | #include "tr/server.h" |
20 | +#include "tr/simple_client.h" | |
20 | 21 | #include "tr/interface/comm_end_point.h" |
21 | 22 | #include "tr/interface/comm_manager.h" |
22 | 23 | #include "tr/interface/protocol.h" | ... | ... |
... | ... | @@ -40,9 +40,14 @@ commManagerCtor(void * _this, va_list * params) |
40 | 40 | |
41 | 41 | TR_PARENTCALL(TR_CommManager, _this, TR_Class, ctor, params); |
42 | 42 | |
43 | - this->accept = TR_new(TR_Hash); | |
44 | - this->write = TR_new(TR_Hash); | |
45 | - this->read = TR_new(TR_Hash); | |
43 | + this->accept = TR_new(TR_Hash); | |
44 | + this->write = TR_new(TR_Hash); | |
45 | + this->read = TR_new(TR_Hash); | |
46 | + | |
47 | + this->accept->cleanup_no_free = TRUE; | |
48 | + this->write->cleanup_no_free = TRUE; | |
49 | + this->read->cleanup_no_free = TRUE; | |
50 | + | |
46 | 51 | this->n_endpoints = sysconf(_SC_OPEN_MAX); |
47 | 52 | this->endpoints = TR_calloc(sizeof(TR_CommEndPoint), this->n_endpoints); |
48 | 53 | |
... | ... | @@ -56,7 +61,7 @@ commManagerDtor(void * _this) |
56 | 61 | TR_CommManager this = _this; |
57 | 62 | nfds_t i; |
58 | 63 | |
59 | - for (i = 0; i < this->n_endpoints; i++) { | |
64 | + for (i=0; i<this->n_endpoints; i++) { | |
60 | 65 | TR_delete(this->endpoints[i]); |
61 | 66 | } |
62 | 67 | TR_MEM_FREE(this->endpoints); |
... | ... | @@ -85,7 +90,7 @@ TR_commManagerAddEndpointEvt(TR_CommManager this, TR_Event event) |
85 | 90 | return TR_EVENT_DONE; |
86 | 91 | } |
87 | 92 | |
88 | -TR_EventDone TR_commManagerSelect(void *, TR_Event, int); | |
93 | +TR_EventDone TR_commManagerSelect(void *, TR_Event); | |
89 | 94 | TR_EventDone TR_commManagerPollWrite(void *, TR_Event); |
90 | 95 | TR_EventDone TR_commManagerPollRead(void *, TR_Event); |
91 | 96 | TR_EventDone TR_commManagerDisableRead(void *, TR_Event); | ... | ... |
... | ... | @@ -83,7 +83,7 @@ TR_commManagerEpollAddEndpoint(void * _this, TR_CommEndPoint endpoint) |
83 | 83 | |
84 | 84 | static |
85 | 85 | void |
86 | -TR_commManagerEpollSelect(void * _this, TR_Event event, int timeout) | |
86 | +TR_commManagerEpollSelect(void * _this, TR_Event event, unsigned long timeout) | |
87 | 87 | { |
88 | 88 | TR_CommManagerEpoll this = _this; |
89 | 89 | TR_CommManager cmgr = _this; | ... | ... |
... | ... | @@ -61,6 +61,7 @@ commManagerPollDtor(void * _this) |
61 | 61 | TR_CommManagerPoll this = _this; |
62 | 62 | |
63 | 63 | TR_MEM_FREE(this->fds); |
64 | + TR_PARENTCALL(TR_CommManagerPoll, _this, TR_Class, dtor); | |
64 | 65 | } |
65 | 66 | |
66 | 67 | static |
... | ... | @@ -75,7 +76,7 @@ TR_commManagerPollAddEndpoint(void * _this, TR_CommEndPoint endpoint) |
75 | 76 | |
76 | 77 | static |
77 | 78 | void |
78 | -TR_commManagerPollSelect(void * _this, TR_Event event, int timeout) | |
79 | +TR_commManagerPollSelect(void * _this, TR_Event event, unsigned long timeout) | |
79 | 80 | { |
80 | 81 | TR_CommManagerPoll this = _this; |
81 | 82 | TR_CommManager cmgr = _this; | ... | ... |
... | ... | @@ -86,19 +86,19 @@ commManagerIssueReadEvents(const void * endpoint, const void * comm_manager) |
86 | 86 | TR_EventDone |
87 | 87 | TR_commManagerSelect(void * _this, TR_Event event) |
88 | 88 | { |
89 | - TR_CommManager this = _this; | |
90 | - int timeout; // milliseconds | |
91 | - int * timeoutptr = event->data; | |
92 | - TR_EventDispatcher dispatcher = (TR_EventDispatcher)event->subject; | |
89 | + TR_CommManager this = _this; | |
90 | + TR_Timer timer = (TR_Timer)event->data; | |
91 | + TR_EventDispatcher dispatcher = (TR_EventDispatcher)event->subject; | |
92 | + unsigned long timeout; // milliseconds | |
93 | 93 | |
94 | 94 | if (! (TR_hashEmpty(this->read) |
95 | 95 | && TR_hashEmpty(this->write) |
96 | 96 | && TR_hashEmpty(this->accept))) { |
97 | 97 | timeout = 0; |
98 | - } else if (NULL == timeoutptr) { | |
98 | + } else if (NULL == timer) { | |
99 | 99 | timeout = TR_eventDispatcherGetDataWaitTime(dispatcher); |
100 | 100 | } else { |
101 | - timeout = *timeoutptr; | |
101 | + timeout = TR_timerGet(timer, NULL); | |
102 | 102 | } |
103 | 103 | |
104 | 104 | TR_CALL(_this, TR_CommManager, select, event, timeout); | ... | ... |
... | ... | @@ -48,7 +48,7 @@ serverCtor(void * _this, va_list * params) |
48 | 48 | #else |
49 | 49 | this->comm_manager = (TR_CommManager)TR_new(TR_CommManagerPoll); |
50 | 50 | #endif |
51 | - this->dispatcher = TR_new(TR_EventDispatcher, TR_EVD_SERVER, NULL, 100); | |
51 | + this->dispatcher = TR_new(TR_EventDispatcher, TR_EVD_SERVER, NULL); | |
52 | 52 | this->connector = TR_new(TR_Connector); |
53 | 53 | this->io_handler = TR_new(TR_IoHandler); |
54 | 54 | this->protocol_handler = TR_new(TR_ProtocolHandler); | ... | ... |
... | ... | @@ -26,9 +26,9 @@ |
26 | 26 | #include "tr/server.h" |
27 | 27 | |
28 | 28 | void |
29 | -TR_serverStart(TR_Server this, int heartbeat) | |
29 | +TR_serverStart(TR_Server this, unsigned long heartbeat) | |
30 | 30 | { |
31 | - TR_eventDispatcherSetHeartbeat(this->dispatcher, 1000); | |
31 | + TR_eventDispatcherSetHeartbeat(this->dispatcher, heartbeat); | |
32 | 32 | TR_eventDispatcherStart(this->dispatcher); |
33 | 33 | } |
34 | 34 | ... | ... |
src/simple_client.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 | +#include <stdint.h> | |
27 | + | |
28 | +#include "trbase.h" | |
29 | +#include "trio.h" | |
30 | +#include "trevent.h" | |
31 | + | |
32 | +#include "tr/simple_client.h" | |
33 | +#include "tr/comm_end_point.h" | |
34 | +#include "tr/comm_manager.h" | |
35 | +#include "tr/comm_manager_poll.h" | |
36 | +#include "tr/comm_manager_epoll.h" | |
37 | +#include "tr/connector.h" | |
38 | +#include "tr/io_handler.h" | |
39 | +#include "tr/protocol_handler.h" | |
40 | + | |
41 | + | |
42 | +static | |
43 | +int | |
44 | +simpleClientCtor(void * _this, va_list * params) | |
45 | +{ | |
46 | + TR_SimpleClient this = _this; | |
47 | + | |
48 | + this->endpoint = va_arg(*params, TR_CommEndPoint); | |
49 | + | |
50 | +#if 1 | |
51 | + this->comm_manager = (TR_CommManager)TR_new(TR_CommManagerEpoll); | |
52 | +#else | |
53 | + this->comm_manager = (TR_CommManager)TR_new(TR_CommManagerPoll); | |
54 | +#endif | |
55 | + this->dispatcher = TR_new(TR_EventDispatcher, TR_EVD_CLIENT, NULL); | |
56 | + this->io_handler = TR_new(TR_IoHandler); | |
57 | + this->protocol_handler = TR_new(TR_ProtocolHandler); | |
58 | + this->timer = TR_new(TR_Timer, TR_TBASE_MIL, 1000); | |
59 | + | |
60 | + TR_eventDispatcherRegisterHandler( | |
61 | + this->dispatcher, | |
62 | + (TR_EventHandler)this->comm_manager); | |
63 | + TR_eventDispatcherRegisterHandler( | |
64 | + this->dispatcher, | |
65 | + (TR_EventHandler)this->io_handler); | |
66 | + TR_eventDispatcherRegisterHandler( | |
67 | + this->dispatcher, | |
68 | + (TR_EventHandler)this->protocol_handler); | |
69 | + TR_eventDispatcherRegisterHandler( | |
70 | + this->dispatcher, | |
71 | + (TR_EventHandler)this); | |
72 | + | |
73 | + TR_commManagerAddEndpoint(this->comm_manager, this->endpoint); | |
74 | + | |
75 | + return 0; | |
76 | +} | |
77 | + | |
78 | +static | |
79 | +void | |
80 | +simpleClientDtor(void * _this) | |
81 | +{ | |
82 | + TR_SimpleClient this = _this; | |
83 | + | |
84 | + TR_delete(this->protocol_handler); | |
85 | + TR_delete(this->io_handler); | |
86 | + TR_delete(this->comm_manager); | |
87 | + TR_delete(this->dispatcher); | |
88 | + TR_delete(this->timer); | |
89 | +} | |
90 | + | |
91 | +static | |
92 | +TR_EventDone | |
93 | +simpleClientUserAction(void * _this, TR_Event event) | |
94 | +{ | |
95 | + TR_SimpleClient this = _this; | |
96 | + | |
97 | + if (this->send_issued) { | |
98 | + unsigned long missed; | |
99 | + | |
100 | + TR_timerGet(this->timer, &missed); | |
101 | + | |
102 | + if (this->response || missed) { | |
103 | + TR_eventDispatcherStop((TR_EventDispatcher)event->subject); | |
104 | + } else { | |
105 | + TR_eventHandlerIssueEvent( | |
106 | + (TR_EventHandler)_this, | |
107 | + TR_eventSubjectEmit( | |
108 | + event->subject, | |
109 | + TR_DISPATCHER_EVENT_DATA_WAIT, | |
110 | + this->timer)); | |
111 | + } | |
112 | + } else { | |
113 | + TR_eventHandlerIssueEvent( | |
114 | + (TR_EventHandler)_this, | |
115 | + TR_eventSubjectEmit( | |
116 | + (TR_EventSubject)this->endpoint, | |
117 | + TR_CEP_EVENT_MSG_READY, | |
118 | + this->request)); | |
119 | + this->send_issued = TRUE; | |
120 | + } | |
121 | + | |
122 | + return TR_EVENT_DONE; | |
123 | +} | |
124 | + | |
125 | +static | |
126 | +TR_EventDone | |
127 | +simpleClientHandleData(void * _this, TR_Event event) | |
128 | +{ | |
129 | + ((TR_SimpleClient)_this)->response = event->data; | |
130 | + | |
131 | + return TR_EVENT_DONE; | |
132 | +} | |
133 | + | |
134 | +static | |
135 | +void | |
136 | +simpleClientCvInit(TR_class_ptr cls) | |
137 | +{ | |
138 | + TR_EVENT_HANDLER_SET_METHOD( | |
139 | + cls, | |
140 | + TR_EventDispatcher, | |
141 | + TR_DISPATCHER_EVENT_USER_WAIT, | |
142 | + simpleClientUserAction); | |
143 | + TR_EVENT_HANDLER_SET_METHOD( | |
144 | + cls, | |
145 | + TR_CommEndPoint, | |
146 | + TR_CEP_EVENT_NEW_MSG, | |
147 | + simpleClientHandleData); | |
148 | +} | |
149 | + | |
150 | +TR_INSTANCE(TR_Hash, simpleClientEventMethods); | |
151 | +TR_INIT_IFACE(TR_Class, simpleClientCtor, simpleClientDtor, NULL); | |
152 | +TR_CREATE_CLASS( | |
153 | + TR_SimpleClient, | |
154 | + TR_EventHandler, | |
155 | + simpleClientCvInit, | |
156 | + TR_IF(TR_Class)) = { | |
157 | + { &(_simpleClientEventMethods.data) } | |
158 | +}; | |
159 | + | |
160 | +// vim: set ts=4 sw=4: | ... | ... |
src/simple_client_issue.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/simple_client.h" | |
27 | +#include "tr/proto_message.h" | |
28 | + | |
29 | +TR_ProtoMessage | |
30 | +TR_simpleClientIssue( | |
31 | + TR_SimpleClient this, | |
32 | + TR_ProtoMessage request, | |
33 | + unsigned long timeout) | |
34 | +{ | |
35 | + TR_ProtoMessage retval; | |
36 | + | |
37 | + TR_timerSetMil(this->timer, timeout); | |
38 | + this->request = request; | |
39 | + this->response = NULL; | |
40 | + this->send_issued = FALSE; | |
41 | + | |
42 | + TR_eventDispatcherStart(((TR_EventHandler)this)->dispatcher[0]); | |
43 | + | |
44 | + retval = this->response; | |
45 | + this->request = this->response = NULL; | |
46 | + | |
47 | + return retval; | |
48 | +} | |
49 | + | |
50 | +// vim: set ts=4 sw=4: | ... | ... |
... | ... | @@ -5,3 +5,4 @@ LIBS="-lcrypto -lssl -lrt -luuid" |
5 | 5 | gcc ${CFLAGS} -c -o test_handler.o test_handler.c |
6 | 6 | gcc ${CFLAGS} -I/usr/local/include -L/usr/local/lib ${LIBS} -o testserver testserver.c test_handler.o ${TRLIBS} |
7 | 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 testclient testclient.c ${TRLIBS} | ... | ... |
testers/testclient.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_TcpSocket socket; | |
19 | + TR_Connection connection; | |
20 | + TR_SimpleClient client; | |
21 | + TR_Protocol protocol; | |
22 | + TR_ProtoMessageRaw message; | |
23 | + TR_RemoteData data; | |
24 | + | |
25 | + TR_logger = TR_INSTANCE_CAST(TR_Logger, mylogger2); | |
26 | + socket = TR_new(TR_TcpSocket, TR_logger, "192.168.2.13", 5678, 0); | |
27 | + protocol = TR_new(TR_ProtocolRaw); | |
28 | + connection = TR_new(TR_Connection, socket, protocol); | |
29 | + | |
30 | + TR_socketConnect((TR_Socket)socket); | |
31 | + TR_socketNonblock((TR_Socket)socket); | |
32 | + | |
33 | + client = TR_new(TR_SimpleClient, connection); | |
34 | + | |
35 | + message = (TR_ProtoMessageRaw)TR_protoCreateRequest(protocol, (TR_Socket)socket); | |
36 | + data = TR_new(TR_RemoteData, "test", sizeof("test"), (TR_Socket)socket); | |
37 | + message->data = data; | |
38 | + message = (TR_ProtoMessageRaw)TR_simpleClientIssue( | |
39 | + client, | |
40 | + (TR_ProtoMessage)message, | |
41 | + 10000000); | |
42 | + | |
43 | + printf("%s\n", ((TR_SizedData)message->data)->data); | |
44 | + TR_delete(message->data); | |
45 | + TR_delete(message); | |
46 | + | |
47 | + puts("cleanup..."); | |
48 | + | |
49 | + TR_delete(client); | |
50 | + TR_delete(protocol); | |
51 | + | |
52 | + TR_simpleClientClassCleanup(); | |
53 | + | |
54 | + TR_cleanup(); | |
55 | + | |
56 | + return 0; | |
57 | +} | |
58 | + | |
59 | +// vim: set ts=4 sw=4: | ... | ... |
Please
register
or
login
to post a comment