testbed peer_create ()
[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  * Details about a peer; kept in a separate struct to avoid bloating
35  * memory consumption everywhere...
36  */
37 struct PeerDetails
38 {
39   /**
40    * Configuration of the peer; NULL if we are not sure what the peer's correct
41    * configuration actually is; non-NULL if this peer is controlled by this
42    * process.
43    */
44   struct GNUNET_CONFIGURATION_Handle *cfg;
45
46   /**
47    * If this process has started this peer's ARM process, this is the handle
48    * to the 'gnunet-service-arm' process of the peer.
49    */
50   struct GNUNET_OS_Process *arm;
51   
52   // ...
53
54 };
55
56
57 /**
58  * A peer controlled by the testing framework.  A peer runs
59  * at a particular host.
60  */ 
61 struct GNUNET_TESTBED_Peer
62 {
63   /**
64    * Our controller context (not necessarily the controller
65    * that is responsible for starting/running the peer!).
66    */
67   struct GNUNET_TESTBED_Controller *controller;
68                            
69   /**
70    * Which host does this peer run on?
71    */
72   struct GNUNET_TESTBED_Host *host;
73
74   /**
75    * Globally unique ID of the peer.
76    */
77   uint32_t unique_id;
78
79   /**
80    * Internals of the peer for the controlling process; NULL if 
81    * this process is not controlling this peer.
82    */
83   struct PeerDetails *details;
84
85 };
86
87
88 /**
89  * Lookup a peer by ID.
90  * 
91  * @param id global peer ID assigned to the peer
92  * @return handle to the host, NULL on error
93  */
94 struct GNUNET_TESTBED_Peer *
95 GNUNET_TESTBED_peer_lookup_by_id_ (uint32_t id)
96 {
97   GNUNET_break (0);
98   return NULL;
99 }
100
101
102 /**
103  * Create the given peer at the specified host using the given
104  * controller.  If the given controller is not running on the target
105  * host, it should find or create a controller at the target host and
106  * delegate creating the peer.  Explicit delegation paths can be setup
107  * using 'GNUNET_TESTBED_controller_link'.  If no explicit delegation
108  * path exists, a direct link with a subordinate controller is setup
109  * for the first delegated peer to a particular host; the subordinate
110  * controller is then destroyed once the last peer that was delegated
111  * to the remote host is stopped.  This function is used in particular
112  * if some other controller has already assigned a unique ID to the
113  * peer.
114  *
115  * Creating the peer only creates the handle to manipulate and further
116  * configure the peer; use "GNUNET_TESTBED_peer_start" and
117  * "GNUNET_TESTBED_peer_stop" to actually start/stop the peer's
118  * processes.
119  *
120  * Note that the given configuration will be adjusted by the
121  * controller to avoid port/path conflicts with other peers.
122  * The "final" configuration can be obtained using
123  * 'GNUNET_TESTBED_peer_get_information'.
124  *
125  * @param unique_id unique ID for this peer
126  * @param controller controller process to use
127  * @param host host to run the peer on
128  * @param cfg configuration to use for the peer
129  * @return handle to the peer (actual startup will happen asynchronously)
130  */
131 struct GNUNET_TESTBED_Peer *
132 GNUNET_TESTBED_peer_create_with_id_ (uint32_t unique_id,
133                                      struct GNUNET_TESTBED_Controller *controller,
134                                      struct GNUNET_TESTBED_Host *host,
135                                      const struct GNUNET_CONFIGURATION_Handle *cfg)
136 {
137   struct GNUNET_TESTBED_Peer *peer;
138   struct GNUNET_TESTBED_PeerCreateMessage *msg;
139   char *config;
140   char *xconfig;
141   size_t c_size;
142   size_t xc_size;
143   uint16_t msize;
144
145   peer = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Peer));
146   peer->controller = controller;
147   peer->host = host;
148   peer->unique_id = unique_id;
149   config = GNUNET_CONFIGURATION_serialize (cfg, &c_size);
150   xc_size = GNUNET_TESTBED_compress_config (config, c_size, &xconfig);
151   GNUNET_free (config);
152   msize = xc_size + sizeof (struct GNUNET_TESTBED_PeerCreateMessage);
153   msg = GNUNET_realloc (xconfig, msize);
154   memmove (&msg[1], msg, sizeof (struct GNUNET_TESTBED_PeerCreateMessage));
155   msg->header.size = htons (msize);
156   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER);
157   msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (peer->host));
158   msg->peer_id = htonl (peer->unique_id);
159   GNUNET_TESTBED_queue_message (controller,
160                                 (struct GNUNET_MessageHeader *) msg);
161   return peer;
162 }
163
164
165 /**
166  * Create the given peer at the specified host using the given
167  * controller.  If the given controller is not running on the target
168  * host, it should find or create a controller at the target host and
169  * delegate creating the peer.  Explicit delegation paths can be setup
170  * using 'GNUNET_TESTBED_controller_link'.  If no explicit delegation
171  * path exists, a direct link with a subordinate controller is setup
172  * for the first delegated peer to a particular host; the subordinate
173  * controller is then destroyed once the last peer that was delegated
174  * to the remote host is stopped.
175  *
176  * Creating the peer only creates the handle to manipulate and further
177  * configure the peer; use "GNUNET_TESTBED_peer_start" and
178  * "GNUNET_TESTBED_peer_stop" to actually start/stop the peer's
179  * processes.
180  *
181  * Note that the given configuration will be adjusted by the
182  * controller to avoid port/path conflicts with other peers.
183  * The "final" configuration can be obtained using
184  * 'GNUNET_TESTBED_peer_get_information'.
185  *
186  * @param controller controller process to use
187  * @param host host to run the peer on
188  * @param cfg configuration to use for the peer
189  * @return handle to the peer (actual startup will happen asynchronously)
190  */
191 struct GNUNET_TESTBED_Peer *
192 GNUNET_TESTBED_peer_create (struct GNUNET_TESTBED_Controller *controller,
193                             struct GNUNET_TESTBED_Host *host,
194                             const struct GNUNET_CONFIGURATION_Handle *cfg)
195 {
196   static uint32_t id_gen;
197
198   return GNUNET_TESTBED_peer_create_with_id_ (++id_gen,
199                                               controller,
200                                               host,
201                                               cfg);
202 }
203
204
205 /**
206  * Start the given peer.
207  *
208  * @param peer peer to start
209  * @return handle to the operation
210  */
211 struct GNUNET_TESTBED_Operation *
212 GNUNET_TESTBED_peer_start (struct GNUNET_TESTBED_Peer *peer)
213 {
214   // FIXME: start locally or delegate...
215   GNUNET_break (0);
216   return NULL;
217 }
218
219
220 /**
221  * Stop the given peer.  The handle remains valid (use
222  * "GNUNET_TESTBED_peer_destroy" to fully clean up the 
223  * state of the peer).
224  *
225  * @param peer peer to stop
226  * @return handle to the operation
227  */
228 struct GNUNET_TESTBED_Operation *
229 GNUNET_TESTBED_peer_stop (struct GNUNET_TESTBED_Peer *peer)
230 {
231   // FIXME: stop locally or delegate...
232   GNUNET_break (0);
233   return NULL;
234 }
235
236
237 /**
238  * Request information about a peer.
239  *
240  * @param peer peer to request information about
241  * @param pit desired information
242  * @return handle to the operation
243  */
244 struct GNUNET_TESTBED_Operation *
245 GNUNET_TESTBED_peer_get_information (struct GNUNET_TESTBED_Peer *peer,
246                                      enum GNUNET_TESTBED_PeerInformationType pit)
247 {
248   // FIXME: handle locally or delegate...
249   GNUNET_break (0);
250   return NULL;
251 }
252
253
254 /**
255  * Change peer configuration.  Must only be called while the
256  * peer is stopped.  Ports and paths cannot be changed this
257  * way.
258  *
259  * @param peer peer to change configuration for
260  * @param cfg new configuration (differences to existing
261  *            configuration only)
262  * @return handle to the operation
263  */
264 struct GNUNET_TESTBED_Operation *
265 GNUNET_TESTBED_peer_update_configuration (struct GNUNET_TESTBED_Peer *peer,
266                                           const struct GNUNET_CONFIGURATION_Handle *cfg)
267 {
268   // FIXME: handle locally or delegate...
269   GNUNET_break (0);
270   return NULL;
271 }
272
273
274 /**
275  * Manipulate the P2P underlay topology by configuring a link
276  * between two peers.  
277  *
278  * @param op_cls closure argument to give with the operation event
279  * @param p1 first peer
280  * @param p2 second peer
281  * @param co option to change
282  * @param ... option-specific values
283  * @return handle to the operation, NULL if configuring the link at this
284  *         time is not allowed
285  */
286 struct GNUNET_TESTBED_Operation *
287 GNUNET_TESTBED_underlay_configure_link (void *op_cls,
288                                         struct GNUNET_TESTBED_Peer *p1,
289                                         struct GNUNET_TESTBED_Peer *p2,
290                                         enum GNUNET_TESTBED_ConnectOption co, ...)
291 {
292   GNUNET_break (0);
293   return NULL;
294 }
295
296
297
298 /**
299  * Both peers must have been started before calling this function.
300  * This function then obtains a HELLO from 'p1', gives it to 'p2'
301  * and asks 'p2' to connect to 'p1'.
302  *
303  * @param op_cls closure argument to give with the operation event
304  * @param p1 first peer
305  * @param p2 second peer
306  * @return handle to the operation, NULL if connecting these two
307  *         peers is fundamentally not possible at this time (peers
308  *         not running or underlay disallows)
309  */
310 struct GNUNET_TESTBED_Operation *
311 GNUNET_TESTBED_overlay_connect (void *op_cls,
312                                 struct GNUNET_TESTBED_Peer *p1,
313                                 struct GNUNET_TESTBED_Peer *p2)
314 {
315   GNUNET_break (0);
316   return NULL;
317 }
318
319
320
321 /* end of testbed_api_peers.c */