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