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
56 * Data accessed during service connections
58 struct ServiceConnectData
61 * helper function callback to establish the connection
63 GNUNET_TESTBED_ConnectAdapter ca;
66 * helper function callback to close the connection
68 GNUNET_TESTBED_DisconnectAdapter da;
71 * Closure to the above callbacks
81 * Closure for operation event
86 * The operation which created this structure
88 struct GNUNET_TESTBED_Operation *operation;
91 * The operation context from GNUNET_TESTBED_forward_operation_msg_()
93 struct OperationContext *opc;
98 struct GNUNET_TESTBED_Peer *peer;
101 * The acquired configuration of the peer
103 struct GNUNET_CONFIGURATION_Handle *cfg;
106 * The op_result pointer from ConnectAdapter
111 * The operation completion callback
113 GNUNET_TESTBED_ServiceConnectCompletionCallback cb;
116 * The closure for operation completion callback
129 * Type of a function to call when we receive a message
132 * @param cls ServiceConnectData
133 * @param msg message received, NULL on timeout or fatal error
136 configuration_receiver (void *cls, const struct GNUNET_MessageHeader *msg)
138 struct ServiceConnectData *data = cls;
139 const struct GNUNET_TESTBED_PeerConfigurationInformationMessage *imsg;
140 struct GNUNET_TESTBED_Controller *c;
141 struct GNUNET_TESTBED_EventInformation info;
144 mtype = ntohs (msg->type);
145 if (GNUNET_MESSAGE_TYPE_TESTBED_OPERATIONEVENT == mtype)
147 GNUNET_assert (0); /* FIXME: Add notification for failure */
150 (const struct GNUNET_TESTBED_PeerConfigurationInformationMessage *) msg;
151 data->cfg = GNUNET_TESTBED_get_config_from_peerinfo_msg_ (imsg);
152 data->op_result = data->ca (data->cada_cls, data->cfg);
153 info.type = GNUNET_TESTBED_ET_OPERATION_FINISHED;
154 info.details.operation_finished.operation = data->operation;
155 info.details.operation_finished.op_cls = data->op_cls;
156 info.details.operation_finished.emsg = NULL;
157 info.details.operation_finished.generic = data->op_result;
158 c = data->peer->controller;
159 data->state = SERVICE_CONNECTED;
160 if ((0 != (GNUNET_TESTBED_ET_OPERATION_FINISHED & c->event_mask)) &&
162 c->cc (c->cc_cls, &info);
163 if (NULL != data->cb)
164 data->cb (data->cb_cls, data->operation, data->op_result, NULL);
169 * Function called when a service connect operation is ready
171 * @param cls the closure from GNUNET_TESTBED_operation_create_()
174 opstart_service_connect (void *cls)
176 struct ServiceConnectData *data = cls;
177 struct GNUNET_TESTBED_PeerGetConfigurationMessage *msg;
178 struct GNUNET_TESTBED_Controller *c;
181 GNUNET_assert (NULL != data);
182 GNUNET_assert (NULL != data->peer);
183 c = data->peer->controller;
184 op_id = c->operation_counter++;
186 GNUNET_TESTBED_generate_peergetconfig_msg_ (data->peer->unique_id, op_id);
188 GNUNET_TESTBED_forward_operation_msg_ (c, op_id, &msg->header,
189 &configuration_receiver, data);
191 data->state = CFG_REQUEST_QUEUED;
196 * Callback which will be called when service connect type operation is
199 * @param cls the closure from GNUNET_TESTBED_operation_create_()
202 oprelease_service_connect (void *cls)
204 struct ServiceConnectData *data = cls;
210 case CFG_REQUEST_QUEUED:
211 GNUNET_assert (NULL != data->opc);
212 GNUNET_TESTBED_forward_operation_msg_cancel_ (data->opc);
214 case SERVICE_CONNECTED:
215 GNUNET_assert (NULL != data->cfg);
216 GNUNET_CONFIGURATION_destroy (data->cfg);
217 if (NULL != data->da)
218 data->da (data->cada_cls, data->op_result);
226 * Connect to a service offered by the given peer. Will ensure that
227 * the request is queued to not overwhelm our ability to create and
228 * maintain connections with other systems. The actual service
229 * handle is then returned via the 'op_result' member in the event
230 * callback. The 'ca' callback is used to create the connection
231 * when the time is right; the 'da' callback will be used to
232 * destroy the connection (upon 'GNUNET_TESTBED_operation_done').
233 * 'GNUNET_TESTBED_operation_cancel' can be used to abort this
234 * operation until the event callback has been called.
236 * @param op_cls closure to pass in operation event
237 * @param peer peer that runs the service
238 * @param service_name name of the service to connect to
239 * @param cb the callback to call when this operation finishes
240 * @param cb_cls closure for the above callback
241 * @param ca helper function to establish the connection
242 * @param da helper function to close the connection
243 * @param cada_cls closure for ca and da
244 * @return handle for the operation
246 struct GNUNET_TESTBED_Operation *
247 GNUNET_TESTBED_service_connect (void *op_cls,
248 struct GNUNET_TESTBED_Peer *peer,
249 const char *service_name,
250 GNUNET_TESTBED_ServiceConnectCompletionCallback cb,
252 GNUNET_TESTBED_ConnectAdapter ca,
253 GNUNET_TESTBED_DisconnectAdapter da,
256 struct ServiceConnectData *data;
258 data = GNUNET_malloc (sizeof (struct ServiceConnectData));
261 data->cada_cls = cada_cls;
262 data->op_cls = op_cls;
266 data->cb_cls = cb_cls;
268 GNUNET_TESTBED_operation_create_ (data, &opstart_service_connect,
269 &oprelease_service_connect);
270 GNUNET_TESTBED_operation_queue_insert_ (peer->
271 controller->opq_parallel_service_connections,
273 GNUNET_TESTBED_operation_queue_insert_ (peer->
274 controller->opq_parallel_operations,
276 return data->operation;
279 /* end of testbed_api_services.c */