c2bf2107f1f5e0fef9e9f6118e954e2357fadd15
[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
33
34 /**
35  * Lookup a peer by ID.
36  * 
37  * @param id global peer ID assigned to the peer
38  * @return handle to the host, NULL on error
39  */
40 struct GNUNET_TESTBED_Peer *
41 GNUNET_TESTBED_peer_lookup_by_id_ (uint32_t id)
42 {
43   GNUNET_break (0);
44   return NULL;
45 }
46
47
48 /**
49  * Create the given peer at the specified host using the given
50  * controller.  If the given controller is not running on the target
51  * host, it should find or create a controller at the target host and
52  * delegate creating the peer.  Explicit delegation paths can be setup
53  * using 'GNUNET_TESTBED_controller_link'.  If no explicit delegation
54  * path exists, a direct link with a subordinate controller is setup
55  * for the first delegated peer to a particular host; the subordinate
56  * controller is then destroyed once the last peer that was delegated
57  * to the remote host is stopped.  This function is used in particular
58  * if some other controller has already assigned a unique ID to the
59  * peer.
60  *
61  * Creating the peer only creates the handle to manipulate and further
62  * configure the peer; use "GNUNET_TESTBED_peer_start" and
63  * "GNUNET_TESTBED_peer_stop" to actually start/stop the peer's
64  * processes.
65  *
66  * Note that the given configuration will be adjusted by the
67  * controller to avoid port/path conflicts with other peers.
68  * The "final" configuration can be obtained using
69  * 'GNUNET_TESTBED_peer_get_information'.
70  *
71  * @param unique_id unique ID for this peer
72  * @param controller controller process to use
73  * @param host host to run the peer on
74  * @param cfg configuration to use for the peer
75  * @param cb the callback to call when the peer has been created
76  * @param cls the closure to the above callback
77  * @return the operation handle
78  */
79 struct GNUNET_TESTBED_Operation *
80 GNUNET_TESTBED_peer_create_with_id_ (uint32_t unique_id,
81                                      struct GNUNET_TESTBED_Controller *controller,
82                                      struct GNUNET_TESTBED_Host *host,
83                                      const struct GNUNET_CONFIGURATION_Handle *cfg,
84                                      GNUNET_TESTBED_PeerCreateCallback cb,
85                                      void *cls)
86 {
87   struct GNUNET_TESTBED_Peer *peer;
88   struct PeerCreateData *data;
89   struct GNUNET_TESTBED_Operation *op;
90   struct GNUNET_TESTBED_PeerCreateMessage *msg;
91   char *config;
92   char *xconfig;
93   size_t c_size;
94   size_t xc_size;
95   uint16_t msize;
96
97   peer = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Peer));
98   peer->controller = controller;
99   peer->host = host;
100   peer->unique_id = unique_id;
101   data = GNUNET_malloc (sizeof (struct PeerCreateData));
102   data->cb = cb;
103   data->cls = cls;
104   data->peer = peer;
105   op = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Operation));
106   op->operation_id = controller->operation_counter++;
107   op->type = OP_PEER_CREATE;
108   op->data = data;
109   config = GNUNET_CONFIGURATION_serialize (cfg, &c_size);
110   xc_size = GNUNET_TESTBED_compress_config_ (config, c_size, &xconfig);
111   GNUNET_free (config);
112   msize = xc_size + sizeof (struct GNUNET_TESTBED_PeerCreateMessage);
113   msg = GNUNET_realloc (xconfig, msize);
114   memmove (&msg[1], msg, xc_size); /* Move the compressed config */
115   msg->header.size = htons (msize);
116   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER);
117   msg->operation_id = GNUNET_htonll (op->operation_id);
118   msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (peer->host));
119   msg->peer_id = htonl (peer->unique_id);
120   msg->config_size = htonl (c_size);
121   GNUNET_CONTAINER_DLL_insert_tail (peer->controller->op_head,
122                                     peer->controller->op_tail, op);
123   GNUNET_TESTBED_queue_message_ (controller,
124                                  (struct GNUNET_MessageHeader *) msg);
125   return op;
126 }
127
128
129 /**
130  * Create the given peer at the specified host using the given
131  * controller.  If the given controller is not running on the target
132  * host, it should find or create a controller at the target host and
133  * delegate creating the peer.  Explicit delegation paths can be setup
134  * using 'GNUNET_TESTBED_controller_link'.  If no explicit delegation
135  * path exists, a direct link with a subordinate controller is setup
136  * for the first delegated peer to a particular host; the subordinate
137  * controller is then destroyed once the last peer that was delegated
138  * to the remote host is stopped.
139  *
140  * Creating the peer only creates the handle to manipulate and further
141  * configure the peer; use "GNUNET_TESTBED_peer_start" and
142  * "GNUNET_TESTBED_peer_stop" to actually start/stop the peer's
143  * processes.
144  *
145  * Note that the given configuration will be adjusted by the
146  * controller to avoid port/path conflicts with other peers.
147  * The "final" configuration can be obtained using
148  * 'GNUNET_TESTBED_peer_get_information'.
149  *
150  * @param controller controller process to use
151  * @param host host to run the peer on
152  * @param cfg configuration to use for the peer
153  * @param cb the callback to call when the peer has been created
154  * @param cls the closure to the above callback
155  * @return the operation handle
156  */
157 struct GNUNET_TESTBED_Operation *
158 GNUNET_TESTBED_peer_create (struct GNUNET_TESTBED_Controller *controller,
159                             struct GNUNET_TESTBED_Host *host,
160                             const struct GNUNET_CONFIGURATION_Handle *cfg,
161                             GNUNET_TESTBED_PeerCreateCallback cb,
162                             void *cls)
163 {
164   static uint32_t id_gen;
165
166   return GNUNET_TESTBED_peer_create_with_id_ (++id_gen,
167                                               controller,
168                                               host,
169                                               cfg,
170                                               cb, cls);
171 }
172
173
174 /**
175  * Start the given peer.
176  *
177  * @param peer peer to start
178  * @return handle to the operation
179  */
180 struct GNUNET_TESTBED_Operation *
181 GNUNET_TESTBED_peer_start (struct GNUNET_TESTBED_Peer *peer)
182 {
183   struct GNUNET_TESTBED_Operation *op;
184   struct GNUNET_TESTBED_PeerStartMessage *msg;
185
186   op = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Operation));
187   op->operation_id = peer->controller->operation_counter++;
188   op->type = OP_PEER_START;
189   op->data = peer;
190   msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerStartMessage));
191   msg->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerStartMessage));
192   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_STARTPEER);
193   msg->peer_id = htonl (peer->unique_id);
194   msg->operation_id = GNUNET_htonll (op->operation_id);
195   GNUNET_CONTAINER_DLL_insert_tail (peer->controller->op_head,
196                                     peer->controller->op_tail, op);
197   GNUNET_TESTBED_queue_message_ (peer->controller, &msg->header);
198   return op;
199 }
200
201
202 /**
203  * Stop the given peer.  The handle remains valid (use
204  * "GNUNET_TESTBED_peer_destroy" to fully clean up the 
205  * state of the peer).
206  *
207  * @param peer peer to stop
208  * @return handle to the operation
209  */
210 struct GNUNET_TESTBED_Operation *
211 GNUNET_TESTBED_peer_stop (struct GNUNET_TESTBED_Peer *peer)
212 {
213   struct GNUNET_TESTBED_Operation *op;
214   struct GNUNET_TESTBED_PeerStopMessage *msg;
215
216   op = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Operation));
217   op->operation_id = peer->controller->operation_counter++;
218   op->type = OP_PEER_STOP;
219   op->data = peer;
220   msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerStopMessage));
221   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_STOPPEER);
222   msg->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerStopMessage));
223   msg->peer_id = htonl (peer->unique_id);
224   msg->operation_id = GNUNET_htonll (op->operation_id);
225   GNUNET_CONTAINER_DLL_insert_tail (peer->controller->op_head,
226                                     peer->controller->op_tail, op);
227   GNUNET_TESTBED_queue_message_ (peer->controller, &msg->header);
228   return op;
229 }
230
231
232 /**
233  * Request information about a peer.
234  *
235  * @param peer peer to request information about
236  * @param pit desired information
237  * @return handle to the operation
238  */
239 struct GNUNET_TESTBED_Operation *
240 GNUNET_TESTBED_peer_get_information (struct GNUNET_TESTBED_Peer *peer,
241                                      enum GNUNET_TESTBED_PeerInformationType pit)
242 {
243   struct GNUNET_TESTBED_PeerGetConfigurationMessage *msg;
244   struct GNUNET_TESTBED_Operation *op;
245   struct PeerInfoData *data;
246   
247   GNUNET_assert (GNUNET_TESTBED_PIT_GENERIC != pit);
248   data = GNUNET_malloc (sizeof (struct PeerInfoData));
249   data->peer = peer;
250   data->pit = pit;
251   op = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Operation));
252   op->type = OP_PEER_INFO;
253   op->operation_id = peer->controller->operation_counter++;
254   op->data = data;
255   msg = GNUNET_malloc (sizeof (struct
256                                GNUNET_TESTBED_PeerGetConfigurationMessage));
257   msg->header.size = htons
258     (sizeof (struct GNUNET_TESTBED_PeerGetConfigurationMessage));
259   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG);
260   msg->peer_id = htonl (peer->unique_id);
261   msg->operation_id = GNUNET_htonll (op->operation_id);
262   GNUNET_CONTAINER_DLL_insert_tail (peer->controller->op_head,
263                                     peer->controller->op_tail, op);
264   GNUNET_TESTBED_queue_message_ (peer->controller, &msg->header);
265   return op;
266 }
267
268
269 /**
270  * Change peer configuration.  Must only be called while the
271  * peer is stopped.  Ports and paths cannot be changed this
272  * way.
273  *
274  * @param peer peer to change configuration for
275  * @param cfg new configuration (differences to existing
276  *            configuration only)
277  * @return handle to the operation
278  */
279 struct GNUNET_TESTBED_Operation *
280 GNUNET_TESTBED_peer_update_configuration (struct GNUNET_TESTBED_Peer *peer,
281                                           const struct GNUNET_CONFIGURATION_Handle *cfg)
282 {
283   // FIXME: handle locally or delegate...
284   GNUNET_break (0);
285   return NULL;
286 }
287
288
289 /**
290  * Destroy the given peer; the peer should have been
291  * stopped first (if it was started).
292  *
293  * @param peer peer to stop
294  * @return handle to the operation
295  */
296 struct GNUNET_TESTBED_Operation *
297 GNUNET_TESTBED_peer_destroy (struct GNUNET_TESTBED_Peer *peer)
298 {
299   struct GNUNET_TESTBED_Operation *op;
300   struct PeerDestroyData *data;
301   struct GNUNET_TESTBED_PeerDestroyMessage *msg;
302   
303   data = GNUNET_malloc (sizeof (struct PeerDestroyData));
304   data->peer = peer;
305   op = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Operation));
306   op->operation_id = peer->controller->operation_counter++;
307   op->type = OP_PEER_DESTROY;
308   op->data = data;
309   msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerDestroyMessage));
310   msg->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerDestroyMessage));
311   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER);
312   msg->peer_id = htonl (peer->unique_id);
313   msg->operation_id = GNUNET_htonll (op->operation_id);
314   GNUNET_CONTAINER_DLL_insert_tail (peer->controller->op_head,
315                                     peer->controller->op_tail, op);
316   GNUNET_TESTBED_queue_message_ (peer->controller, 
317                                  (struct GNUNET_MessageHeader *) msg);
318   return op;
319 }
320
321
322 /**
323  * Manipulate the P2P underlay topology by configuring a link
324  * between two peers.  
325  *
326  * @param op_cls closure argument to give with the operation event
327  * @param p1 first peer
328  * @param p2 second peer
329  * @param co option to change
330  * @param ... option-specific values
331  * @return handle to the operation, NULL if configuring the link at this
332  *         time is not allowed
333  */
334 struct GNUNET_TESTBED_Operation *
335 GNUNET_TESTBED_underlay_configure_link (void *op_cls,
336                                         struct GNUNET_TESTBED_Peer *p1,
337                                         struct GNUNET_TESTBED_Peer *p2,
338                                         enum GNUNET_TESTBED_ConnectOption co, ...)
339 {
340   GNUNET_break (0);
341   return NULL;
342 }
343
344
345
346 /**
347  * Both peers must have been started before calling this function.
348  * This function then obtains a HELLO from 'p1', gives it to 'p2'
349  * and asks 'p2' to connect to 'p1'.
350  *
351  * @param op_cls closure argument to give with the operation event
352  * @param p1 first peer
353  * @param p2 second peer
354  * @return handle to the operation, NULL if connecting these two
355  *         peers is fundamentally not possible at this time (peers
356  *         not running or underlay disallows)
357  */
358 struct GNUNET_TESTBED_Operation *
359 GNUNET_TESTBED_overlay_connect (void *op_cls,
360                                 struct GNUNET_TESTBED_Peer *p1,
361                                 struct GNUNET_TESTBED_Peer *p2)
362 {
363   GNUNET_break (0);
364   return NULL;
365 }
366
367
368
369 /* end of testbed_api_peers.c */