testbed api peer start
[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  * @return handle to the peer (actual startup will happen asynchronously)
76  */
77 struct GNUNET_TESTBED_Peer *
78 GNUNET_TESTBED_peer_create_with_id_ (uint32_t unique_id,
79                                      struct GNUNET_TESTBED_Controller *controller,
80                                      struct GNUNET_TESTBED_Host *host,
81                                      const struct GNUNET_CONFIGURATION_Handle *cfg)
82 {
83   struct GNUNET_TESTBED_Peer *peer;
84   struct GNUNET_TESTBED_PeerCreateMessage *msg;
85   char *config;
86   char *xconfig;
87   size_t c_size;
88   size_t xc_size;
89   uint16_t msize;
90
91   peer = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Peer));
92   peer->controller = controller;
93   peer->host = host;
94   peer->unique_id = unique_id;
95   config = GNUNET_CONFIGURATION_serialize (cfg, &c_size);
96   xc_size = GNUNET_TESTBED_compress_config_ (config, c_size, &xconfig);
97   GNUNET_free (config);
98   msize = xc_size + sizeof (struct GNUNET_TESTBED_PeerCreateMessage);
99   msg = GNUNET_realloc (xconfig, msize);
100   memmove (&msg[1], msg, xc_size); /* Move the compressed config */
101   msg->header.size = htons (msize);
102   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER);
103   msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (peer->host));
104   msg->peer_id = htonl (peer->unique_id);
105   msg->config_size = htonl (c_size);
106   GNUNET_TESTBED_queue_message_ (controller,
107                                  (struct GNUNET_MessageHeader *) msg);
108   return peer;
109 }
110
111
112 /**
113  * Create the given peer at the specified host using the given
114  * controller.  If the given controller is not running on the target
115  * host, it should find or create a controller at the target host and
116  * delegate creating the peer.  Explicit delegation paths can be setup
117  * using 'GNUNET_TESTBED_controller_link'.  If no explicit delegation
118  * path exists, a direct link with a subordinate controller is setup
119  * for the first delegated peer to a particular host; the subordinate
120  * controller is then destroyed once the last peer that was delegated
121  * to the remote host is stopped.
122  *
123  * Creating the peer only creates the handle to manipulate and further
124  * configure the peer; use "GNUNET_TESTBED_peer_start" and
125  * "GNUNET_TESTBED_peer_stop" to actually start/stop the peer's
126  * processes.
127  *
128  * Note that the given configuration will be adjusted by the
129  * controller to avoid port/path conflicts with other peers.
130  * The "final" configuration can be obtained using
131  * 'GNUNET_TESTBED_peer_get_information'.
132  *
133  * @param controller controller process to use
134  * @param host host to run the peer on
135  * @param cfg configuration to use for the peer
136  * @return handle to the peer (actual startup will happen asynchronously)
137  */
138 struct GNUNET_TESTBED_Peer *
139 GNUNET_TESTBED_peer_create (struct GNUNET_TESTBED_Controller *controller,
140                             struct GNUNET_TESTBED_Host *host,
141                             const struct GNUNET_CONFIGURATION_Handle *cfg)
142 {
143   static uint32_t id_gen;
144
145   return GNUNET_TESTBED_peer_create_with_id_ (++id_gen,
146                                               controller,
147                                               host,
148                                               cfg);
149 }
150
151
152 /**
153  * Start the given peer.
154  *
155  * @param peer peer to start
156  * @return handle to the operation
157  */
158 struct GNUNET_TESTBED_Operation *
159 GNUNET_TESTBED_peer_start (struct GNUNET_TESTBED_Peer *peer)
160 {
161   struct GNUNET_TESTBED_Operation *op;
162   struct GNUNET_TESTBED_PeerStartMessage *msg;
163
164   op = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Operation));
165   op->operation_id = peer->controller->operation_counter++;
166   op->type = OP_PEER_START;
167   op->data = peer;
168   msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerStartMessage));
169   msg->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerStartMessage));
170   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_STARTPEER);
171   msg->peer_id = htonl (peer->unique_id);
172   msg->operation_id = GNUNET_htonll (op->operation_id);
173   GNUNET_CONTAINER_DLL_insert_tail (peer->controller->op_head,
174                                     peer->controller->op_tail, op);
175   GNUNET_TESTBED_queue_message_ (peer->controller, 
176                                  (struct GNUNET_MessageHeader *) msg);
177   return NULL;
178 }
179
180
181 /**
182  * Stop the given peer.  The handle remains valid (use
183  * "GNUNET_TESTBED_peer_destroy" to fully clean up the 
184  * state of the peer).
185  *
186  * @param peer peer to stop
187  * @return handle to the operation
188  */
189 struct GNUNET_TESTBED_Operation *
190 GNUNET_TESTBED_peer_stop (struct GNUNET_TESTBED_Peer *peer)
191 {
192   // FIXME: stop locally or delegate...
193   GNUNET_break (0);
194   return NULL;
195 }
196
197
198 /**
199  * Request information about a peer.
200  *
201  * @param peer peer to request information about
202  * @param pit desired information
203  * @return handle to the operation
204  */
205 struct GNUNET_TESTBED_Operation *
206 GNUNET_TESTBED_peer_get_information (struct GNUNET_TESTBED_Peer *peer,
207                                      enum GNUNET_TESTBED_PeerInformationType pit)
208 {
209   // FIXME: handle locally or delegate...
210   GNUNET_break (0);
211   return NULL;
212 }
213
214
215 /**
216  * Change peer configuration.  Must only be called while the
217  * peer is stopped.  Ports and paths cannot be changed this
218  * way.
219  *
220  * @param peer peer to change configuration for
221  * @param cfg new configuration (differences to existing
222  *            configuration only)
223  * @return handle to the operation
224  */
225 struct GNUNET_TESTBED_Operation *
226 GNUNET_TESTBED_peer_update_configuration (struct GNUNET_TESTBED_Peer *peer,
227                                           const struct GNUNET_CONFIGURATION_Handle *cfg)
228 {
229   // FIXME: handle locally or delegate...
230   GNUNET_break (0);
231   return NULL;
232 }
233
234
235 /**
236  * Destroy the given peer; the peer should have been
237  * stopped first (if it was started).
238  *
239  * @param peer peer to stop
240  * @return handle to the operation
241  */
242 struct GNUNET_TESTBED_Operation *
243 GNUNET_TESTBED_peer_destroy (struct GNUNET_TESTBED_Peer *peer)
244 {
245   struct GNUNET_TESTBED_Operation *op;
246   struct PeerDestroyData *data;
247   struct GNUNET_TESTBED_PeerDestroyMessage *msg;
248   
249   data = GNUNET_malloc (sizeof (struct PeerDestroyData));
250   data->peer = peer;
251   op = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Operation));
252   op->operation_id = peer->controller->operation_counter++;
253   op->type = OP_PEER_DESTROY;
254   op->data = data;
255   msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerDestroyMessage));
256   msg->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerDestroyMessage));
257   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER);
258   msg->peer_id = htonl (peer->unique_id);
259   msg->operation_id = GNUNET_htonll (op->operation_id);
260   GNUNET_CONTAINER_DLL_insert_tail (peer->controller->op_head,
261                                     peer->controller->op_tail, op);
262   GNUNET_TESTBED_queue_message_ (peer->controller, 
263                                  (struct GNUNET_MessageHeader *) msg);
264   return op;
265 }
266
267
268 /**
269  * Manipulate the P2P underlay topology by configuring a link
270  * between two peers.  
271  *
272  * @param op_cls closure argument to give with the operation event
273  * @param p1 first peer
274  * @param p2 second peer
275  * @param co option to change
276  * @param ... option-specific values
277  * @return handle to the operation, NULL if configuring the link at this
278  *         time is not allowed
279  */
280 struct GNUNET_TESTBED_Operation *
281 GNUNET_TESTBED_underlay_configure_link (void *op_cls,
282                                         struct GNUNET_TESTBED_Peer *p1,
283                                         struct GNUNET_TESTBED_Peer *p2,
284                                         enum GNUNET_TESTBED_ConnectOption co, ...)
285 {
286   GNUNET_break (0);
287   return NULL;
288 }
289
290
291
292 /**
293  * Both peers must have been started before calling this function.
294  * This function then obtains a HELLO from 'p1', gives it to 'p2'
295  * and asks 'p2' to connect to 'p1'.
296  *
297  * @param op_cls closure argument to give with the operation event
298  * @param p1 first peer
299  * @param p2 second peer
300  * @return handle to the operation, NULL if connecting these two
301  *         peers is fundamentally not possible at this time (peers
302  *         not running or underlay disallows)
303  */
304 struct GNUNET_TESTBED_Operation *
305 GNUNET_TESTBED_overlay_connect (void *op_cls,
306                                 struct GNUNET_TESTBED_Peer *p1,
307                                 struct GNUNET_TESTBED_Peer *p2)
308 {
309   GNUNET_break (0);
310   return NULL;
311 }
312
313
314
315 /* end of testbed_api_peers.c */