fix
[oweals/gnunet.git] / src / testbed / gnunet-service-test-barriers.c
1 /*
2       This file is part of GNUnet
3       Copyright (C) 2008--2013 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/gnunet-service-test-barriers.c
21  * @brief Daemon acting as a service for testing testbed barriers.  It is
22  *   started as a peer service and waits for a barrier to be crossed.
23  * @author Sree Harsha Totakura <sreeharsha@totakura.in>
24  */
25
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_testbed_service.h"
29 #include "test_testbed_api_barriers.h"
30
31 /**
32  * logging short hand
33  */
34 #define LOG(type,...) \
35   GNUNET_log (type, __VA_ARGS__);
36
37 /**
38  * Our barrier wait handle
39  */
40 static struct GNUNET_TESTBED_BarrierWaitHandle *wh;
41
42 static struct GNUNET_SCHEDULER_Task *tt;
43
44
45 /**
46  * Dummy task callback to keep us running forever
47  *
48  * @param cls NULL
49  */
50 static void
51 do_shutdown (void *cls)
52 {
53   if (NULL != wh)
54   {
55     GNUNET_TESTBED_barrier_wait_cancel (wh);
56     wh = NULL;
57   }
58   if (NULL != tt)
59   {
60     GNUNET_SCHEDULER_cancel (tt);
61     tt = NULL;
62   }
63 }
64
65
66 /**
67  * Functions of this type are to be given as acallback argumetn to
68  * GNUNET_TESTBED_barrier_wait().  The callback will be called when the barrier
69  * corresponding given in GNUNET_TESTBED_barrier_wait() is crossed or cancelled.
70  *
71  * @param cls NULL
72  * @param name the barrier name
73  * @param status #GNUNET_SYSERR in case of error while waiting for the barrier;
74  *   #GNUNET_OK if the barrier is crossed
75  */
76 static void
77 barrier_wait_cb (void *cls,
78                  const char *name,
79                  int status)
80 {
81   GNUNET_break (NULL == cls);
82   wh = NULL;
83   GNUNET_break (GNUNET_OK == status);
84 }
85
86
87 /**
88  * Task to wait for the barrier
89  *
90  * @param cls NULL
91  * @return
92  */
93 static void
94 do_wait (void *cls)
95 {
96   tt = NULL;
97   wh = GNUNET_TESTBED_barrier_wait (TEST_BARRIER_NAME,
98                                     &barrier_wait_cb,
99                                     NULL);
100   GNUNET_break (NULL != wh);
101 }
102
103
104 /**
105  * Main run function.
106  *
107  * @param cls NULL
108  * @param args arguments passed to GNUNET_PROGRAM_run
109  * @param cfgfile the path to configuration file
110  * @param config the configuration file handle
111  */
112 static void
113 run (void *cls,
114      char *const *args,
115      const char *cfgfile,
116      const struct GNUNET_CONFIGURATION_Handle *config)
117 {
118   unsigned int rsec;
119
120   rsec = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
121                                    10);
122   tt = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
123                                                                     rsec),
124                                      &do_wait,
125                                      NULL);
126   GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
127 }
128
129
130
131 /**
132  * Main
133  */
134 int
135 main (int argc, char **argv)
136 {
137   struct GNUNET_GETOPT_CommandLineOption options[] = {
138     GNUNET_GETOPT_OPTION_END
139   };
140   int ret;
141
142   ret =
143       GNUNET_PROGRAM_run (argc, argv,
144                           "test-barriers",
145                           "nohelp",
146                           options,
147                           &run,
148                           NULL);
149   return ret;
150 }