-rps service: prevent division by zero
[oweals/gnunet.git] / src / multicast / test_multicast_multipeer.c
1 /*
2  * This file is part of GNUnet
3  * Copyright (C) 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 multicast/test_multicast_multipeers.c
23  * @brief Tests for the Multicast API with multiple peers.
24  * @author xrs
25  */
26
27 #include <inttypes.h>
28
29 #include "platform.h"
30 #include "gnunet_crypto_lib.h"
31 #include "gnunet_common.h"
32 #include "gnunet_util_lib.h"
33 #include "gnunet_testbed_service.h"
34 #include "gnunet_core_service.h"
35 #include "gnunet_multicast_service.h"
36
37 #define NUM_PEERS 2
38
39 static struct GNUNET_TESTBED_Operation *multicast_peer0;
40 static struct GNUNET_TESTBED_Operation *multicast_peer1;
41
42 static struct GNUNET_SCHEDULER_Task *timeout_tid;
43
44
45 /**
46  * Global result for testcase.
47  */
48 static int result;
49
50
51 /**
52  * Function run on CTRL-C or shutdown (i.e. success/timeout/etc.).
53  * Cleans up.
54  */
55 static void
56 shutdown_task (void *cls)
57 {
58   if (NULL != multicast_peer0)
59   {
60     GNUNET_TESTBED_operation_done (multicast_peer0); 
61     multicast_peer0 = NULL;
62   }
63   if (NULL != timeout_tid)
64     {
65       GNUNET_SCHEDULER_cancel (timeout_tid);
66       timeout_tid = NULL;
67     }
68 }
69
70
71 static void
72 timeout_task (void *cls)
73 {
74   timeout_tid = NULL;
75   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
76               "Timeout!\n");
77   result = GNUNET_SYSERR;
78   GNUNET_SCHEDULER_shutdown ();
79 }
80
81
82 static void 
83 service_close_peer0 (void *cls,
84                      void *op_result)
85 {
86   struct GNUNET_MULTICAST_Origin *orig = op_result;
87   
88   GNUNET_MULTICAST_origin_stop (orig,
89                                 NULL,
90                                 NULL);
91 }
92
93
94 /**
95  * Function run when service multicast has started and is providing us
96  * with a configuration file.
97  */ 
98 static void *
99 service_conf_peer0 (void *cls,
100                     const struct GNUNET_CONFIGURATION_Handle *cfg)
101 {
102   #if 0
103   return GNUNET_MULTICAST_origin_start (cfg,
104                                         priv_key,
105                                         42,
106                                         &join_rcb,
107                                         &reply_fcb,
108                                         &reply_mcb,
109                                         &request_cb,
110                                         &message_cb,
111                                         NULL);
112 #else
113   return NULL;
114
115 #endif
116 }
117
118
119 /**
120  * Test logic of peer "0" being origin starts here. 
121  *
122  * @param cls closure, for the example: NULL
123  * @param op should be equal to "dht_op"
124  * @param ca_result result of the connect operation, the
125  *        connection to the DHT service
126  * @param emsg error message, if testbed somehow failed to
127  *        connect to the DHT.
128  */
129 static void
130 service_connect_peer0 (void *cls,
131                        struct GNUNET_TESTBED_Operation *op,
132                        void *ca_result,
133                        const char *emsg)
134 {
135   struct GNUNET_MULTICAST_Origin *orig = ca_result;
136
137   /* Connection to service successful. Here we'd usually do something with 
138    * the service. */
139   result = GNUNET_OK;
140   GNUNET_SCHEDULER_shutdown (); /* Also kills the testbed */
141 }
142
143
144 /**
145  * Main function inovked from TESTBED once all of the
146  * peers are up and running.  This one then connects
147  * just to the multicast service of peer 0 and 1.
148  * Peer 0 is going to be origin.
149  * Peer 1 is going to be one member.
150  * Origin will start a multicast group and the member will try to join it. 
151  * After that we execute some multicast test. 
152  *
153  * @param cls closure
154  * @param h the run handle
155  * @param peers started peers for the test
156  * @param num_peers size of the 'peers' array
157  * @param links_succeeded number of links between peers that were created
158  * @param links_failed number of links testbed was unable to establish
159  */
160 static void
161 test_master (void *cls,
162              struct GNUNET_TESTBED_RunHandle *h,
163              unsigned int num_peers,
164              struct GNUNET_TESTBED_Peer **peers,
165              unsigned int links_succeeded,
166              unsigned int links_failed)
167 {
168   /* Testbed is ready with peers running and connected in a pre-defined overlay
169      topology (FIXME)  */
170
171   /* connect to a peers service */
172   multicast_peer0 = GNUNET_TESTBED_service_connect
173       (NULL,                    /* Closure for operation */
174        peers[0],                /* The peer whose service to connect to */
175        "multicast",             /* The name of the service */
176        &service_connect_peer0,   /* callback to call after a handle to service
177                                    is opened */
178        NULL,                    /* closure for the above callback */
179        &service_conf_peer0,      /* callback to call with peer's configuration;
180                                    this should open the needed service connection */
181        &service_close_peer0,     /* callback to be called when closing the
182                                    opened service connection */
183        NULL);                   /* closure for the above two callbacks */
184   GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);
185   timeout_tid = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
186                                               &timeout_task, NULL);
187 }
188
189
190 int
191 main (int argc, char *argv[])
192 {
193   int ret;
194
195   result = GNUNET_SYSERR;
196   ret = GNUNET_TESTBED_test_run
197       ("test-multicast-multipeer",  /* test case name */
198        "test_multicast.conf", /* template configuration */
199        NUM_PEERS,       /* number of peers to start */
200        0LL, /* Event mask - set to 0 for no event notifications */
201        NULL, /* Controller event callback */
202        NULL, /* Closure for controller event callback */
203        &test_master, /* continuation callback to be called when testbed setup is
204                         complete */
205        NULL); /* Closure for the test_master callback */
206   if ( (GNUNET_OK != ret) || (GNUNET_OK != result) )
207     return 1;
208   return 0;
209 }
210
211 /* end of test_multicast_multipeer.c */