fixes
[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 #define FAST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
36 /**
37  * Core handle
38  */
39 static struct GNUNET_CORE_Handle *ch;
40
41
42 /**
43  * Peer's own identity
44  */
45 static struct GNUNET_PeerIdentity me;
46
47
48 /**
49  * Nodes with a pending request
50  */
51 struct GNUNET_CONTAINER_MultiHashMap *nodes_requested;
52
53
54 /**
55  * Active experimentation nodes
56  */
57 struct GNUNET_CONTAINER_MultiHashMap *nodes_active;
58
59
60 /**
61  * Inactive experimentation nodes
62  * To be excluded from future requests
63  */
64 struct GNUNET_CONTAINER_MultiHashMap *nodes_inactive;
65
66 struct NodeComCtx
67 {
68         struct NodeComCtx *prev;
69         struct NodeComCtx *next;
70
71         struct Node *n;
72         struct Experiment *e;
73
74         size_t size;
75         GNUNET_CONNECTION_TransmitReadyNotify notify;
76         void *notify_cls;
77 };
78
79
80 /**
81  * Update statistics
82  *
83  * @param m hashmap to update values from
84  */
85 static void update_stats (struct GNUNET_CONTAINER_MultiHashMap *m)
86 {
87         GNUNET_assert (NULL != m);
88         GNUNET_assert (NULL != GED_stats);
89
90         if (m == nodes_active)
91         {
92                         GNUNET_STATISTICS_set (GED_stats, "# nodes active",
93                                         GNUNET_CONTAINER_multihashmap_size(m), GNUNET_NO);
94         }
95         else if (m == nodes_inactive)
96         {
97                         GNUNET_STATISTICS_set (GED_stats, "# nodes inactive",
98                                         GNUNET_CONTAINER_multihashmap_size(m), GNUNET_NO);
99         }
100         else if (m == nodes_requested)
101         {
102                         GNUNET_STATISTICS_set (GED_stats, "# nodes requested",
103                                         GNUNET_CONTAINER_multihashmap_size(m), GNUNET_NO);
104         }
105         else
106                 GNUNET_break (0);
107
108 }
109
110
111 /**
112  * Clean up node
113  *
114  * @param cls the hashmap to clean up
115  * @param key key of the current node
116  * @param value related node object
117  * @return always GNUNET_OK
118  */
119 static int
120 cleanup_node (void *cls,
121                                                          const struct GNUNET_HashCode * key,
122                                                          void *value)
123 {
124         struct Node *n;
125         struct NodeComCtx *e_cur;
126         struct NodeComCtx *e_next;
127         struct GNUNET_CONTAINER_MultiHashMap *cur = cls;
128
129         n = value;
130         if (GNUNET_SCHEDULER_NO_TASK != n->timeout_task)
131         {
132                 GNUNET_SCHEDULER_cancel (n->timeout_task);
133                 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
134         }
135
136         if (NULL != n->cth)
137         {
138                 GNUNET_CORE_notify_transmit_ready_cancel (n->cth);
139                 n->cth = NULL;
140         }
141         e_next = n->e_req_head;
142         while (NULL != (e_cur = e_next))
143         {
144                 e_next = e_cur->next;
145                 GNUNET_CONTAINER_DLL_remove (n->e_req_head, n->e_req_tail, e_cur);
146                 GNUNET_free (e_cur);
147         }
148
149         GNUNET_free_non_null (n->issuer_id);
150
151         GNUNET_CONTAINER_multihashmap_remove (cur, key, value);
152         GNUNET_free (value);
153         return GNUNET_OK;
154 }
155
156
157 /**
158  * Check if id passed is my id
159  *
160  * @param id the id to check
161  * @return GNUNET_YES or GNUNET_NO
162  */
163 static int is_me (const struct GNUNET_PeerIdentity *id)
164 {
165         if (0 == memcmp (&me, id, sizeof (me)))
166                 return GNUNET_YES;
167         else
168                 return GNUNET_NO;
169 }
170
171 /**
172  * Core startup callback
173  *
174  * @param cls unused
175  * @param server core service's server handle
176  * @param my_identity my id
177  */
178 static void
179 core_startup_handler (void *cls,
180                                                                                         struct GNUNET_CORE_Handle *server,
181                       const struct GNUNET_PeerIdentity *my_identity)
182 {
183         me = *my_identity;
184 }
185
186 void
187 schedule_transmisson (struct NodeComCtx *e_ctx);
188
189 size_t
190 transmit_read_wrapper (void *cls, size_t bufsize, void *buf)
191 {
192         struct NodeComCtx *e_ctx = cls;
193         struct NodeComCtx *next = NULL;
194
195         size_t res = e_ctx->notify (e_ctx->notify_cls, bufsize, buf);
196         e_ctx->n->cth = NULL;
197
198         GNUNET_CONTAINER_DLL_remove (e_ctx->n->e_req_head, e_ctx->n->e_req_tail, e_ctx);
199         next = e_ctx->n->e_req_head;
200         GNUNET_free (e_ctx);
201
202         if (NULL != next)
203         {
204                 /* Schedule next message */
205                 schedule_transmisson (next);
206         }
207         return res;
208 }
209
210 void
211 schedule_transmisson (struct NodeComCtx *e_ctx)
212 {
213         if (NULL != e_ctx->n->cth)
214                 return;
215
216         e_ctx->n->cth = GNUNET_CORE_notify_transmit_ready (ch, GNUNET_NO, 0, FAST_TIMEOUT,
217                         &e_ctx->n->id, e_ctx->size, transmit_read_wrapper, e_ctx);
218         if (NULL == e_ctx->n->cth)
219         {
220                 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Cannot send message to peer `%s' for experiment `%s'\n"),
221                                 GNUNET_i2s(&e_ctx->n->id), e_ctx->e->name);
222                 GNUNET_free (e_ctx);
223         }
224
225 }
226
227
228 /**
229  * Remove experimentation request due to timeout
230  *
231  * @param cls the related node
232  * @param tc scheduler's task context
233  */
234 static void
235 remove_request (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
236 {
237         struct Node *n = cls;
238
239         GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Removing request for peer %s due to timeout\n"),
240                         GNUNET_i2s (&n->id));
241
242         if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (nodes_requested, &n->id.hashPubKey))
243         {
244                         GNUNET_CONTAINER_multihashmap_remove (nodes_requested, &n->id.hashPubKey, n);
245                         update_stats (nodes_requested);
246                         GNUNET_CONTAINER_multihashmap_put (nodes_inactive, &n->id.hashPubKey, n,
247                                         GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
248                         update_stats (nodes_inactive);
249         }
250         n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
251 }
252
253
254 /**
255  * Core's transmit notify callback to send request
256  *
257  * @param cls the related node
258  * @param bufsize buffer size
259  * @param buf the buffer to copy to
260  * @return bytes passed
261  */
262 size_t send_experimentation_request_cb (void *cls, size_t bufsize, void *buf)
263 {
264         struct Node *n = cls;
265         struct Experimentation_Request msg;
266         size_t msg_size = sizeof (msg);
267         size_t ri_size = sizeof (struct Experimentation_Issuer) * GSE_my_issuer_count;
268         size_t total_size = msg_size + ri_size;
269
270         memset (buf, '0', bufsize);
271         n->cth = NULL;
272   if (buf == NULL)
273   {
274     /* client disconnected */
275     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client disconnected\n");
276     if (GNUNET_SCHEDULER_NO_TASK != n->timeout_task)
277                 GNUNET_SCHEDULER_cancel (n->timeout_task);
278     GNUNET_SCHEDULER_add_now (&remove_request, n);
279     return 0;
280   }
281   GNUNET_assert (bufsize >= total_size);
282
283         msg.msg.size = htons (total_size);
284         msg.msg.type = htons (GNUNET_MESSAGE_TYPE_EXPERIMENTATION_REQUEST);
285         msg.capabilities = htonl (GSE_node_capabilities);
286         msg.issuer_count = htonl (GSE_my_issuer_count);
287         memcpy (buf, &msg, msg_size);
288         memcpy (&((char *) buf)[msg_size], GSE_my_issuer, ri_size);
289
290         GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Sending request to peer %s\n"),
291                         GNUNET_i2s (&n->id));
292         return total_size;
293 }
294
295
296 /**
297  * Send request to peer to start add him to to the set of experimentation nodes
298  *
299  * @param peer the peer to send to
300  */
301 static void send_experimentation_request (const struct GNUNET_PeerIdentity *peer)
302 {
303         struct Node *n;
304         struct NodeComCtx *e_ctx;
305         size_t size;
306         size_t c_issuers;
307
308         c_issuers = GSE_my_issuer_count;
309
310         size = sizeof (struct Experimentation_Request) +
311                                  c_issuers * sizeof (struct Experimentation_Issuer);
312         n = GNUNET_malloc (sizeof (struct Node));
313         n->id = *peer;
314         n->timeout_task = GNUNET_SCHEDULER_add_delayed (EXP_RESPONSE_TIMEOUT, &remove_request, n);
315         n->capabilities = NONE;
316
317         e_ctx = GNUNET_malloc (sizeof (struct NodeComCtx));
318         e_ctx->n = n;
319         e_ctx->e = NULL;
320         e_ctx->size = size;
321         e_ctx->notify = &send_experimentation_request_cb;
322         e_ctx->notify_cls = n;
323         GNUNET_CONTAINER_DLL_insert_tail(n->e_req_head, n->e_req_tail, e_ctx);
324         schedule_transmisson (e_ctx);
325
326         GNUNET_assert (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (nodes_requested,
327                         &peer->hashPubKey, n, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
328         update_stats (nodes_requested);
329 }
330
331
332 /**
333  * Core's transmit notify callback to send response
334  *
335  * @param cls the related node
336  * @param bufsize buffer size
337  * @param buf the buffer to copy to
338  * @return bytes passed
339  */
340 size_t send_response_cb (void *cls, size_t bufsize, void *buf)
341 {
342         struct Node *n = cls;
343         struct Experimentation_Response msg;
344         size_t ri_size = GSE_my_issuer_count * sizeof (struct Experimentation_Issuer);
345         size_t msg_size = sizeof (msg);
346         size_t total_size = msg_size + ri_size;
347
348         n->cth = NULL;
349   if (buf == NULL)
350   {
351     /* client disconnected */
352     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client disconnected\n");
353     return 0;
354   }
355   GNUNET_assert (bufsize >= total_size);
356
357         msg.msg.size = htons (total_size);
358         msg.msg.type = htons (GNUNET_MESSAGE_TYPE_EXPERIMENTATION_RESPONSE);
359         msg.capabilities = htonl (GSE_node_capabilities);
360         msg.issuer_count = htonl (GSE_my_issuer_count);
361         memcpy (buf, &msg, msg_size);
362         memcpy (&((char *) buf)[msg_size], GSE_my_issuer, ri_size);
363
364         GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Sending response to peer %s\n"),
365                         GNUNET_i2s (&n->id));
366         return total_size;
367 }
368
369
370 static void
371 get_experiments_cb (struct Node *n, struct Experiment *e)
372 {
373         static int counter = 0;
374         if (NULL == e)
375         {
376                         GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Added %u experiments for peer %s\n"),
377                                         counter, GNUNET_i2s (&n->id));
378                         return;
379         }
380
381         GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Starting experiment `%s' with peer %s\n"),
382                         e->name,
383                         GNUNET_i2s (&n->id));
384
385         /* Tell the scheduler to add a node with an experiment */
386         GED_scheduler_add (n, e, GNUNET_YES);
387         counter ++;
388 }
389
390 struct Node *
391 get_node (const struct GNUNET_PeerIdentity *id)
392 {
393         struct Node * res;
394         struct Node * tmp;
395
396         res = NULL;
397         tmp = NULL;
398         tmp = GNUNET_CONTAINER_multihashmap_get (nodes_active, &id->hashPubKey);
399         if (res == NULL)
400                 res = tmp;
401
402         tmp = GNUNET_CONTAINER_multihashmap_get (nodes_inactive, &id->hashPubKey);
403         if (res == NULL)
404                 res = tmp;
405         else
406                 GNUNET_break (0); /* Multiple instances */
407
408         tmp = GNUNET_CONTAINER_multihashmap_get (nodes_requested, &id->hashPubKey);
409         if (res == NULL)
410                 res = tmp;
411         else
412                 GNUNET_break (0); /* Multiple instances */
413
414         return res;
415 }
416
417
418 /**
419  * Set a specific node as active
420  *
421  * @param n the node
422  */
423 static void node_make_active (struct Node *n)
424 {
425         int c1;
426   GNUNET_CONTAINER_multihashmap_put (nodes_active,
427                         &n->id.hashPubKey, n, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
428         update_stats (nodes_active);
429         GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Added peer `%s' as active node\n"),
430                         GNUNET_i2s (&n->id));
431 return;
432         /* Request experiments for this node to start them */
433         for (c1 = 0; c1 < n->issuer_count; c1++)
434         {
435
436                 GED_experiments_get (n, &n->issuer_id[c1], &get_experiments_cb);
437         }
438 }
439
440
441 /**
442  * Handle a request and send a response
443  *
444  * @param peer the source
445  * @param message the message
446  */
447 static void handle_request (const struct GNUNET_PeerIdentity *peer,
448                                                                                                                 const struct GNUNET_MessageHeader *message)
449 {
450         struct Node *n;
451         struct NodeComCtx *e_ctx;
452         struct Experimentation_Request *rm = (struct Experimentation_Request *) message;
453         struct Experimentation_Issuer *rmi = (struct Experimentation_Issuer *) &rm[1];
454         int c1;
455         int c2;
456         uint32_t ic;
457         uint32_t ic_accepted;
458         int make_active;
459
460         if (ntohs (message->size) < sizeof (struct Experimentation_Request))
461         {
462                 GNUNET_break (0);
463                 return;
464         }
465         ic = ntohl (rm->issuer_count);
466         if (ntohs (message->size) != sizeof (struct Experimentation_Request) + ic * sizeof (struct Experimentation_Issuer))
467         {
468                 GNUNET_break (0);
469                 return;
470         }
471
472         make_active = GNUNET_NO;
473         if (NULL != (n = GNUNET_CONTAINER_multihashmap_get (nodes_active, &peer->hashPubKey)))
474         {
475                         /* Nothing to do */
476         }
477         else if (NULL != (n = GNUNET_CONTAINER_multihashmap_get (nodes_requested, &peer->hashPubKey)))
478         {
479                         GNUNET_CONTAINER_multihashmap_remove (nodes_requested, &peer->hashPubKey, n);
480                         if (GNUNET_SCHEDULER_NO_TASK != n->timeout_task)
481                         {
482                                 GNUNET_SCHEDULER_cancel (n->timeout_task);
483                                 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
484                         }
485                         update_stats (nodes_requested);
486                         make_active = GNUNET_YES;
487         }
488         else if (NULL != (n = GNUNET_CONTAINER_multihashmap_get (nodes_inactive, &peer->hashPubKey)))
489         {
490                         GNUNET_CONTAINER_multihashmap_remove (nodes_inactive, &peer->hashPubKey, n);
491                         update_stats (nodes_inactive);
492                         make_active = GNUNET_YES;
493         }
494         else
495         {
496                         /* Create new node */
497                         n = GNUNET_malloc (sizeof (struct Node));
498                         n->id = *peer;
499                         n->capabilities = NONE;
500                         make_active = GNUNET_YES;
501         }
502
503         /* Update node */
504         n->capabilities = ntohl (rm->capabilities);
505
506         /* Filter accepted issuer */
507         ic_accepted = 0;
508         for (c1 = 0; c1 < ic; c1++)
509         {
510                 if (GNUNET_YES == GED_experiments_issuer_accepted(&rmi[c1].issuer_id))
511                         ic_accepted ++;
512         }
513         GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Request from peer `%s' with %u issuers, we accepted %u issuer \n"),
514                         GNUNET_i2s (peer), ic, ic_accepted);
515         GNUNET_free_non_null (n->issuer_id);
516         n->issuer_id = GNUNET_malloc (ic_accepted * sizeof (struct GNUNET_PeerIdentity));
517         c2 = 0;
518         for (c1 = 0; c1 < ic; c1++)
519         {
520                         if (GNUNET_YES == GED_experiments_issuer_accepted(&rmi[c1].issuer_id))
521                         {
522                                 n->issuer_id[c2] = rmi[c1].issuer_id;
523                                 c2 ++;
524                         }
525         }
526         n->issuer_count = ic_accepted;
527
528         if (GNUNET_YES == make_active)
529                 node_make_active (n);
530
531         /* Send response */
532         e_ctx = GNUNET_malloc (sizeof (struct NodeComCtx));
533         e_ctx->n = n;
534         e_ctx->e = NULL;
535         e_ctx->size = sizeof (struct Experimentation_Response) + GSE_my_issuer_count * sizeof (struct Experimentation_Issuer);
536         e_ctx->notify = &send_response_cb;
537         e_ctx->notify_cls = n;
538
539         GNUNET_CONTAINER_DLL_insert_tail(n->e_req_head, n->e_req_tail, e_ctx);
540         schedule_transmisson (e_ctx);
541 }
542
543
544 /**
545  * Handle a response
546  *
547  * @param peer the source
548  * @param message the message
549  */
550 static void handle_response (const struct GNUNET_PeerIdentity *peer,
551                                                                                                                  const struct GNUNET_MessageHeader *message)
552 {
553         struct Node *n;
554         struct Experimentation_Response *rm = (struct Experimentation_Response *) message;
555         struct Experimentation_Issuer *rmi = (struct Experimentation_Issuer *) &rm[1];
556         uint32_t ic;
557         uint32_t ic_accepted;
558         int make_active;
559         unsigned int c1;
560         unsigned int c2;
561
562         if (ntohs (message->size) < sizeof (struct Experimentation_Response))
563         {
564                 GNUNET_break (0);
565                 return;
566         }
567         ic = ntohl (rm->issuer_count);
568         if (ntohs (message->size) != sizeof (struct Experimentation_Response) + ic * sizeof (struct Experimentation_Issuer))
569         {
570                 GNUNET_break (0);
571                 return;
572         }
573
574         make_active = GNUNET_NO;
575         if (NULL != (n = GNUNET_CONTAINER_multihashmap_get (nodes_active, &peer->hashPubKey)))
576         {
577                         GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Received %s from %s peer `%s'\n"),
578                                         "RESPONSE", "active", GNUNET_i2s (peer));
579         }
580         else if (NULL != (n = GNUNET_CONTAINER_multihashmap_get (nodes_requested, &peer->hashPubKey)))
581         {
582                         GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Received %s from %s peer `%s'\n"),
583                                         "RESPONSE", "requested", GNUNET_i2s (peer));
584                         GNUNET_CONTAINER_multihashmap_remove (nodes_requested, &peer->hashPubKey, n);
585                         if (GNUNET_SCHEDULER_NO_TASK != n->timeout_task)
586                         {
587                                 GNUNET_SCHEDULER_cancel (n->timeout_task);
588                                 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
589                         }
590                         update_stats (nodes_requested);
591                         make_active = GNUNET_YES;
592         }
593         else if (NULL != (n = GNUNET_CONTAINER_multihashmap_get (nodes_inactive, &peer->hashPubKey)))
594         {
595                         GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Received %s from peer `%s'\n"),
596                                         "RESPONSE", "inactive", GNUNET_i2s (peer));
597                         GNUNET_CONTAINER_multihashmap_remove (nodes_inactive, &peer->hashPubKey, n);
598                         update_stats (nodes_inactive);
599                         make_active = GNUNET_YES;
600         }
601         else
602         {
603                         GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Received %s from %s peer `%s'\n"),
604                                         "RESPONSE", "unknown", GNUNET_i2s (peer));
605                         return;
606         }
607
608         /* Update */
609         n->capabilities = ntohl (rm->capabilities);
610
611         /* Filter accepted issuer */
612         ic_accepted = 0;
613         for (c1 = 0; c1 < ic; c1++)
614         {
615                 if (GNUNET_YES == GED_experiments_issuer_accepted(&rmi[c1].issuer_id))
616                         ic_accepted ++;
617         }
618         GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Response from peer `%s' with %u issuers, we accepted %u issuer \n"),
619                         GNUNET_i2s (peer), ic, ic_accepted);
620         GNUNET_free_non_null (n->issuer_id);
621         n->issuer_id = GNUNET_malloc (ic_accepted * sizeof (struct GNUNET_PeerIdentity));
622         c2 = 0;
623         for (c1 = 0; c1 < ic; c1++)
624         {
625                         if (GNUNET_YES == GED_experiments_issuer_accepted(&rmi[c1].issuer_id))
626                         {
627                                 n->issuer_id[c2] = rmi[c1].issuer_id;
628                                 c2 ++;
629                         }
630         }
631         n->issuer_count = ic_accepted;
632
633         if (GNUNET_YES == make_active)
634                 node_make_active (n);
635 }
636
637 /**
638  * Handle a response
639  *
640  * @param peer the source
641  * @param message the message
642  */
643 static void handle_start (const struct GNUNET_PeerIdentity *peer,
644                                                                                                                  const struct GNUNET_MessageHeader *message)
645 {
646         uint16_t size;
647         uint32_t name_len;
648         const struct GED_start_message *msg;
649         const char *name;
650         struct Node *n;
651         struct Experiment *e;
652
653         if (NULL == peer)
654         {
655                 GNUNET_break (0);
656                 return;
657         }
658         if (NULL == message)
659         {
660                 GNUNET_break (0);
661                 return;
662         }
663
664         size = ntohs (message->size);
665         if (size < sizeof (struct GED_start_message))
666         {
667                 GNUNET_break (0);
668                 return;
669         }
670         msg = (const struct GED_start_message *) message;
671         name_len = ntohl (msg->len_name);
672         if (size != sizeof (struct GED_start_message) + name_len)
673         {
674                 GNUNET_break (0);
675                 return;
676         }
677
678         n = get_node (peer);
679         if (NULL == n)
680         {
681                 GNUNET_break (0);
682                 return;
683         }
684         name = (const char *) &msg[1];
685         if (name[name_len-1] != '\0')
686         {
687                 GNUNET_break (0);
688                 return;
689         }
690
691         if (name_len != strlen (name) + 1)
692         {
693                 GNUNET_break (0);
694                 return;
695         }
696
697         e = GED_experiments_find (&msg->issuer, name, GNUNET_TIME_absolute_ntoh(msg->version_nbo));
698         if (NULL == e)
699         {
700                 GNUNET_break (0);
701                 return;
702         }
703
704         GED_scheduler_handle_start (n, e);
705 }
706
707 /**
708  * Handle a response
709  *
710  * @param peer the source
711  * @param message the message
712  */
713 static void handle_start_ack (const struct GNUNET_PeerIdentity *peer,
714                                                                                                                  const struct GNUNET_MessageHeader *message)
715 {
716         uint16_t size;
717         uint32_t name_len;
718         const struct GED_start_ack_message *msg;
719         const char *name;
720         struct Node *n;
721         struct Experiment *e;
722
723         if (NULL == peer)
724         {
725                 GNUNET_break (0);
726                 return;
727         }
728         if (NULL == message)
729         {
730                 GNUNET_break (0);
731                 return;
732         }
733
734         size = ntohs (message->size);
735         if (size < sizeof (struct GED_start_ack_message))
736         {
737                 GNUNET_break (0);
738                 return;
739         }
740         msg = (const struct GED_start_ack_message *) message;
741         name_len = ntohl (msg->len_name);
742         if (size != sizeof (struct GED_start_message) + name_len)
743         {
744                 GNUNET_break (0);
745                 return;
746         }
747
748         n = get_node (peer);
749         if (NULL == n)
750         {
751                 GNUNET_break (0);
752                 return;
753         }
754         name = (const char *) &msg[1];
755         if (name[name_len-1] != '\0')
756         {
757                 GNUNET_break (0);
758                 return;
759         }
760
761         if (name_len != strlen (name) + 1)
762         {
763                 GNUNET_break (0);
764                 return;
765         }
766
767         e = GED_experiments_find (&msg->issuer, name, GNUNET_TIME_absolute_ntoh(msg->version_nbo));
768         if (NULL == e)
769         {
770                 GNUNET_break (0);
771                 return;
772         }
773         GED_scheduler_handle_start_ack (n, e);
774 }
775
776 /**
777  * Handle a response
778  *
779  * @param peer the source
780  * @param message the message
781  */
782 static void handle_stop (const struct GNUNET_PeerIdentity *peer,
783                                                                                                  const struct GNUNET_MessageHeader *message)
784 {
785         uint16_t size;
786         uint32_t name_len;
787         const struct GED_stop_message *msg;
788         const char *name;
789         struct Node *n;
790         struct Experiment *e;
791
792         if (NULL == peer)
793         {
794                 GNUNET_break (0);
795                 return;
796         }
797         if (NULL == message)
798         {
799                 GNUNET_break (0);
800                 return;
801         }
802
803         size = ntohs (message->size);
804         if (size < sizeof (struct GED_stop_message))
805         {
806                 GNUNET_break (0);
807                 return;
808         }
809         msg = (const struct GED_stop_message *) message;
810         name_len = ntohl (msg->len_name);
811         if (size != sizeof (struct GED_start_message) + name_len)
812         {
813                 GNUNET_break (0);
814                 return;
815         }
816
817         n = get_node (peer);
818         if (NULL == n)
819         {
820                 GNUNET_break (0);
821                 return;
822         }
823         name = (const char *) &msg[1];
824         if (name[name_len-1] != '\0')
825         {
826                 GNUNET_break (0);
827                 return;
828         }
829
830         if (name_len != strlen (name) + 1)
831         {
832                 GNUNET_break (0);
833                 return;
834         }
835
836         e = GED_experiments_find (&msg->issuer, name, GNUNET_TIME_absolute_ntoh(msg->version_nbo));
837         if (NULL == e)
838         {
839                 GNUNET_break (0);
840                 return;
841         }
842         GED_scheduler_handle_stop (n, e);
843 }
844
845 /**
846  * Method called whenever a given peer connects.
847  *
848  * @param cls closure
849  * @param peer peer identity this notification is about
850  */
851 void core_connect_handler (void *cls,
852                            const struct GNUNET_PeerIdentity *peer)
853 {
854         if (GNUNET_YES == is_me(peer))
855                 return;
856
857         GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connected to peer %s\n"),
858                         GNUNET_i2s (peer));
859
860         if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (nodes_requested, &peer->hashPubKey))
861                 return; /* We already sent a request */
862
863         if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (nodes_active, &peer->hashPubKey))
864                 return; /* This peer is known as active  */
865
866         if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (nodes_inactive, &peer->hashPubKey))
867                 return; /* This peer is known as inactive  */
868
869         send_experimentation_request (peer);
870 }
871
872
873 /**
874  * Method called whenever a given peer disconnects.
875  *
876  * @param cls closure
877  * @param peer peer identity this notification is about
878  */
879 void core_disconnect_handler (void *cls,
880                            const struct GNUNET_PeerIdentity * peer)
881 {
882         struct Node *n;
883         if (GNUNET_YES == is_me(peer))
884                 return;
885
886         GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Disconnected from peer %s\n"),
887                         GNUNET_i2s (peer));
888
889         if (NULL != (n = GNUNET_CONTAINER_multihashmap_get (nodes_requested, &peer->hashPubKey)))
890                 cleanup_node (nodes_requested, &peer->hashPubKey, n);
891
892         if (NULL != (n = GNUNET_CONTAINER_multihashmap_get (nodes_active, &peer->hashPubKey)))
893                 cleanup_node (nodes_active, &peer->hashPubKey, n);
894
895         if (NULL != (n = GNUNET_CONTAINER_multihashmap_get (nodes_inactive, &peer->hashPubKey)))
896                 cleanup_node (nodes_inactive, &peer->hashPubKey, n);
897 }
898
899
900 /**
901  * Handle a request and send a response
902  *
903  * @param cls unused
904  * @param other the sender
905  * @param message the message
906  * @return GNUNET_OK to keep connection, GNUNET_SYSERR on error
907  */
908 static int
909 core_receive_handler (void *cls,
910                                                                                         const struct GNUNET_PeerIdentity *other,
911                                                                                         const struct GNUNET_MessageHeader *message)
912 {
913         if (ntohs (message->size) < sizeof (struct GNUNET_MessageHeader))
914         {
915                         GNUNET_break (0);
916                         return GNUNET_SYSERR;
917         }
918
919         switch (ntohs (message->type)) {
920                 case GNUNET_MESSAGE_TYPE_EXPERIMENTATION_REQUEST:
921                         handle_request (other, message);
922                         break;
923                 case GNUNET_MESSAGE_TYPE_EXPERIMENTATION_RESPONSE:
924                         handle_response (other, message);
925                         break;
926                 case GNUNET_MESSAGE_TYPE_EXPERIMENTATION_START:
927                         handle_start (other, message);
928                         break;
929                 case GNUNET_MESSAGE_TYPE_EXPERIMENTATION_START_ACK:
930                         handle_start_ack (other, message);
931                         break;
932                 case GNUNET_MESSAGE_TYPE_EXPERIMENTATION_STOP:
933                         handle_stop (other, message);
934                         break;
935                 default:
936                         break;
937         }
938
939         return GNUNET_OK;
940 }
941
942
943 size_t node_experiment_start_cb (void *cls, size_t bufsize, void *buf)
944 {
945         struct NodeComCtx *e_ctx = cls;
946         struct GED_start_message *msg;
947         size_t name_len;
948         size_t size;
949
950         if (NULL == buf)
951                 return 0;
952
953         name_len = strlen(e_ctx->e->name) + 1;
954         size = sizeof (struct GED_start_message) + name_len;
955
956         msg = GNUNET_malloc (size);
957         msg->header.size = htons (size);
958         msg->header.type = htons (GNUNET_MESSAGE_TYPE_EXPERIMENTATION_START);
959         msg->issuer = e_ctx->e->issuer;
960         msg->version_nbo = GNUNET_TIME_absolute_hton(e_ctx->e->version);
961         msg->len_name = htonl (name_len);
962         memcpy (&msg[1], e_ctx->e->name, name_len);
963
964         memcpy (buf, msg, size);
965         GNUNET_free (msg);
966         return size;
967 }
968
969 size_t node_experiment_start_ack_cb (void *cls, size_t bufsize, void *buf)
970 {
971         struct NodeComCtx *e_ctx = cls;
972         struct GED_start_ack_message *msg;
973         size_t name_len;
974         size_t size;
975         if (NULL == buf)
976                 return 0;
977
978         name_len = strlen(e_ctx->e->name) + 1;
979         size = sizeof (struct GED_start_ack_message) + name_len;
980
981         msg = GNUNET_malloc (size);
982         msg->header.size = htons (size);
983         msg->header.type = htons (GNUNET_MESSAGE_TYPE_EXPERIMENTATION_START_ACK);
984         msg->issuer = e_ctx->e->issuer;
985         msg->version_nbo = GNUNET_TIME_absolute_hton(e_ctx->e->version);
986         msg->len_name = htonl (name_len);
987         memcpy (&msg[1], e_ctx->e->name, name_len);
988
989         memcpy (buf, msg, size);
990         GNUNET_free (msg);
991         return size;
992 }
993
994
995
996
997 /**
998  * Confirm a experiment START with a node
999  *
1000  * @return GNUNET_NO if core was busy with sending, GNUNET_OK otherwise
1001  */
1002 int
1003 GED_nodes_send_start_ack (struct Node *n, struct Experiment *e)
1004 {
1005         struct NodeComCtx *e_ctx;
1006
1007         GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Sending %s for experiment request to peer `%s' for experiment `%s'\n"),
1008                         "START_ACK" ,GNUNET_i2s(&n->id), e->name);
1009
1010         e_ctx = GNUNET_malloc (sizeof (struct NodeComCtx));
1011         e_ctx->n = n;
1012         e_ctx->e = e;
1013         e_ctx->size = sizeof (struct GED_start_ack_message) + strlen (e->name) + 1;
1014         e_ctx->notify = &node_experiment_start_ack_cb;
1015         e_ctx->notify_cls = e_ctx;
1016
1017         GNUNET_CONTAINER_DLL_insert_tail (n->e_req_head, n->e_req_tail, e_ctx);
1018         schedule_transmisson (e_ctx);
1019         return GNUNET_OK;
1020 }
1021
1022
1023 /**
1024  * Request a experiment to start with a node
1025  *
1026  * @return GNUNET_NO if core was busy with sending, GNUNET_OK otherwise
1027  */
1028 int
1029 GED_nodes_request_start (struct Node *n, struct Experiment *e)
1030 {
1031         struct NodeComCtx *e_ctx;
1032
1033         GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Sending %s for experiment request to peer `%s' for experiment `%s'\n"),
1034                         "START", GNUNET_i2s(&n->id), e->name);
1035
1036         e_ctx = GNUNET_malloc (sizeof (struct NodeComCtx));
1037         e_ctx->n = n;
1038         e_ctx->e = e;
1039         e_ctx->size = sizeof (struct GED_start_message) + strlen (e->name) + 1;
1040         e_ctx->notify = &node_experiment_start_cb;
1041         e_ctx->notify_cls = e_ctx;
1042
1043         GNUNET_CONTAINER_DLL_insert_tail (n->e_req_head, n->e_req_tail, e_ctx);
1044         schedule_transmisson (e_ctx);
1045         return GNUNET_OK;
1046 }
1047
1048
1049 /**
1050  * Start the nodes management
1051  */
1052 void
1053 GED_nodes_start ()
1054 {
1055         /* Connecting to core service to find partners */
1056         ch = GNUNET_CORE_connect (GED_cfg, NULL,
1057                                                                                                                 &core_startup_handler,
1058                                                                                                                 &core_connect_handler,
1059                                                                                                                 &core_disconnect_handler,
1060                                                                                                                 &core_receive_handler,
1061                                                                                                                 GNUNET_NO, NULL, GNUNET_NO, NULL);
1062         if (NULL == ch)
1063         {
1064                         GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Failed to connect to CORE service!\n"));
1065                         return;
1066         }
1067
1068         nodes_requested = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
1069         nodes_active = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
1070         nodes_inactive = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
1071 }
1072
1073
1074 /**
1075  * Stop the nodes management
1076  */
1077 void
1078 GED_nodes_stop ()
1079 {
1080   if (NULL != nodes_requested)
1081   {
1082                 GNUNET_CONTAINER_multihashmap_iterate (nodes_requested,
1083                                                                                                                                                                          &cleanup_node,
1084                                                                                                                                                                          nodes_requested);
1085                 update_stats (nodes_requested);
1086                 GNUNET_CONTAINER_multihashmap_destroy (nodes_requested);
1087                 nodes_requested = NULL;
1088   }
1089
1090   if (NULL != nodes_active)
1091   {
1092                 GNUNET_CONTAINER_multihashmap_iterate (nodes_active,
1093                                                                                                                                                                          &cleanup_node,
1094                                                                                                                                                                          nodes_active);
1095                 update_stats (nodes_active);
1096                 GNUNET_CONTAINER_multihashmap_destroy (nodes_active);
1097                 nodes_active = NULL;
1098   }
1099
1100   if (NULL != nodes_inactive)
1101   {
1102                 GNUNET_CONTAINER_multihashmap_iterate (nodes_inactive,
1103                                                                                                                                                                          &cleanup_node,
1104                                                                                                                                                                          nodes_inactive);
1105                 update_stats (nodes_inactive);
1106                 GNUNET_CONTAINER_multihashmap_destroy (nodes_inactive);
1107                 nodes_inactive = NULL;
1108   }
1109   if (NULL != ch)
1110   {
1111                 GNUNET_CORE_disconnect (ch);
1112                 ch = NULL;
1113   }
1114 }
1115
1116 /* end of gnunet-daemon-experimentation_nodes.c */