2 This file is part of GNUnet
3 (C) 2008--2012 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file testbed/testbed_api_services.c
23 * @brief convenience functions for accessing services
24 * @author Christian Grothoff
27 #include "testbed_api.h"
28 #include "testbed_api_peers.h"
29 #include "testbed_api_operations.h"
33 * States for Service connect operations
43 * The configuration request has been sent
48 * connected to service
55 * Data accessed during service connections
57 struct ServiceConnectData
60 * helper function callback to establish the connection
62 GNUNET_TESTBED_ConnectAdapter ca;
65 * helper function callback to close the connection
67 GNUNET_TESTBED_DisconnectAdapter da;
70 * Closure to the above callbacks
80 * Closure for operation event
85 * The operation which created this structure
87 struct GNUNET_TESTBED_Operation *operation;
90 * The operation context from GNUNET_TESTBED_forward_operation_msg_()
92 struct OperationContext *opc;
97 struct GNUNET_TESTBED_Peer *peer;
100 * The acquired configuration of the peer
102 struct GNUNET_CONFIGURATION_Handle *cfg;
105 * The op_result pointer from ConnectAdapter
110 * The operation completion callback
112 GNUNET_TESTBED_ServiceConnectCompletionCallback cb;
115 * The closure for operation completion callback
128 * Type of a function to call when we receive a message
131 * @param cls ServiceConnectData
132 * @param msg message received, NULL on timeout or fatal error
135 configuration_receiver (void *cls, const struct GNUNET_MessageHeader *msg)
137 struct ServiceConnectData *data = cls;
138 struct GNUNET_TESTBED_Controller *c;
140 struct GNUNET_TESTBED_EventInformation info;
143 c = data->peer->controller;
144 mtype = ntohs (msg->type);
146 info.type = GNUNET_TESTBED_ET_OPERATION_FINISHED;
147 info.details.operation_finished.operation = data->operation;
148 info.details.operation_finished.op_cls = data->op_cls;
149 if (GNUNET_MESSAGE_TYPE_TESTBED_OPERATION_FAIL_EVENT == mtype)
152 GNUNET_TESTBED_parse_error_string_ ((const struct
153 GNUNET_TESTBED_OperationFailureEventMessage
156 emsg = "Unknown error";
157 info.details.operation_finished.emsg = emsg;
158 info.details.operation_finished.generic = NULL;
161 data->cfg = GNUNET_TESTBED_extract_config_ (msg);
162 GNUNET_assert (NULL == data->op_result);
163 data->op_result = data->ca (data->cada_cls, data->cfg);
164 info.details.operation_finished.emsg = NULL;
165 info.details.operation_finished.generic = data->op_result;
166 data->state = SERVICE_CONNECTED;
169 if ((0 != (GNUNET_TESTBED_ET_OPERATION_FINISHED & c->event_mask)) &&
171 c->cc (c->cc_cls, &info);
172 if (NULL != data->cb)
173 data->cb (data->cb_cls, data->operation, data->op_result, emsg);
178 * Function called when a service connect operation is ready
180 * @param cls the closure from GNUNET_TESTBED_operation_create_()
183 opstart_service_connect (void *cls)
185 struct ServiceConnectData *data = cls;
186 struct GNUNET_TESTBED_PeerGetConfigurationMessage *msg;
187 struct GNUNET_TESTBED_Controller *c;
190 GNUNET_assert (NULL != data);
191 GNUNET_assert (NULL != data->peer);
192 c = data->peer->controller;
193 op_id = GNUNET_TESTBED_get_next_op_id (c);
195 GNUNET_TESTBED_generate_peergetconfig_msg_ (data->peer->unique_id, op_id);
197 GNUNET_TESTBED_forward_operation_msg_ (c, op_id, &msg->header,
198 &configuration_receiver, data);
200 data->state = CFG_REQUEST_QUEUED;
205 * Callback which will be called when service connect type operation is
208 * @param cls the closure from GNUNET_TESTBED_operation_create_()
211 oprelease_service_connect (void *cls)
213 struct ServiceConnectData *data = cls;
219 case CFG_REQUEST_QUEUED:
220 GNUNET_assert (NULL != data->opc);
221 GNUNET_TESTBED_forward_operation_msg_cancel_ (data->opc);
223 case SERVICE_CONNECTED:
224 GNUNET_assert (NULL != data->cfg);
225 GNUNET_CONFIGURATION_destroy (data->cfg);
226 if (NULL != data->da)
227 data->da (data->cada_cls, data->op_result);
235 * Connect to a service offered by the given peer. Will ensure that
236 * the request is queued to not overwhelm our ability to create and
237 * maintain connections with other systems. The actual service
238 * handle is then returned via the 'op_result' member in the event
239 * callback. The 'ca' callback is used to create the connection
240 * when the time is right; the 'da' callback will be used to
241 * destroy the connection (upon 'GNUNET_TESTBED_operation_done').
242 * 'GNUNET_TESTBED_operation_cancel' can be used to abort this
243 * operation until the event callback has been called.
245 * @param op_cls closure to pass in operation event
246 * @param peer peer that runs the service
247 * @param service_name name of the service to connect to
248 * @param cb the callback to call when this operation finishes
249 * @param cb_cls closure for the above callback
250 * @param ca helper function to establish the connection
251 * @param da helper function to close the connection
252 * @param cada_cls closure for ca and da
253 * @return handle for the operation
255 struct GNUNET_TESTBED_Operation *
256 GNUNET_TESTBED_service_connect (void *op_cls, struct GNUNET_TESTBED_Peer *peer,
257 const char *service_name,
258 GNUNET_TESTBED_ServiceConnectCompletionCallback
260 GNUNET_TESTBED_ConnectAdapter ca,
261 GNUNET_TESTBED_DisconnectAdapter da,
264 struct ServiceConnectData *data;
266 data = GNUNET_malloc (sizeof (struct ServiceConnectData));
269 data->cada_cls = cada_cls;
270 data->op_cls = op_cls;
274 data->cb_cls = cb_cls;
276 GNUNET_TESTBED_operation_create_ (data, &opstart_service_connect,
277 &oprelease_service_connect);
278 GNUNET_TESTBED_operation_queue_insert_ (peer->
279 controller->opq_parallel_service_connections,
281 GNUNET_TESTBED_operation_queue_insert_ (peer->
282 controller->opq_parallel_operations,
284 GNUNET_TESTBED_operation_begin_wait_ (data->operation);
285 return data->operation;
288 /* end of testbed_api_services.c */