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