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