split up code to multiple files
[oweals/gnunet.git] / src / experimentation / gnunet-daemon-experimentation_nodes.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file experimentation/gnunet-daemon-experimentation.c
23  * @brief experimentation daemon
24  * @author Christian Grothoff
25  * @author Matthias Wachs
26  */
27 #include "platform.h"
28 #include "gnunet_getopt_lib.h"
29 #include "gnunet_util_lib.h"
30 #include "gnunet_core_service.h"
31 #include "gnunet_statistics_service.h"
32 #include "gnunet-daemon-experimentation.h"
33
34 #define EXP_RESPONSE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
35
36 static struct GNUNET_CORE_Handle *ch;
37
38 static struct GNUNET_PeerIdentity me;
39
40 static struct GNUNET_STATISTICS_Handle *stats;
41
42 /**
43  * Nodes with a pending request
44  */
45
46 struct GNUNET_CONTAINER_MultiHashMap *nodes_requested;
47
48 /**
49  * Active experimentation nodes
50  */
51 struct GNUNET_CONTAINER_MultiHashMap *nodes_active;
52
53 /**
54  * Inactive experimentation nodes
55  * To be excluded from future requests
56  */
57 struct GNUNET_CONTAINER_MultiHashMap *nodes_inactive;
58
59
60 static void update_stats (struct GNUNET_CONTAINER_MultiHashMap *m)
61 {
62         GNUNET_assert (NULL != m);
63         GNUNET_assert (NULL != stats);
64
65         if (m == nodes_active)
66         {
67                         GNUNET_STATISTICS_set (stats, "# nodes active",
68                                         GNUNET_CONTAINER_multihashmap_size(m), GNUNET_NO);
69         }
70         else if (m == nodes_inactive)
71         {
72                         GNUNET_STATISTICS_set (stats, "# nodes inactive",
73                                         GNUNET_CONTAINER_multihashmap_size(m), GNUNET_NO);
74         }
75         else if (m == nodes_requested)
76         {
77                         GNUNET_STATISTICS_set (stats, "# nodes requested",
78                                         GNUNET_CONTAINER_multihashmap_size(m), GNUNET_NO);
79         }
80         else
81                 GNUNET_break (0);
82
83 }
84
85 static int
86 cleanup_nodes (void *cls,
87                                                          const struct GNUNET_HashCode * key,
88                                                          void *value)
89 {
90         struct Node *n;
91         struct GNUNET_CONTAINER_MultiHashMap *cur = cls;
92
93         n = value;
94         if (GNUNET_SCHEDULER_NO_TASK != n->timeout_task)
95         {
96                 GNUNET_SCHEDULER_cancel (n->timeout_task);
97                 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
98         }
99         if (NULL != n->cth)
100         {
101                 GNUNET_CORE_notify_transmit_ready_cancel (n->cth);
102                 n->cth = NULL;
103         }
104
105
106         GNUNET_CONTAINER_multihashmap_remove (cur, key, value);
107         GNUNET_free (value);
108         return GNUNET_OK;
109 }
110
111
112 static int is_me (const struct GNUNET_PeerIdentity *id)
113 {
114         if (0 == memcmp (&me, id, sizeof (me)))
115                 return GNUNET_YES;
116         else
117                 return GNUNET_NO;
118 }
119
120 static void
121 core_startup_handler (void *cls,
122                                                                                         struct GNUNET_CORE_Handle * server,
123                       const struct GNUNET_PeerIdentity *my_identity)
124 {
125         me = *my_identity;
126 }
127
128 static void
129 remove_request (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
130 {
131         struct Node *n = cls;
132
133         GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Removing request for peer %s due to timeout\n"),
134                         GNUNET_i2s (&n->id));
135
136         if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (nodes_requested, &n->id.hashPubKey))
137         {
138                         GNUNET_CONTAINER_multihashmap_remove (nodes_requested, &n->id.hashPubKey, n);
139                         update_stats (nodes_requested);
140                         GNUNET_CONTAINER_multihashmap_put (nodes_inactive, &n->id.hashPubKey, n,
141                                         GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
142                         update_stats (nodes_inactive);
143         }
144
145         n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
146         if (NULL != n->cth)
147         {
148                 GNUNET_CORE_notify_transmit_ready_cancel (n->cth);
149                 n->cth = NULL;
150         }
151 }
152
153 size_t send_request_cb (void *cls, size_t bufsize, void *buf)
154 {
155         struct Node *n = cls;
156         struct Experimentation_Request msg;
157         size_t size = sizeof (msg);
158
159         n->cth = NULL;
160   if (buf == NULL)
161   {
162     /* client disconnected */
163     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client disconnected\n");
164     if (GNUNET_SCHEDULER_NO_TASK != n->timeout_task)
165                 GNUNET_SCHEDULER_cancel (n->timeout_task);
166     GNUNET_SCHEDULER_add_now (&remove_request, n);
167     return 0;
168   }
169   GNUNET_assert (bufsize >= size);
170
171         msg.msg.size = htons (size);
172         msg.msg.type = htons (GNUNET_MESSAGE_TYPE_EXPERIMENTATION_REQUEST);
173         memcpy (buf, &msg, size);
174
175         GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Sending request to peer %s\n"),
176                         GNUNET_i2s (&n->id));
177         return size;
178 }
179
180 static void send_request (const struct GNUNET_PeerIdentity *peer)
181 {
182         struct Node *n;
183         size_t size;
184
185         size = sizeof (struct Experimentation_Request);
186         n = GNUNET_malloc (sizeof (struct Node));
187         n->id = *peer;
188         n->timeout_task = GNUNET_SCHEDULER_add_delayed (EXP_RESPONSE_TIMEOUT, &remove_request, n);
189         n->cth = GNUNET_CORE_notify_transmit_ready(ch, GNUNET_NO, 0,
190                                                                 GNUNET_TIME_relative_get_forever_(),
191                                                                 peer, size, send_request_cb, n);
192
193         GNUNET_assert (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (nodes_requested,
194                         &peer->hashPubKey, n, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
195
196         update_stats (nodes_requested);
197 }
198
199 size_t send_response_cb (void *cls, size_t bufsize, void *buf)
200 {
201         struct Node *n = cls;
202         struct Experimentation_Response msg;
203         size_t size = sizeof (msg);
204
205         n->cth = NULL;
206   if (buf == NULL)
207   {
208     /* client disconnected */
209     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client disconnected\n");
210     return 0;
211   }
212   GNUNET_assert (bufsize >= size);
213
214         msg.msg.size = htons (size);
215         msg.msg.type = htons (GNUNET_MESSAGE_TYPE_EXPERIMENTATION_RESPONSE);
216         memcpy (buf, &msg, size);
217
218         GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Sending response to peer %s\n"),
219                         GNUNET_i2s (&n->id));
220         return size;
221 }
222
223
224 static void handle_request (const struct GNUNET_PeerIdentity *peer)
225 {
226         struct Node *n;
227
228         if (NULL != (n = GNUNET_CONTAINER_multihashmap_get (nodes_active, &peer->hashPubKey)))
229         {
230                         GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Received %s from %s peer `%s'\n"),
231                                         "REQUEST", "active", GNUNET_i2s (peer));
232         }
233         else if (NULL != (n = GNUNET_CONTAINER_multihashmap_get (nodes_requested, &peer->hashPubKey)))
234         {
235                         GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Received %s from %s peer `%s'\n"),
236                                         "REQUEST", "requested", GNUNET_i2s (peer));
237                         GNUNET_CONTAINER_multihashmap_remove (nodes_requested, &peer->hashPubKey, n);
238                         if (GNUNET_SCHEDULER_NO_TASK != n->timeout_task)
239                         {
240                                 GNUNET_SCHEDULER_cancel (n->timeout_task);
241                                 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
242                         }
243                         if (NULL != n->cth)
244                         {
245                                 GNUNET_CORE_notify_transmit_ready_cancel (n->cth);
246                                 n->cth = NULL;
247                         }
248                         update_stats (nodes_requested);
249                   GNUNET_CONTAINER_multihashmap_put (nodes_active,
250                                         &peer->hashPubKey, n, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
251                         update_stats (nodes_active);
252                         GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Added peer `%s' active node \n"),
253                                         GNUNET_i2s (peer));
254         }
255         else if (NULL != (n = GNUNET_CONTAINER_multihashmap_get (nodes_inactive, &peer->hashPubKey)))
256         {
257                         GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Received %s from %s peer `%s'\n"),
258                                         "REQUEST", "inactive", GNUNET_i2s (peer));
259                         GNUNET_CONTAINER_multihashmap_remove (nodes_inactive, &peer->hashPubKey, n);
260                         update_stats (nodes_inactive);
261                   GNUNET_CONTAINER_multihashmap_put (nodes_active,
262                                         &peer->hashPubKey, n, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
263                         update_stats (nodes_active);
264                         GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Added peer `%s' active node \n"),
265                                         GNUNET_i2s (peer));
266         }
267         else
268         {
269                         /* Create new node */
270                         n = GNUNET_malloc (sizeof (struct Node));
271                         n->id = *peer;
272                         GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Received %s from %s peer `%s'\n"),
273                                         "REQUEST", "new", GNUNET_i2s (peer));
274                   GNUNET_CONTAINER_multihashmap_put (nodes_active,
275                                         &peer->hashPubKey, n, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
276                         update_stats (nodes_active);
277                         GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Added peer `%s' active node \n"),
278                                         GNUNET_i2s (peer));
279         }
280
281         n->cth = GNUNET_CORE_notify_transmit_ready (ch, GNUNET_NO, 0,
282                                                                 GNUNET_TIME_relative_get_forever_(),
283                                                                 peer, sizeof (struct Experimentation_Response),
284                                                                 send_response_cb, n);
285 }
286
287 static void handle_response (const struct GNUNET_PeerIdentity *peer)
288 {
289         struct Node *n;
290
291         if (NULL != (n = GNUNET_CONTAINER_multihashmap_get (nodes_active, &peer->hashPubKey)))
292         {
293                         GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Received %s from %s peer `%s'\n"),
294                                         "RESPONSE", "active", GNUNET_i2s (peer));
295         }
296         else if (NULL != (n = GNUNET_CONTAINER_multihashmap_get (nodes_requested, &peer->hashPubKey)))
297         {
298                         GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Received %s from %s peer `%s'\n"),
299                                         "RESPONSE", "requested", GNUNET_i2s (peer));
300                         GNUNET_CONTAINER_multihashmap_remove (nodes_requested, &peer->hashPubKey, n);
301                         if (GNUNET_SCHEDULER_NO_TASK != n->timeout_task)
302                         {
303                                 GNUNET_SCHEDULER_cancel (n->timeout_task);
304                                 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
305                         }
306                         if (NULL != n->cth)
307                         {
308                                 GNUNET_CORE_notify_transmit_ready_cancel (n->cth);
309                                 n->cth = NULL;
310                         }
311                         update_stats (nodes_requested);
312                   GNUNET_CONTAINER_multihashmap_put (nodes_active,
313                                         &peer->hashPubKey, n, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
314                         update_stats (nodes_active);
315                         GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Added peer `%s' active node \n"),
316                                         GNUNET_i2s (peer));
317         }
318         else if (NULL != (n = GNUNET_CONTAINER_multihashmap_get (nodes_inactive, &peer->hashPubKey)))
319         {
320                         GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Received %s from peer `%s'\n"),
321                                         "RESPONSE", "inactive", GNUNET_i2s (peer));
322                         GNUNET_CONTAINER_multihashmap_remove (nodes_inactive, &peer->hashPubKey, n);
323                         update_stats (nodes_inactive);
324                   GNUNET_CONTAINER_multihashmap_put (nodes_active,
325                                         &peer->hashPubKey, n, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
326                         update_stats (nodes_active);
327                         GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Added peer `%s' active node \n"),
328                                         GNUNET_i2s (peer));
329         }
330         else
331         {
332                         GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Received %s from %s peer `%s'\n"),
333                                         "RESPONSE", "unknown", GNUNET_i2s (peer));
334         }
335 }
336
337 /**
338  * Method called whenever a given peer connects.
339  *
340  * @param cls closure
341  * @param peer peer identity this notification is about
342  */
343 void core_connect_handler (void *cls,
344                            const struct GNUNET_PeerIdentity *peer)
345 {
346         if (GNUNET_YES == is_me(peer))
347                 return;
348
349         GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connected to peer %s\n"),
350                         GNUNET_i2s (peer));
351
352         if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (nodes_requested, &peer->hashPubKey))
353                 return; /* We already sent a request */
354
355         if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (nodes_active, &peer->hashPubKey))
356                 return; /* This peer is known as active  */
357
358         if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (nodes_inactive, &peer->hashPubKey))
359                 return; /* This peer is known as inactive  */
360
361         send_request (peer);
362
363 }
364
365
366 /**
367  * Method called whenever a given peer disconnects.
368  *
369  * @param cls closure
370  * @param peer peer identity this notification is about
371  */
372 void core_disconnect_handler (void *cls,
373                            const struct GNUNET_PeerIdentity * peer)
374 {
375         if (GNUNET_YES == is_me(peer))
376                 return;
377
378         GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Disconnected from peer %s\n"),
379                         GNUNET_i2s (peer));
380
381 }
382
383
384 static int
385 core_receive_handler (void *cls,
386                                                                                         const struct GNUNET_PeerIdentity *other,
387                                                                                         const struct GNUNET_MessageHeader *message)
388 {
389         if (ntohs (message->size) < sizeof (struct GNUNET_MessageHeader))
390         {
391                         GNUNET_break (0);
392                         return GNUNET_SYSERR;
393         }
394
395         switch (ntohs (message->type)) {
396                 case GNUNET_MESSAGE_TYPE_EXPERIMENTATION_REQUEST:
397                         handle_request (other);
398                         break;
399                 case GNUNET_MESSAGE_TYPE_EXPERIMENTATION_RESPONSE:
400                         handle_response (other);
401                         break;
402                 default:
403                         break;
404         }
405
406         return GNUNET_OK;
407 }
408
409
410
411 /**
412  * The main function for the experimentation daemon.
413  *
414  * @param argc number of arguments from the command line
415  * @param argv command line arguments
416  */
417 void
418 GNUNET_EXPERIMENTATION_nodes_start (const struct GNUNET_CONFIGURATION_Handle *cfg)
419 {
420         GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Experimentation daemon starting ...\n"));
421
422         stats = GNUNET_STATISTICS_create ("experimentation", cfg);
423         if (NULL == stats)
424         {
425                 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to create statistics!\n"));
426                 return;
427         }
428
429         /* Connecting to core service to find partners */
430         ch = GNUNET_CORE_connect (cfg, NULL,
431                                                                                                                 &core_startup_handler,
432                                                                                                                 &core_connect_handler,
433                                                                                                                 &core_disconnect_handler,
434                                                                                                                 &core_receive_handler,
435                                                                                                                 GNUNET_NO, NULL, GNUNET_NO, NULL);
436         if (NULL == ch)
437         {
438                         GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Failed to connect to CORE service!\n"));
439                         return;
440         }
441
442         nodes_requested = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
443         nodes_active = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
444         nodes_inactive = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
445 }
446
447
448 void
449 GNUNET_EXPERIMENTATION_nodes_stop ()
450 {
451   GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Experimentation daemon shutting down ...\n"));
452   if (NULL != ch)
453   {
454                 GNUNET_CORE_disconnect (ch);
455                 ch = NULL;
456   }
457
458   if (NULL != nodes_requested)
459   {
460                 GNUNET_CONTAINER_multihashmap_iterate (nodes_requested,
461                                                                                                                                                                          &cleanup_nodes,
462                                                                                                                                                                          nodes_requested);
463                 update_stats (nodes_requested);
464                 GNUNET_CONTAINER_multihashmap_destroy (nodes_requested);
465                 nodes_requested = NULL;
466   }
467
468   if (NULL != nodes_active)
469   {
470                 GNUNET_CONTAINER_multihashmap_iterate (nodes_active,
471                                                                                                                                                                          &cleanup_nodes,
472                                                                                                                                                                          nodes_active);
473                 update_stats (nodes_active);
474                 GNUNET_CONTAINER_multihashmap_destroy (nodes_active);
475                 nodes_active = NULL;
476   }
477
478   if (NULL != nodes_inactive)
479   {
480                 GNUNET_CONTAINER_multihashmap_iterate (nodes_inactive,
481                                                                                                                                                                          &cleanup_nodes,
482                                                                                                                                                                          nodes_inactive);
483                 update_stats (nodes_inactive);
484                 GNUNET_CONTAINER_multihashmap_destroy (nodes_inactive);
485                 nodes_inactive = NULL;
486   }
487
488   if (NULL != stats)
489   {
490                 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
491                 stats = NULL;
492   }
493 }
494
495 /* end of gnunet-daemon-experimentation.c */