3ec4a905e405df77532035051360bf0a62375543
[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
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.
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   General Public License for more details.
14
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., 51 Franklin Street, Fifth Floor,
18   Boston, MA 02110-1301, USA.
19 */
20
21 /**
22  * @file testbed/testbed_api_barriers.c
23  * @brief API implementation for testbed barriers
24  * @author Sree Harsha Totakura <sreeharsha@totakura.in>
25  */
26 #include "platform.h"
27 #include "gnunet_testbed_service.h"
28 #include "testbed_api.h"
29
30 /**
31  * Logging shorthand
32  */
33 #define LOG(type, ...)                          \
34   GNUNET_log_from (type, "testbed-api-barriers", __VA_ARGS__);
35
36 /**
37  * Debug logging shorthand
38  */
39 #define LOG_DEBUG(...)                          \
40   LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__);
41
42
43 /**
44  * Barrier wait handle
45  */
46 struct GNUNET_TESTBED_BarrierWaitHandle
47 {
48   /**
49    * The name of the barrier
50    */
51   char *name;
52
53   /**
54    * Then configuration used for the client connection
55    */
56   struct GNUNET_CONFIGURATION_Handle *cfg;
57
58   /**
59    * The client connection
60    */
61   struct GNUNET_CLIENT_Connection *conn;
62
63   /**
64    * Transmit handle
65    */
66   struct GNUNET_CLIENT_TransmitHandle *tx;
67
68   /**
69    * The message to transmit with tx
70    */
71   struct GNUNET_MessageHeader *msg;
72
73   /**
74    * The barrier wait callback
75    */
76   GNUNET_TESTBED_barrier_wait_cb cb;
77
78   /**
79    * The closure for the above callback
80    */
81   void *cls;
82 };
83
84
85
86 /**
87  * Type of a function to call when we receive a message
88  * from the service.
89  *
90  * @param cls closure
91  * @param message received message; NULL on timeout or fatal error
92  */
93 static void
94 receive_handler (void *cls,
95                  const struct GNUNET_MessageHeader *message)
96 {
97   struct GNUNET_TESTBED_BarrierWaitHandle *h = cls;
98   const struct GNUNET_TESTBED_BarrierStatusMsg *msg;
99   uint16_t msize;
100
101   if (NULL == message)
102   {
103     GNUNET_break_op (0);
104     goto fail;
105   }
106   if (GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_STATUS != ntohs (message->type))
107   {
108     GNUNET_break_op (0);
109     goto fail;
110   }
111   msize = ntohs (message->size);
112   if (msize <= sizeof (struct GNUNET_TESTBED_BarrierStatusMsg))
113   {
114     GNUNET_break_op (0);
115     goto fail;
116   }
117   msg = (const struct GNUNET_TESTBED_BarrierStatusMsg *) message;
118   switch (ntohs (msg->status))
119   {
120   case GNUNET_TESTBED_BARRIERSTATUS_ERROR:
121     goto fail;
122   case GNUNET_TESTBED_BARRIERSTATUS_INITIALISED:
123     GNUNET_break (0);
124     goto fail;
125   case GNUNET_TESTBED_BARRIERSTATUS_CROSSED:
126     h->cb (h->cls, h->name, GNUNET_OK);
127     goto destroy;
128   default:
129     GNUNET_break_op (0);
130   }
131
132  fail:
133   h->cb (h->cls, h->name, GNUNET_SYSERR);
134
135  destroy:
136   GNUNET_TESTBED_barrier_wait_cancel (h);
137 }
138
139
140 /**
141  * Function called to notify a client about the connection
142  * begin ready to queue more data.  "buf" will be
143  * NULL and "size" zero if the connection was closed for
144  * writing in the meantime.
145  *
146  * @param cls closure
147  * @param size number of bytes available in buf
148  * @param buf where the callee should write the message
149  * @return number of bytes written to buf
150  */
151 static size_t
152 transmit_notify (void *cls, size_t size, void *buf)
153 {
154   struct GNUNET_TESTBED_BarrierWaitHandle *h = cls;
155   uint16_t msize;
156
157   h->tx = NULL;
158   if ((0 == size) || (NULL == buf))
159   {
160     h->cb (h->cls, h->name, GNUNET_SYSERR);
161     GNUNET_TESTBED_barrier_wait_cancel (h);
162     return 0;
163   }
164   msize = htons (h->msg->size);
165   GNUNET_assert (msize <= size);
166   (void) memcpy (buf, h->msg, msize);
167   GNUNET_free (h->msg);
168   h->msg = NULL;
169   GNUNET_CLIENT_receive (h->conn, &receive_handler, h, GNUNET_TIME_UNIT_FOREVER_REL);
170   return msize;
171 }
172
173
174 /**
175  * Wait for a barrier to be crossed.  This function should be called by the
176  * peers which have been started by the testbed.  If the peer is not started by
177  * testbed this function may return error
178  *
179  * @param name the name of the barrier
180  * @param cb the barrier wait callback
181  * @param cls the closure for the above callback
182  * @return barrier wait handle which can be used to cancel the waiting at
183  *   anytime before the callback is called.  NULL upon error.
184  */
185 struct GNUNET_TESTBED_BarrierWaitHandle *
186 GNUNET_TESTBED_barrier_wait (const char *name,
187                              GNUNET_TESTBED_barrier_wait_cb cb,
188                              void *cls)
189 {
190   struct GNUNET_TESTBED_BarrierWait *msg;
191   struct GNUNET_CONFIGURATION_Handle *cfg;
192   struct GNUNET_TESTBED_BarrierWaitHandle *h;
193   char *cfg_filename;
194   size_t name_len;
195   uint16_t msize;
196
197   GNUNET_assert (NULL != cb);
198   GNUNET_assert (NULL != name);
199   cfg_filename = getenv (ENV_TESTBED_CONFIG);
200   if (NULL == cfg_filename)
201   {
202     LOG (GNUNET_ERROR_TYPE_ERROR, "Are you running under testbed?\n");
203     return NULL;
204   }
205   cfg = GNUNET_CONFIGURATION_create ();
206   if (GNUNET_OK != GNUNET_CONFIGURATION_load (cfg, cfg_filename))
207   {
208     LOG (GNUNET_ERROR_TYPE_ERROR, "Unable to load configuration from file `%s'\n",
209          cfg_filename);
210     GNUNET_CONFIGURATION_destroy (cfg);
211     return NULL;
212   }
213   h = GNUNET_new (struct GNUNET_TESTBED_BarrierWaitHandle);
214   h->name = GNUNET_strdup (name);
215   h->cfg = cfg;
216   h->conn = GNUNET_CLIENT_connect ("testbed-barrier", h->cfg);
217   h->cb = cb;
218   h->cls = cls;
219   if (NULL == h->conn)
220   {
221     LOG (GNUNET_ERROR_TYPE_ERROR,
222          "Unable to connect to local testbed-barrier service\n");
223     GNUNET_TESTBED_barrier_wait_cancel (h);
224     return NULL;
225   }
226   name_len = strlen (name);
227   msize = sizeof (struct GNUNET_TESTBED_BarrierWait) + name_len;
228   msg = GNUNET_malloc (msize);
229   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_WAIT);
230   msg->header.size = htons (msize);
231   (void) memcpy (msg->name, name, name_len);
232   h->msg = &msg->header;
233   h->tx =
234       GNUNET_CLIENT_notify_transmit_ready (h->conn, msize,
235                                            GNUNET_TIME_UNIT_FOREVER_REL,
236                                            GNUNET_NO,
237                                            &transmit_notify,
238                                            h);
239   return h;
240 }
241
242
243 /**
244  * Cancel a barrier wait handle
245  *
246  * @param h the barrier wait handle
247  */
248 void
249 GNUNET_TESTBED_barrier_wait_cancel (struct GNUNET_TESTBED_BarrierWaitHandle *h)
250 {
251   GNUNET_free (h->name);
252   if (NULL != h->tx)
253     GNUNET_CLIENT_notify_transmit_ready_cancel (h->tx);
254   if (NULL != h->conn)
255     GNUNET_CLIENT_disconnect (h->conn);
256   if (NULL != h->msg)
257     GNUNET_free (h->msg);
258   GNUNET_CONFIGURATION_destroy (h->cfg);
259   GNUNET_free (h);
260 }
261
262 /* end of testbed_api_barriers.c */