test working, but fails
[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
57
58 /**
59  * Data accessed during service connections
60  */
61 struct ServiceConnectData
62 {
63   /**
64    * helper function callback to establish the connection
65    */
66   GNUNET_TESTBED_ConnectAdapter ca;
67
68   /**
69    * helper function callback to close the connection
70    */
71   GNUNET_TESTBED_DisconnectAdapter da;
72
73   /**
74    * Closure to the above callbacks
75    */
76   void *cada_cls;
77
78   /**
79    * Service name
80    */
81   char *service_name;
82
83   /**
84    * Closure for operation event
85    */
86   void *op_cls;
87
88   /**
89    * The operation which created this structure
90    */
91   struct GNUNET_TESTBED_Operation *operation;
92
93   /**
94    * The operation context from GNUNET_TESTBED_forward_operation_msg_()
95    */
96   struct OperationContext *opc;
97
98   /**
99    * The peer handle
100    */
101   struct GNUNET_TESTBED_Peer *peer;
102
103   /**
104    * The acquired configuration of the peer
105    */
106   struct GNUNET_CONFIGURATION_Handle *cfg;
107
108   /**
109    * The op_result pointer from ConnectAdapter
110    */
111   void *op_result;
112
113   /**
114    * State information
115    */
116   enum State state;
117
118 };
119
120
121 /**
122  * Type of a function to call when we receive a message
123  * from the service.
124  *
125  * @param cls ServiceConnectData
126  * @param msg message received, NULL on timeout or fatal error
127  */
128 static void
129 configuration_receiver (void *cls, const struct GNUNET_MessageHeader *msg)
130 {
131   struct ServiceConnectData *data = cls;
132   const struct GNUNET_TESTBED_PeerConfigurationInformationMessage *imsg;
133   struct GNUNET_TESTBED_Controller *c;
134   struct GNUNET_TESTBED_EventInformation info;
135
136   imsg =
137       (const struct GNUNET_TESTBED_PeerConfigurationInformationMessage *) msg;
138   data->cfg = GNUNET_TESTBED_get_config_from_peerinfo_msg_ (imsg);
139   data->op_result = data->ca (data->cada_cls, data->cfg);
140   info.type = GNUNET_TESTBED_ET_OPERATION_FINISHED;
141   info.details.operation_finished.operation = data->operation;
142   info.details.operation_finished.op_cls = data->op_cls;
143   info.details.operation_finished.emsg = NULL;
144   info.details.operation_finished.pit = GNUNET_TESTBED_PIT_GENERIC;
145   info.details.operation_finished.op_result.generic = data->op_result;
146   c = data->peer->controller;
147   data->state = SERVICE_CONNECTED;
148   if ((0 != (GNUNET_TESTBED_ET_OPERATION_FINISHED & c->event_mask)) &&
149       (NULL != c->cc))
150     c->cc (c->cc_cls, &info);
151 }
152
153
154 /**
155  * Function called when a service connect operation is ready
156  *
157  * @param cls the closure from GNUNET_TESTBED_operation_create_()
158  */
159 static void
160 opstart_service_connect (void *cls)
161 {
162   struct ServiceConnectData *data = cls;
163   struct GNUNET_TESTBED_PeerGetConfigurationMessage *msg;
164   struct GNUNET_TESTBED_Controller *c;
165   uint64_t op_id;
166
167   GNUNET_assert (NULL != data);
168   GNUNET_assert (NULL != data->peer);
169   c = data->peer->controller;
170   op_id = c->operation_counter++;
171   msg =
172       GNUNET_TESTBED_generate_peergetconfig_msg_ (data->peer->unique_id, op_id);
173   data->opc =
174       GNUNET_TESTBED_forward_operation_msg_ (c, op_id, &msg->header,
175                                              &configuration_receiver, data);
176   GNUNET_free (msg);
177   data->state = CFG_REQUEST_QUEUED;
178 }
179
180
181 /**
182  * Callback which will be called when service connect type operation is
183  * released
184  *
185  * @param cls the closure from GNUNET_TESTBED_operation_create_()
186  */
187 static void
188 oprelease_service_connect (void *cls)
189 {
190   struct ServiceConnectData *data = cls;
191
192   switch (data->state)
193   {
194   case INIT:
195     break;
196   case CFG_REQUEST_QUEUED:
197     GNUNET_assert (NULL != data->opc);
198     GNUNET_TESTBED_forward_operation_msg_cancel_ (data->opc);
199     break;
200   case SERVICE_CONNECTED:
201     GNUNET_assert (NULL != data->cfg);
202     GNUNET_CONFIGURATION_destroy (data->cfg);
203     if (NULL != data->da)
204       data->da (data->cada_cls, data->op_result);
205     break;
206   }
207   GNUNET_free (data);
208 }
209
210
211 /**
212  * Connect to a service offered by the given peer.  Will ensure that
213  * the request is queued to not overwhelm our ability to create and
214  * maintain connections with other systems.  The actual service
215  * handle is then returned via the 'op_result' member in the event
216  * callback.  The 'ca' callback is used to create the connection
217  * when the time is right; the 'da' callback will be used to
218  * destroy the connection (upon 'GNUNET_TESTBED_operation_done').
219  * 'GNUNET_TESTBED_operation_cancel' can be used to abort this
220  * operation until the event callback has been called.
221  *
222  * @param op_cls closure to pass in operation event
223  * @param peer peer that runs the service
224  * @param service_name name of the service to connect to
225  * @param ca helper function to establish the connection
226  * @param da helper function to close the connection
227  * @param cada_cls closure for ca and da
228  * @return handle for the operation
229  */
230 struct GNUNET_TESTBED_Operation *
231 GNUNET_TESTBED_service_connect (void *op_cls, struct GNUNET_TESTBED_Peer *peer,
232                                 const char *service_name,
233                                 GNUNET_TESTBED_ConnectAdapter ca,
234                                 GNUNET_TESTBED_DisconnectAdapter da,
235                                 void *cada_cls)
236 {
237   struct ServiceConnectData *data;
238
239   data = GNUNET_malloc (sizeof (struct ServiceConnectData));
240   data->ca = ca;
241   data->da = da;
242   data->cada_cls = cada_cls;
243   data->op_cls = op_cls;
244   data->peer = peer;
245   data->state = INIT;
246   data->operation =
247       GNUNET_TESTBED_operation_create_ (data, &opstart_service_connect,
248                                         &oprelease_service_connect);
249   GNUNET_TESTBED_operation_queue_insert_ (peer->
250                                           controller->opq_parallel_service_connections,
251                                           data->operation);
252   GNUNET_TESTBED_operation_queue_insert_ (peer->
253                                           controller->opq_parallel_operations,
254                                           data->operation);
255   return data->operation;
256 }
257
258 /* end of testbed_api_services.c */