efac01c0ccdb71ca97e5b5c3f5efe945bacbddb1
[oweals/gnunet.git] / src / testbed / testbed_api_barriers.c
1 /*
2   This file is part of GNUnet.
3   Copyright (C) 2008--2013, 2016 GNUnet e.V.
4
5   GNUnet is free software: you can redistribute it and/or modify it
6   under the terms of the GNU Affero General Public License as published
7   by the Free Software Foundation, either version 3 of the License,
8   or (at your 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   Affero General Public License for more details.
14  
15   You should have received a copy of the GNU Affero General Public License
16   along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 /**
20  * @file testbed/testbed_api_barriers.c
21  * @brief API implementation for testbed barriers
22  * @author Sree Harsha Totakura <sreeharsha@totakura.in>
23  */
24 #include "platform.h"
25 #include "gnunet_testbed_service.h"
26 #include "testbed_api.h"
27
28 /**
29  * Logging shorthand
30  */
31 #define LOG(type, ...)                          \
32   GNUNET_log_from (type, "testbed-api-barriers", __VA_ARGS__);
33
34 /**
35  * Debug logging shorthand
36  */
37 #define LOG_DEBUG(...)                          \
38   LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__);
39
40
41 /**
42  * Barrier wait handle
43  */
44 struct GNUNET_TESTBED_BarrierWaitHandle
45 {
46   /**
47    * The name of the barrier
48    */
49   char *name;
50
51   /**
52    * Then configuration used for the client connection
53    */
54   struct GNUNET_CONFIGURATION_Handle *cfg;
55
56   /**
57    * The testbed-barrier service message queue.
58    */
59   struct GNUNET_MQ_Handle *mq;
60
61   /**
62    * The barrier wait callback
63    */
64   GNUNET_TESTBED_barrier_wait_cb cb;
65
66   /**
67    * The closure for @e cb.
68    */
69   void *cb_cls;
70 };
71
72
73
74 /**
75  * Check if barrier status message is well-formed.
76  *
77  * @param cls closure
78  * @param msg received message
79  * @return #GNUNET_OK if the message is well-formed.
80  */
81 static int
82 check_status (void *cls,
83               const struct GNUNET_TESTBED_BarrierStatusMsg *msg)
84 {
85   /* FIXME: this fails to actually check that the message
86      follows the protocol spec (0-terminations!).  However,
87      not critical as #handle_status() doesn't interpret the
88      variable-size part anyway right now. */
89   return GNUNET_OK;
90 }
91
92
93 /**
94  * Type of a function to call when we receive a message
95  * from the service.
96  *
97  * @param cls closure
98  * @param msg received message
99  */
100 static void
101 handle_status (void *cls,
102                const struct GNUNET_TESTBED_BarrierStatusMsg *msg)
103 {
104   struct GNUNET_TESTBED_BarrierWaitHandle *h = cls;
105
106   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
107               "Got barrier status %d\n",
108               (int) ntohs (msg->status));
109   switch (ntohs (msg->status))
110   {
111   case GNUNET_TESTBED_BARRIERSTATUS_ERROR:
112     h->cb (h->cb_cls,
113            h->name,
114            GNUNET_SYSERR);
115     break;
116   case GNUNET_TESTBED_BARRIERSTATUS_INITIALISED:
117     h->cb (h->cb_cls,
118            h->name,
119            GNUNET_SYSERR);
120     GNUNET_break (0);
121     break;
122   case GNUNET_TESTBED_BARRIERSTATUS_CROSSED:
123     h->cb (h->cb_cls,
124            h->name,
125            GNUNET_OK);
126     break;
127   default:
128     GNUNET_break_op (0);
129     h->cb (h->cb_cls,
130            h->name,
131            GNUNET_SYSERR);
132     break;
133   }
134   GNUNET_TESTBED_barrier_wait_cancel (h);
135 }
136
137
138 /**
139  * Generic error handler, called with the appropriate error code and
140  * the same closure specified at the creation of the message queue.
141  * Not every message queue implementation supports an error handler.
142  *
143  * @param cls closure with the `struct GNUNET_TESTBED_BarrierWaitHandle *`
144  * @param error error code
145  */
146 static void
147 mq_error_handler (void *cls,
148                   enum GNUNET_MQ_Error error)
149 {
150   struct GNUNET_TESTBED_BarrierWaitHandle *h = cls;
151
152   h->cb (h->cb_cls,
153          h->name,
154          GNUNET_SYSERR);
155   GNUNET_TESTBED_barrier_wait_cancel (h);
156 }
157
158
159 /**
160  * Wait for a barrier to be crossed.  This function should be called by the
161  * peers which have been started by the testbed.  If the peer is not started by
162  * testbed this function may return error
163  *
164  * @param name the name of the barrier
165  * @param cb the barrier wait callback
166  * @param cb_cls the closure for @a cb
167  * @return barrier wait handle which can be used to cancel the waiting at
168  *   anytime before the callback is called.  NULL upon error.
169  */
170 struct GNUNET_TESTBED_BarrierWaitHandle *
171 GNUNET_TESTBED_barrier_wait (const char *name,
172                              GNUNET_TESTBED_barrier_wait_cb cb,
173                              void *cb_cls)
174 {
175   struct GNUNET_TESTBED_BarrierWaitHandle *h
176     = GNUNET_new (struct GNUNET_TESTBED_BarrierWaitHandle);
177   struct GNUNET_MQ_MessageHandler handlers[] = {
178     GNUNET_MQ_hd_var_size (status,
179                            GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_STATUS,
180                            struct GNUNET_TESTBED_BarrierStatusMsg,
181                            h),
182     GNUNET_MQ_handler_end ()
183   };
184   struct GNUNET_MQ_Envelope *env;
185   struct GNUNET_TESTBED_BarrierWait *msg;
186   const char *cfg_filename;
187   size_t name_len;
188
189   GNUNET_assert (NULL != cb);
190   cfg_filename = getenv (ENV_TESTBED_CONFIG);
191   if (NULL == cfg_filename)
192   {
193     LOG (GNUNET_ERROR_TYPE_ERROR,
194          "Are you running under testbed?\n");
195     GNUNET_free (h);
196     return NULL;
197   }
198   h->cfg = GNUNET_CONFIGURATION_create ();
199   if (GNUNET_OK !=
200       GNUNET_CONFIGURATION_load (h->cfg,
201                                  cfg_filename))
202   {
203     LOG (GNUNET_ERROR_TYPE_ERROR,
204          "Unable to load configuration from file `%s'\n",
205          cfg_filename);
206     GNUNET_CONFIGURATION_destroy (h->cfg);
207     GNUNET_free (h);
208     return NULL;
209   }
210   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
211               "Waiting on barrier `%s'\n",
212               name);
213   h->name = GNUNET_strdup (name);
214   h->cb = cb;
215   h->cb_cls = cb_cls;
216   h->mq = GNUNET_CLIENT_connect (h->cfg,
217                                  "testbed-barrier",
218                                  handlers,
219                                  &mq_error_handler,
220                                  h);
221   if (NULL == h->mq)
222   {
223     LOG (GNUNET_ERROR_TYPE_ERROR,
224          "Unable to connect to local testbed-barrier service\n");
225     GNUNET_TESTBED_barrier_wait_cancel (h);
226     return NULL;
227   }
228   name_len = strlen (name); /* NOTE: unusual to not have 0-termination, change? */
229   env = GNUNET_MQ_msg_extra (msg,
230                              name_len,
231                              GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_WAIT);
232   GNUNET_memcpy (msg->name,
233                  name,
234                  name_len);
235   GNUNET_MQ_send (h->mq,
236                   env);
237   return h;
238 }
239
240
241 /**
242  * Cancel a barrier wait handle
243  *
244  * @param h the barrier wait handle
245  */
246 void
247 GNUNET_TESTBED_barrier_wait_cancel (struct GNUNET_TESTBED_BarrierWaitHandle *h)
248 {
249   if (NULL != h->mq)
250   {
251     GNUNET_MQ_destroy (h->mq);
252     h->mq = NULL;
253   }
254   GNUNET_free (h->name);
255   GNUNET_CONFIGURATION_destroy (h->cfg);
256   GNUNET_free (h);
257 }
258
259 /* end of testbed_api_barriers.c */