small API change: do no longer pass rarely needed GNUNET_SCHEDULER_TaskContext to...
[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
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/gnunet-service-test-barriers.c
23  * @brief Daemon acting as a service for testing testbed barriers.  It is
24  *   started as a peer service and waits for a barrier to be crossed.
25  * @author Sree Harsha Totakura <sreeharsha@totakura.in>
26  */
27
28 #include "platform.h"
29 #include "gnunet_util_lib.h"
30 #include "gnunet_testbed_service.h"
31 #include "test_testbed_api_barriers.h"
32
33 /**
34  * logging short hand
35  */
36 #define LOG(type,...) \
37   GNUNET_log (type, __VA_ARGS__);
38
39 /**
40  * Our barrier wait handle
41  */
42 struct GNUNET_TESTBED_BarrierWaitHandle *wh;
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     GNUNET_TESTBED_barrier_wait_cancel (wh);
55   wh = NULL;
56 }
57
58
59 /**
60  * Functions of this type are to be given as acallback argumetn to
61  * GNUNET_TESTBED_barrier_wait().  The callback will be called when the barrier
62  * corresponding given in GNUNET_TESTBED_barrier_wait() is crossed or cancelled.
63  *
64  * @param cls NULL
65  * @param name the barrier name
66  * @param status GNUNET_SYSERR in case of error while waiting for the barrier;
67  *   GNUNET_OK if the barrier is crossed
68  */
69 static void
70 barrier_wait_cb (void *cls, const char *name, int status)
71 {
72   GNUNET_break (NULL == cls);
73   wh = NULL;
74   GNUNET_break (GNUNET_OK == status);
75 }
76
77
78 /**
79  * Task to wait for the barrier
80  *
81  * @param cls NULL
82  * @return
83  */
84 static void
85 do_wait (void *cls)
86 {
87   const struct GNUNET_SCHEDULER_TaskContext *tc;
88
89   tc = GNUNET_SCHEDULER_get_task_context ();
90   if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
91     return;
92   wh = GNUNET_TESTBED_barrier_wait (TEST_BARRIER_NAME, &barrier_wait_cb, NULL);
93   GNUNET_break (NULL != wh);
94 }
95
96
97 /**
98  * Main run function.
99  *
100  * @param cls NULL
101  * @param args arguments passed to GNUNET_PROGRAM_run
102  * @param cfgfile the path to configuration file
103  * @param config the configuration file handle
104  */
105 static void
106 run (void *cls, char *const *args, const char *cfgfile,
107      const struct GNUNET_CONFIGURATION_Handle *config)
108 {
109   unsigned int rsec;
110
111   rsec = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, 10);
112   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
113                                 (GNUNET_TIME_UNIT_SECONDS, rsec),
114                                 &do_wait, NULL);
115   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
116                                 &do_shutdown, NULL);
117 }
118
119
120
121 /**
122  * Main
123  */
124 int main (int argc, char **argv)
125 {
126   struct GNUNET_GETOPT_CommandLineOption options[] = {
127     GNUNET_GETOPT_OPTION_END
128   };
129   int ret;
130
131   ret =
132       GNUNET_PROGRAM_run (argc, argv,
133                           "test-barriers", "nohelp", options, &run, NULL);
134   return ret;
135 }