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