-doxygen
[oweals/gnunet.git] / src / transport / transport-testing.h
1 /*
2      This file is part of GNUnet.
3      (C) 2006, 2009 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 transport-testing.h
23  * @brief testing lib for transport service
24  *
25  * @author Matthias Wachs
26  */
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_hello_lib.h"
30 #include "gnunet_transport_service.h"
31 #include "gnunet_testing_lib.h"
32
33
34 #define GNUNET_TRANSPORT_TESTING_ConnectRequest void *
35
36
37 /**
38  * Context for a single peer
39  */
40 struct PeerContext;
41
42 /**
43  * Callback when two peers are connected and both have called the connect callback
44  * to notify clients about a new peer
45  */
46 typedef void (*GNUNET_TRANSPORT_TESTING_start_cb) (struct PeerContext * p,
47                                                    void *cls);
48
49 /**
50  * Callback when two peers are connected and both have called the connect callback
51  * to notify clients about a new peer
52  */
53 typedef void (*GNUNET_TRANSPORT_TESTING_connect_cb) (struct PeerContext * p1,
54                                                      struct PeerContext * p2,
55                                                      void *cls);
56
57
58 /**
59  * Definition for a transport testing handle
60  */
61 struct GNUNET_TRANSPORT_TESTING_handle;
62
63 /**
64  * Context for a single peer
65  */
66 struct PeerContext
67 {
68   /**
69    * Next element in the DLL
70    */
71   struct PeerContext *next;
72
73   /**
74    * Previous element in the DLL
75    */
76   struct PeerContext *prev;
77
78   /**
79    * Transport testing handle this peer belongs to
80    */
81   struct GNUNET_TRANSPORT_TESTING_handle *tth;
82
83   /**
84    * Peer's configuration
85    */
86   struct GNUNET_CONFIGURATION_Handle *cfg;
87
88   /**
89    * Peer's transport service handle
90    */
91   struct GNUNET_TRANSPORT_Handle *th;
92
93   /**
94    * Peer's transport get hello handle to retrieve peer's HELLO message
95    */
96   struct GNUNET_TRANSPORT_GetHelloHandle *ghh;
97
98   /**
99    * Peer's testing handle
100    */
101   struct GNUNET_TESTING_Peer *peer;
102
103   /**
104    * Peer identity
105    */
106   struct GNUNET_PeerIdentity id;
107
108   /**
109    * Handle for the peer's ARM process
110    */
111   struct GNUNET_OS_Process *arm_proc;
112
113   /**
114    * Receive callback
115    */
116   GNUNET_TRANSPORT_ReceiveCallback rec;
117
118   /**
119    * Notify connect callback
120    */
121   GNUNET_TRANSPORT_NotifyConnect nc;
122
123   /**
124    * Notify disconnect callback
125    */
126   GNUNET_TRANSPORT_NotifyDisconnect nd;
127
128   /**
129    * Startup completed callback
130    */
131   GNUNET_TRANSPORT_TESTING_start_cb start_cb;
132
133   /**
134    * Peers HELLO Message
135    */
136   struct GNUNET_HELLO_Message *hello;
137
138   /**
139    * Closure for the callbacks
140    */
141   void *cb_cls;
142
143   /**
144    * An unique number to identify the peer
145    */
146   unsigned int no;
147 };
148
149
150 struct ConnectingContext
151 {
152   struct ConnectingContext *next;
153   struct ConnectingContext *prev;
154   struct PeerContext *p1;
155   struct PeerContext *p2;
156   GNUNET_SCHEDULER_TaskIdentifier tct;
157   GNUNET_TRANSPORT_TESTING_connect_cb cb;
158   void *cb_cls;
159   struct GNUNET_TRANSPORT_Handle *th_p1;
160   struct GNUNET_TRANSPORT_Handle *th_p2;
161   int p1_c;
162   int p2_c;
163 };
164
165 struct GNUNET_TRANSPORT_TESTING_handle
166 {
167   /**
168    * Testing library system handle
169    */
170   struct GNUNET_TESTING_System *tl_system;
171
172   /**
173    * head DLL of connect contexts
174    */
175   struct ConnectingContext *cc_head;
176
177   /**
178    * head DLL of connect contexts
179    */
180   struct ConnectingContext *cc_tail;
181
182   /**
183    * head DLL of peers
184    */
185   struct PeerContext *p_head;
186
187   /**
188    * tail DLL of peers
189    */
190   struct PeerContext *p_tail;
191 };
192
193
194 /**
195 * Start a peer with the given configuration
196 * @param tth the testing handle
197 * @param cfgname configuration file
198 * @param peer_id the peer_id
199 * @param rec receive callback
200 * @param nc connect callback
201 * @param nd disconnect callback
202 * @param start_cb start callback
203 * @param cb_cls closure for callback
204 * @return the peer context
205 */
206 struct PeerContext *
207 GNUNET_TRANSPORT_TESTING_start_peer (struct GNUNET_TRANSPORT_TESTING_handle
208                                      *tth, const char *cfgname, int peer_id,
209                                      GNUNET_TRANSPORT_ReceiveCallback rec,
210                                      GNUNET_TRANSPORT_NotifyConnect nc,
211                                      GNUNET_TRANSPORT_NotifyDisconnect nd,
212                                      GNUNET_TRANSPORT_TESTING_start_cb start_cb,
213                                      void *cb_cls);
214
215
216 /**
217  * shutdown the given peer
218  * @param tth the testing handle
219  * @param p the peer
220  */
221
222 void
223 GNUNET_TRANSPORT_TESTING_stop_peer (struct GNUNET_TRANSPORT_TESTING_handle *tth,
224                                     struct PeerContext *pc);
225
226
227 /**
228 * Restart the given peer
229 * @param tth testing handle
230 * @param p the peer
231 * @param cfgname the cfg file used to restart
232 * @param restart_cb restart callback
233 * @param cb_cls callback closure
234 * @return GNUNET_OK in success otherwise GNUNET_SYSERR
235 */
236 int
237 GNUNET_TRANSPORT_TESTING_restart_peer (struct GNUNET_TRANSPORT_TESTING_handle
238                                        *tth, struct PeerContext *p,
239                                        const char *cfgname,
240                                        GNUNET_TRANSPORT_TESTING_start_cb
241                                        restart_cb, void *cb_cls);
242
243 /**
244  * Connect the given peers and call the callback when both peers report the
245  * inbound connection. Remarks: start_peer's notify_connect callback can be called
246  * before.
247  *
248  * @param tth transport testing handle
249  * @param p1 peer 1
250  * @param p2 peer 2
251  * @param cb the callback to call when both peers notified that they are connected
252  * @param cls callback cls
253  * @return a connect request handle
254  */
255 GNUNET_TRANSPORT_TESTING_ConnectRequest
256 GNUNET_TRANSPORT_TESTING_connect_peers (struct GNUNET_TRANSPORT_TESTING_handle *tth,
257                                         struct PeerContext *p1,
258                                         struct PeerContext *p2,
259                                         GNUNET_TRANSPORT_TESTING_connect_cb cb,
260                                         void *cls);
261
262 /**
263  * Cancel the request to connect two peers
264  * Tou MUST cancel the request if you stop the peers before the peers connected succesfully
265  * @param tth testing
266  * @param ccr a connect request handle
267  */
268 void
269 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (struct
270                                                GNUNET_TRANSPORT_TESTING_handle
271                                                *tth,
272                                                GNUNET_TRANSPORT_TESTING_ConnectRequest
273                                                ccr);
274
275 /**
276  * Clean up the transport testing
277  * @param tth transport testing handle
278  */
279 void
280 GNUNET_TRANSPORT_TESTING_done (struct GNUNET_TRANSPORT_TESTING_handle *tth);
281
282 /**
283  * Initialize the transport testing
284  * @return transport testing handle
285  */
286 struct GNUNET_TRANSPORT_TESTING_handle *
287 GNUNET_TRANSPORT_TESTING_init ();
288
289 /*
290  * Some utility functions
291  */
292
293 /**
294  * Extracts the test filename from an absolute file name and removes the extension
295  * @param file absolute file name
296  * @param dest where to store result
297  */
298 void
299 GNUNET_TRANSPORT_TESTING_get_test_name (const char *file, char **dest);
300
301 /**
302  * This function takes the filename (e.g. argv[0), removes a "lt-"-prefix and
303  * if existing ".exe"-prefix and adds the peer-number
304  *
305  * @param file filename of the test, e.g. argv[0]
306  * @param dest where to write the filename
307  * @param count peer number
308  */
309 void
310 GNUNET_TRANSPORT_TESTING_get_config_name (const char *file, char **dest,
311                                           int count);
312
313
314 /**
315  * Extracts the plugin anme from an absolute file name and the test name
316  * @param file absolute file name
317  * @param test test name
318  * @param dest where to store result
319  */
320 void
321 GNUNET_TRANSPORT_TESTING_get_test_plugin_name (const char *executable,
322                                                const char *testname,
323                                                char **pluginname);
324
325
326 /**
327  * Extracts the filename from an absolute file name and removes the extenstion
328  * @param file absolute file name
329  * @param dest where to store result
330  */
331 void
332 GNUNET_TRANSPORT_TESTING_get_test_source_name (const char *file,
333                                                char **testname);
334
335 /* end of transport_testing.h */