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
59 * Data accessed during service connections
61 struct ServiceConnectData
64 * helper function callback to establish the connection
66 GNUNET_TESTBED_ConnectAdapter ca;
69 * helper function callback to close the connection
71 GNUNET_TESTBED_DisconnectAdapter da;
74 * Closure to the above callbacks
84 * Closure for operation event
89 * The operation which created this structure
91 struct GNUNET_TESTBED_Operation *operation;
94 * The operation context from GNUNET_TESTBED_forward_operation_msg_()
96 struct OperationContext *opc;
101 struct GNUNET_TESTBED_Peer *peer;
104 * The acquired configuration of the peer
106 struct GNUNET_CONFIGURATION_Handle *cfg;
109 * The op_result pointer from ConnectAdapter
122 * Type of a function to call when we receive a message
125 * @param cls ServiceConnectData
126 * @param msg message received, NULL on timeout or fatal error
129 configuration_receiver (void *cls,
130 const struct GNUNET_MessageHeader *msg)
132 struct ServiceConnectData *data = cls;
133 const struct GNUNET_TESTBED_PeerConfigurationInformationMessage *imsg;
134 struct GNUNET_TESTBED_Controller *c;
135 struct GNUNET_TESTBED_EventInformation info;
137 imsg = (const struct GNUNET_TESTBED_PeerConfigurationInformationMessage *)
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))
151 c->cc (c->cc_cls, &info);
156 * Function called when a service connect operation is ready
158 * @param cls the closure from GNUNET_TESTBED_operation_create_()
161 opstart_service_connect (void *cls)
163 struct ServiceConnectData *data = cls;
164 struct GNUNET_TESTBED_PeerGetConfigurationMessage *msg;
165 struct GNUNET_TESTBED_Controller *c;
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,
175 GNUNET_TESTBED_forward_operation_msg_ (c, op_id, &msg->header,
176 &configuration_receiver, data);
178 data->state = CFG_REQUEST_QUEUED;
183 * Callback which will be called when service connect type operation is
186 * @param cls the closure from GNUNET_TESTBED_operation_create_()
189 oprelease_service_connect (void *cls)
191 struct ServiceConnectData *data = cls;
197 case CFG_REQUEST_QUEUED:
198 GNUNET_assert (NULL != data->opc);
199 GNUNET_TESTBED_forward_operation_msg_cancel_ (data->opc);
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);
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.
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
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,
239 struct ServiceConnectData *data;
241 data = GNUNET_malloc (sizeof (struct ServiceConnectData));
244 data->cada_cls = cada_cls;
245 data->op_cls = op_cls;
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;
258 /* end of testbed_api_services.c */