diff options
Diffstat (limited to 'src/transport')
-rw-r--r-- | src/transport/test_communicator_unix.c | 5 | ||||
-rw-r--r-- | src/transport/transport-testing2.c | 192 | ||||
-rw-r--r-- | src/transport/transport-testing2.h | 3 |
3 files changed, 168 insertions, 32 deletions
diff --git a/src/transport/test_communicator_unix.c b/src/transport/test_communicator_unix.c index ef7ed32c0..fd189659c 100644 --- a/src/transport/test_communicator_unix.c +++ b/src/transport/test_communicator_unix.c | |||
@@ -63,6 +63,11 @@ run (void *cls) | |||
63 | "test_communicator_1.conf", | 63 | "test_communicator_1.conf", |
64 | &communicator_available, | 64 | &communicator_available, |
65 | NULL); /* cls */ | 65 | NULL); /* cls */ |
66 | GNUNET_TRANSPORT_TESTING_transport_communicator_service_start ( | ||
67 | "transport", | ||
68 | "test_communicator_2.conf", | ||
69 | &communicator_available, | ||
70 | NULL); /* cls */ | ||
66 | } | 71 | } |
67 | 72 | ||
68 | int | 73 | int |
diff --git a/src/transport/transport-testing2.c b/src/transport/transport-testing2.c index df48e7782..51791e981 100644 --- a/src/transport/transport-testing2.c +++ b/src/transport/transport-testing2.c | |||
@@ -37,6 +37,57 @@ | |||
37 | #define LOG(kind,...) GNUNET_log_from (kind, "transport-testing2", __VA_ARGS__) | 37 | #define LOG(kind,...) GNUNET_log_from (kind, "transport-testing2", __VA_ARGS__) |
38 | 38 | ||
39 | 39 | ||
40 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle | ||
41 | { | ||
42 | /** | ||
43 | * @brief Handle to the configuration | ||
44 | */ | ||
45 | struct GNUNET_CONFIGURATION_Handle *cfg; | ||
46 | |||
47 | /** | ||
48 | * @brief File name of configuration file | ||
49 | */ | ||
50 | char *cfg_filename; | ||
51 | |||
52 | /** | ||
53 | * @brief Handle to the transport service | ||
54 | */ | ||
55 | struct GNUNET_SERVICE_Handle *tsh; | ||
56 | |||
57 | /** | ||
58 | * @brief Task that will be run on shutdown to stop and clean transport | ||
59 | * service | ||
60 | */ | ||
61 | struct GNUNET_SCHEDULER_Task *ts_shutdown_task; | ||
62 | |||
63 | /** | ||
64 | * @brief Handle to the client | ||
65 | */ | ||
66 | struct GNUNET_SERVICE_Client *client; | ||
67 | |||
68 | /** | ||
69 | * @brief Process of the communicator | ||
70 | */ | ||
71 | struct GNUNET_OS_Process *c_proc; | ||
72 | |||
73 | /** | ||
74 | * @brief Task that will be run on shutdown to stop and clean communicator | ||
75 | */ | ||
76 | struct GNUNET_SCHEDULER_Task *c_shutdown_task; | ||
77 | |||
78 | /* Callbacks + Closures */ | ||
79 | /** | ||
80 | * @brief Callback called when a new communicator connects | ||
81 | */ | ||
82 | GNUNET_TRANSPORT_TESTING_CommunicatorAvailableCallback communicator_available; | ||
83 | |||
84 | /** | ||
85 | * @brief Closure to the callback | ||
86 | */ | ||
87 | void *communicator_available_cls; | ||
88 | }; | ||
89 | |||
90 | |||
40 | /** | 91 | /** |
41 | * @brief Check whether incoming msg indicating available communicator is | 92 | * @brief Check whether incoming msg indicating available communicator is |
42 | * correct | 93 | * correct |
@@ -50,9 +101,13 @@ static int | |||
50 | check_communicator_available (void *cls, | 101 | check_communicator_available (void *cls, |
51 | const struct GNUNET_TRANSPORT_CommunicatorAvailableMessage *msg) | 102 | const struct GNUNET_TRANSPORT_CommunicatorAvailableMessage *msg) |
52 | { | 103 | { |
53 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 104 | uint16_t size; |
54 | "check_communicator_available()\n"); | 105 | |
55 | return GNUNET_YES; | 106 | size = ntohs (msg->header.size) - sizeof (*msg); |
107 | if (0 == size) | ||
108 | return GNUNET_OK; /* receive-only communicator */ | ||
109 | GNUNET_MQ_check_zero_termination (msg); | ||
110 | return GNUNET_OK; | ||
56 | } | 111 | } |
57 | 112 | ||
58 | 113 | ||
@@ -66,16 +121,19 @@ static void | |||
66 | handle_communicator_available (void *cls, | 121 | handle_communicator_available (void *cls, |
67 | const struct GNUNET_TRANSPORT_CommunicatorAvailableMessage *msg) | 122 | const struct GNUNET_TRANSPORT_CommunicatorAvailableMessage *msg) |
68 | { | 123 | { |
69 | GNUNET_TRANSPORT_TESTING_CommunicatorAvailableCallback communicator_available = cls; | 124 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = cls; |
70 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 125 | uint16_t size; |
71 | "handle_communicator_available()\n"); | 126 | |
72 | if (NULL != communicator_available) | 127 | size = ntohs (msg->header.size) - sizeof (*msg); |
128 | if (0 == size) | ||
129 | return; /* receive-only communicator */ | ||
130 | if (NULL != tc_h->communicator_available) | ||
73 | { | 131 | { |
74 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 132 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
75 | "calling communicator_available()\n"); | 133 | "calling communicator_available()\n"); |
76 | communicator_available (NULL, msg); | 134 | tc_h->communicator_available (tc_h->communicator_available_cls, msg); |
77 | } | 135 | } |
78 | //GNUNET_SERVICE_client_continue (client); | 136 | GNUNET_SERVICE_client_continue (tc_h->client); |
79 | } | 137 | } |
80 | 138 | ||
81 | 139 | ||
@@ -94,6 +152,49 @@ shutdown_service (void *cls) | |||
94 | 152 | ||
95 | 153 | ||
96 | /** | 154 | /** |
155 | * @brief Callback called when new Client (Communicator) connects | ||
156 | * | ||
157 | * @param cls Closure - TransporCommmunicator Handle | ||
158 | * @param client Client | ||
159 | * @param mq Messagequeue | ||
160 | * | ||
161 | * @return TransportCommunicator Handle | ||
162 | */ | ||
163 | static void * | ||
164 | connect_cb (void *cls, | ||
165 | struct GNUNET_SERVICE_Client *client, | ||
166 | struct GNUNET_MQ_Handle *mq) | ||
167 | { | ||
168 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = cls; | ||
169 | |||
170 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
171 | "Client connected.\n"); | ||
172 | tc_h->client = client; | ||
173 | return tc_h; | ||
174 | } | ||
175 | |||
176 | |||
177 | /** | ||
178 | * @brief Callback called when Client disconnects | ||
179 | * | ||
180 | * @param cls Closure - TransportCommunicator Handle | ||
181 | * @param client Client | ||
182 | * @param internal_cls TransporCommmunicator Handle | ||
183 | */ | ||
184 | static void | ||
185 | disconnect_cb (void *cls, | ||
186 | struct GNUNET_SERVICE_Client *client, | ||
187 | void *internal_cls) | ||
188 | { | ||
189 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = cls; | ||
190 | |||
191 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
192 | "Client disconnected.\n"); | ||
193 | tc_h->client = NULL; | ||
194 | } | ||
195 | |||
196 | |||
197 | /** | ||
97 | * @brief Start the communicator part of the transport service | 198 | * @brief Start the communicator part of the transport service |
98 | * | 199 | * |
99 | * @param communicator_available Callback to be called when a new communicator | 200 | * @param communicator_available Callback to be called when a new communicator |
@@ -101,14 +202,13 @@ shutdown_service (void *cls) | |||
101 | * @param cfg Configuration | 202 | * @param cfg Configuration |
102 | */ | 203 | */ |
103 | static void | 204 | static void |
104 | transport_communicator_start (GNUNET_TRANSPORT_TESTING_CommunicatorAvailableCallback communicator_available, | 205 | transport_communicator_start (struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h) |
105 | struct GNUNET_CONFIGURATION_Handle *cfg) | ||
106 | { | 206 | { |
107 | struct GNUNET_MQ_MessageHandler mh[] = { | 207 | struct GNUNET_MQ_MessageHandler mh[] = { |
108 | GNUNET_MQ_hd_var_size (communicator_available, | 208 | GNUNET_MQ_hd_var_size (communicator_available, |
109 | GNUNET_MESSAGE_TYPE_TRANSPORT_NEW_COMMUNICATOR, | 209 | GNUNET_MESSAGE_TYPE_TRANSPORT_NEW_COMMUNICATOR, |
110 | struct GNUNET_TRANSPORT_CommunicatorAvailableMessage, | 210 | struct GNUNET_TRANSPORT_CommunicatorAvailableMessage, |
111 | &communicator_available), | 211 | &tc_h), |
112 | //GNUNET_MQ_hd_var_size (communicator_backchannel, | 212 | //GNUNET_MQ_hd_var_size (communicator_backchannel, |
113 | // GNUNET_MESSAGE_TYPE_TRANSPORT_COMMUNICATOR_BACKCHANNEL, | 213 | // GNUNET_MESSAGE_TYPE_TRANSPORT_COMMUNICATOR_BACKCHANNEL, |
114 | // struct GNUNET_TRANSPORT_CommunicatorBackchannel, | 214 | // struct GNUNET_TRANSPORT_CommunicatorBackchannel, |
@@ -149,10 +249,10 @@ transport_communicator_start (GNUNET_TRANSPORT_TESTING_CommunicatorAvailableCall | |||
149 | struct GNUNET_SERVICE_Handle *h; | 249 | struct GNUNET_SERVICE_Handle *h; |
150 | 250 | ||
151 | h = GNUNET_SERVICE_start ("transport", | 251 | h = GNUNET_SERVICE_start ("transport", |
152 | cfg, | 252 | tc_h->cfg, |
153 | NULL, | 253 | &connect_cb, |
154 | NULL, | 254 | &disconnect_cb, |
155 | NULL, | 255 | tc_h, |
156 | mh); | 256 | mh); |
157 | if (NULL == h) | 257 | if (NULL == h) |
158 | LOG (GNUNET_ERROR_TYPE_ERROR, | 258 | LOG (GNUNET_ERROR_TYPE_ERROR, |
@@ -161,48 +261,70 @@ transport_communicator_start (GNUNET_TRANSPORT_TESTING_CommunicatorAvailableCall | |||
161 | { | 261 | { |
162 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 262 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
163 | "Started service\n"); | 263 | "Started service\n"); |
164 | GNUNET_SCHEDULER_add_shutdown (&shutdown_service, h); | 264 | /* TODO */ GNUNET_SCHEDULER_add_shutdown (&shutdown_service, h); |
165 | } | 265 | } |
166 | } | 266 | } |
167 | 267 | ||
168 | 268 | ||
169 | /** | 269 | /** |
270 | * @brief Task run at shutdown to kill communicator and clean up | ||
271 | * | ||
272 | * @param cls Closure - Process of communicator | ||
273 | */ | ||
274 | static void | ||
275 | shutdown_communicator (void *cls) | ||
276 | { | ||
277 | struct GNUNET_OS_Process *proc = cls; | ||
278 | |||
279 | if (GNUNET_OK != GNUNET_OS_process_kill (proc, | ||
280 | SIGTERM)) | ||
281 | { | ||
282 | LOG (GNUNET_ERROR_TYPE_WARNING, | ||
283 | "Error shutting down communicator with SIGERM, trying SIGKILL\n"); | ||
284 | if (GNUNET_OK != GNUNET_OS_process_kill (proc, | ||
285 | SIGKILL)) | ||
286 | { | ||
287 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
288 | "Error shutting down communicator with SIGERM and SIGKILL\n"); | ||
289 | } | ||
290 | } | ||
291 | GNUNET_OS_process_destroy (proc); | ||
292 | } | ||
293 | |||
294 | |||
295 | /** | ||
170 | * @brief Start the communicator | 296 | * @brief Start the communicator |
171 | * | 297 | * |
172 | * @param cfgname Name of the communicator | 298 | * @param cfgname Name of the communicator |
173 | */ | 299 | */ |
174 | static void | 300 | static void |
175 | communicator_start (const char *cfgname) | 301 | communicator_start (struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h) |
176 | { | 302 | { |
177 | char *binary; | 303 | char *binary; |
178 | struct GNUNET_CONFIGURATION_Handle *cfg; | ||
179 | struct GNUNET_OS_Process *proc; | ||
180 | 304 | ||
181 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 305 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
182 | "communicator_start\n"); | 306 | "communicator_start\n"); |
183 | binary = GNUNET_OS_get_libexec_binary_path ("gnunet-communicator-unix"); | 307 | binary = GNUNET_OS_get_libexec_binary_path ("gnunet-communicator-unix"); |
184 | cfg = GNUNET_CONFIGURATION_create (); | 308 | tc_h->c_proc = |
185 | proc = | ||
186 | GNUNET_OS_start_process (GNUNET_YES, | 309 | GNUNET_OS_start_process (GNUNET_YES, |
187 | GNUNET_OS_INHERIT_STD_OUT_AND_ERR, | 310 | GNUNET_OS_INHERIT_STD_OUT_AND_ERR, |
188 | NULL, NULL, NULL, | 311 | NULL, NULL, NULL, |
189 | binary, | 312 | binary, |
190 | "./gnunet-communicator-unix", | 313 | "./gnunet-communicator-unix", |
191 | "-c", | 314 | "-c", |
192 | cfgname, | 315 | tc_h->cfg_filename, |
193 | NULL); | 316 | NULL); |
194 | if (NULL == proc) | 317 | if (NULL == tc_h->c_proc) |
195 | { | 318 | { |
196 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 319 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, |
197 | "Failed to start communicator!"); | 320 | "Failed to start communicator!"); |
198 | return; | 321 | return; |
199 | } | 322 | } |
200 | GNUNET_assert (GNUNET_OK == | ||
201 | GNUNET_CONFIGURATION_load (cfg, | ||
202 | cfgname)); | ||
203 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 323 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
204 | "started communicator\n"); | 324 | "started communicator\n"); |
205 | GNUNET_free (binary); | 325 | GNUNET_free (binary); |
326 | /* TODO */ GNUNET_SCHEDULER_add_shutdown (&shutdown_communicator, | ||
327 | tc_h->c_proc); | ||
206 | } | 328 | } |
207 | 329 | ||
208 | 330 | ||
@@ -227,11 +349,13 @@ GNUNET_TRANSPORT_TESTING_transport_communicator_service_start | |||
227 | //GNUNET_TRANSPORT_TESTING_Callback4 cb4, | 349 | //GNUNET_TRANSPORT_TESTING_Callback4 cb4, |
228 | void *cb_cls) | 350 | void *cb_cls) |
229 | { | 351 | { |
230 | struct GNUNET_CONFIGURATION_Handle *cfg; | 352 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h; |
231 | 353 | ||
232 | cfg = GNUNET_CONFIGURATION_create (); | 354 | tc_h = GNUNET_new (struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle); |
355 | tc_h->cfg_filename = GNUNET_strdup (cfg_filename); | ||
356 | tc_h->cfg = GNUNET_CONFIGURATION_create (); | ||
233 | if ( (GNUNET_SYSERR == | 357 | if ( (GNUNET_SYSERR == |
234 | GNUNET_CONFIGURATION_load (cfg, | 358 | GNUNET_CONFIGURATION_load (tc_h->cfg, |
235 | cfg_filename)) ) | 359 | cfg_filename)) ) |
236 | { | 360 | { |
237 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 361 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, |
@@ -239,11 +363,15 @@ GNUNET_TRANSPORT_TESTING_transport_communicator_service_start | |||
239 | cfg_filename); | 363 | cfg_filename); |
240 | return NULL; | 364 | return NULL; |
241 | } | 365 | } |
366 | tc_h->communicator_available = communicator_available; | ||
367 | tc_h->communicator_available_cls = cb_cls; | ||
368 | |||
242 | /* Start communicator part of service */ | 369 | /* Start communicator part of service */ |
243 | transport_communicator_start (communicator_available, cfg); | 370 | transport_communicator_start (tc_h); |
244 | 371 | ||
245 | /* Schedule start communicator */ | 372 | /* Schedule start communicator */ |
246 | communicator_start ("test_communicator_1.conf"); | 373 | communicator_start (tc_h); |
374 | return tc_h; | ||
247 | } | 375 | } |
248 | 376 | ||
249 | //void | 377 | //void |
diff --git a/src/transport/transport-testing2.h b/src/transport/transport-testing2.h index b4d4fd3be..c5adda4eb 100644 --- a/src/transport/transport-testing2.h +++ b/src/transport/transport-testing2.h | |||
@@ -29,6 +29,9 @@ | |||
29 | #include "gnunet_ats_transport_service.h" | 29 | #include "gnunet_ats_transport_service.h" |
30 | #include "transport.h" | 30 | #include "transport.h" |
31 | 31 | ||
32 | |||
33 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle; | ||
34 | |||
32 | /** | 35 | /** |
33 | * @brief Function signature for callbacks that are called when new communicators become available | 36 | * @brief Function signature for callbacks that are called when new communicators become available |
34 | * | 37 | * |