3ef1e6611732b682e6118b676a691b0c43ef53c2
[oweals/gnunet.git] / src / rps / test_rps.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009, 2012 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  * @file rps/test_rps.c
20  * @brief Testcase for the random peer sampling service.  Starts
21  *        a peergroup with a given number of peers, then waits to
22  *        receive size pushes/pulls from each peer.  Expects to wait
23  *        for one message from each peer.
24  */
25 #include "platform.h"
26 #include "gnunet_util_lib.h"
27 #include "gnunet_testbed_service.h"
28
29 #include "gnunet_rps_service.h"
30 #include "rps-test_util.h"
31 #include "gnunet-service-rps_sampler_elem.h"
32
33 #include <inttypes.h>
34
35
36 /**
37  * How many peers do we start?
38  */
39 static uint32_t num_peers;
40
41 /**
42  * How long do we run the test?
43  * In seconds.
44  */
45 static uint32_t timeout_s;
46
47 /**
48  * How long do we run the test?
49  */
50 //#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
51 static struct GNUNET_TIME_Relative timeout;
52
53
54 /**
55  * Portion of malicious peers
56  */
57 static double portion = .1;
58
59 /**
60  * Type of malicious peer to test
61  */
62 static unsigned int mal_type = 0;
63
64 /**
65  * Handles to all of the running peers
66  */
67 static struct GNUNET_TESTBED_Peer **testbed_peers;
68
69 /**
70  * @brief Indicates whether peer should go off- or online
71  */
72 enum PEER_ONLINE_DELTA {
73   /**
74    * @brief Indicates peer going online
75    */
76   PEER_GO_ONLINE = 1,
77   /**
78    * @brief Indicates peer going offline
79    */
80   PEER_GO_OFFLINE = -1,
81 };
82
83 /**
84  * Operation map entry
85  */
86 struct OpListEntry
87 {
88   /**
89    * DLL next ptr
90    */
91   struct OpListEntry *next;
92
93   /**
94    * DLL prev ptr
95    */
96   struct OpListEntry *prev;
97
98   /**
99    * The testbed operation
100    */
101   struct GNUNET_TESTBED_Operation *op;
102
103   /**
104    * Depending on whether we start or stop RPS service at the peer, set this to
105    * #PEER_GO_ONLINE (1) or #PEER_GO_OFFLINE (-1)
106    */
107   enum PEER_ONLINE_DELTA delta;
108
109   /**
110    * Index of the regarding peer
111    */
112   unsigned int index;
113 };
114
115 /**
116  * OpList DLL head
117  */
118 static struct OpListEntry *oplist_head;
119
120 /**
121  * OpList DLL tail
122  */
123 static struct OpListEntry *oplist_tail;
124
125
126 /**
127  * A pending reply: A request was sent and the reply is pending.
128  */
129 struct PendingReply
130 {
131   /**
132    * DLL next,prev ptr
133    */
134   struct PendingReply *next;
135   struct PendingReply *prev;
136
137   /**
138    * Handle to the request we are waiting for
139    */
140   struct GNUNET_RPS_Request_Handle *req_handle;
141
142   /**
143    * The peer that requested
144    */
145   struct RPSPeer *rps_peer;
146 };
147
148
149 /**
150  * A pending request: A request was not made yet but is scheduled for later.
151  */
152 struct PendingRequest
153 {
154   /**
155    * DLL next,prev ptr
156    */
157   struct PendingRequest *next;
158   struct PendingRequest *prev;
159
160   /**
161    * Handle to the request we are waiting for
162    */
163   struct GNUNET_SCHEDULER_Task *request_task;
164
165   /**
166    * The peer that requested
167    */
168   struct RPSPeer *rps_peer;
169 };
170
171
172 /**
173  * Information we track for each peer.
174  */
175 struct RPSPeer
176 {
177   /**
178    * Index of the peer.
179    */
180   unsigned int index;
181
182   /**
183    * Handle for RPS connect operation.
184    */
185   struct GNUNET_TESTBED_Operation *op;
186
187   /**
188    * Handle to RPS service.
189    */
190   struct GNUNET_RPS_Handle *rps_handle;
191
192   /**
193    * ID of the peer.
194    */
195   struct GNUNET_PeerIdentity *peer_id;
196
197   /**
198    * A request handle to check for an request
199    */
200   //struct GNUNET_RPS_Request_Handle *req_handle;
201
202   /**
203    * Peer on- or offline?
204    */
205   int online;
206
207   /**
208    * Number of Peer IDs to request during the whole test
209    */
210   unsigned int num_ids_to_request;
211
212   /**
213    * Pending requests DLL
214    */
215   struct PendingRequest *pending_req_head;
216   struct PendingRequest *pending_req_tail;
217
218   /**
219    * Number of pending requests
220    */
221   unsigned int num_pending_reqs;
222
223   /**
224    * Pending replies DLL
225    */
226   struct PendingReply *pending_rep_head;
227   struct PendingReply *pending_rep_tail;
228
229   /**
230    * Number of pending replies
231    */
232   unsigned int num_pending_reps;
233
234   /**
235    * Number of received PeerIDs
236    */
237   unsigned int num_recv_ids;
238
239   /**
240    * Pending operation on that peer
241    */
242   const struct OpListEntry *entry_op_manage;
243
244   /**
245    * Testbed operation to connect to statistics service
246    */
247   struct GNUNET_TESTBED_Operation *stat_op;
248
249   /**
250    * Handle to the statistics service
251    */
252   struct GNUNET_STATISTICS_Handle *stats_h;
253
254   /**
255    * @brief flags to indicate which statistics values have been already
256    * collected from the statistics service.
257    * Used to check whether we are able to shutdown.
258    */
259   uint32_t stat_collected_flags;
260
261   /**
262    * @brief File name of the file the stats are finally written to
263    */
264   const char *file_name_stats;
265
266   /**
267    * @brief File name of the file the stats are finally written to
268    */
269   const char *file_name_probs;
270
271   /**
272    * @brief The current view
273    */
274   struct GNUNET_PeerIdentity *cur_view;
275
276   /**
277    * @brief Number of peers in the #cur_view.
278    */
279   uint32_t cur_view_count;
280
281   /**
282    * @brief Number of occurrences in other peer's view
283    */
284   uint32_t count_in_views;
285
286   /**
287    * @brief statistics values
288    */
289   uint64_t num_rounds;
290   uint64_t num_blocks;
291   uint64_t num_blocks_many_push;
292   uint64_t num_blocks_no_push;
293   uint64_t num_blocks_no_pull;
294   uint64_t num_blocks_many_push_no_pull;
295   uint64_t num_blocks_no_push_no_pull;
296   uint64_t num_issued_push;
297   uint64_t num_issued_pull_req;
298   uint64_t num_issued_pull_rep;
299   uint64_t num_sent_push;
300   uint64_t num_sent_pull_req;
301   uint64_t num_sent_pull_rep;
302   uint64_t num_recv_push;
303   uint64_t num_recv_pull_req;
304   uint64_t num_recv_pull_rep;
305 };
306
307 enum STAT_TYPE
308 {
309   STAT_TYPE_ROUNDS                    =    0x1, /*   1 */
310   STAT_TYPE_BLOCKS                    =    0x2, /*   2 */
311   STAT_TYPE_BLOCKS_MANY_PUSH          =    0x4, /*   3 */
312   STAT_TYPE_BLOCKS_NO_PUSH            =    0x8, /*   4 */
313   STAT_TYPE_BLOCKS_NO_PULL            =   0x10, /*   5 */
314   STAT_TYPE_BLOCKS_MANY_PUSH_NO_PULL  =   0x20, /*   6 */
315   STAT_TYPE_BLOCKS_NO_PUSH_NO_PULL    =   0x40, /*   7 */
316   STAT_TYPE_ISSUED_PUSH_SEND          =   0x80, /*   8 */
317   STAT_TYPE_ISSUED_PULL_REQ           =  0x100, /*   9 */
318   STAT_TYPE_ISSUED_PULL_REP           =  0x200, /*  10 */
319   STAT_TYPE_SENT_PUSH_SEND            =  0x400, /*  11 */
320   STAT_TYPE_SENT_PULL_REQ             =  0x800, /*  12 */
321   STAT_TYPE_SENT_PULL_REP             = 0x1000, /*  13 */
322   STAT_TYPE_RECV_PUSH_SEND            = 0x2000, /*  14 */
323   STAT_TYPE_RECV_PULL_REQ             = 0x4000, /*  15 */
324   STAT_TYPE_RECV_PULL_REP             = 0x8000, /*  16 */
325   STAT_TYPE_MAX          = 0x80000000, /*  32 */
326 };
327
328 struct STATcls
329 {
330   struct RPSPeer *rps_peer;
331   enum STAT_TYPE stat_type;
332 };
333
334
335 /**
336  * Information for all the peers.
337  */
338 static struct RPSPeer *rps_peers;
339
340 /**
341  * Peermap to get the index of a given peer ID quick.
342  */
343 static struct GNUNET_CONTAINER_MultiPeerMap *peer_map;
344
345 /**
346  * IDs of the peers.
347  */
348 static struct GNUNET_PeerIdentity *rps_peer_ids;
349
350 /**
351  * ID of the targeted peer.
352  */
353 static struct GNUNET_PeerIdentity *target_peer;
354
355 /**
356  * ID of the peer that requests for the evaluation.
357  */
358 static struct RPSPeer *eval_peer;
359
360 /**
361  * Number of online peers.
362  */
363 static unsigned int num_peers_online;
364
365 /**
366  * @brief The added sizes of the peer's views
367  */
368 static unsigned int view_sizes;
369
370 /**
371  * Return value from 'main'.
372  */
373 static int ok;
374
375 /**
376  * Identifier for the churn task that runs periodically
377  */
378 static struct GNUNET_SCHEDULER_Task *post_test_task;
379
380 /**
381  * Identifier for the churn task that runs periodically
382  */
383 static struct GNUNET_SCHEDULER_Task *shutdown_task;
384
385 /**
386  * Identifier for the churn task that runs periodically
387  */
388 static struct GNUNET_SCHEDULER_Task *churn_task;
389
390 /**
391  * Called to initialise the given RPSPeer
392  */
393 typedef void (*InitPeer) (struct RPSPeer *rps_peer);
394
395 /**
396  * @brief Called directly after connecting to the service
397  *
398  * @param rps_peer Specific peer the function is called on
399  * @param h the handle to the rps service
400  */
401 typedef void (*PreTest) (struct RPSPeer *rps_peer, struct GNUNET_RPS_Handle *h);
402
403 /**
404  * @brief Executes functions to test the api/service for a given peer
405  *
406  * Called from within #rps_connect_complete_cb ()
407  * Implemented by #churn_test_cb, #profiler_cb, #mal_cb, #single_req_cb,
408  * #delay_req_cb, #seed_big_cb, #single_peer_seed_cb, #seed_cb, #req_cancel_cb
409  *
410  * @param rps_peer the peer the task runs on
411  */
412 typedef void (*MainTest) (struct RPSPeer *rps_peer);
413
414 /**
415  * Callback called once the requested random peers are available
416  */
417 typedef void (*ReplyHandle) (void *cls,
418                              uint64_t n,
419                              const struct GNUNET_PeerIdentity *recv_peers);
420
421 /**
422  * Called directly before disconnecting from the service
423  */
424 typedef void (*PostTest) (struct RPSPeer *peer);
425
426 /**
427  * Function called after disconnect to evaluate test success
428  */
429 typedef int (*EvaluationCallback) (void);
430
431 /**
432  * @brief Do we have Churn?
433  */
434 enum OPTION_CHURN {
435   /**
436    * @brief If we have churn this is set
437    */
438   HAVE_CHURN,
439   /**
440    * @brief If we have no churn this is set
441    */
442   HAVE_NO_CHURN,
443 };
444
445 /**
446  * @brief Is it ok to quit the test before the timeout?
447  */
448 enum OPTION_QUICK_QUIT {
449   /**
450    * @brief It is ok for the test to quit before the timeout triggers
451    */
452   HAVE_QUICK_QUIT,
453
454   /**
455    * @brief It is NOT ok for the test to quit before the timeout triggers
456    */
457   HAVE_NO_QUICK_QUIT,
458 };
459
460 /**
461  * @brief Do we collect statistics at the end?
462  */
463 enum OPTION_COLLECT_STATISTICS {
464   /**
465    * @brief We collect statistics at the end
466    */
467   COLLECT_STATISTICS,
468
469   /**
470    * @brief We do not collect statistics at the end
471    */
472   NO_COLLECT_STATISTICS,
473 };
474
475 /**
476  * @brief Do we collect views during run?
477  */
478 enum OPTION_COLLECT_VIEW {
479   /**
480    * @brief We collect view during run
481    */
482   COLLECT_VIEW,
483
484   /**
485    * @brief We do not collect the view during run
486    */
487   NO_COLLECT_VIEW,
488 };
489
490 /**
491  * Structure to define a single test
492  */
493 struct SingleTestRun
494 {
495   /**
496    * Name of the test
497    */
498   char *name;
499
500   /**
501    * Called with a single peer in order to initialise that peer
502    */
503   InitPeer init_peer;
504
505   /**
506    * Called directly after connecting to the service
507    */
508   PreTest pre_test;
509
510   /**
511    * Main function for each peer
512    */
513   MainTest main_test;
514
515   /**
516    * Callback called once the requested peers are available
517    */
518   ReplyHandle reply_handle;
519
520   /**
521    * Called directly before disconnecting from the service
522    */
523   PostTest post_test;
524
525   /**
526    * Function to evaluate the test results
527    */
528   EvaluationCallback eval_cb;
529
530   /**
531    * Request interval
532    */
533   uint32_t request_interval;
534
535   /**
536    * Number of Requests to make.
537    */
538   uint32_t num_requests;
539
540   /**
541    * Run with (-out) churn
542    */
543   enum OPTION_CHURN have_churn;
544
545   /**
546    * Quit test before timeout?
547    */
548   enum OPTION_QUICK_QUIT have_quick_quit;
549
550   /**
551    * Collect statistics at the end?
552    */
553   enum OPTION_COLLECT_STATISTICS have_collect_statistics;
554
555   /**
556    * Collect view during run?
557    */
558   enum OPTION_COLLECT_VIEW have_collect_view;
559
560   /**
561    * @brief Mark which values from the statistics service to collect at the end
562    * of the run
563    */
564   uint32_t stat_collect_flags;
565 } cur_test_run;
566
567 /**
568  * Did we finish the test?
569  */
570 static int post_test;
571
572 /**
573  * Are we shutting down?
574  */
575 static int in_shutdown;
576
577 /**
578  * Append arguments to file
579  */
580 static void
581 tofile_ (const char *file_name, const char *line)
582 {
583   struct GNUNET_DISK_FileHandle *f;
584   /* char output_buffer[512]; */
585   size_t size;
586   /* int size; */
587   size_t size2;
588
589   if (NULL == (f = GNUNET_DISK_file_open (file_name,
590                                           GNUNET_DISK_OPEN_APPEND |
591                                           GNUNET_DISK_OPEN_WRITE |
592                                           GNUNET_DISK_OPEN_CREATE,
593                                           GNUNET_DISK_PERM_USER_READ |
594                                           GNUNET_DISK_PERM_USER_WRITE |
595                                           GNUNET_DISK_PERM_GROUP_READ |
596                                           GNUNET_DISK_PERM_OTHER_READ)))
597   {
598     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
599                 "Not able to open file %s\n",
600                 file_name);
601     return;
602   }
603   /* size = GNUNET_snprintf (output_buffer,
604                           sizeof (output_buffer),
605                           "%llu %s\n",
606                           GNUNET_TIME_absolute_get ().abs_value_us,
607                           line);
608   if (0 > size)
609   {
610     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
611                 "Failed to write string to buffer (size: %i)\n",
612                 size);
613     return;
614   } */
615
616   size = strlen (line) * sizeof (char);
617
618   size2 = GNUNET_DISK_file_write (f, line, size);
619   if (size != size2)
620   {
621     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
622                 "Unable to write to file! (Size: %lu, size2: %lu)\n",
623                 size,
624                 size2);
625     if (GNUNET_YES != GNUNET_DISK_file_close (f))
626     {
627       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
628                   "Unable to close file\n");
629     }
630     return;
631   }
632
633   if (GNUNET_YES != GNUNET_DISK_file_close (f))
634   {
635     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
636                 "Unable to close file\n");
637   }
638 }
639
640 /**
641  * This function is used to facilitate writing important information to disk
642  */
643 #define tofile(file_name, ...) do {\
644   char tmp_buf[512];\
645     int size;\
646     size = GNUNET_snprintf(tmp_buf,sizeof(tmp_buf),__VA_ARGS__);\
647     if (0 > size)\
648       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,\
649                      "Failed to create tmp_buf\n");\
650     else\
651       tofile_(file_name,tmp_buf);\
652   } while (0);
653
654
655 /**
656  * Write the ids and their according index in the given array to a file
657  * Unused
658  */
659 /* static void
660 ids_to_file (char *file_name,
661              struct GNUNET_PeerIdentity *peer_ids,
662              unsigned int num_peer_ids)
663 {
664   unsigned int i;
665
666   for (i=0 ; i < num_peer_ids ; i++)
667   {
668     to_file (file_name,
669              "%u\t%s",
670              i,
671              GNUNET_i2s_full (&peer_ids[i]));
672   }
673 } */
674
675 /**
676  * Test the success of a single test
677  */
678 static int
679 evaluate (void)
680 {
681   unsigned int i;
682   int tmp_ok;
683
684   tmp_ok = 1;
685
686   for (i = 0; i < num_peers; i++)
687   {
688     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
689         "%u. peer [%s] received %u of %u expected peer_ids: %i\n",
690         i,
691         GNUNET_i2s (rps_peers[i].peer_id),
692         rps_peers[i].num_recv_ids,
693         rps_peers[i].num_ids_to_request,
694         (rps_peers[i].num_ids_to_request == rps_peers[i].num_recv_ids));
695     tmp_ok &= (rps_peers[i].num_ids_to_request == rps_peers[i].num_recv_ids);
696   }
697   return tmp_ok? 0 : 1;
698 }
699
700
701 /**
702  * Creates an oplist entry and adds it to the oplist DLL
703  */
704 static struct OpListEntry *
705 make_oplist_entry ()
706 {
707   struct OpListEntry *entry;
708
709   entry = GNUNET_new (struct OpListEntry);
710   GNUNET_CONTAINER_DLL_insert_tail (oplist_head, oplist_tail, entry);
711   return entry;
712 }
713
714
715 /**
716  * @brief Checks if given peer already received its statistics value from the
717  * statistics service.
718  *
719  * @param rps_peer the peer to check for
720  *
721  * @return #GNUNET_YES if so
722  *         #GNUNET_NO otherwise
723  */
724 static int check_statistics_collect_completed_single_peer (
725     const struct RPSPeer *rps_peer)
726 {
727   if (cur_test_run.stat_collect_flags !=
728         (cur_test_run.stat_collect_flags &
729           rps_peer->stat_collected_flags))
730   {
731     return GNUNET_NO;
732   }
733   return GNUNET_YES;
734 }
735 /**
736  * @brief Checks if all peers already received their statistics value from the
737  * statistics service.
738  *
739  * @return #GNUNET_YES if so
740  *         #GNUNET_NO otherwise
741  */
742 static int check_statistics_collect_completed ()
743 {
744   uint32_t i;
745
746   for (i = 0; i < num_peers; i++)
747   {
748     if (GNUNET_NO == check_statistics_collect_completed_single_peer (&rps_peers[i]))
749     {
750       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
751           "At least Peer %" PRIu32 " did not yet receive all statistics values\n",
752           i);
753       return GNUNET_NO;
754     }
755   }
756   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
757       "All peers received their statistics values\n");
758   return GNUNET_YES;
759 }
760
761 /**
762  * Task run on timeout to shut everything down.
763  */
764 static void
765 shutdown_op (void *cls)
766 {
767   unsigned int i;
768
769   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
770               "Shutdown task scheduled, going down.\n");
771   in_shutdown = GNUNET_YES;
772   if (NULL != post_test_task)
773   {
774     GNUNET_SCHEDULER_cancel (post_test_task);
775   }
776   if (NULL != churn_task)
777   {
778     GNUNET_SCHEDULER_cancel (churn_task);
779     churn_task = NULL;
780   }
781   for (i = 0; i < num_peers; i++)
782   {
783     if (NULL != rps_peers[i].rps_handle)
784     {
785       GNUNET_RPS_disconnect (rps_peers[i].rps_handle);
786     }
787     if (NULL != rps_peers[i].op)
788     {
789       GNUNET_TESTBED_operation_done (rps_peers[i].op);
790     }
791   }
792 }
793
794
795 /**
796  * Task run on timeout to collect statistics and potentially shut down.
797  */
798 static void
799 post_test_op (void *cls)
800 {
801   unsigned int i;
802
803   post_test_task = NULL;
804   post_test = GNUNET_YES;
805   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
806               "Post test task scheduled, going down.\n");
807   if (NULL != churn_task)
808   {
809     GNUNET_SCHEDULER_cancel (churn_task);
810     churn_task = NULL;
811   }
812   for (i = 0; i < num_peers; i++)
813   {
814     if (NULL != rps_peers[i].op)
815     {
816       GNUNET_TESTBED_operation_done (rps_peers[i].op);
817       rps_peers[i].op = NULL;
818     }
819     if (NULL != cur_test_run.post_test)
820     {
821       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Executing post_test for peer %u\n", i);
822       cur_test_run.post_test (&rps_peers[i]);
823     }
824   }
825   /* If we do not collect statistics, shut down directly */
826   if (NO_COLLECT_STATISTICS == cur_test_run.have_collect_statistics ||
827       GNUNET_YES == check_statistics_collect_completed())
828   {
829     GNUNET_SCHEDULER_shutdown ();
830   }
831 }
832
833
834 /**
835  * Seed peers.
836  */
837 static void
838 seed_peers (void *cls)
839 {
840   struct RPSPeer *peer = cls;
841   unsigned int amount;
842   unsigned int i;
843
844   // TODO if malicious don't seed mal peers
845   amount = round (.5 * num_peers);
846
847   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Seeding peers:\n");
848   for (i = 0 ; i < amount ; i++)
849     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Seeding %u. peer: %s\n",
850                 i,
851                 GNUNET_i2s (&rps_peer_ids[i]));
852
853   GNUNET_RPS_seed_ids (peer->rps_handle, amount, rps_peer_ids);
854 }
855
856
857 /**
858  * Seed peers.
859  */
860 static void
861 seed_peers_big (void *cls)
862 {
863   struct RPSPeer *peer = cls;
864   unsigned int seed_msg_size;
865   uint32_t num_peers_max;
866   unsigned int amount;
867   unsigned int i;
868
869   seed_msg_size = 8; /* sizeof (struct GNUNET_RPS_CS_SeedMessage) */
870   num_peers_max = (GNUNET_MAX_MESSAGE_SIZE - seed_msg_size) /
871     sizeof (struct GNUNET_PeerIdentity);
872   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
873       "Peers that fit in one seed msg; %u\n",
874       num_peers_max);
875   amount = num_peers_max + (0.5 * num_peers_max);
876   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
877       "Seeding many (%u) peers:\n",
878       amount);
879   struct GNUNET_PeerIdentity ids_to_seed[amount];
880   for (i = 0; i < amount; i++)
881   {
882     ids_to_seed[i] = *peer->peer_id;
883     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Seeding %u. peer: %s\n",
884                 i,
885                 GNUNET_i2s (&ids_to_seed[i]));
886   }
887
888   GNUNET_RPS_seed_ids (peer->rps_handle, amount, ids_to_seed);
889 }
890
891 /**
892  * Get the id of peer i.
893  */
894   void
895 info_cb (void *cb_cls,
896          struct GNUNET_TESTBED_Operation *op,
897          const struct GNUNET_TESTBED_PeerInformation *pinfo,
898          const char *emsg)
899 {
900   struct OpListEntry *entry = (struct OpListEntry *) cb_cls;
901
902   if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
903   {
904     return;
905   }
906
907   if (NULL == pinfo || NULL != emsg)
908   {
909     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Got Error: %s\n", emsg);
910     GNUNET_TESTBED_operation_done (entry->op);
911     return;
912   }
913
914   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
915               "Peer %u is %s\n",
916               entry->index,
917               GNUNET_i2s (pinfo->result.id));
918
919   rps_peer_ids[entry->index] = *(pinfo->result.id);
920   rps_peers[entry->index].peer_id = &rps_peer_ids[entry->index];
921
922   GNUNET_assert (GNUNET_OK ==
923       GNUNET_CONTAINER_multipeermap_put (peer_map,
924         &rps_peer_ids[entry->index],
925         &rps_peers[entry->index],
926         GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
927   tofile ("/tmp/rps/peer_ids",
928            "%u\t%s\n",
929            entry->index,
930            GNUNET_i2s_full (&rps_peer_ids[entry->index]));
931
932   GNUNET_CONTAINER_DLL_remove (oplist_head, oplist_tail, entry);
933   GNUNET_TESTBED_operation_done (entry->op);
934   GNUNET_free (entry);
935 }
936
937
938 /**
939  * Callback to be called when RPS service connect operation is completed
940  *
941  * @param cls the callback closure from functions generating an operation
942  * @param op the operation that has been finished
943  * @param ca_result the RPS service handle returned from rps_connect_adapter
944  * @param emsg error message in case the operation has failed; will be NULL if
945  *          operation has executed successfully.
946  */
947 static void
948 rps_connect_complete_cb (void *cls,
949                          struct GNUNET_TESTBED_Operation *op,
950                          void *ca_result,
951                          const char *emsg)
952 {
953   struct RPSPeer *rps_peer = cls;
954   struct GNUNET_RPS_Handle *rps = ca_result;
955
956   if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
957   {
958     return;
959   }
960
961   rps_peer->rps_handle = rps;
962   rps_peer->online = GNUNET_YES;
963   num_peers_online++;
964
965   GNUNET_assert (op == rps_peer->op);
966   if (NULL != emsg)
967   {
968     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
969                 "Failed to connect to RPS service: %s\n",
970                 emsg);
971     ok = 1;
972     GNUNET_SCHEDULER_shutdown ();
973     return;
974   }
975
976   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Started client successfully\n");
977
978   cur_test_run.main_test (rps_peer);
979 }
980
981
982 /**
983  * Adapter function called to establish a connection to
984  * the RPS service.
985  *
986  * @param cls closure
987  * @param cfg configuration of the peer to connect to; will be available until
988  *          GNUNET_TESTBED_operation_done() is called on the operation returned
989  *          from GNUNET_TESTBED_service_connect()
990  * @return service handle to return in 'op_result', NULL on error
991  */
992 static void *
993 rps_connect_adapter (void *cls,
994                                  const struct GNUNET_CONFIGURATION_Handle *cfg)
995 {
996   struct GNUNET_RPS_Handle *h;
997
998   h = GNUNET_RPS_connect (cfg);
999
1000   if (NULL != cur_test_run.pre_test)
1001     cur_test_run.pre_test (cls, h);
1002
1003   return h;
1004 }
1005
1006 /**
1007  * Called to open a connection to the peer's statistics
1008  *
1009  * @param cls peer context
1010  * @param cfg configuration of the peer to connect to; will be available until
1011  *          GNUNET_TESTBED_operation_done() is called on the operation returned
1012  *          from GNUNET_TESTBED_service_connect()
1013  * @return service handle to return in 'op_result', NULL on error
1014  */
1015 static void *
1016 stat_connect_adapter (void *cls,
1017                       const struct GNUNET_CONFIGURATION_Handle *cfg)
1018 {
1019   struct RPSPeer *peer = cls;
1020
1021   peer->stats_h = GNUNET_STATISTICS_create ("rps-profiler", cfg);
1022   return peer->stats_h;
1023 }
1024
1025 /**
1026  * Called to disconnect from peer's statistics service
1027  *
1028  * @param cls peer context
1029  * @param op_result service handle returned from the connect adapter
1030  */
1031 static void
1032 stat_disconnect_adapter (void *cls, void *op_result)
1033 {
1034   struct RPSPeer *peer = cls;
1035
1036   //GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch_cancel
1037   //              (peer->stats_h, "core", "# peers connected",
1038   //               stat_iterator, peer));
1039   //GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch_cancel
1040   //              (peer->stats_h, "nse", "# peers connected",
1041   //               stat_iterator, peer));
1042   GNUNET_STATISTICS_destroy (op_result, GNUNET_NO);
1043   peer->stats_h = NULL;
1044 }
1045
1046 /**
1047  * Called after successfully opening a connection to a peer's statistics
1048  * service; we register statistics monitoring for CORE and NSE here.
1049  *
1050  * @param cls the callback closure from functions generating an operation
1051  * @param op the operation that has been finished
1052  * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
1053  * @param emsg error message in case the operation has failed; will be NULL if
1054  *          operation has executed successfully.
1055  */
1056 static void
1057 stat_complete_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
1058                   void *ca_result, const char *emsg )
1059 {
1060   //struct GNUNET_STATISTICS_Handle *sh = ca_result;
1061   //struct RPSPeer *peer = (struct RPSPeer *) cls;
1062
1063   if (NULL != emsg)
1064   {
1065     GNUNET_break (0);
1066     return;
1067   }
1068   //GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch
1069   //              (sh, "core", "# peers connected",
1070   //               stat_iterator, peer));
1071   //GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch
1072   //              (sh, "nse", "# peers connected",
1073   //               stat_iterator, peer));
1074 }
1075
1076
1077 /**
1078  * Adapter function called to destroy connection to
1079  * RPS service.
1080  *
1081  * @param cls closure
1082  * @param op_result service handle returned from the connect adapter
1083  */
1084 static void
1085 rps_disconnect_adapter (void *cls,
1086                                           void *op_result)
1087 {
1088   struct RPSPeer *peer = cls;
1089   struct GNUNET_RPS_Handle *h = op_result;
1090   GNUNET_assert (NULL != peer);
1091   GNUNET_RPS_disconnect (h);
1092   peer->rps_handle = NULL;
1093 }
1094
1095
1096 /***********************************************************************
1097  * Definition of tests
1098 ***********************************************************************/
1099
1100 // TODO check whether tests can be stopped earlier
1101 static int
1102 default_eval_cb (void)
1103 {
1104   return evaluate ();
1105 }
1106
1107 static int
1108 no_eval (void)
1109 {
1110   return 0;
1111 }
1112
1113 /**
1114  * Initialise given RPSPeer
1115  */
1116 static void default_init_peer (struct RPSPeer *rps_peer)
1117 {
1118   rps_peer->num_ids_to_request = 1;
1119 }
1120
1121 /**
1122  * Callback to call on receipt of a reply
1123  *
1124  * @param cls closure
1125  * @param n number of peers
1126  * @param recv_peers the received peers
1127  */
1128 static void
1129 default_reply_handle (void *cls,
1130                       uint64_t n,
1131                       const struct GNUNET_PeerIdentity *recv_peers)
1132 {
1133   struct RPSPeer *rps_peer;
1134   struct PendingReply *pending_rep = (struct PendingReply *) cls;
1135   unsigned int i;
1136
1137   rps_peer = pending_rep->rps_peer;
1138   GNUNET_CONTAINER_DLL_remove (rps_peer->pending_rep_head,
1139                                rps_peer->pending_rep_tail,
1140                                pending_rep);
1141   rps_peer->num_pending_reps--;
1142   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1143               "[%s] got %" PRIu64 " peers:\n",
1144               GNUNET_i2s (rps_peer->peer_id),
1145               n);
1146
1147   for (i = 0; i < n; i++)
1148   {
1149     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1150                 "%u: %s\n",
1151                 i,
1152                 GNUNET_i2s (&recv_peers[i]));
1153
1154     rps_peer->num_recv_ids++;
1155   }
1156
1157   if (0 == evaluate () && HAVE_QUICK_QUIT == cur_test_run.have_quick_quit)
1158   {
1159     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test succeeded before timeout\n");
1160     GNUNET_assert (NULL != post_test_task);
1161     GNUNET_SCHEDULER_cancel (post_test_task);
1162     post_test_task = GNUNET_SCHEDULER_add_now (&post_test_op, NULL);
1163     GNUNET_assert (NULL!= post_test_task);
1164   }
1165 }
1166
1167 /**
1168  * Request random peers.
1169  */
1170 static void
1171 request_peers (void *cls)
1172 {
1173   struct PendingRequest *pending_req = cls;
1174   struct RPSPeer *rps_peer;
1175   struct PendingReply *pending_rep;
1176
1177   if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
1178     return;
1179   rps_peer = pending_req->rps_peer;
1180   GNUNET_assert (1 <= rps_peer->num_pending_reqs);
1181   GNUNET_CONTAINER_DLL_remove (rps_peer->pending_req_head,
1182                                rps_peer->pending_req_tail,
1183                                pending_req);
1184   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1185               "Requesting one peer\n");
1186   pending_rep = GNUNET_new (struct PendingReply);
1187   pending_rep->rps_peer = rps_peer;
1188   pending_rep->req_handle = GNUNET_RPS_request_peers (rps_peer->rps_handle,
1189       1,
1190       cur_test_run.reply_handle,
1191       pending_rep);
1192   GNUNET_CONTAINER_DLL_insert_tail (rps_peer->pending_rep_head,
1193                                     rps_peer->pending_rep_tail,
1194                                     pending_rep);
1195   rps_peer->num_pending_reps++;
1196   rps_peer->num_pending_reqs--;
1197 }
1198
1199 static void
1200 cancel_pending_req (struct PendingRequest *pending_req)
1201 {
1202   struct RPSPeer *rps_peer;
1203
1204   rps_peer = pending_req->rps_peer;
1205   GNUNET_CONTAINER_DLL_remove (rps_peer->pending_req_head,
1206                                rps_peer->pending_req_tail,
1207                                pending_req);
1208   rps_peer->num_pending_reqs--;
1209   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1210               "Cancelling pending request\n");
1211   GNUNET_SCHEDULER_cancel (pending_req->request_task);
1212   GNUNET_free (pending_req);
1213 }
1214
1215 static void
1216 cancel_request (struct PendingReply *pending_rep)
1217 {
1218   struct RPSPeer *rps_peer;
1219
1220   rps_peer = pending_rep->rps_peer;
1221   GNUNET_CONTAINER_DLL_remove (rps_peer->pending_rep_head,
1222                                rps_peer->pending_rep_tail,
1223                                pending_rep);
1224   rps_peer->num_pending_reps--;
1225   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1226               "Cancelling request\n");
1227   GNUNET_RPS_request_cancel (pending_rep->req_handle);
1228   GNUNET_free (pending_rep);
1229 }
1230
1231 /**
1232  * Cancel a request.
1233  */
1234 static void
1235 cancel_request_cb (void *cls)
1236 {
1237   struct RPSPeer *rps_peer = cls;
1238   struct PendingReply *pending_rep;
1239
1240   if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
1241     return;
1242   pending_rep = rps_peer->pending_rep_head;
1243   GNUNET_assert (1 <= rps_peer->num_pending_reps);
1244   cancel_request (pending_rep);
1245 }
1246
1247
1248 /**
1249  * Schedule requests for peer @a rps_peer that have neither been scheduled, nor
1250  * issued, nor replied
1251  */
1252 void
1253 schedule_missing_requests (struct RPSPeer *rps_peer)
1254 {
1255   unsigned int i;
1256   struct PendingRequest *pending_req;
1257
1258   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1259       "Scheduling %u - %u missing requests\n",
1260       rps_peer->num_ids_to_request,
1261       rps_peer->num_pending_reqs + rps_peer->num_pending_reps);
1262   GNUNET_assert (rps_peer->num_pending_reqs + rps_peer->num_pending_reps <=
1263       rps_peer->num_ids_to_request);
1264   for (i = rps_peer->num_pending_reqs + rps_peer->num_pending_reps;
1265        i < rps_peer->num_ids_to_request; i++)
1266   {
1267     pending_req = GNUNET_new (struct PendingRequest);
1268     pending_req->rps_peer = rps_peer;
1269     pending_req->request_task = GNUNET_SCHEDULER_add_delayed (
1270         GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
1271           cur_test_run.request_interval * i),
1272         request_peers,
1273         pending_req);
1274     GNUNET_CONTAINER_DLL_insert_tail (rps_peer->pending_req_head,
1275                                       rps_peer->pending_req_tail,
1276                                       pending_req);
1277     rps_peer->num_pending_reqs++;
1278   }
1279 }
1280
1281 void
1282 cancel_pending_req_rep (struct RPSPeer *rps_peer)
1283 {
1284   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1285       "Cancelling all (pending) requests.\n");
1286   while (NULL != rps_peer->pending_req_head)
1287     cancel_pending_req (rps_peer->pending_req_head);
1288   GNUNET_assert (0 == rps_peer->num_pending_reqs);
1289   while (NULL != rps_peer->pending_rep_head)
1290     cancel_request (rps_peer->pending_rep_head);
1291   GNUNET_assert (0 == rps_peer->num_pending_reps);
1292 }
1293
1294 /***********************************
1295  * MALICIOUS
1296 ***********************************/
1297
1298 /**
1299  * Initialise only non-mal RPSPeers
1300  */
1301 static void mal_init_peer (struct RPSPeer *rps_peer)
1302 {
1303   if (rps_peer->index >= round (portion * num_peers))
1304     rps_peer->num_ids_to_request = 1;
1305 }
1306
1307
1308 /**
1309  * @brief Set peers to (non-)malicious before execution
1310  *
1311  * Of signature #PreTest
1312  *
1313  * @param rps_peer the peer to set (non-) malicious
1314  * @param h the handle to the service
1315  */
1316 static void
1317 mal_pre (struct RPSPeer *rps_peer, struct GNUNET_RPS_Handle *h)
1318 {
1319   #ifdef ENABLE_MALICIOUS
1320   uint32_t num_mal_peers;
1321
1322   GNUNET_assert ( (1 >= portion) &&
1323                   (0 <  portion) );
1324   num_mal_peers = round (portion * num_peers);
1325
1326   if (rps_peer->index < num_mal_peers)
1327   {
1328     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1329                 "%u. peer [%s] of %" PRIu32 " malicious peers turning malicious\n",
1330                 rps_peer->index,
1331                 GNUNET_i2s (rps_peer->peer_id),
1332                 num_mal_peers);
1333
1334     GNUNET_RPS_act_malicious (h, mal_type, num_mal_peers,
1335                               rps_peer_ids, target_peer);
1336   }
1337   #endif /* ENABLE_MALICIOUS */
1338 }
1339
1340 static void
1341 mal_cb (struct RPSPeer *rps_peer)
1342 {
1343   uint32_t num_mal_peers;
1344
1345   if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
1346   {
1347     return;
1348   }
1349
1350   #ifdef ENABLE_MALICIOUS
1351   GNUNET_assert ( (1 >= portion) &&
1352                   (0 <  portion) );
1353   num_mal_peers = round (portion * num_peers);
1354
1355   if (rps_peer->index >= num_mal_peers)
1356   { /* It's useless to ask a malicious peer about a random sample -
1357        it's not sampling */
1358     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
1359                                   seed_peers, rps_peer);
1360     schedule_missing_requests (rps_peer);
1361   }
1362   #endif /* ENABLE_MALICIOUS */
1363 }
1364
1365
1366 /***********************************
1367  * SINGLE_REQUEST
1368 ***********************************/
1369 static void
1370 single_req_cb (struct RPSPeer *rps_peer)
1371 {
1372   if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
1373   {
1374     return;
1375   }
1376
1377   schedule_missing_requests (rps_peer);
1378 }
1379
1380 /***********************************
1381  * DELAYED_REQUESTS
1382 ***********************************/
1383 static void
1384 delay_req_cb (struct RPSPeer *rps_peer)
1385 {
1386   if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
1387   {
1388     return;
1389   }
1390
1391   schedule_missing_requests (rps_peer);
1392 }
1393
1394 /***********************************
1395  * SEED
1396 ***********************************/
1397 static void
1398 seed_cb (struct RPSPeer *rps_peer)
1399 {
1400   if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
1401   {
1402     return;
1403   }
1404
1405   GNUNET_SCHEDULER_add_delayed (
1406       GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10),
1407       seed_peers, rps_peer);
1408 }
1409
1410 /***********************************
1411  * SEED_BIG
1412 ***********************************/
1413 static void
1414 seed_big_cb (struct RPSPeer *rps_peer)
1415 {
1416   if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
1417   {
1418     return;
1419   }
1420
1421   // TODO test seeding > GNUNET_MAX_MESSAGE_SIZE peers
1422   GNUNET_SCHEDULER_add_delayed (
1423       GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
1424       seed_peers_big, rps_peer);
1425 }
1426
1427 /***********************************
1428  * SINGLE_PEER_SEED
1429 ***********************************/
1430 static void
1431 single_peer_seed_cb (struct RPSPeer *rps_peer)
1432 {
1433   // TODO
1434 }
1435
1436 /***********************************
1437  * SEED_REQUEST
1438 ***********************************/
1439 static void
1440 seed_req_cb (struct RPSPeer *rps_peer)
1441 {
1442   if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
1443   {
1444     return;
1445   }
1446
1447   GNUNET_SCHEDULER_add_delayed (
1448       GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
1449       seed_peers, rps_peer);
1450   schedule_missing_requests (rps_peer);
1451 }
1452
1453 //TODO start big mal
1454
1455 /***********************************
1456  * REQUEST_CANCEL
1457 ***********************************/
1458 static void
1459 req_cancel_cb (struct RPSPeer *rps_peer)
1460 {
1461   if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
1462   {
1463     return;
1464   }
1465
1466   schedule_missing_requests (rps_peer);
1467   GNUNET_SCHEDULER_add_delayed (
1468       GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
1469                                      (cur_test_run.request_interval + 1)),
1470       cancel_request_cb, rps_peer);
1471 }
1472
1473 /***********************************
1474  * CHURN
1475 ***********************************/
1476
1477 static void
1478 churn (void *cls);
1479
1480 /**
1481  * @brief Starts churn
1482  *
1483  * Has signature of #MainTest
1484  *
1485  * This is not implemented too nicely as this is called for each peer, but we
1486  * only need to call it once. (Yes we check that we only schedule the task
1487  * once.)
1488  *
1489  * @param rps_peer The peer it's called for
1490  */
1491 static void
1492 churn_test_cb (struct RPSPeer *rps_peer)
1493 {
1494   if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
1495   {
1496     return;
1497   }
1498
1499   /* Start churn */
1500   if (HAVE_CHURN == cur_test_run.have_churn && NULL == churn_task)
1501   {
1502     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1503                 "Starting churn task\n");
1504     churn_task = GNUNET_SCHEDULER_add_delayed (
1505           GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
1506           churn,
1507           NULL);
1508   } else {
1509     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1510                 "Not starting churn task\n");
1511   }
1512
1513   schedule_missing_requests (rps_peer);
1514 }
1515
1516 /***********************************
1517  * PROFILER
1518 ***********************************/
1519
1520 /**
1521  * Callback to be called when RPS service is started or stopped at peers
1522  *
1523  * @param cls NULL
1524  * @param op the operation handle
1525  * @param emsg NULL on success; otherwise an error description
1526  */
1527 static void
1528 churn_cb (void *cls,
1529           struct GNUNET_TESTBED_Operation *op,
1530           const char *emsg)
1531 {
1532   // FIXME
1533   struct OpListEntry *entry = cls;
1534
1535   if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
1536   {
1537     return;
1538   }
1539
1540   GNUNET_TESTBED_operation_done (entry->op);
1541   if (NULL != emsg)
1542   {
1543     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to start/stop RPS at a peer\n");
1544     GNUNET_SCHEDULER_shutdown ();
1545     return;
1546   }
1547   GNUNET_assert (0 != entry->delta);
1548
1549   num_peers_online += entry->delta;
1550
1551   if (PEER_GO_OFFLINE == entry->delta)
1552   { /* Peer hopefully just went offline */
1553     if (GNUNET_YES != rps_peers[entry->index].online)
1554     {
1555       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1556                   "peer %s was expected to go offline but is still marked as online\n",
1557                   GNUNET_i2s (rps_peers[entry->index].peer_id));
1558       GNUNET_break (0);
1559     }
1560     else
1561     {
1562       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1563                   "peer %s probably went offline as expected\n",
1564                   GNUNET_i2s (rps_peers[entry->index].peer_id));
1565     }
1566     rps_peers[entry->index].online = GNUNET_NO;
1567   }
1568
1569   else if (PEER_GO_ONLINE < entry->delta)
1570   { /* Peer hopefully just went online */
1571     if (GNUNET_NO != rps_peers[entry->index].online)
1572     {
1573       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1574                   "peer %s was expected to go online but is still marked as offline\n",
1575                   GNUNET_i2s (rps_peers[entry->index].peer_id));
1576       GNUNET_break (0);
1577     }
1578     else
1579     {
1580       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1581                   "peer %s probably went online as expected\n",
1582                   GNUNET_i2s (rps_peers[entry->index].peer_id));
1583       if (NULL != cur_test_run.pre_test)
1584       {
1585         cur_test_run.pre_test (&rps_peers[entry->index],
1586             rps_peers[entry->index].rps_handle);
1587         schedule_missing_requests (&rps_peers[entry->index]);
1588       }
1589     }
1590     rps_peers[entry->index].online = GNUNET_YES;
1591   }
1592   else
1593   {
1594     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1595         "Invalid value for delta: %i\n", entry->delta);
1596     GNUNET_break (0);
1597   }
1598
1599   GNUNET_CONTAINER_DLL_remove (oplist_head, oplist_tail, entry);
1600   rps_peers[entry->index].entry_op_manage = NULL;
1601   GNUNET_free (entry);
1602   //if (num_peers_in_round[current_round] == peers_running)
1603   //  run_round ();
1604 }
1605
1606 /**
1607  * @brief Set the rps-service up or down for a specific peer
1608  *
1609  * @param i index of action
1610  * @param j index of peer
1611  * @param delta (#PEER_ONLINE_DELTA) down (-1) or up (1)
1612  * @param prob_go_on_off the probability of the action
1613  */
1614 static void
1615 manage_service_wrapper (unsigned int i, unsigned int j,
1616                         enum PEER_ONLINE_DELTA delta,
1617                         double prob_go_on_off)
1618 {
1619   struct OpListEntry *entry = NULL;
1620   uint32_t prob;
1621
1622   /* make sure that management operation is not already scheduled */
1623   if (NULL != rps_peers[j].entry_op_manage)
1624   {
1625     return;
1626   }
1627
1628   prob = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1629                                    UINT32_MAX);
1630   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1631               "%u. selected peer (%u: %s) is %s.\n",
1632               i,
1633               j,
1634               GNUNET_i2s (rps_peers[j].peer_id),
1635               (PEER_GO_ONLINE == delta) ? "online" : "offline");
1636   if (prob < prob_go_on_off * UINT32_MAX)
1637   {
1638     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1639                 "%s goes %s\n",
1640                 GNUNET_i2s (rps_peers[j].peer_id),
1641                 (PEER_GO_OFFLINE == delta) ? "offline" : "online");
1642
1643     if (PEER_GO_OFFLINE == delta)
1644       cancel_pending_req_rep (&rps_peers[j]);
1645     entry = make_oplist_entry ();
1646     entry->delta = delta;
1647     entry->index = j;
1648     entry->op = GNUNET_TESTBED_peer_manage_service (NULL,
1649                                                     testbed_peers[j],
1650                                                     "rps",
1651                                                     &churn_cb,
1652                                                     entry,
1653                                                     (PEER_GO_OFFLINE == delta) ? 0 : 1);
1654     rps_peers[j].entry_op_manage = entry;
1655   }
1656 }
1657
1658
1659 static void
1660 churn (void *cls)
1661 {
1662   unsigned int i;
1663   unsigned int j;
1664   double portion_online;
1665   unsigned int *permut;
1666   double prob_go_offline;
1667   double portion_go_online;
1668   double portion_go_offline;
1669
1670   if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
1671   {
1672     return;
1673   }
1674   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1675               "Churn function executing\n");
1676
1677   churn_task = NULL; /* Should be invalid by now */
1678
1679   /* Compute the probability for an online peer to go offline
1680    * this round */
1681   portion_online = num_peers_online * 1.0 / num_peers;
1682   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1683               "Portion online: %f\n",
1684               portion_online);
1685   portion_go_online = ((1 - portion_online) * .5 * .66);
1686   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1687               "Portion that should go online: %f\n",
1688               portion_go_online);
1689   portion_go_offline = (portion_online + portion_go_online) - .75;
1690   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1691               "Portion that probably goes offline: %f\n",
1692               portion_go_offline);
1693   prob_go_offline = portion_go_offline / (portion_online * .5);
1694   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1695               "Probability of a selected online peer to go offline: %f\n",
1696               prob_go_offline);
1697
1698   permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_WEAK,
1699                                          (unsigned int) num_peers);
1700
1701   /* Go over 50% randomly chosen peers */
1702   for (i = 0; i < .5 * num_peers; i++)
1703   {
1704     j = permut[i];
1705
1706     /* If online, shut down with certain probability */
1707     if (GNUNET_YES == rps_peers[j].online)
1708     {
1709       manage_service_wrapper (i, j, -1, prob_go_offline);
1710     }
1711
1712     /* If offline, restart with certain probability */
1713     else if (GNUNET_NO == rps_peers[j].online)
1714     {
1715       manage_service_wrapper (i, j, 1, 0.66);
1716     }
1717   }
1718
1719   GNUNET_free (permut);
1720
1721   churn_task = GNUNET_SCHEDULER_add_delayed (
1722         GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
1723         churn,
1724         NULL);
1725 }
1726
1727
1728 /**
1729  * Initialise given RPSPeer
1730  */
1731 static void profiler_init_peer (struct RPSPeer *rps_peer)
1732 {
1733   if (num_peers - 1 == rps_peer->index)
1734     rps_peer->num_ids_to_request = cur_test_run.num_requests;
1735 }
1736
1737
1738 /**
1739  * Callback to call on receipt of a reply
1740  *
1741  * @param cls closure
1742  * @param n number of peers
1743  * @param recv_peers the received peers
1744  */
1745 static void
1746 profiler_reply_handle (void *cls,
1747                       uint64_t n,
1748                       const struct GNUNET_PeerIdentity *recv_peers)
1749 {
1750   struct RPSPeer *rps_peer;
1751   struct RPSPeer *rcv_rps_peer;
1752   char *file_name;
1753   char *file_name_dh;
1754   unsigned int i;
1755   struct PendingReply *pending_rep = (struct PendingReply *) cls;
1756
1757   rps_peer = pending_rep->rps_peer;
1758   file_name = "/tmp/rps/received_ids";
1759   file_name_dh = "/tmp/rps/diehard_input";
1760   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1761               "[%s] got %" PRIu64 " peers:\n",
1762               GNUNET_i2s (rps_peer->peer_id),
1763               n);
1764   for (i = 0; i < n; i++)
1765   {
1766     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1767                 "%u: %s\n",
1768                 i,
1769                 GNUNET_i2s (&recv_peers[i]));
1770     tofile (file_name,
1771              "%s\n",
1772              GNUNET_i2s_full (&recv_peers[i]));
1773     rcv_rps_peer = GNUNET_CONTAINER_multipeermap_get (peer_map, &recv_peers[i]);
1774     GNUNET_assert (NULL != rcv_rps_peer);
1775     tofile (file_name_dh,
1776              "%" PRIu32 "\n",
1777              (uint32_t) rcv_rps_peer->index);
1778   }
1779   default_reply_handle (cls, n, recv_peers);
1780 }
1781
1782
1783 static void
1784 profiler_cb (struct RPSPeer *rps_peer)
1785 {
1786   if (GNUNET_YES == in_shutdown || GNUNET_YES == post_test)
1787   {
1788     return;
1789   }
1790
1791   /* Start churn */
1792   if (HAVE_CHURN == cur_test_run.have_churn && NULL == churn_task)
1793   {
1794     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1795                 "Starting churn task\n");
1796     churn_task = GNUNET_SCHEDULER_add_delayed (
1797           GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
1798           churn,
1799           NULL);
1800   } else {
1801     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1802                 "Not starting churn task\n");
1803   }
1804
1805   /* Only request peer ids at one peer.
1806    * (It's the before-last because last one is target of the focussed attack.)
1807    */
1808   if (eval_peer == rps_peer)
1809     schedule_missing_requests (rps_peer);
1810 }
1811
1812 /**
1813  * Function called from #profiler_eval with a filename.
1814  *
1815  * @param cls closure
1816  * @param filename complete filename (absolute path)
1817  * @return #GNUNET_OK to continue to iterate,
1818  *  #GNUNET_NO to stop iteration with no error,
1819  *  #GNUNET_SYSERR to abort iteration with error!
1820  */
1821 int
1822 file_name_cb (void *cls, const char *filename)
1823 {
1824   if (NULL != strstr (filename, "sampler_el"))
1825   {
1826     struct RPS_SamplerElement *s_elem;
1827     struct GNUNET_CRYPTO_AuthKey auth_key;
1828     const char *key_char;
1829     uint32_t i;
1830
1831     key_char = filename + 20; /* Length of "/tmp/rps/sampler_el-" */
1832     tofile (filename, "--------------------------\n");
1833
1834     auth_key = string_to_auth_key (key_char);
1835     s_elem = RPS_sampler_elem_create ();
1836     RPS_sampler_elem_set (s_elem, auth_key);
1837
1838     for (i = 0; i < num_peers; i++)
1839     {
1840       RPS_sampler_elem_next (s_elem, &rps_peer_ids[i]);
1841     }
1842     RPS_sampler_elem_destroy (s_elem);
1843   }
1844   return GNUNET_OK;
1845 }
1846
1847 /**
1848  * This is run after the test finished.
1849  *
1850  * Compute all perfect samples.
1851  */
1852 int
1853 profiler_eval (void)
1854 {
1855   /* Compute perfect sample for each sampler element */
1856   if (-1 == GNUNET_DISK_directory_scan ("/tmp/rps/", file_name_cb, NULL))
1857   {
1858     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Scan of directory failed\n");
1859   }
1860
1861   return evaluate ();
1862 }
1863
1864 static uint32_t fac (uint32_t x)
1865 {
1866   if (1 >= x)
1867   {
1868     return x;
1869   }
1870   return x * fac (x - 1);
1871 }
1872
1873 static uint32_t binom (uint32_t n, uint32_t k)
1874 {
1875   //GNUNET_assert (n >= k);
1876   if (k > n) return 0;
1877   if (0 > n) return 0;
1878   if (0 > k) return 0;
1879   if (0 == k) return 1;
1880   return fac (n)
1881     /
1882     fac(k) * fac(n - k);
1883 }
1884
1885 /**
1886  * @brief is b in view of a?
1887  *
1888  * @param a
1889  * @param b
1890  *
1891  * @return
1892  */
1893 static int is_in_view (uint32_t a, uint32_t b)
1894 {
1895   uint32_t i;
1896   for (i = 0; i < rps_peers[a].cur_view_count; i++)
1897   {
1898     if (0 == memcmp (rps_peers[b].peer_id,
1899           &rps_peers[a].cur_view[i],
1900           sizeof (struct GNUNET_PeerIdentity)))
1901     {
1902       return GNUNET_YES;
1903     }
1904   }
1905   return GNUNET_NO;
1906 }
1907
1908 static uint32_t get_idx_of_pid (const struct GNUNET_PeerIdentity *pid)
1909 {
1910   uint32_t i;
1911
1912   for (i = 0; i < num_peers; i++)
1913   {
1914     if (0 == memcmp (pid,
1915           rps_peers[i].peer_id,
1916           sizeof (struct GNUNET_PeerIdentity)))
1917     {
1918       return i;
1919     }
1920   }
1921   //return 0; /* Should not happen - make compiler happy */
1922   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1923              "No known _PeerIdentity %s!\n",
1924              GNUNET_i2s_full (pid));
1925   GNUNET_assert (0);
1926 }
1927
1928 /**
1929  * @brief Counts number of peers in view of a that have b in their view
1930  *
1931  * @param a
1932  * @param uint32_tb
1933  *
1934  * @return
1935  */
1936 static uint32_t count_containing_views (uint32_t a, uint32_t b)
1937 {
1938   uint32_t i;
1939   uint32_t peer_idx;
1940   uint32_t count = 0;
1941
1942   for (i = 0; i < rps_peers[a].cur_view_count; i++)
1943   {
1944     peer_idx = get_idx_of_pid (&rps_peers[a].cur_view[i]);
1945     if (GNUNET_YES == is_in_view (peer_idx, b))
1946     {
1947       count++;
1948     }
1949   }
1950   return count;
1951 }
1952
1953 /**
1954  * @brief Computes the probability for each other peer to be selected by the
1955  * sampling process based on the views of all peers
1956  *
1957  * @param peer_idx index of the peer that is about to sample
1958  */
1959 static void compute_probabilities (uint32_t peer_idx)
1960 {
1961   //double probs[num_peers] = { 0 };
1962   double probs[num_peers];
1963   size_t probs_as_str_size = (num_peers * 10 + 1) * sizeof (char);
1964   char *probs_as_str = GNUNET_malloc (probs_as_str_size);
1965   char *probs_as_str_cpy;
1966   uint32_t i;
1967   double prob_push;
1968   double prob_pull;
1969   uint32_t view_size;
1970   uint32_t cont_views;
1971   uint32_t number_of_being_in_pull_events;
1972   int tmp;
1973   uint32_t count_non_zero_prob = 0;
1974
1975   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1976       "Computing probabilities for peer %" PRIu32 "\n", peer_idx);
1977   /* Firstly without knowledge of old views */
1978   for (i = 0; i < num_peers; i++)
1979   {
1980     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1981         "\tfor peer %" PRIu32 ":\n", i);
1982     view_size = rps_peers[i].cur_view_count;
1983     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1984         "\t\tview_size: %" PRIu32 "\n", view_size);
1985     /* For peer i the probability of being sampled is
1986      * evenly distributed among all possibly observed peers. */
1987     /* We could have observed a peer in three cases:
1988      *   1. peer sent a push
1989      *   2. peer was contained in a pull reply
1990      *   3. peer was in history (sampler) - ignored for now */
1991     /* 1. Probability of having received a push from peer i */
1992     if ((GNUNET_YES == is_in_view (i, peer_idx)) &&
1993         (1 <= (0.45 * view_size)))
1994     {
1995       prob_push = 1.0 * binom (0.45 * view_size, 1)
1996         /
1997         binom (view_size, 0.45 * view_size);
1998       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1999                  "\t\t%" PRIu32 " is in %" PRIu32 "'s view, prob: %f\n",
2000                  peer_idx,
2001                  i,
2002                  prob_push);
2003       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2004                  "\t\tposs choices from view: %" PRIu32 ", containing i: %" PRIu32 "\n",
2005                  binom (view_size, 0.45 * view_size),
2006                  binom (0.45 * view_size, 1));
2007     } else {
2008       prob_push = 0;
2009       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2010                  "\t\t%" PRIu32 " is not in %" PRIu32 "'s view, prob: 0\n",
2011                  peer_idx,
2012                  i);
2013     }
2014     /* 2. Probability of peer i being contained in pulls */
2015     view_size = rps_peers[peer_idx].cur_view_count;
2016     cont_views = count_containing_views (peer_idx, i);
2017     number_of_being_in_pull_events =
2018       (binom (view_size, 0.45 * view_size) -
2019        binom (view_size - cont_views, 0.45 * view_size));
2020     if (0 != number_of_being_in_pull_events)
2021     {
2022       prob_pull = number_of_being_in_pull_events
2023         /
2024         (1.0 * binom (view_size, 0.45 * view_size));
2025     } else
2026     {
2027       prob_pull = 0;
2028     }
2029     probs[i] = prob_push + prob_pull - (prob_push * prob_pull);
2030     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2031                "\t\t%" PRIu32 " has %" PRIu32 " of %" PRIu32
2032                " peers in its view who know %" PRIu32 " prob: %f\n",
2033                peer_idx,
2034                cont_views,
2035                view_size,
2036                i,
2037                prob_pull);
2038     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2039                "\t\tnumber of possible pull combinations: %" PRIu32 "\n",
2040                binom (view_size, 0.45 * view_size));
2041     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2042                "\t\tnumber of possible pull combinations without %" PRIu32
2043                ": %" PRIu32 "\n",
2044                i,
2045                binom (view_size - cont_views, 0.45 * view_size));
2046     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2047                "\t\tnumber of possible pull combinations with %" PRIu32
2048                ": %" PRIu32 "\n",
2049                i,
2050                number_of_being_in_pull_events);
2051
2052     if (0 != probs[i]) count_non_zero_prob++;
2053   }
2054   /* normalize */
2055   if (0 != count_non_zero_prob)
2056   {
2057     for (i = 0; i < num_peers; i++)
2058     {
2059       probs[i] = probs[i] * (1.0 / count_non_zero_prob);
2060     }
2061   } else {
2062     for (i = 0; i < num_peers; i++)
2063     {
2064       probs[i] = 0;
2065     }
2066   }
2067   /* str repr */
2068   for (i = 0; i < num_peers; i++)
2069   {
2070     probs_as_str_cpy = GNUNET_strndup (probs_as_str, probs_as_str_size);
2071     tmp = GNUNET_snprintf (probs_as_str,
2072                            probs_as_str_size,
2073                            "%s %7.6f", probs_as_str_cpy, probs[i]);
2074     GNUNET_free (probs_as_str_cpy);
2075     GNUNET_assert (0 <= tmp);
2076   }
2077
2078   to_file_w_len (rps_peers[peer_idx].file_name_probs,
2079                  probs_as_str_size,
2080                  probs_as_str);
2081   GNUNET_free (probs_as_str);
2082 }
2083
2084 /**
2085  * @brief This counts the number of peers in which views a given peer occurs.
2086  *
2087  * It also stores this value in the rps peer.
2088  *
2089  * @param peer_idx the index of the peer to count the representation
2090  *
2091  * @return the number of occurrences
2092  */
2093 static uint32_t count_peer_in_views_2 (uint32_t peer_idx)
2094 {
2095   uint32_t i, j;
2096   uint32_t count = 0;
2097
2098   for (i = 0; i < num_peers; i++) /* Peer in which view is counted */
2099   {
2100     for (j = 0; j < rps_peers[i].cur_view_count; j++) /* entry in view */
2101     {
2102       if (0 == memcmp (rps_peers[peer_idx].peer_id,
2103             &rps_peers[i].cur_view[j],
2104             sizeof (struct GNUNET_PeerIdentity)))
2105       {
2106         count++;
2107         break;
2108       }
2109     }
2110   }
2111   rps_peers[peer_idx].count_in_views = count;
2112   return count;
2113 }
2114
2115 static uint32_t cumulated_view_sizes ()
2116 {
2117   uint32_t i;
2118
2119   view_sizes = 0;
2120   for (i = 0; i < num_peers; i++) /* Peer in which view is counted */
2121   {
2122     view_sizes += rps_peers[i].cur_view_count;
2123   }
2124   return view_sizes;
2125 }
2126
2127 static void count_peer_in_views (uint32_t *count_peers)
2128 {
2129   uint32_t i, j;
2130
2131   for (i = 0; i < num_peers; i++) /* Peer in which view is counted */
2132   {
2133     for (j = 0; j < rps_peers[i].cur_view_count; j++) /* entry in view */
2134     {
2135       if (0 == memcmp (rps_peers[i].peer_id,
2136             &rps_peers[i].cur_view[j],
2137             sizeof (struct GNUNET_PeerIdentity)))
2138       {
2139         count_peers[i]++;
2140       }
2141     }
2142   }
2143 }
2144
2145 void compute_diversity ()
2146 {
2147   uint32_t i;
2148   /* ith entry represents the numer of occurrences in other peer's views */
2149   uint32_t *count_peers = GNUNET_new_array (num_peers, uint32_t);
2150   uint32_t views_total_size;
2151   double expected;
2152   /* deviation from expected number of peers */
2153   double *deviation = GNUNET_new_array (num_peers, double);
2154
2155   views_total_size = 0;
2156   expected = 0;
2157
2158   /* For each peer count its representation in other peer's views*/
2159   for (i = 0; i < num_peers; i++) /* Peer to count */
2160   {
2161     views_total_size += rps_peers[i].cur_view_count;
2162     count_peer_in_views (count_peers);
2163     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2164                "Counted representation of %" PRIu32 "th peer [%s]: %" PRIu32"\n",
2165                i,
2166                GNUNET_i2s (rps_peers[i].peer_id),
2167                count_peers[i]);
2168   }
2169
2170   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2171              "size of all views combined: %" PRIu32 "\n",
2172              views_total_size);
2173   expected = ((double) 1/num_peers) * views_total_size;
2174   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2175              "Expected number of occurrences of each peer in all views: %f\n",
2176              expected);
2177   for (i = 0; i < num_peers; i++) /* Peer to count */
2178   {
2179     deviation[i] = expected - count_peers[i];
2180     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2181                "Deviation from expectation: %f\n", deviation[i]);
2182   }
2183   GNUNET_free (count_peers);
2184   GNUNET_free (deviation);
2185 }
2186
2187 void print_view_sizes()
2188 {
2189   uint32_t i;
2190
2191   for (i = 0; i < num_peers; i++) /* Peer to count */
2192   {
2193     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2194                "View size of %" PRIu32 ". [%s] is %" PRIu32 "\n",
2195                i,
2196                GNUNET_i2s (rps_peers[i].peer_id),
2197                rps_peers[i].cur_view_count);
2198   }
2199 }
2200
2201 void all_views_updated_cb()
2202 {
2203   compute_diversity();
2204   print_view_sizes();
2205 }
2206
2207 void view_update_cb (void *cls,
2208                      uint64_t view_size,
2209                      const struct GNUNET_PeerIdentity *peers)
2210 {
2211   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2212               "View was updated (%" PRIu64 ")\n", view_size);
2213   struct RPSPeer *rps_peer = (struct RPSPeer *) cls;
2214   to_file ("/tmp/rps/view_sizes.txt",
2215          "%" PRIu64 " %" PRIu32 "",
2216          rps_peer->index,
2217          view_size);
2218   for (int i = 0; i < view_size; i++)
2219   {
2220     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2221                "\t%s\n", GNUNET_i2s (&peers[i]));
2222   }
2223   GNUNET_array_grow (rps_peer->cur_view,
2224                      rps_peer->cur_view_count,
2225                      view_size);
2226   //*rps_peer->cur_view = *peers;
2227   GNUNET_memcpy (rps_peer->cur_view,
2228                  peers,
2229                  view_size * sizeof (struct GNUNET_PeerIdentity));
2230   to_file ("/tmp/rps/count_in_views.txt",
2231          "%" PRIu64 " %" PRIu32 "",
2232          rps_peer->index,
2233          count_peer_in_views_2 (rps_peer->index));
2234   cumulated_view_sizes();
2235   if (0 != view_size)
2236   {
2237     to_file ("/tmp/rps/repr.txt",
2238            "%" PRIu64 /* index */
2239            " %" PRIu32 /* occurrence in views */
2240            " %" PRIu32 /* view sizes */
2241            " %f" /* fraction of repr in views */
2242            " %f" /* average view size */
2243            " %f" /* prob of occurrence in view slot */
2244            " %f" "", /* exp frac of repr in views */
2245            rps_peer->index,
2246            count_peer_in_views_2 (rps_peer->index),
2247            view_sizes,
2248            count_peer_in_views_2 (rps_peer->index) / (view_size * 1.0), /* fraction of representation in views */
2249            view_sizes / (view_size * 1.0), /* average view size */
2250            1.0 /view_size, /* prob of occurrence in view slot */
2251            (1.0/view_size) * (view_sizes/view_size) /* expected fraction of repr in views */
2252            );
2253   }
2254   compute_probabilities (rps_peer->index);
2255   all_views_updated_cb();
2256 }
2257
2258 static void
2259 pre_profiler (struct RPSPeer *rps_peer, struct GNUNET_RPS_Handle *h)
2260 {
2261   rps_peer->file_name_probs =
2262     store_prefix_file_name (rps_peer->peer_id, "probs");
2263   GNUNET_RPS_view_request (h, 0, view_update_cb, rps_peer);
2264 }
2265
2266 void write_final_stats (void){
2267   uint32_t i;
2268
2269   for (i = 0; i < num_peers; i++)
2270   {
2271     to_file ("/tmp/rps/final_stats.dat",
2272              "%" PRIu32 " " /* index */
2273              "%s %" /* id */
2274              PRIu64 " %" /* rounds */
2275              PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" /* blocking */
2276              PRIu64 " %" PRIu64 " %" PRIu64 " %" /* issued */
2277              PRIu64 " %" PRIu64 " %" PRIu64 " %" /* sent */
2278              PRIu64 " %" PRIu64 " %" PRIu64 /* recv */,
2279              i,
2280              GNUNET_i2s (rps_peers[i].peer_id),
2281              rps_peers[i].num_rounds,
2282              rps_peers[i].num_blocks,
2283              rps_peers[i].num_blocks_many_push,
2284              rps_peers[i].num_blocks_no_push,
2285              rps_peers[i].num_blocks_no_pull,
2286              rps_peers[i].num_blocks_many_push_no_pull,
2287              rps_peers[i].num_blocks_no_push_no_pull,
2288              rps_peers[i].num_issued_push,
2289              rps_peers[i].num_issued_pull_req,
2290              rps_peers[i].num_issued_pull_rep,
2291              rps_peers[i].num_sent_push,
2292              rps_peers[i].num_sent_pull_req,
2293              rps_peers[i].num_sent_pull_rep,
2294              rps_peers[i].num_recv_push,
2295              rps_peers[i].num_recv_pull_req,
2296              rps_peers[i].num_recv_pull_rep);
2297   }
2298 }
2299
2300 /**
2301  * Continuation called by #GNUNET_STATISTICS_get() functions.
2302  *
2303  * Remembers that this specific statistics value was received for this peer.
2304  * Checks whether all peers received their statistics yet.
2305  * Issues the shutdown.
2306  *
2307  * @param cls closure
2308  * @param success #GNUNET_OK if statistics were
2309  *        successfully obtained, #GNUNET_SYSERR if not.
2310  */
2311 void
2312 post_test_shutdown_ready_cb (void *cls,
2313                              int success)
2314 {
2315   struct STATcls *stat_cls = (struct STATcls *) cls;
2316   struct RPSPeer *rps_peer = stat_cls->rps_peer;
2317   if (GNUNET_OK == success)
2318   {
2319     /* set flag that we we got the value */
2320     rps_peer->stat_collected_flags |= stat_cls->stat_type;
2321   } else {
2322     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2323         "Peer %u did not receive statistics value\n",
2324         rps_peer->index);
2325     GNUNET_free (stat_cls);
2326     GNUNET_break (0);
2327   }
2328
2329   if (NULL != rps_peer->stat_op &&
2330       GNUNET_YES == check_statistics_collect_completed_single_peer (rps_peer))
2331   {
2332     GNUNET_TESTBED_operation_done (rps_peer->stat_op);
2333   }
2334
2335   write_final_stats ();
2336   if (GNUNET_YES == check_statistics_collect_completed())
2337   {
2338     //write_final_stats ();
2339     GNUNET_free (stat_cls);
2340     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2341         "Shutting down\n");
2342     GNUNET_SCHEDULER_shutdown ();
2343   } else {
2344     GNUNET_free (stat_cls);
2345   }
2346 }
2347
2348 /**
2349  * @brief Converts string representation to the corresponding #STAT_TYPE enum.
2350  *
2351  * @param stat_str string representation of statistics specifier
2352  *
2353  * @return corresponding enum
2354  */
2355 enum STAT_TYPE stat_str_2_type (const char *stat_str)
2356 {
2357   if (0 == strncmp ("# rounds blocked - no pull replies", stat_str, strlen ("# rounds blocked - no pull replies")))
2358   {
2359     return STAT_TYPE_BLOCKS_NO_PULL;
2360   }
2361   else if (0 == strncmp ("# rounds blocked - too many pushes, no pull replies", stat_str, strlen ("# rounds blocked - too many pushes, no pull replies")))
2362   {
2363     return STAT_TYPE_BLOCKS_MANY_PUSH_NO_PULL;
2364   }
2365   else if (0 == strncmp ("# rounds blocked - too many pushes", stat_str, strlen ("# rounds blocked - too many pushes")))
2366   {
2367     return STAT_TYPE_BLOCKS_MANY_PUSH;
2368   }
2369   else if (0 == strncmp ("# rounds blocked - no pushes, no pull replies", stat_str, strlen ("# rounds blocked - no pushes, no pull replies")))
2370   {
2371     return STAT_TYPE_BLOCKS_NO_PUSH_NO_PULL;
2372   }
2373   else if (0 == strncmp ("# rounds blocked - no pushes", stat_str, strlen ("# rounds blocked - no pushes")))
2374   {
2375     return STAT_TYPE_BLOCKS_NO_PUSH;
2376   }
2377   else if (0 == strncmp ("# rounds blocked", stat_str, strlen ("# rounds blocked")))
2378   {
2379     return STAT_TYPE_BLOCKS;
2380   }
2381   else if (0 == strncmp ("# rounds", stat_str, strlen ("# rounds")))
2382   {
2383     return STAT_TYPE_ROUNDS;
2384   }
2385   else if (0 == strncmp ("# push send issued", stat_str, strlen ("# push send issued")))
2386   {
2387     return STAT_TYPE_ISSUED_PUSH_SEND;
2388   }
2389   else if (0 == strncmp ("# pull request send issued", stat_str, strlen ("# pull request send issued")))
2390   {
2391     return STAT_TYPE_ISSUED_PULL_REQ;
2392   }
2393   else if (0 == strncmp ("# pull reply send issued", stat_str, strlen ("# pull reply send issued")))
2394   {
2395     return STAT_TYPE_ISSUED_PULL_REP;
2396   }
2397   else if (0 == strncmp ("# pushes sent", stat_str, strlen ("# pushes sent")))
2398   {
2399     return STAT_TYPE_SENT_PUSH_SEND;
2400   }
2401   else if (0 == strncmp ("# pull requests sent", stat_str, strlen ("# pull requests sent")))
2402   {
2403     return STAT_TYPE_SENT_PULL_REQ;
2404   }
2405   else if (0 == strncmp ("# pull replys sent", stat_str, strlen ("# pull replys sent")))
2406   {
2407     return STAT_TYPE_SENT_PULL_REP;
2408   }
2409   else if (0 == strncmp ("# push message received", stat_str, strlen ("# push message received")))
2410   {
2411     return STAT_TYPE_RECV_PUSH_SEND;
2412   }
2413   else if (0 == strncmp ("# pull request message received", stat_str, strlen ("# pull request message received")))
2414   {
2415     return STAT_TYPE_RECV_PULL_REQ;
2416   }
2417   else if (0 == strncmp ("# pull reply messages received", stat_str, strlen ("# pull reply messages received")))
2418   {
2419     return STAT_TYPE_RECV_PULL_REP;
2420   }
2421   return STAT_TYPE_MAX;
2422 }
2423
2424
2425 /**
2426  * @brief Converts #STAT_TYPE enum to the equivalent string representation that
2427  * is stored with the statistics service.
2428  *
2429  * @param stat_type #STAT_TYPE enum
2430  *
2431  * @return string representation that matches statistics value
2432  */
2433 char* stat_type_2_str (enum STAT_TYPE stat_type)
2434 {
2435   switch (stat_type)
2436   {
2437     case STAT_TYPE_ROUNDS:
2438       return "# rounds";
2439     case STAT_TYPE_BLOCKS:
2440       return "# rounds blocked";
2441     case STAT_TYPE_BLOCKS_MANY_PUSH:
2442       return "# rounds blocked - too many pushes";
2443     case STAT_TYPE_BLOCKS_NO_PUSH:
2444       return "# rounds blocked - no pushes";
2445     case STAT_TYPE_BLOCKS_NO_PULL:
2446       return "# rounds blocked - no pull replies";
2447     case STAT_TYPE_BLOCKS_MANY_PUSH_NO_PULL:
2448       return "# rounds blocked - too many pushes, no pull replies";
2449     case STAT_TYPE_BLOCKS_NO_PUSH_NO_PULL:
2450       return "# rounds blocked - no pushes, no pull replies";
2451     case STAT_TYPE_ISSUED_PUSH_SEND:
2452       return "# push send issued";
2453     case STAT_TYPE_ISSUED_PULL_REQ:
2454       return "# pull request send issued";
2455     case STAT_TYPE_ISSUED_PULL_REP:
2456       return "# pull reply send issued";
2457     case STAT_TYPE_SENT_PUSH_SEND:
2458       return "# pushes sent";
2459     case STAT_TYPE_SENT_PULL_REQ:
2460       return "# pull requests sent";
2461     case STAT_TYPE_SENT_PULL_REP:
2462       return "# pull replys sent";
2463     case STAT_TYPE_RECV_PUSH_SEND:
2464       return "# push message received";
2465     case STAT_TYPE_RECV_PULL_REQ:
2466       return "# pull request message received";
2467     case STAT_TYPE_RECV_PULL_REP:
2468       return "# pull reply messages received";
2469     case STAT_TYPE_MAX:
2470     default:
2471       return "ERROR";
2472       ;
2473   }
2474 }
2475
2476 /**
2477  * Callback function to process statistic values.
2478  *
2479  * @param cls closure
2480  * @param subsystem name of subsystem that created the statistic
2481  * @param name the name of the datum
2482  * @param value the current value
2483  * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
2484  * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
2485  */
2486 int
2487 stat_iterator (void *cls,
2488                const char *subsystem,
2489                const char *name,
2490                uint64_t value,
2491                int is_persistent)
2492 {
2493   const struct STATcls *stat_cls = (const struct STATcls *) cls;
2494   struct RPSPeer *rps_peer = (struct RPSPeer *) stat_cls->rps_peer;
2495   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got stat value: %s - %" PRIu64 "\n",
2496       //stat_type_2_str (stat_cls->stat_type),
2497       name,
2498       value);
2499   to_file (rps_peer->file_name_stats,
2500           "%s: %" PRIu64 "\n",
2501           name,
2502           value);
2503   switch (stat_str_2_type (name))
2504   {
2505     case STAT_TYPE_ROUNDS:
2506       rps_peer->num_rounds = value;
2507       break;
2508     case STAT_TYPE_BLOCKS:
2509       rps_peer->num_blocks = value;
2510       break;
2511     case STAT_TYPE_BLOCKS_MANY_PUSH:
2512       rps_peer->num_blocks_many_push = value;
2513       break;
2514     case STAT_TYPE_BLOCKS_NO_PUSH:
2515       rps_peer->num_blocks_no_push = value;
2516       break;
2517     case STAT_TYPE_BLOCKS_NO_PULL:
2518       rps_peer->num_blocks_no_pull = value;
2519       break;
2520     case STAT_TYPE_BLOCKS_MANY_PUSH_NO_PULL:
2521       rps_peer->num_blocks_many_push_no_pull = value;
2522       break;
2523     case STAT_TYPE_BLOCKS_NO_PUSH_NO_PULL:
2524       rps_peer->num_blocks_no_push_no_pull = value;
2525       break;
2526     case STAT_TYPE_ISSUED_PUSH_SEND:
2527       rps_peer->num_issued_push = value;
2528       break;
2529     case STAT_TYPE_ISSUED_PULL_REQ:
2530       rps_peer->num_issued_pull_req = value;
2531       break;
2532     case STAT_TYPE_ISSUED_PULL_REP:
2533       rps_peer->num_issued_pull_rep = value;
2534       break;
2535     case STAT_TYPE_SENT_PUSH_SEND:
2536       rps_peer->num_sent_push = value;
2537       break;
2538     case STAT_TYPE_SENT_PULL_REQ:
2539       rps_peer->num_sent_pull_req = value;
2540       break;
2541     case STAT_TYPE_SENT_PULL_REP:
2542       rps_peer->num_sent_pull_rep = value;
2543       break;
2544     case STAT_TYPE_RECV_PUSH_SEND:
2545       rps_peer->num_recv_push = value;
2546       break;
2547     case STAT_TYPE_RECV_PULL_REQ:
2548       rps_peer->num_recv_pull_req = value;
2549       break;
2550     case STAT_TYPE_RECV_PULL_REP:
2551       rps_peer->num_recv_pull_rep = value;
2552       break;
2553     case STAT_TYPE_MAX:
2554     default:
2555       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2556                  "Unknown statistics string: %s\n",
2557                  name);
2558       break;
2559   }
2560   return GNUNET_OK;
2561 }
2562
2563 void post_profiler (struct RPSPeer *rps_peer)
2564 {
2565   if (COLLECT_STATISTICS != cur_test_run.have_collect_statistics)
2566   {
2567     return;
2568   }
2569
2570   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2571       "Going to request statistic values with mask 0x%" PRIx32 "\n",
2572       cur_test_run.stat_collect_flags);
2573
2574   struct STATcls *stat_cls;
2575   uint32_t stat_type;
2576   for (stat_type = STAT_TYPE_ROUNDS;
2577       stat_type < STAT_TYPE_MAX;
2578       stat_type = stat_type <<1)
2579   {
2580     if (stat_type & cur_test_run.stat_collect_flags)
2581     {
2582       stat_cls = GNUNET_malloc (sizeof (struct STATcls));
2583       stat_cls->rps_peer = rps_peer;
2584       stat_cls->stat_type = stat_type;
2585       rps_peer->file_name_stats =
2586         store_prefix_file_name (rps_peer->peer_id, "stats");
2587       GNUNET_STATISTICS_get (rps_peer->stats_h,
2588                              "rps",
2589                              stat_type_2_str (stat_type),
2590                              post_test_shutdown_ready_cb,
2591                              stat_iterator,
2592                              (struct STATcls *) stat_cls);
2593       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2594           "Requested statistics for %s (peer %" PRIu32 ")\n",
2595           stat_type_2_str (stat_type),
2596           rps_peer->index);
2597     }
2598   }
2599 }
2600
2601
2602 /***********************************************************************
2603  * /Definition of tests
2604 ***********************************************************************/
2605
2606
2607 /**
2608  * Actual "main" function for the testcase.
2609  *
2610  * @param cls closure
2611  * @param h the run handle
2612  * @param n_peers number of peers in 'peers'
2613  * @param peers handle to peers run in the testbed
2614  * @param links_succeeded the number of overlay link connection attempts that
2615  *          succeeded
2616  * @param links_failed the number of overlay link connection attempts that
2617  *          failed
2618  */
2619 static void
2620 run (void *cls,
2621      struct GNUNET_TESTBED_RunHandle *h,
2622      unsigned int n_peers,
2623      struct GNUNET_TESTBED_Peer **peers,
2624      unsigned int links_succeeded,
2625      unsigned int links_failed)
2626 {
2627   unsigned int i;
2628   struct OpListEntry *entry;
2629
2630   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "RUN was called\n");
2631
2632   /* Check whether we timed out */
2633   if (n_peers != num_peers ||
2634       NULL == peers ||
2635       0 == links_succeeded)
2636   {
2637     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Going down due to args (eg. timeout)\n");
2638     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\tn_peers: %u\n", n_peers);
2639     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\tnum_peers: %" PRIu32 "\n", num_peers);
2640     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\tpeers: %p\n", peers);
2641     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\tlinks_succeeded: %u\n", links_succeeded);
2642     GNUNET_SCHEDULER_shutdown ();
2643     return;
2644   }
2645
2646
2647   /* Initialize peers */
2648   testbed_peers = peers;
2649   num_peers_online = 0;
2650   for (i = 0; i < num_peers; i++)
2651   {
2652     entry = make_oplist_entry ();
2653     entry->index = i;
2654     rps_peers[i].index = i;
2655     if (NULL != cur_test_run.init_peer)
2656       cur_test_run.init_peer (&rps_peers[i]);
2657     if (NO_COLLECT_VIEW == cur_test_run.have_collect_view)
2658     {
2659       rps_peers->cur_view_count = 0;
2660       rps_peers->cur_view = NULL;
2661     }
2662     entry->op = GNUNET_TESTBED_peer_get_information (peers[i],
2663                                                      GNUNET_TESTBED_PIT_IDENTITY,
2664                                                      &info_cb,
2665                                                      entry);
2666   }
2667
2668   /* Bring peers up */
2669   GNUNET_assert (num_peers == n_peers);
2670   for (i = 0; i < n_peers; i++)
2671   {
2672     rps_peers[i].index = i;
2673     rps_peers[i].op =
2674       GNUNET_TESTBED_service_connect (&rps_peers[i],
2675                                       peers[i],
2676                                       "rps",
2677                                       &rps_connect_complete_cb,
2678                                       &rps_peers[i],
2679                                       &rps_connect_adapter,
2680                                       &rps_disconnect_adapter,
2681                                       &rps_peers[i]);
2682     /* Connect all peers to statistics service */
2683     if (COLLECT_STATISTICS == cur_test_run.have_collect_statistics)
2684     {
2685       rps_peers[i].stat_op =
2686         GNUNET_TESTBED_service_connect (NULL,
2687                                         peers[i],
2688                                         "statistics",
2689                                         stat_complete_cb,
2690                                         &rps_peers[i],
2691                                         &stat_connect_adapter,
2692                                         &stat_disconnect_adapter,
2693                                         &rps_peers[i]);
2694     }
2695   }
2696
2697   if (NULL != churn_task)
2698     GNUNET_SCHEDULER_cancel (churn_task);
2699   post_test_task = GNUNET_SCHEDULER_add_delayed (timeout, &post_test_op, NULL);
2700   timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
2701       (timeout_s * 1.2) + 0.1 * num_peers);
2702   shutdown_task = GNUNET_SCHEDULER_add_delayed (timeout, &shutdown_op, NULL);
2703   shutdown_task = GNUNET_SCHEDULER_add_shutdown (shutdown_op, NULL);
2704
2705 }
2706
2707
2708 /**
2709  * Entry point for the testcase, sets up the testbed.
2710  *
2711  * @param argc unused
2712  * @param argv unused
2713  * @return 0 on success
2714  */
2715 int
2716 main (int argc, char *argv[])
2717 {
2718   int ret_value;
2719
2720   /* Defaults for tests */
2721   num_peers = 5;
2722   cur_test_run.name = "test-rps-default";
2723   cur_test_run.init_peer = default_init_peer;
2724   cur_test_run.pre_test = NULL;
2725   cur_test_run.reply_handle = default_reply_handle;
2726   cur_test_run.eval_cb = default_eval_cb;
2727   cur_test_run.post_test = NULL;
2728   cur_test_run.have_churn = HAVE_CHURN;
2729   cur_test_run.have_collect_statistics = NO_COLLECT_STATISTICS;
2730   cur_test_run.stat_collect_flags = 0;
2731   cur_test_run.have_collect_view = NO_COLLECT_VIEW;
2732   churn_task = NULL;
2733   timeout_s = 30;
2734
2735   if (strstr (argv[0], "malicious") != NULL)
2736   {
2737     cur_test_run.pre_test = mal_pre;
2738     cur_test_run.main_test = mal_cb;
2739     cur_test_run.init_peer = mal_init_peer;
2740
2741     if (strstr (argv[0], "_1") != NULL)
2742     {
2743       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test malicious peer type 1\n");
2744       cur_test_run.name = "test-rps-malicious_1";
2745       mal_type = 1;
2746     }
2747     else if (strstr (argv[0], "_2") != NULL)
2748     {
2749       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test malicious peer type 2\n");
2750       cur_test_run.name = "test-rps-malicious_2";
2751       mal_type = 2;
2752     }
2753     else if (strstr (argv[0], "_3") != NULL)
2754     {
2755       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test malicious peer type 3\n");
2756       cur_test_run.name = "test-rps-malicious_3";
2757       mal_type = 3;
2758     }
2759   }
2760
2761   else if (strstr (argv[0], "_single_req") != NULL)
2762   {
2763     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Test single request\n");
2764     cur_test_run.name = "test-rps-single-req";
2765     cur_test_run.main_test = single_req_cb;
2766     cur_test_run.have_churn = HAVE_NO_CHURN;
2767   }
2768
2769   else if (strstr (argv[0], "_delayed_reqs") != NULL)
2770   {
2771     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test delayed requests\n");
2772     cur_test_run.name = "test-rps-delayed-reqs";
2773     cur_test_run.main_test = delay_req_cb;
2774     cur_test_run.have_churn = HAVE_NO_CHURN;
2775   }
2776
2777   else if (strstr (argv[0], "_seed_big") != NULL)
2778   {
2779     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding (num_peers > GNUNET_MAX_MESSAGE_SIZE)\n");
2780     num_peers = 1;
2781     cur_test_run.name = "test-rps-seed-big";
2782     cur_test_run.main_test = seed_big_cb;
2783     cur_test_run.eval_cb = no_eval;
2784     cur_test_run.have_churn = HAVE_NO_CHURN;
2785     timeout_s = 10;
2786   }
2787
2788   else if (strstr (argv[0], "_single_peer_seed") != NULL)
2789   {
2790     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding and requesting on a single peer\n");
2791     cur_test_run.name = "test-rps-single-peer-seed";
2792     cur_test_run.main_test = single_peer_seed_cb;
2793     cur_test_run.have_churn = HAVE_NO_CHURN;
2794   }
2795
2796   else if (strstr (argv[0], "_seed_request") != NULL)
2797   {
2798     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding and requesting on multiple peers\n");
2799     cur_test_run.name = "test-rps-seed-request";
2800     cur_test_run.main_test = seed_req_cb;
2801     cur_test_run.have_churn = HAVE_NO_CHURN;
2802   }
2803
2804   else if (strstr (argv[0], "_seed") != NULL)
2805   {
2806     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test seeding\n");
2807     cur_test_run.name = "test-rps-seed";
2808     cur_test_run.main_test = seed_cb;
2809     cur_test_run.eval_cb = no_eval;
2810     cur_test_run.have_churn = HAVE_NO_CHURN;
2811   }
2812
2813   else if (strstr (argv[0], "_req_cancel") != NULL)
2814   {
2815     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test cancelling a request\n");
2816     cur_test_run.name = "test-rps-req-cancel";
2817     num_peers = 1;
2818     cur_test_run.main_test = req_cancel_cb;
2819     cur_test_run.eval_cb = no_eval;
2820     cur_test_run.have_churn = HAVE_NO_CHURN;
2821     timeout_s = 10;
2822   }
2823
2824   else if (strstr (argv[0], "_churn") != NULL)
2825   {
2826     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test churn\n");
2827     cur_test_run.name = "test-rps-churn";
2828     num_peers = 5;
2829     cur_test_run.init_peer = default_init_peer;
2830     cur_test_run.main_test = churn_test_cb;
2831     cur_test_run.reply_handle = default_reply_handle;
2832     cur_test_run.eval_cb = default_eval_cb;
2833     cur_test_run.have_churn = HAVE_NO_CHURN;
2834     cur_test_run.have_quick_quit = HAVE_NO_QUICK_QUIT;
2835     timeout_s = 10;
2836   }
2837
2838   else if (strstr (argv[0], "profiler") != NULL)
2839   {
2840     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "This is the profiler\n");
2841     cur_test_run.name = "test-rps-profiler";
2842     num_peers = 100;
2843     mal_type = 3;
2844     cur_test_run.init_peer = profiler_init_peer;
2845     //cur_test_run.pre_test = mal_pre;
2846     cur_test_run.pre_test = pre_profiler;
2847     cur_test_run.main_test = profiler_cb;
2848     cur_test_run.reply_handle = profiler_reply_handle;
2849     cur_test_run.eval_cb = profiler_eval;
2850     cur_test_run.post_test = post_profiler;
2851     cur_test_run.request_interval = 2;
2852     cur_test_run.num_requests = 5;
2853     //cur_test_run.have_churn = HAVE_CHURN;
2854     cur_test_run.have_churn = HAVE_NO_CHURN;
2855     cur_test_run.have_quick_quit = HAVE_NO_QUICK_QUIT;
2856     cur_test_run.have_collect_statistics = COLLECT_STATISTICS;
2857     cur_test_run.stat_collect_flags = STAT_TYPE_ROUNDS |
2858                                       STAT_TYPE_BLOCKS |
2859                                       STAT_TYPE_BLOCKS_MANY_PUSH |
2860                                       STAT_TYPE_BLOCKS_NO_PUSH |
2861                                       STAT_TYPE_BLOCKS_NO_PULL |
2862                                       STAT_TYPE_BLOCKS_MANY_PUSH_NO_PULL |
2863                                       STAT_TYPE_BLOCKS_NO_PUSH_NO_PULL |
2864                                       STAT_TYPE_ISSUED_PUSH_SEND |
2865                                       STAT_TYPE_ISSUED_PULL_REQ |
2866                                       STAT_TYPE_ISSUED_PULL_REP |
2867                                       STAT_TYPE_SENT_PUSH_SEND |
2868                                       STAT_TYPE_SENT_PULL_REQ |
2869                                       STAT_TYPE_SENT_PULL_REP |
2870                                       STAT_TYPE_RECV_PUSH_SEND |
2871                                       STAT_TYPE_RECV_PULL_REQ |
2872                                       STAT_TYPE_RECV_PULL_REP;
2873     cur_test_run.have_collect_view = COLLECT_VIEW;
2874     timeout_s = 150;
2875
2876     /* 'Clean' directory */
2877     (void) GNUNET_DISK_directory_remove ("/tmp/rps/");
2878     GNUNET_DISK_directory_create ("/tmp/rps/");
2879   }
2880   timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, timeout_s);
2881
2882   rps_peers = GNUNET_new_array (num_peers, struct RPSPeer);
2883   peer_map = GNUNET_CONTAINER_multipeermap_create (num_peers, GNUNET_NO);
2884   rps_peer_ids = GNUNET_new_array (num_peers, struct GNUNET_PeerIdentity);
2885   if ( (2 == mal_type) ||
2886        (3 == mal_type))
2887     target_peer = &rps_peer_ids[num_peers - 2];
2888   if (profiler_eval == cur_test_run.eval_cb)
2889     eval_peer = &rps_peers[num_peers - 1];    /* FIXME: eval_peer could be a
2890                                                  malicious peer if not careful
2891                                                  with the malicious portion */
2892
2893   ok = 1;
2894   ret_value = GNUNET_TESTBED_test_run (cur_test_run.name,
2895                                        "test_rps.conf",
2896                                        num_peers,
2897                                        0, NULL, NULL,
2898                                        &run, NULL);
2899   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2900               "_test_run returned.\n");
2901   if (GNUNET_OK != ret_value)
2902   {
2903     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2904                 "Test did not run successfully!\n");
2905   }
2906
2907   ret_value = cur_test_run.eval_cb();
2908   if (NO_COLLECT_VIEW == cur_test_run.have_collect_view)
2909   {
2910     GNUNET_array_grow (rps_peers->cur_view,
2911                        rps_peers->cur_view_count,
2912                        0);
2913   }
2914   GNUNET_free (rps_peers);
2915   GNUNET_free (rps_peer_ids);
2916   GNUNET_CONTAINER_multipeermap_destroy (peer_map);
2917   return ret_value;
2918 }
2919
2920 /* end of test_rps.c */