ea7b50def5c62a5f1ea7c5c92512b116dcedd148
[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,
130                         const struct GNUNET_MessageHeader *msg)
131 {
132   struct ServiceConnectData *data = cls;
133   const struct GNUNET_TESTBED_PeerConfigurationInformationMessage *imsg;
134   struct GNUNET_TESTBED_Controller *c;  
135   struct GNUNET_TESTBED_EventInformation info;  
136   
137   imsg = (const struct GNUNET_TESTBED_PeerConfigurationInformationMessage *)
138     msg;
139   data->cfg = GNUNET_TESTBED_get_config_from_peerinfo_msg_ (imsg);
140   data->op_result = data->ca (data->cada_cls, data->cfg);
141   info.type = GNUNET_TESTBED_ET_OPERATION_FINISHED;
142   info.details.operation_finished.operation = data->operation;
143   info.details.operation_finished.op_cls = data->op_cls;
144   info.details.operation_finished.emsg = NULL;
145   info.details.operation_finished.pit = GNUNET_TESTBED_PIT_GENERIC;
146   info.details.operation_finished.op_result.generic = data->op_result;
147   c = data->peer->controller;
148   data->state = SERVICE_CONNECTED;
149   if ((0 != (GNUNET_TESTBED_ET_OPERATION_FINISHED & c->event_mask))
150       && (NULL != c->cc))
151       c->cc (c->cc_cls, &info);
152 }
153
154
155 /**
156  * Function called when a service connect operation is ready
157  *
158  * @param cls the closure from GNUNET_TESTBED_operation_create_()
159  */
160 static void 
161 opstart_service_connect (void *cls)
162 {
163   struct ServiceConnectData *data = cls;
164   struct GNUNET_TESTBED_PeerGetConfigurationMessage *msg;
165   struct GNUNET_TESTBED_Controller *c;  
166   uint64_t op_id;  
167   
168   GNUNET_assert (NULL != data);
169   GNUNET_assert (NULL != data->peer);
170   c = data->peer->controller;  
171   op_id = c->operation_counter++;  
172   msg = GNUNET_TESTBED_generate_peergetconfig_msg_ (data->peer->unique_id,
173                                                     op_id);
174   data->opc =
175     GNUNET_TESTBED_forward_operation_msg_ (c, op_id, &msg->header,
176                                            &configuration_receiver, data);
177   GNUNET_free (msg);
178   data->state = CFG_REQUEST_QUEUED;
179 }
180
181
182 /**
183  * Callback which will be called when service connect type operation is
184  * released
185  *
186  * @param cls the closure from GNUNET_TESTBED_operation_create_()
187  */
188 static void 
189 oprelease_service_connect (void *cls)
190 {
191   struct ServiceConnectData *data = cls;
192
193   switch (data->state)
194   {
195   case INIT:
196     break;
197   case CFG_REQUEST_QUEUED:
198     GNUNET_assert (NULL != data->opc);
199     GNUNET_TESTBED_forward_operation_msg_cancel_ (data->opc);
200     break;
201   case SERVICE_CONNECTED:
202     GNUNET_assert (NULL != data->cfg);
203     GNUNET_CONFIGURATION_destroy (data->cfg);
204     if (NULL != data->da)
205       data->da (data->cada_cls, data->op_result);
206     break;
207   }
208   GNUNET_free (data);
209 }
210
211
212 /**
213  * Connect to a service offered by the given peer.  Will ensure that
214  * the request is queued to not overwhelm our ability to create and
215  * maintain connections with other systems.  The actual service
216  * handle is then returned via the 'op_result' member in the event
217  * callback.  The 'ca' callback is used to create the connection
218  * when the time is right; the 'da' callback will be used to 
219  * destroy the connection (upon 'GNUNET_TESTBED_operation_done').
220  * 'GNUNET_TESTBED_operation_cancel' can be used to abort this
221  * operation until the event callback has been called.
222  *
223  * @param op_cls closure to pass in operation event
224  * @param peer peer that runs the service
225  * @param service_name name of the service to connect to
226  * @param ca helper function to establish the connection
227  * @param da helper function to close the connection
228  * @param cada_cls closure for ca and da
229  * @return handle for the operation
230  */
231 struct GNUNET_TESTBED_Operation *
232 GNUNET_TESTBED_service_connect (void *op_cls,
233                                 struct GNUNET_TESTBED_Peer *peer,
234                                 const char *service_name,
235                                 GNUNET_TESTBED_ConnectAdapter ca,
236                                 GNUNET_TESTBED_DisconnectAdapter da,
237                                 void *cada_cls)
238 {
239   struct ServiceConnectData *data;
240
241   data = GNUNET_malloc (sizeof (struct ServiceConnectData));
242   data->ca = ca;
243   data->da = da;
244   data->cada_cls = cada_cls;
245   data->op_cls = op_cls;
246   data->peer = peer;
247   data->state = INIT;
248   data->operation = 
249     GNUNET_TESTBED_operation_create_ (data, &opstart_service_connect,
250                                       &oprelease_service_connect);
251   GNUNET_TESTBED_operation_queue_insert_
252     (peer->controller->opq_parallel_service_connections, data->operation);
253   GNUNET_TESTBED_operation_queue_insert_
254     (peer->controller->opq_parallel_operations, data->operation);
255   return data->operation;  
256 }
257
258 /* end of testbed_api_services.c */