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, const struct GNUNET_MessageHeader *msg)
131 struct ServiceConnectData *data = cls;
132 const struct GNUNET_TESTBED_PeerConfigurationInformationMessage *imsg;
133 struct GNUNET_TESTBED_Controller *c;
134 struct GNUNET_TESTBED_EventInformation info;
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)) &&
150 c->cc (c->cc_cls, &info);
155 * Function called when a service connect operation is ready
157 * @param cls the closure from GNUNET_TESTBED_operation_create_()
160 opstart_service_connect (void *cls)
162 struct ServiceConnectData *data = cls;
163 struct GNUNET_TESTBED_PeerGetConfigurationMessage *msg;
164 struct GNUNET_TESTBED_Controller *c;
167 GNUNET_assert (NULL != data);
168 GNUNET_assert (NULL != data->peer);
169 c = data->peer->controller;
170 op_id = c->operation_counter++;
172 GNUNET_TESTBED_generate_peergetconfig_msg_ (data->peer->unique_id, op_id);
174 GNUNET_TESTBED_forward_operation_msg_ (c, op_id, &msg->header,
175 &configuration_receiver, data);
177 data->state = CFG_REQUEST_QUEUED;
182 * Callback which will be called when service connect type operation is
185 * @param cls the closure from GNUNET_TESTBED_operation_create_()
188 oprelease_service_connect (void *cls)
190 struct ServiceConnectData *data = cls;
196 case CFG_REQUEST_QUEUED:
197 GNUNET_assert (NULL != data->opc);
198 GNUNET_TESTBED_forward_operation_msg_cancel_ (data->opc);
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);
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.
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
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,
237 struct ServiceConnectData *data;
239 data = GNUNET_malloc (sizeof (struct ServiceConnectData));
242 data->cada_cls = cada_cls;
243 data->op_cls = op_cls;
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,
252 GNUNET_TESTBED_operation_queue_insert_ (peer->
253 controller->opq_parallel_operations,
255 return data->operation;
258 /* end of testbed_api_services.c */