75a568cf7d65d1f800cc8007effa7549ed5ee485
[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 2, 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
28 #include "platform.h"
29 #include "gnunet_common.h"
30 #include "gnunet_getopt_lib.h"
31 #include "gnunet_hello_lib.h"
32 #include "gnunet_os_lib.h"
33 #include "gnunet_program_lib.h"
34 #include "gnunet_transport_service.h"
35
36 #define GNUNET_TRANSPORT_TESTING_ConnectRequest void *
37
38 /**
39  * Context for a single peer
40  */
41 struct PeerContext
42 {
43   struct GNUNET_CONFIGURATION_Handle *cfg;
44
45   struct GNUNET_TRANSPORT_Handle *th;
46
47   struct GNUNET_TRANSPORT_GetHelloHandle *ghh;
48
49   struct GNUNET_PeerIdentity id;
50
51   struct GNUNET_OS_Process *arm_proc;
52
53   GNUNET_TRANSPORT_ReceiveCallback rec;
54
55   GNUNET_TRANSPORT_NotifyConnect nc;
56
57   GNUNET_TRANSPORT_NotifyDisconnect nd;
58
59   void *cb_cls;
60
61   char *servicehome;
62
63   unsigned int no;
64 };
65
66 /**
67  * Callback when two peers are connected and both have called the connect callback
68  * to notify clients about a new peer
69  */
70 typedef void (*GNUNET_TRANSPORT_TESTING_connect_cb) (struct PeerContext * p1,
71                                                      struct PeerContext * p2,
72                                                      void *cls);
73
74
75 /**
76  * Start a peer with the given configuration
77  * @param rec receive callback
78  * @param nc connect callback
79  * @param nd disconnect callback
80  * @param cb_cls closure for callback
81  *   if NULL passed the PeerContext * will be used!
82  * @return the peer context
83  */
84 struct PeerContext *
85 GNUNET_TRANSPORT_TESTING_start_peer (const char *cfgname,
86                                      GNUNET_TRANSPORT_ReceiveCallback rec,
87                                      GNUNET_TRANSPORT_NotifyConnect nc,
88                                      GNUNET_TRANSPORT_NotifyDisconnect nd,
89                                      void *cb_cls);
90
91
92 /**
93  * shutdown the given peer
94  * @param p the peer
95  */
96
97 void
98 GNUNET_TRANSPORT_TESTING_stop_peer (struct PeerContext *pc);
99
100
101 /**
102  * Connect the given peers and call the callback when both peers report the
103  * inbound connection. Remarks: start_peer's notify_connect callback can be called
104  * before.
105  * @param p1 peer 1
106  * @param p2 peer 2
107  * @param cb the callback to call
108  * @param cb_cls callback cls
109  * @return a connect request handle
110  */
111 GNUNET_TRANSPORT_TESTING_ConnectRequest
112 GNUNET_TRANSPORT_TESTING_connect_peers (struct PeerContext *p1,
113                                         struct PeerContext *p2,
114                                         GNUNET_TRANSPORT_TESTING_connect_cb cb,
115                                         void *cls);
116
117 /**
118  * Cancel the request to connect two peers
119  * Tou MUST cancel the request if you stop the peers before the peers connected succesfully
120  * @param cc a connect request handle
121  */
122 void
123 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (void *cc);
124
125 /*
126  * Some utility functions
127  */
128
129 /**
130  * Extracts the test filename from an absolute file name and removes the extension
131  * @param file absolute file name
132  * @param dest where to store result
133  */
134 void
135 GNUNET_TRANSPORT_TESTING_get_test_name (const char *file, char **dest);
136
137 /**
138  * This function takes the filename (e.g. argv[0), removes a "lt-"-prefix and
139  * if existing ".exe"-prefix and adds the peer-number
140  * @param file filename of the test, e.g. argv[0]
141  * @param cfgname where to write the result
142  * @param count peer number
143  */
144 void
145 GNUNET_TRANSPORT_TESTING_get_config_name (const char *file, char **cfgname,
146                                           int count);
147
148
149 /**
150  * Extracts the plugin anme from an absolute file name and the test name
151  * @param file absolute file name
152  * @param test test name
153  * @param dest where to store result
154  */
155 void
156 GNUNET_TRANSPORT_TESTING_get_test_plugin_name (const char *executable,
157                                                const char *testname,
158                                                char **pluginname);
159
160
161 /**
162  * Extracts the filename from an absolute file name and removes the extenstion
163  * @param file absolute file name
164  * @param dest where to store result
165  */
166 void
167 GNUNET_TRANSPORT_TESTING_get_test_source_name (const char *file,
168                                                char **testname);
169
170 /* end of transport_testing.h */