state information for OperationContext
[oweals/gnunet.git] / src / testbed / testbed_api_peers.c
1 /*
2       This file is part of GNUnet
3       (C) 2008--2012 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 testbed/testbed_api_peers.c
23  * @brief management of the knowledge about peers in this library
24  *        (we know the peer ID, its host, pending operations, etc.)
25  * @author Christian Grothoff
26  */
27 #include "platform.h"
28 #include "testbed_api_peers.h"
29 #include "testbed_api.h"
30 #include "testbed.h"
31 #include "testbed_api_hosts.h"
32 #include "testbed_api_operations.h"
33
34 /**
35  * Function to call to start a peer_create type operation once all
36  * queues the operation is part of declare that the
37  * operation can be activated.
38  *
39  * @param cls the closure from GNUNET_TESTBED_operation_create_()
40  */
41 static void 
42 opstart_peer_create (void *cls)
43 {
44   struct OperationContext *opc = cls;
45   struct PeerCreateData *data;
46   struct GNUNET_TESTBED_PeerCreateMessage *msg;
47   char *config;
48   char *xconfig;
49   size_t c_size;
50   size_t xc_size;
51   uint16_t msize;
52
53   GNUNET_assert (OP_PEER_CREATE == opc->type);  
54   data = opc->data;
55   GNUNET_assert (NULL != data);
56   GNUNET_assert (NULL != data->peer);
57   opc->state = OPC_STATE_STARTED;
58   config = GNUNET_CONFIGURATION_serialize (data->cfg, &c_size);
59   xc_size = GNUNET_TESTBED_compress_config_ (config, c_size, &xconfig);
60   GNUNET_free (config);
61   msize = xc_size + sizeof (struct GNUNET_TESTBED_PeerCreateMessage);
62   msg = GNUNET_realloc (xconfig, msize);
63   memmove (&msg[1], msg, xc_size);
64   msg->header.size = htons (msize);
65   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER);
66   msg->operation_id = GNUNET_htonll (opc->id);
67   msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (data->peer->host));
68   msg->peer_id = htonl (data->peer->unique_id);
69   msg->config_size = htonl (c_size);
70   GNUNET_CONTAINER_DLL_insert_tail (opc->c->ocq_head,
71                                     opc->c->ocq_tail, opc);
72   GNUNET_TESTBED_queue_message_ (opc->c,
73                                  (struct GNUNET_MessageHeader *) msg);
74 };
75
76
77 /**
78  * Callback which will be called when peer_create type operation is released
79  *
80  * @param cls the closure from GNUNET_TESTBED_operation_create_()
81  */
82 static void 
83 oprelease_peer_create (void *cls)
84 {
85   struct OperationContext *opc = cls;  
86
87   if (OPC_STATE_FINISHED != opc->state)
88   {
89     GNUNET_free (((struct PeerCreateData *) opc->data)->peer);
90     GNUNET_free (opc->data);
91     GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
92   }
93   GNUNET_free (opc);
94 }
95
96
97 /**
98  * Function to called when a peer destroy operation is ready
99  *
100  * @param cls the closure from GNUNET_TESTBED_operation_create_()
101  */
102 static void 
103 opstart_peer_destroy (void *cls)
104 {
105   struct OperationContext *opc = cls;
106   struct GNUNET_TESTBED_Peer *peer;
107   struct GNUNET_TESTBED_PeerDestroyMessage *msg;
108
109   GNUNET_assert (OP_PEER_DESTROY == opc->type);
110   peer = opc->data;
111   GNUNET_assert (NULL != peer);
112   opc->state = OPC_STATE_STARTED;
113   msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerDestroyMessage));
114   msg->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerDestroyMessage));
115   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER);
116   msg->peer_id = htonl (peer->unique_id);
117   msg->operation_id = GNUNET_htonll (opc->id);
118   GNUNET_CONTAINER_DLL_insert_tail (opc->c->ocq_head,
119                                     opc->c->ocq_tail, opc);
120   GNUNET_TESTBED_queue_message_ (peer->controller, &msg->header);
121 }
122
123
124 /**
125  * Callback which will be called when peer_create type operation is released
126  *
127  * @param cls the closure from GNUNET_TESTBED_operation_create_()
128  */
129 static void 
130 oprelease_peer_destroy (void *cls)
131 {
132   struct OperationContext *opc = cls;
133   
134   if (OPC_STATE_FINISHED != opc->state)
135     GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
136   GNUNET_free (opc);
137 }
138
139
140 /**
141  * Function to called when a peer start operation is ready
142  *
143  * @param cls the closure from GNUNET_TESTBED_operation_create_()
144  */
145 static void 
146 opstart_peer_start (void *cls)
147 {
148   struct OperationContext *opc = cls;
149   struct GNUNET_TESTBED_PeerStartMessage *msg;
150   struct GNUNET_TESTBED_Peer *peer;
151
152   GNUNET_assert (OP_PEER_START == opc->type);
153   GNUNET_assert (NULL != opc->data);
154   peer = opc->data;
155   GNUNET_assert ((PS_CREATED == peer->state) || (PS_STOPPED == peer->state));
156   opc->state = OPC_STATE_STARTED;
157   msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerStartMessage));
158   msg->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerStartMessage));
159   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_STARTPEER);
160   msg->peer_id = htonl (peer->unique_id);
161   msg->operation_id = GNUNET_htonll (opc->id);
162   GNUNET_CONTAINER_DLL_insert_tail (opc->c->ocq_head, opc->c->ocq_tail, opc);
163   GNUNET_TESTBED_queue_message_ (peer->controller, &msg->header);
164 }
165
166
167 /**
168  * Callback which will be called when peer start type operation is released
169  *
170  * @param cls the closure from GNUNET_TESTBED_operation_create_()
171  */
172 static void 
173 oprelease_peer_start (void *cls)
174 {
175   struct OperationContext *opc = cls;
176   
177   if (OPC_STATE_FINISHED != opc->state)
178     GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
179   GNUNET_free (opc);
180 }
181
182
183 /**
184  * Function to called when a peer stop operation is ready
185  *
186  * @param cls the closure from GNUNET_TESTBED_operation_create_()
187  */
188 static void 
189 opstart_peer_stop (void *cls)
190 {
191   struct OperationContext *opc = cls;
192   struct GNUNET_TESTBED_PeerStopMessage *msg;
193   struct GNUNET_TESTBED_Peer *peer;
194
195   GNUNET_assert (NULL != opc->data);
196   peer = opc->data;
197   GNUNET_assert (PS_STARTED == peer->state); 
198   opc->state = OPC_STATE_STARTED;  
199   msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerStopMessage));
200   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_STOPPEER);
201   msg->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerStopMessage));
202   msg->peer_id = htonl (peer->unique_id);
203   msg->operation_id = GNUNET_htonll (opc->id);
204   GNUNET_CONTAINER_DLL_insert_tail (opc->c->ocq_head, opc->c->ocq_tail, opc);
205   GNUNET_TESTBED_queue_message_ (peer->controller, &msg->header);
206 }
207
208
209 /**
210  * Callback which will be called when peer stop type operation is released
211  *
212  * @param cls the closure from GNUNET_TESTBED_operation_create_()
213  */
214 static void 
215 oprelease_peer_stop (void *cls)
216 {
217   struct OperationContext *opc = cls;
218   
219   if (OPC_STATE_FINISHED != opc->state)
220     GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
221   GNUNET_free (opc);
222 }
223
224
225 /**
226  * Function to called when a peer get information operation is ready
227  *
228  * @param cls the closure from GNUNET_TESTBED_operation_create_()
229  */
230 static void 
231 opstart_peer_getinfo (void *cls)
232 {
233   struct OperationContext *opc = cls;
234   struct PeerInfoData *data;  
235   struct GNUNET_TESTBED_PeerGetConfigurationMessage *msg;
236
237   data = opc->data;
238   GNUNET_assert (NULL != data);
239   opc->state = OPC_STATE_STARTED;
240   msg = GNUNET_malloc (sizeof (struct
241                                GNUNET_TESTBED_PeerGetConfigurationMessage));
242   msg->header.size = htons
243     (sizeof (struct GNUNET_TESTBED_PeerGetConfigurationMessage));
244   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG);
245   msg->peer_id = htonl (data->peer->unique_id);
246   msg->operation_id = GNUNET_htonll (opc->id);
247   GNUNET_CONTAINER_DLL_insert_tail (opc->c->ocq_head, opc->c->ocq_tail, opc);
248   GNUNET_TESTBED_queue_message_ (opc->c, &msg->header);
249 }
250
251
252 /**
253  * Callback which will be called when peer stop type operation is released
254  *
255  * @param cls the closure from GNUNET_TESTBED_operation_create_()
256  */
257 static void 
258 oprelease_peer_getinfo (void *cls)
259 {
260   struct OperationContext *opc = cls;
261   struct PeerInfoData2 *data;
262   
263   if (OPC_STATE_FINISHED != opc->state)
264   {
265     GNUNET_free_non_null (opc->data);
266     GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);  
267   }
268   else
269   {
270     data = opc->data;
271     GNUNET_assert (NULL != data);
272     switch (data->pit)
273     {
274     case GNUNET_TESTBED_PIT_CONFIGURATION:
275       GNUNET_CONFIGURATION_destroy (data->details.cfg);
276       break;
277     case GNUNET_TESTBED_PIT_IDENTITY:
278       GNUNET_free (data->details.peer_identity);
279       break;
280     default:
281       GNUNET_assert (0);        /* We should never reach here */
282     }
283     GNUNET_free (data);
284   }
285   GNUNET_free (opc);
286 }
287
288
289
290 /**
291  * Lookup a peer by ID.
292  * 
293  * @param id global peer ID assigned to the peer
294  * @return handle to the host, NULL on error
295  */
296 struct GNUNET_TESTBED_Peer *
297 GNUNET_TESTBED_peer_lookup_by_id_ (uint32_t id)
298 {
299   GNUNET_break (0);
300   return NULL;
301 }
302
303
304 /**
305  * Create the given peer at the specified host using the given
306  * controller.  If the given controller is not running on the target
307  * host, it should find or create a controller at the target host and
308  * delegate creating the peer.  Explicit delegation paths can be setup
309  * using 'GNUNET_TESTBED_controller_link'.  If no explicit delegation
310  * path exists, a direct link with a subordinate controller is setup
311  * for the first delegated peer to a particular host; the subordinate
312  * controller is then destroyed once the last peer that was delegated
313  * to the remote host is stopped.  This function is used in particular
314  * if some other controller has already assigned a unique ID to the
315  * peer.
316  *
317  * Creating the peer only creates the handle to manipulate and further
318  * configure the peer; use "GNUNET_TESTBED_peer_start" and
319  * "GNUNET_TESTBED_peer_stop" to actually start/stop the peer's
320  * processes.
321  *
322  * Note that the given configuration will be adjusted by the
323  * controller to avoid port/path conflicts with other peers.
324  * The "final" configuration can be obtained using
325  * 'GNUNET_TESTBED_peer_get_information'.
326  *
327  * @param unique_id unique ID for this peer
328  * @param controller controller process to use
329  * @param host host to run the peer on
330  * @param cfg Template configuration to use for the peer. Should exist until
331  *          operation is cancelled or GNUNET_TESTBED_operation_done() is called
332  * @param cb the callback to call when the peer has been created
333  * @param cls the closure to the above callback
334  * @return the operation handle
335  */
336 struct GNUNET_TESTBED_Operation *
337 GNUNET_TESTBED_peer_create_with_id_ (uint32_t unique_id,
338                                      struct GNUNET_TESTBED_Controller *controller,
339                                      struct GNUNET_TESTBED_Host *host,
340                                      const struct GNUNET_CONFIGURATION_Handle *cfg,
341                                      GNUNET_TESTBED_PeerCreateCallback cb,
342                                      void *cls)
343 {
344   struct GNUNET_TESTBED_Peer *peer;
345   struct PeerCreateData *data;
346   struct OperationContext *opc;
347
348   peer = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Peer));
349   peer->controller = controller;
350   peer->host = host;
351   peer->unique_id = unique_id;
352   peer->state = PS_INVALID;
353   data = GNUNET_malloc (sizeof (struct PeerCreateData));
354   data->host = host;
355   data->cfg = cfg;
356   data->cb = cb;
357   data->cls = cls;
358   data->peer = peer;
359   opc = GNUNET_malloc (sizeof (struct OperationContext));
360   opc->c = controller;
361   opc->data = data;
362   opc->id = controller->operation_counter++;
363   opc->type = OP_PEER_CREATE;
364   opc->op = GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_create,
365                                               &oprelease_peer_create);
366   GNUNET_TESTBED_operation_queue_insert_ (controller->opq_peer_create, opc->op);
367   return opc->op;
368 }
369
370
371 /**
372  * Create the given peer at the specified host using the given
373  * controller.  If the given controller is not running on the target
374  * host, it should find or create a controller at the target host and
375  * delegate creating the peer.  Explicit delegation paths can be setup
376  * using 'GNUNET_TESTBED_controller_link'.  If no explicit delegation
377  * path exists, a direct link with a subordinate controller is setup
378  * for the first delegated peer to a particular host; the subordinate
379  * controller is then destroyed once the last peer that was delegated
380  * to the remote host is stopped.
381  *
382  * Creating the peer only creates the handle to manipulate and further
383  * configure the peer; use "GNUNET_TESTBED_peer_start" and
384  * "GNUNET_TESTBED_peer_stop" to actually start/stop the peer's
385  * processes.
386  *
387  * Note that the given configuration will be adjusted by the
388  * controller to avoid port/path conflicts with other peers.
389  * The "final" configuration can be obtained using
390  * 'GNUNET_TESTBED_peer_get_information'.
391  *
392  * @param controller controller process to use
393  * @param host host to run the peer on
394  * @param cfg Template configuration to use for the peer. Should exist until
395  *          operation is cancelled or GNUNET_TESTBED_operation_done() is called
396  * @param cb the callback to call when the peer has been created
397  * @param cls the closure to the above callback
398  * @return the operation handle
399  */
400 struct GNUNET_TESTBED_Operation *
401 GNUNET_TESTBED_peer_create (struct GNUNET_TESTBED_Controller *controller,
402                             struct GNUNET_TESTBED_Host *host,
403                             const struct GNUNET_CONFIGURATION_Handle *cfg,
404                             GNUNET_TESTBED_PeerCreateCallback cb,
405                             void *cls)
406 {
407   static uint32_t id_gen;
408
409   return GNUNET_TESTBED_peer_create_with_id_ (++id_gen,
410                                               controller,
411                                               host,
412                                               cfg,
413                                               cb, cls);
414 }
415
416
417 /**
418  * Start the given peer.
419  *
420  * @param peer peer to start
421  * @return handle to the operation
422  */
423 struct GNUNET_TESTBED_Operation *
424 GNUNET_TESTBED_peer_start (struct GNUNET_TESTBED_Peer *peer)
425 {
426   struct OperationContext *opc;
427
428   opc = GNUNET_malloc (sizeof (struct OperationContext));
429   opc->c = peer->controller;
430   opc->data = peer;
431   opc->id = opc->c->operation_counter++;
432   opc->type = OP_PEER_START;
433   opc->op = GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_start,
434                                              &oprelease_peer_start);
435   GNUNET_TESTBED_operation_queue_insert_ (opc->c->opq_peer_create, opc->op);
436   return opc->op;  
437 }
438
439
440 /**
441  * Stop the given peer.  The handle remains valid (use
442  * "GNUNET_TESTBED_peer_destroy" to fully clean up the 
443  * state of the peer).
444  *
445  * @param peer peer to stop
446  * @return handle to the operation
447  */
448 struct GNUNET_TESTBED_Operation *
449 GNUNET_TESTBED_peer_stop (struct GNUNET_TESTBED_Peer *peer)
450 {
451   struct OperationContext *opc;
452
453   opc = GNUNET_malloc (sizeof (struct OperationContext));
454   opc->c = peer->controller;
455   opc->data = peer;
456   opc->id = opc->c->operation_counter++;
457   opc->type = OP_PEER_STOP;
458   opc->op = GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_stop,
459                                               &oprelease_peer_stop);
460   GNUNET_TESTBED_operation_queue_insert_ (opc->c->opq_peer_create, opc->op);
461   return opc->op;
462 }
463
464
465 /**
466  * Request information about a peer.
467  *
468  * @param peer peer to request information about
469  * @param pit desired information
470  * @return handle to the operation
471  */
472 struct GNUNET_TESTBED_Operation *
473 GNUNET_TESTBED_peer_get_information (struct GNUNET_TESTBED_Peer *peer,
474                                      enum GNUNET_TESTBED_PeerInformationType pit)
475 {
476   struct OperationContext *opc;
477   struct PeerInfoData *data;
478
479   GNUNET_assert (GNUNET_TESTBED_PIT_GENERIC != pit);
480   data = GNUNET_malloc (sizeof (struct PeerInfoData));
481   data->peer = peer;
482   data->pit = pit;
483   opc = GNUNET_malloc (sizeof (struct OperationContext));
484   opc->c = peer->controller;
485   opc->data = data;
486   opc->type = OP_PEER_INFO;
487   opc->id = opc->c->operation_counter++;
488   opc->op = GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_getinfo,
489                                              &oprelease_peer_getinfo);
490   GNUNET_TESTBED_operation_queue_insert_ (opc->c->opq_peer_create, opc->op);
491   return opc->op;
492 }
493
494
495 /**
496  * Change peer configuration.  Must only be called while the
497  * peer is stopped.  Ports and paths cannot be changed this
498  * way.
499  *
500  * @param peer peer to change configuration for
501  * @param cfg new configuration (differences to existing
502  *            configuration only)
503  * @return handle to the operation
504  */
505 struct GNUNET_TESTBED_Operation *
506 GNUNET_TESTBED_peer_update_configuration (struct GNUNET_TESTBED_Peer *peer,
507                                           const struct GNUNET_CONFIGURATION_Handle *cfg)
508 {
509   // FIXME: handle locally or delegate...
510   GNUNET_break (0);
511   return NULL;
512 }
513
514
515 /**
516  * Destroy the given peer; the peer should have been
517  * stopped first (if it was started).
518  *
519  * @param peer peer to stop
520  * @return handle to the operation
521  */
522 struct GNUNET_TESTBED_Operation *
523 GNUNET_TESTBED_peer_destroy (struct GNUNET_TESTBED_Peer *peer)
524 {
525   struct OperationContext *opc;
526
527   opc = GNUNET_malloc (sizeof (struct OperationContext));
528   opc->data = peer;
529   opc->c = peer->controller;
530   opc->id = peer->controller->operation_counter++;
531   opc->type = OP_PEER_DESTROY;
532   opc->op = GNUNET_TESTBED_operation_create_ (opc, &opstart_peer_destroy,
533                                               &oprelease_peer_destroy);
534   GNUNET_TESTBED_operation_queue_insert_ (opc->c->opq_peer_create,
535                                           opc->op);
536   return opc->op;
537 }
538
539
540 /**
541  * Manipulate the P2P underlay topology by configuring a link
542  * between two peers.  
543  *
544  * @param op_cls closure argument to give with the operation event
545  * @param p1 first peer
546  * @param p2 second peer
547  * @param co option to change
548  * @param ... option-specific values
549  * @return handle to the operation, NULL if configuring the link at this
550  *         time is not allowed
551  */
552 struct GNUNET_TESTBED_Operation *
553 GNUNET_TESTBED_underlay_configure_link (void *op_cls,
554                                         struct GNUNET_TESTBED_Peer *p1,
555                                         struct GNUNET_TESTBED_Peer *p2,
556                                         enum GNUNET_TESTBED_ConnectOption co, ...)
557 {
558   GNUNET_break (0);
559   return NULL;
560 }
561
562
563
564 /**
565  * Both peers must have been started before calling this function.
566  * This function then obtains a HELLO from 'p1', gives it to 'p2'
567  * and asks 'p2' to connect to 'p1'.
568  *
569  * @param op_cls closure argument to give with the operation event
570  * @param p1 first peer
571  * @param p2 second peer
572  * @return handle to the operation, NULL if connecting these two
573  *         peers is fundamentally not possible at this time (peers
574  *         not running or underlay disallows)
575  */
576 struct GNUNET_TESTBED_Operation *
577 GNUNET_TESTBED_overlay_connect (void *op_cls,
578                                 struct GNUNET_TESTBED_Peer *p1,
579                                 struct GNUNET_TESTBED_Peer *p2)
580 {
581   struct GNUNET_TESTBED_Operation *op;
582   struct OverlayConnectData *data;
583   struct GNUNET_TESTBED_OverlayConnectMessage *msg;
584   
585   GNUNET_assert ((PS_STARTED == p1->state) && (PS_STARTED == p2->state));
586   GNUNET_assert (p1->controller == p2->controller);
587   data = GNUNET_malloc (sizeof (struct OverlayConnectData));
588   data->p1 = p1;
589   data->p2 = p2;  
590   op = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Operation)); 
591   op->controller = p1->controller;
592   op->operation_id = op->controller->operation_counter++;
593   op->type = OP_OVERLAY_CONNECT;
594   op->data = data;
595   msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_OverlayConnectMessage));
596   msg->header.size = htons (sizeof (struct
597                                     GNUNET_TESTBED_OverlayConnectMessage));
598   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_OLCONNECT);
599   msg->peer1 = htonl (p1->unique_id);
600   msg->peer2 = htonl (p2->unique_id);
601   msg->operation_id = GNUNET_htonll (op->operation_id);
602   GNUNET_CONTAINER_DLL_insert_tail (op->controller->op_head,
603                                     op->controller->op_tail, op);
604   GNUNET_TESTBED_queue_message_ (op->controller, 
605                                  (struct GNUNET_MessageHeader *) msg);
606   return NULL;
607 }
608
609
610
611 /* end of testbed_api_peers.c */