changes``
[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_nodes.c
23  * @brief experimentation daemon: node management
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
35 /**
36  * Core handle
37  */
38 static struct GNUNET_CORE_Handle *ch;
39
40
41 /**
42  * Peer's own identity
43  */
44 static struct GNUNET_PeerIdentity me;
45
46
47 /**
48  * Nodes with a pending request
49  */
50 struct GNUNET_CONTAINER_MultiHashMap *nodes_requested;
51
52
53 /**
54  * Active experimentation nodes
55  */
56 struct GNUNET_CONTAINER_MultiHashMap *nodes_active;
57
58
59 /**
60  * Inactive experimentation nodes
61  * To be excluded from future requests
62  */
63 struct GNUNET_CONTAINER_MultiHashMap *nodes_inactive;
64
65
66 /**
67  * Update statistics
68  *
69  * @param m hashmap to update values from
70  */
71 static void update_stats (struct GNUNET_CONTAINER_MultiHashMap *m)
72 {
73         GNUNET_assert (NULL != m);
74         GNUNET_assert (NULL != GSE_stats);
75
76         if (m == nodes_active)
77         {
78                         GNUNET_STATISTICS_set (GSE_stats, "# nodes active",
79                                         GNUNET_CONTAINER_multihashmap_size(m), GNUNET_NO);
80         }
81         else if (m == nodes_inactive)
82         {
83                         GNUNET_STATISTICS_set (GSE_stats, "# nodes inactive",
84                                         GNUNET_CONTAINER_multihashmap_size(m), GNUNET_NO);
85         }
86         else if (m == nodes_requested)
87         {
88                         GNUNET_STATISTICS_set (GSE_stats, "# nodes requested",
89                                         GNUNET_CONTAINER_multihashmap_size(m), GNUNET_NO);
90         }
91         else
92                 GNUNET_break (0);
93
94 }
95
96
97 /**
98  * Clean up nodes
99  *
100  * @param cls the hashmap to clean up
101  * @param key key of the current node
102  * @param value related node object
103  * @return always GNUNET_OK
104  */
105 static int
106 cleanup_nodes (void *cls,
107                                                          const struct GNUNET_HashCode * key,
108                                                          void *value)
109 {
110         struct Node *n;
111         struct GNUNET_CONTAINER_MultiHashMap *cur = cls;
112
113         n = value;
114         if (GNUNET_SCHEDULER_NO_TASK != n->timeout_task)
115         {
116                 GNUNET_SCHEDULER_cancel (n->timeout_task);
117                 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
118         }
119         if (NULL != n->cth)
120         {
121                 GNUNET_CORE_notify_transmit_ready_cancel (n->cth);
122                 n->cth = NULL;
123         }
124         GNUNET_free_non_null (n->issuer_id);
125
126         GNUNET_CONTAINER_multihashmap_remove (cur, key, value);
127         GNUNET_free (value);
128         return GNUNET_OK;
129 }
130
131
132 /**
133  * Check if id passed is my id
134  *
135  * @param id the id to check
136  * @return GNUNET_YES or GNUNET_NO
137  */
138 static int is_me (const struct GNUNET_PeerIdentity *id)
139 {
140         if (0 == memcmp (&me, id, sizeof (me)))
141                 return GNUNET_YES;
142         else
143                 return GNUNET_NO;
144 }
145
146 /**
147  * Core startup callback
148  *
149  * @param cls unused
150  * @param server core service's server handle
151  * @param my_identity my id
152  */
153 static void
154 core_startup_handler (void *cls,
155                                                                                         struct GNUNET_CORE_Handle *server,
156                       const struct GNUNET_PeerIdentity *my_identity)
157 {
158         me = *my_identity;
159 }
160
161
162 /**
163  * Remove experimentation request due to timeout
164  *
165  * @param cls the related node
166  * @param tc scheduler's task context
167  */
168 static void
169 remove_request (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
170 {
171         struct Node *n = cls;
172
173         GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Removing request for peer %s due to timeout\n"),
174                         GNUNET_i2s (&n->id));
175
176         if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (nodes_requested, &n->id.hashPubKey))
177         {
178                         GNUNET_CONTAINER_multihashmap_remove (nodes_requested, &n->id.hashPubKey, n);
179                         update_stats (nodes_requested);
180                         GNUNET_CONTAINER_multihashmap_put (nodes_inactive, &n->id.hashPubKey, n,
181                                         GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
182                         update_stats (nodes_inactive);
183         }
184
185         n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
186         if (NULL != n->cth)
187         {
188                 GNUNET_CORE_notify_transmit_ready_cancel (n->cth);
189                 n->cth = NULL;
190         }
191 }
192
193
194 /**
195  * Core's transmit notify callback to send request
196  *
197  * @param cls the related node
198  * @param bufsize buffer size
199  * @param buf the buffer to copy to
200  * @return bytes passed
201  */
202 size_t send_request_cb (void *cls, size_t bufsize, void *buf)
203 {
204         struct Node *n = cls;
205         struct Experimentation_Request msg;
206         size_t msg_size = sizeof (msg);
207         size_t ri_size = sizeof (struct Experimentation_Request_Issuer) * GSE_my_issuer_count;
208         size_t total_size = msg_size + ri_size;
209
210         memset (buf, '0', bufsize);
211         n->cth = NULL;
212   if (buf == NULL)
213   {
214     /* client disconnected */
215     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client disconnected\n");
216     if (GNUNET_SCHEDULER_NO_TASK != n->timeout_task)
217                 GNUNET_SCHEDULER_cancel (n->timeout_task);
218     GNUNET_SCHEDULER_add_now (&remove_request, n);
219     return 0;
220   }
221   GNUNET_assert (bufsize >= total_size);
222
223         msg.msg.size = htons (total_size);
224         msg.msg.type = htons (GNUNET_MESSAGE_TYPE_EXPERIMENTATION_REQUEST);
225         msg.capabilities = htonl (GSE_node_capabilities);
226         msg.issuer_count = htonl (GSE_my_issuer_count);
227         memcpy (buf, &msg, msg_size);
228         memcpy (&buf[msg_size], GSE_my_issuer, ri_size);
229
230         GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Sending request to peer %s\n"),
231                         GNUNET_i2s (&n->id));
232         return total_size;
233 }
234
235
236 /**
237  * Send request
238  *
239  * @param peer the peer to send to
240  */
241 static void send_request (const struct GNUNET_PeerIdentity *peer)
242 {
243         struct Node *n;
244         size_t size;
245         size_t c_issuers;
246
247         c_issuers = GSE_my_issuer_count;
248
249         size = sizeof (struct Experimentation_Request) +
250                                  c_issuers * sizeof (struct Experimentation_Request_Issuer);
251         n = GNUNET_malloc (sizeof (struct Node));
252         n->id = *peer;
253         n->timeout_task = GNUNET_SCHEDULER_add_delayed (EXP_RESPONSE_TIMEOUT, &remove_request, n);
254         n->capabilities = NONE;
255         n->cth = GNUNET_CORE_notify_transmit_ready(ch, GNUNET_NO, 0,
256                                                                 GNUNET_TIME_relative_get_forever_(),
257                                                                 peer, size, send_request_cb, n);
258
259         GNUNET_assert (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (nodes_requested,
260                         &peer->hashPubKey, n, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
261         update_stats (nodes_requested);
262 }
263
264
265 /**
266  * Core's transmit notify callback to send response
267  *
268  * @param cls the related node
269  * @param bufsize buffer size
270  * @param buf the buffer to copy to
271  * @return bytes passed
272  */
273 size_t send_response_cb (void *cls, size_t bufsize, void *buf)
274 {
275         struct Node *n = cls;
276         struct Experimentation_Response msg;
277         size_t size = sizeof (msg);
278
279         n->cth = NULL;
280   if (buf == NULL)
281   {
282     /* client disconnected */
283     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client disconnected\n");
284     return 0;
285   }
286   GNUNET_assert (bufsize >= size);
287
288         msg.msg.size = htons (size);
289         msg.msg.type = htons (GNUNET_MESSAGE_TYPE_EXPERIMENTATION_RESPONSE);
290         msg.capabilities = htonl (GSE_node_capabilities);
291         memcpy (buf, &msg, size);
292
293         GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Sending response to peer %s\n"),
294                         GNUNET_i2s (&n->id));
295         return size;
296 }
297
298
299 static void
300 get_experiments_cb (struct Node *n, struct Experiment *e)
301 {
302         static int counter = 0;
303         if (NULL == e)
304         {
305                         GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Added %u experiments for peer %s\n"),
306                                         counter, GNUNET_i2s (&n->id));
307                         return;
308         }
309
310         GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Scheduling experiment `%s' for peer %s\n"),
311                         GNUNET_i2s (&n->id));
312         GNUNET_EXPERIMENTATION_scheduler_add (e);
313         counter ++;
314 }
315
316 /**
317  * Set a specific node as active
318  *
319  * @param n the node
320  */
321 static void node_make_active (struct Node *n)
322 {
323         int c1;
324   GNUNET_CONTAINER_multihashmap_put (nodes_active,
325                         &n->id.hashPubKey, n, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
326         update_stats (nodes_active);
327         GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Added peer `%s' as active node\n"),
328                         GNUNET_i2s (&n->id));
329
330         /* Request experiments for this node to start them */
331         for (c1 = 0; c1 < n->issuer_count; c1++)
332                 GNUNET_EXPERIMENTATION_experiments_get (n, &n->issuer_id[c1], &get_experiments_cb);
333 }
334
335
336 /**
337  * Handle a request and send a response
338  *
339  * @param peer the source
340  * @param message the message
341  */
342 static void handle_request (const struct GNUNET_PeerIdentity *peer,
343                                                                                                                 const struct GNUNET_MessageHeader *message)
344 {
345         struct Node *n;
346         struct Experimentation_Request *rm = (struct Experimentation_Request *) message;
347         struct Experimentation_Request_Issuer *rmi = (struct Experimentation_Request_Issuer *) &rm[1];
348         int c1;
349         int c2;
350         uint32_t ic;
351         uint32_t ic_accepted;
352         int make_active;
353
354         if (ntohs (message->size) < sizeof (struct Experimentation_Request))
355         {
356                 GNUNET_break (0);
357                 return;
358         }
359         ic = ntohl (rm->issuer_count);
360         if (ntohs (message->size) != sizeof (struct Experimentation_Request) + ic * sizeof (struct Experimentation_Request_Issuer))
361         {
362                 GNUNET_break (0);
363                 return;
364         }
365
366         make_active = GNUNET_NO;
367         if (NULL != (n = GNUNET_CONTAINER_multihashmap_get (nodes_active, &peer->hashPubKey)))
368         {
369                         /* Nothing to do */
370         }
371         else if (NULL != (n = GNUNET_CONTAINER_multihashmap_get (nodes_requested, &peer->hashPubKey)))
372         {
373                         GNUNET_CONTAINER_multihashmap_remove (nodes_requested, &peer->hashPubKey, n);
374                         if (GNUNET_SCHEDULER_NO_TASK != n->timeout_task)
375                         {
376                                 GNUNET_SCHEDULER_cancel (n->timeout_task);
377                                 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
378                         }
379                         if (NULL != n->cth)
380                         {
381                                 GNUNET_CORE_notify_transmit_ready_cancel (n->cth);
382                                 n->cth = NULL;
383                         }
384                         update_stats (nodes_requested);
385                         node_make_active (n);
386         }
387         else if (NULL != (n = GNUNET_CONTAINER_multihashmap_get (nodes_inactive, &peer->hashPubKey)))
388         {
389                         GNUNET_CONTAINER_multihashmap_remove (nodes_inactive, &peer->hashPubKey, n);
390                         update_stats (nodes_inactive);
391                         make_active = GNUNET_YES;
392         }
393         else
394         {
395                         /* Create new node */
396                         n = GNUNET_malloc (sizeof (struct Node));
397                         n->id = *peer;
398                         n->capabilities = NONE;
399                         node_make_active (n);
400         }
401
402         /* Update node */
403         n->capabilities = ntohl (rm->capabilities);
404
405         /* Filter accepted issuer */
406         ic_accepted = 0;
407         for (c1 = 0; c1 < ic; c1++)
408         {
409                 if (GNUNET_YES == GNUNET_EXPERIMENTATION_experiments_issuer_accepted(&rmi[c1].issuer_id))
410                         ic_accepted ++;
411         }
412         GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Request from peer `%s' with %u issuers, we accepted %u issuer \n"),
413                         GNUNET_i2s (peer), ic, ic_accepted);
414         GNUNET_free_non_null (n->issuer_id);
415         n->issuer_id = GNUNET_malloc (ic_accepted * sizeof (struct GNUNET_PeerIdentity));
416         c2 = 0;
417         for (c1 = 0; c1 < ic; c1++)
418         {
419                         if (GNUNET_YES == GNUNET_EXPERIMENTATION_experiments_issuer_accepted(&rmi[c1].issuer_id))
420                         {
421                                 n->issuer_id[c2] = rmi[c1].issuer_id;
422                                 c2 ++;
423                         }
424         }
425         n->issuer_count = ic_accepted;
426
427         if (GNUNET_YES == make_active)
428                 node_make_active (n);
429
430         /* Send response */
431         n->cth = GNUNET_CORE_notify_transmit_ready (ch, GNUNET_NO, 0,
432                                                                 GNUNET_TIME_relative_get_forever_(),
433                                                                 peer, sizeof (struct Experimentation_Response),
434                                                                 send_response_cb, n);
435 }
436
437
438 /**
439  * Handle a response
440  *
441  * @param peer the source
442  * @param message the message
443  */
444 static void handle_response (const struct GNUNET_PeerIdentity *peer,
445                                                                                                                  const struct GNUNET_MessageHeader *message)
446 {
447         struct Node *n;
448         struct Experimentation_Request *rm = (struct Experimentation_Request *) message;
449
450         if (NULL != (n = GNUNET_CONTAINER_multihashmap_get (nodes_active, &peer->hashPubKey)))
451         {
452                         GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Received %s from %s peer `%s'\n"),
453                                         "RESPONSE", "active", GNUNET_i2s (peer));
454                         n->capabilities = ntohl (rm->capabilities);
455         }
456         else if (NULL != (n = GNUNET_CONTAINER_multihashmap_get (nodes_requested, &peer->hashPubKey)))
457         {
458                         GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Received %s from %s peer `%s'\n"),
459                                         "RESPONSE", "requested", GNUNET_i2s (peer));
460                         n->capabilities = ntohl (rm->capabilities);
461                         GNUNET_CONTAINER_multihashmap_remove (nodes_requested, &peer->hashPubKey, n);
462                         if (GNUNET_SCHEDULER_NO_TASK != n->timeout_task)
463                         {
464                                 GNUNET_SCHEDULER_cancel (n->timeout_task);
465                                 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
466                         }
467                         if (NULL != n->cth)
468                         {
469                                 GNUNET_CORE_notify_transmit_ready_cancel (n->cth);
470                                 n->cth = NULL;
471                         }
472                         update_stats (nodes_requested);
473                         node_make_active (n);
474         }
475         else if (NULL != (n = GNUNET_CONTAINER_multihashmap_get (nodes_inactive, &peer->hashPubKey)))
476         {
477                         GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Received %s from peer `%s'\n"),
478                                         "RESPONSE", "inactive", GNUNET_i2s (peer));
479                         n->capabilities = ntohl (rm->capabilities);
480                         GNUNET_CONTAINER_multihashmap_remove (nodes_inactive, &peer->hashPubKey, n);
481                         update_stats (nodes_inactive);
482                         node_make_active (n);
483         }
484         else
485         {
486                         GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Received %s from %s peer `%s'\n"),
487                                         "RESPONSE", "unknown", GNUNET_i2s (peer));
488                         return;
489         }
490 }
491
492 /**
493  * Method called whenever a given peer connects.
494  *
495  * @param cls closure
496  * @param peer peer identity this notification is about
497  */
498 void core_connect_handler (void *cls,
499                            const struct GNUNET_PeerIdentity *peer)
500 {
501         if (GNUNET_YES == is_me(peer))
502                 return;
503
504         GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connected to peer %s\n"),
505                         GNUNET_i2s (peer));
506
507         if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (nodes_requested, &peer->hashPubKey))
508                 return; /* We already sent a request */
509
510         if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (nodes_active, &peer->hashPubKey))
511                 return; /* This peer is known as active  */
512
513         if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (nodes_inactive, &peer->hashPubKey))
514                 return; /* This peer is known as inactive  */
515
516         send_request (peer);
517
518 }
519
520
521 /**
522  * Method called whenever a given peer disconnects.
523  *
524  * @param cls closure
525  * @param peer peer identity this notification is about
526  */
527 void core_disconnect_handler (void *cls,
528                            const struct GNUNET_PeerIdentity * peer)
529 {
530         if (GNUNET_YES == is_me(peer))
531                 return;
532
533         GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Disconnected from peer %s\n"),
534                         GNUNET_i2s (peer));
535
536 }
537
538
539 /**
540  * Handle a request and send a response
541  *
542  * @param cls unused
543  * @param other the sender
544  * @param message the message
545  * @return GNUNET_OK to keep connection, GNUNET_SYSERR on error
546  */
547 static int
548 core_receive_handler (void *cls,
549                                                                                         const struct GNUNET_PeerIdentity *other,
550                                                                                         const struct GNUNET_MessageHeader *message)
551 {
552         if (ntohs (message->size) < sizeof (struct GNUNET_MessageHeader))
553         {
554                         GNUNET_break (0);
555                         return GNUNET_SYSERR;
556         }
557
558         switch (ntohs (message->type)) {
559                 case GNUNET_MESSAGE_TYPE_EXPERIMENTATION_REQUEST:
560                         handle_request (other, message);
561                         break;
562                 case GNUNET_MESSAGE_TYPE_EXPERIMENTATION_RESPONSE:
563                         handle_response (other, message);
564                         break;
565                 default:
566                         break;
567         }
568
569         return GNUNET_OK;
570 }
571
572
573
574 /**
575  * Start the nodes management
576  */
577 void
578 GNUNET_EXPERIMENTATION_nodes_start ()
579 {
580         /* Connecting to core service to find partners */
581         ch = GNUNET_CORE_connect (GSE_cfg, NULL,
582                                                                                                                 &core_startup_handler,
583                                                                                                                 &core_connect_handler,
584                                                                                                                 &core_disconnect_handler,
585                                                                                                                 &core_receive_handler,
586                                                                                                                 GNUNET_NO, NULL, GNUNET_NO, NULL);
587         if (NULL == ch)
588         {
589                         GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Failed to connect to CORE service!\n"));
590                         return;
591         }
592
593         nodes_requested = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
594         nodes_active = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
595         nodes_inactive = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
596 }
597
598
599 /**
600  * Stop the nodes management
601  */
602 void
603 GNUNET_EXPERIMENTATION_nodes_stop ()
604 {
605   if (NULL != ch)
606   {
607                 GNUNET_CORE_disconnect (ch);
608                 ch = NULL;
609   }
610
611   if (NULL != nodes_requested)
612   {
613                 GNUNET_CONTAINER_multihashmap_iterate (nodes_requested,
614                                                                                                                                                                          &cleanup_nodes,
615                                                                                                                                                                          nodes_requested);
616                 update_stats (nodes_requested);
617                 GNUNET_CONTAINER_multihashmap_destroy (nodes_requested);
618                 nodes_requested = NULL;
619   }
620
621   if (NULL != nodes_active)
622   {
623                 GNUNET_CONTAINER_multihashmap_iterate (nodes_active,
624                                                                                                                                                                          &cleanup_nodes,
625                                                                                                                                                                          nodes_active);
626                 update_stats (nodes_active);
627                 GNUNET_CONTAINER_multihashmap_destroy (nodes_active);
628                 nodes_active = NULL;
629   }
630
631   if (NULL != nodes_inactive)
632   {
633                 GNUNET_CONTAINER_multihashmap_iterate (nodes_inactive,
634                                                                                                                                                                          &cleanup_nodes,
635                                                                                                                                                                          nodes_inactive);
636                 update_stats (nodes_inactive);
637                 GNUNET_CONTAINER_multihashmap_destroy (nodes_inactive);
638                 nodes_inactive = NULL;
639   }
640 }
641
642 /* end of gnunet-daemon-experimentation_nodes.c */