Make process_log more generic
[oweals/gnunet.git] / src / rps / test_rps_multipeer.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2012 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20 /**
21  * @file rps/test_rps_multipeer.c
22  * @brief Testcase for the random peer sampling service.  Starts
23  *        a peergroup with a given number of peers, then waits to
24  *        receive size pushes/pulls from each peer.  Expects to wait
25  *        for one message from each peer.
26  */
27 #include"platform.h"
28 #include "gnunet_testbed_service.h"
29 #include "gnunet_rps_service.h"
30 #include <time.h>
31
32
33 /**
34  * How many peers do we start?
35  */
36 #define NUM_PEERS 3
37
38 /**
39  * How long do we run the test?
40  */
41 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
42
43
44 /**
45  * Information we track for each peer.
46  */
47 struct RPSPeer
48 {
49   /**
50    * Handle for RPS connect operation.
51    */
52   struct GNUNET_TESTBED_Operation *op;
53
54   /**
55    * Handle to RPS service.
56    */
57   struct GNUNET_RPS_Handle *rps_handle;
58 };
59
60
61 /**
62  * Information for all the peers.
63  */
64 static struct RPSPeer rps_peers[NUM_PEERS];
65
66 /**
67  * Return value from 'main'.
68  */
69 static int ok;
70
71
72 /**
73  * Task run on timeout to shut everything down.
74  */
75 static void
76 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
77 {
78   unsigned int i;
79
80   for (i=0;i<NUM_PEERS;i++)
81     GNUNET_TESTBED_operation_done (rps_peers[i].op);
82   GNUNET_SCHEDULER_shutdown ();
83 }
84
85
86 /**
87  * Callback to call when network size estimate is updated.
88  *
89  * @param cls closure
90  * @param timestamp server timestamp
91  * @param estimate the value of the current network size estimate
92  * @param std_dev standard deviation (rounded down to nearest integer)
93  *                of the size estimation values seen
94  *
95  */
96 static void
97 handle_reply (void *cls, uint64_t n, struct GNUNET_PeerIdentity *peers)
98 {
99   //struct RPSPeer *peer = cls;
100
101   //FPRINTF (stderr,
102   //         "Received network size estimate from peer %u. logSize: %f std.dev. %f (%f/%u)\n",
103   //         (unsigned int) (peer - rps_peers),
104         //   estimate, std_dev,
105   //         GNUNET_NSE_log_estimate_to_n (estimate),
106         //   NUM_PEERS);
107
108   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got peer %s\n", GNUNET_i2s(peers));
109   
110   ok = 0;
111 }
112
113
114 /**
115  * Callback to be called when RPS service connect operation is completed
116  *
117  * @param cls the callback closure from functions generating an operation
118  * @param op the operation that has been finished
119  * @param ca_result the RPS service handle returned from rps_connect_adapter
120  * @param emsg error message in case the operation has failed; will be NULL if
121  *          operation has executed successfully.
122  */
123 static void
124 rps_connect_complete_cb (void *cls,
125                          struct GNUNET_TESTBED_Operation *op,
126                          void *ca_result,
127                          const char *emsg)
128 {
129   struct RPSPeer *peer = cls;
130   struct GNUNET_RPS_Handle *rps = ca_result;
131
132   GNUNET_assert (op == peer->op);
133   if (NULL != emsg)
134     {
135       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
136                   "Failed to connect to RPS service: %s\n",
137                   emsg);
138       ok = 1;
139       GNUNET_SCHEDULER_shutdown ();
140       return;
141     }
142   peer->rps_handle = rps;
143   //sleep(50);
144   GNUNET_RPS_request_peers(rps, 1, handle_reply, NULL);
145   GNUNET_RPS_request_peers(rps, 1, handle_reply, NULL);
146   //sleep(10000);
147   //GNUNET_RPS_request_peers(rps, 1, handle_reply, NULL);
148   //sleep(10000);
149   //GNUNET_RPS_request_peers(rps, 1, handle_reply, NULL);
150   //sleep(10000);
151   //GNUNET_RPS_request_peers(rps, 1, handle_reply, NULL);
152 }
153
154
155 /**
156  * Adapter function called to establish a connection to
157  * the RPS service.
158  *
159  * @param cls closure
160  * @param cfg configuration of the peer to connect to; will be available until
161  *          GNUNET_TESTBED_operation_done() is called on the operation returned
162  *          from GNUNET_TESTBED_service_connect()
163  * @return service handle to return in 'op_result', NULL on error
164  */
165 static void *
166 rps_connect_adapter (void *cls,
167                      const struct GNUNET_CONFIGURATION_Handle *cfg)
168 {
169   return GNUNET_RPS_connect (cfg);
170 }
171
172
173 /**
174  * Adapter function called to destroy connection to
175  * RPS service.
176  *
177  * @param cls closure
178  * @param op_result service handle returned from the connect adapter
179  */
180 static void
181 rps_disconnect_adapter (void *cls,
182                         void *op_result)
183 {
184   struct GNUNET_RPS_Handle *h = op_result;
185   GNUNET_RPS_disconnect (h);
186 }
187
188
189 /**
190  * Actual "main" function for the testcase.
191  *
192  * @param cls closure
193  * @param h the run handle
194  * @param num_peers number of peers in 'peers'
195  * @param peers handle to peers run in the testbed
196  * @param links_succeeded the number of overlay link connection attempts that
197  *          succeeded
198  * @param links_failed the number of overlay link connection attempts that
199  *          failed
200  */
201 static void
202 run (void *cls,
203      struct GNUNET_TESTBED_RunHandle *h,
204      unsigned int num_peers,
205      struct GNUNET_TESTBED_Peer **peers,
206      unsigned int links_succeeded,
207      unsigned int links_failed)
208 {
209   unsigned int i;
210
211   GNUNET_assert (NUM_PEERS == num_peers);
212   for (i=0;i<num_peers;i++)
213     rps_peers[i].op = GNUNET_TESTBED_service_connect (&rps_peers[i],
214                                                       peers[i],
215                                                       "rps",
216                                                       &rps_connect_complete_cb,
217                                                       &rps_peers[i],
218                                                       &rps_connect_adapter,
219                                                       &rps_disconnect_adapter,
220                                                       &rps_peers[i]);
221   GNUNET_SCHEDULER_add_delayed (TIMEOUT, &shutdown_task, NULL);
222 }
223
224
225 /**
226  * Entry point for the testcase, sets up the testbed.
227  *
228  * @param argc unused
229  * @param argv unused
230  * @return 0 on success
231  */
232 int
233 main (int argc, char *argv[])
234 {
235   ok = 1;
236   (void) GNUNET_TESTBED_test_run ("test-rps-multipeer",
237                                   "test_rps.conf",
238                                   NUM_PEERS,
239                                   0, NULL, NULL,
240                                   &run, NULL);
241   return ok;
242 }
243
244 /* end of test_rps_multipeer.c */