-tests
[oweals/gnunet.git] / src / testbed / testbed_api_services.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_services.c
23  * @brief convenience functions for accessing services
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "testbed_api.h"
28 #include "testbed_api_peers.h"
29 #include "testbed_api_operations.h"
30
31
32 /**
33  * States for Service connect operations
34  */
35 enum State
36 {
37     /**
38      * Initial state
39      */
40   INIT,
41
42     /**
43      * The configuration request has been sent
44      */
45   CFG_REQUEST_QUEUED,
46
47     /**
48      * connected to service
49      */
50   SERVICE_CONNECTED,
51
52 };
53
54
55 /**
56  * Data accessed during service connections
57  */
58 struct ServiceConnectData
59 {
60   /**
61    * helper function callback to establish the connection
62    */
63   GNUNET_TESTBED_ConnectAdapter ca;
64
65   /**
66    * helper function callback to close the connection
67    */
68   GNUNET_TESTBED_DisconnectAdapter da;
69
70   /**
71    * Closure to the above callbacks
72    */
73   void *cada_cls;
74
75   /**
76    * Service name
77    */
78   char *service_name;
79
80   /**
81    * Closure for operation event
82    */
83   void *op_cls;
84
85   /**
86    * The operation which created this structure
87    */
88   struct GNUNET_TESTBED_Operation *operation;
89
90   /**
91    * The operation context from GNUNET_TESTBED_forward_operation_msg_()
92    */
93   struct OperationContext *opc;
94
95   /**
96    * The peer handle
97    */
98   struct GNUNET_TESTBED_Peer *peer;
99
100   /**
101    * The acquired configuration of the peer
102    */
103   struct GNUNET_CONFIGURATION_Handle *cfg;
104
105   /**
106    * The op_result pointer from ConnectAdapter
107    */
108   void *op_result;
109
110   /**
111    * The operation completion callback
112    */
113   GNUNET_TESTBED_ServiceConnectCompletionCallback cb;
114
115   /**
116    * The closure for operation completion callback
117    */
118   void *cb_cls;
119
120   /**
121    * State information
122    */
123   enum State state;
124
125 };
126
127
128 /**
129  * Type of a function to call when we receive a message
130  * from the service.
131  *
132  * @param cls ServiceConnectData
133  * @param msg message received, NULL on timeout or fatal error
134  */
135 static void
136 configuration_receiver (void *cls, const struct GNUNET_MessageHeader *msg)
137 {
138   struct ServiceConnectData *data = cls;
139   const struct GNUNET_TESTBED_PeerConfigurationInformationMessage *imsg;
140   struct GNUNET_TESTBED_Controller *c;
141   const char *emsg;
142   struct GNUNET_TESTBED_EventInformation info;
143   uint16_t mtype;
144
145   mtype = ntohs (msg->type);
146   emsg = NULL;
147   info.type = GNUNET_TESTBED_ET_OPERATION_FINISHED;
148   info.details.operation_finished.operation = data->operation;
149   info.details.operation_finished.op_cls = data->op_cls;
150   if (GNUNET_MESSAGE_TYPE_TESTBED_OPERATIONFAILEVENT == mtype)
151   {
152     emsg = GNUNET_TESTBED_parse_error_string_ ((const struct
153                                                 GNUNET_TESTBED_OperationFailureEventMessage
154                                                 *) msg);
155     if (NULL == emsg)
156       emsg = "Unknown error";
157     info.details.operation_finished.emsg = emsg;
158     info.details.operation_finished.generic = NULL;
159     goto call_cb;
160   }  
161   imsg =
162       (const struct GNUNET_TESTBED_PeerConfigurationInformationMessage *) msg;
163   data->cfg = GNUNET_TESTBED_get_config_from_peerinfo_msg_ (imsg);
164   GNUNET_assert (NULL == data->op_result);
165   data->op_result = data->ca (data->cada_cls, data->cfg);  
166   info.details.operation_finished.emsg = NULL;
167   info.details.operation_finished.generic = data->op_result;
168   c = data->peer->controller;
169   data->state = SERVICE_CONNECTED;
170   
171  call_cb:
172   if ((0 != (GNUNET_TESTBED_ET_OPERATION_FINISHED & c->event_mask)) &&
173       (NULL != c->cc))
174     c->cc (c->cc_cls, &info);
175   if (NULL != data->cb)
176     data->cb (data->cb_cls, data->operation, data->op_result, emsg);
177 }
178
179
180 /**
181  * Function called when a service connect operation is ready
182  *
183  * @param cls the closure from GNUNET_TESTBED_operation_create_()
184  */
185 static void
186 opstart_service_connect (void *cls)
187 {
188   struct ServiceConnectData *data = cls;
189   struct GNUNET_TESTBED_PeerGetConfigurationMessage *msg;
190   struct GNUNET_TESTBED_Controller *c;
191   uint64_t op_id;
192
193   GNUNET_assert (NULL != data);
194   GNUNET_assert (NULL != data->peer);
195   c = data->peer->controller;
196   op_id = c->operation_counter++;
197   msg =
198       GNUNET_TESTBED_generate_peergetconfig_msg_ (data->peer->unique_id, op_id);
199   data->opc =
200       GNUNET_TESTBED_forward_operation_msg_ (c, op_id, &msg->header,
201                                              &configuration_receiver, data);
202   GNUNET_free (msg);
203   data->state = CFG_REQUEST_QUEUED;
204 }
205
206
207 /**
208  * Callback which will be called when service connect type operation is
209  * released
210  *
211  * @param cls the closure from GNUNET_TESTBED_operation_create_()
212  */
213 static void
214 oprelease_service_connect (void *cls)
215 {
216   struct ServiceConnectData *data = cls;
217
218   switch (data->state)
219   {
220   case INIT:
221     break;
222   case CFG_REQUEST_QUEUED:
223     GNUNET_assert (NULL != data->opc);
224     GNUNET_TESTBED_forward_operation_msg_cancel_ (data->opc);
225     break;
226   case SERVICE_CONNECTED:
227     GNUNET_assert (NULL != data->cfg);
228     GNUNET_CONFIGURATION_destroy (data->cfg);
229     if (NULL != data->da)
230       data->da (data->cada_cls, data->op_result);
231     break;
232   }
233   GNUNET_free (data);
234 }
235
236
237 /**
238  * Connect to a service offered by the given peer.  Will ensure that
239  * the request is queued to not overwhelm our ability to create and
240  * maintain connections with other systems.  The actual service
241  * handle is then returned via the 'op_result' member in the event
242  * callback.  The 'ca' callback is used to create the connection
243  * when the time is right; the 'da' callback will be used to 
244  * destroy the connection (upon 'GNUNET_TESTBED_operation_done').
245  * 'GNUNET_TESTBED_operation_cancel' can be used to abort this
246  * operation until the event callback has been called.
247  *
248  * @param op_cls closure to pass in operation event
249  * @param peer peer that runs the service
250  * @param service_name name of the service to connect to
251  * @param cb the callback to call when this operation finishes
252  * @param cb_cls closure for the above callback
253  * @param ca helper function to establish the connection
254  * @param da helper function to close the connection
255  * @param cada_cls closure for ca and da
256  * @return handle for the operation
257  */
258 struct GNUNET_TESTBED_Operation *
259 GNUNET_TESTBED_service_connect (void *op_cls,
260                                 struct GNUNET_TESTBED_Peer *peer,
261                                 const char *service_name,
262                                 GNUNET_TESTBED_ServiceConnectCompletionCallback cb,
263                                 void *cb_cls,
264                                 GNUNET_TESTBED_ConnectAdapter ca,
265                                 GNUNET_TESTBED_DisconnectAdapter da,
266                                 void *cada_cls)
267 {
268   struct ServiceConnectData *data;
269
270   data = GNUNET_malloc (sizeof (struct ServiceConnectData));
271   data->ca = ca;
272   data->da = da;
273   data->cada_cls = cada_cls;
274   data->op_cls = op_cls;
275   data->peer = peer;
276   data->state = INIT;
277   data->cb = cb;
278   data->cb_cls = cb_cls;
279   data->operation =
280       GNUNET_TESTBED_operation_create_ (data, &opstart_service_connect,
281                                         &oprelease_service_connect);
282   GNUNET_TESTBED_operation_queue_insert_ (peer->
283                                           controller->opq_parallel_service_connections,
284                                           data->operation);
285   GNUNET_TESTBED_operation_queue_insert_ (peer->
286                                           controller->opq_parallel_operations,
287                                           data->operation);
288   return data->operation;
289 }
290
291 /* end of testbed_api_services.c */