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