Make process_log more generic
[oweals/gnunet.git] / src / rps / gnunet-service-rps.c
1 /*
2      This file is part of GNUnet.
3      (C)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file rps/gnunet-service-rps.c
23  * @brief rps service implementation
24  * @author Julius Bünger
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_cadet_service.h"
29 #include "gnunet_nse_service.h"
30 #include "rps.h"
31
32 #include <math.h>
33 #include <inttypes.h>
34
35 #define LOG(kind, ...) GNUNET_log(kind, __VA_ARGS__)
36
37 // TODO modify @brief in every file
38
39 // TODO take care that messages are not longer than 64k
40
41 // TODO check for overflows
42
43 // TODO align message structs
44
45 // TODO multipeerlist indep of gossiped list
46
47 // (TODO api -- possibility of getting weak random peer immideately)
48
49 // TODO malicious peer
50
51 // TODO switch Slist -> DLL
52
53 /**
54  * Our configuration.
55  */
56 static const struct GNUNET_CONFIGURATION_Handle *cfg;
57
58 /**
59  * Our own identity.
60  */
61 struct GNUNET_PeerIdentity *own_identity;
62
63 /**
64  * Compare two peer identities. Taken from secretsharing.
65  *
66  * @param p1 Some peer identity.
67  * @param p2 Some peer identity.
68  * @return 1 if p1 > p2, -1 if p1 < p2 and 0 if p1 == p2.
69  */
70 static int
71 peer_id_cmp (const void *p1, const void *p2)
72 {
73   return memcmp (p1, p2, sizeof (struct GNUNET_PeerIdentity));
74 }
75
76 /***********************************************************************
77  * Sampler
78  *
79  * WARNING: This section needs to be reviewed regarding the use of
80  * functions providing (pseudo)randomness!
81 ***********************************************************************/
82
83 // TODO care about invalid input of the caller (size 0 or less...)
84
85 /**
86  * A sampler sampling PeerIDs.
87  */
88 struct Sampler
89 {
90   /**
91    * Min-wise linear permutation used by this sampler.
92    *
93    * This is an key later used by a hmac.
94    */
95   struct GNUNET_CRYPTO_AuthKey auth_key;
96
97   /**
98    * The PeerID this sampler currently samples.
99    */
100   struct GNUNET_PeerIdentity *peer_id;
101
102   /**
103    * The according hash value of this PeerID.
104    */
105   struct GNUNET_HashCode peer_id_hash;
106
107   /**
108    * Samplers are kept in a linked list.
109    */
110   struct Sampler *next;
111
112   /**
113    * Samplers are kept in a linked list.
114    */
115   struct Sampler *prev;
116
117 };
118
119 /**
120  * A n-tuple of samplers.
121  */
122 struct Samplers
123 {
124   /**
125    * Number of samplers we hold.
126    */
127   unsigned int size;
128   //size_t size;
129   
130   /**
131    * All PeerIDs in one array.
132    */
133   struct GNUNET_PeerIdentity *peer_ids;
134
135   /**
136    * The head of the DLL.
137    */
138   struct Sampler *head;
139
140   /**
141    * The tail of the DLL.
142    */
143   struct Sampler *tail;
144
145 };
146
147
148 typedef void (* SAMPLER_deleteCB) (void *cls, const struct GNUNET_PeerIdentity *id, struct GNUNET_HashCode hash);
149
150 /**
151  * (Re)Initialise given Sampler with random min-wise independent function.
152  *
153  * In this implementation this means choosing an auth_key for later use in
154  * a hmac at random.
155  *
156  * @param id pointer to the place where this sampler will store the PeerID.
157  *           This will be overwritten.
158  */
159   struct Sampler *
160 SAMPLER_init(struct GNUNET_PeerIdentity *id)
161 {
162   struct Sampler *s;
163   
164   s = GNUNET_new(struct Sampler);
165
166   // I guess I don't need to call GNUNET_CRYPTO_hmac_derive_key()...
167   GNUNET_CRYPTO_random_block(GNUNET_CRYPTO_QUALITY_STRONG,
168                              &(s->auth_key.key),
169                              GNUNET_CRYPTO_HASH_LENGTH);
170
171   //s->peer_id = GNUNET_new( struct GNUNET_PeerIdentity );
172   GNUNET_assert(NULL != id);
173   s->peer_id = id;
174   memcpy(s->peer_id, own_identity, sizeof(struct GNUNET_PeerIdentity));
175   //s->peer_id = own_identity; // Maybe set to own PeerID. So we always have
176                      // a valid PeerID in the sampler.
177                      // Maybe take a PeerID as second argument.
178   LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: initialised with PeerID %s (at %p) \n", GNUNET_i2s(s->peer_id), s->peer_id);
179
180   GNUNET_CRYPTO_hmac(&s->auth_key, s->peer_id,
181                      sizeof(struct GNUNET_PeerIdentity),
182                      &s->peer_id_hash);
183
184   s->prev = NULL;
185   s->next = NULL;
186
187   return s;
188 }
189
190 /**
191  * Compare two hashes.
192  *
193  * Returns if the first one is smaller then the second.
194  * Used by SAMPLER_next() to compare hashes.
195  */
196   int
197 hash_cmp(struct GNUNET_HashCode hash1, struct GNUNET_HashCode hash2)
198 {
199   return memcmp( (const void *) &hash1, (const void *) & hash2, sizeof(struct GNUNET_HashCode));
200 }
201
202 /**
203  * Input an PeerID into the given sampler.
204  */
205   static void
206 SAMPLER_next(struct Sampler *s, const struct GNUNET_PeerIdentity *id, SAMPLER_deleteCB del_cb, void *cb_cls)
207   // TODO set id in peer_ids
208 {
209   struct GNUNET_HashCode other_hash;
210
211   if ( id == s->peer_id )
212   {
213     LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER:          Got PeerID %s\n",
214         GNUNET_i2s(id));
215     LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Have already PeerID %s\n",
216         GNUNET_i2s(s->peer_id));
217   }
218   else
219   {
220     GNUNET_CRYPTO_hmac(&s->auth_key,
221         id,
222         sizeof(struct GNUNET_PeerIdentity),
223         &other_hash);
224
225     if ( NULL == s->peer_id )
226     { // Or whatever is a valid way to say
227       // "we have no PeerID at the moment"
228       LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Got PeerID %s; Simply accepting (got NULL previously).\n",
229           GNUNET_i2s(id));
230       memcpy(s->peer_id, id, sizeof(struct GNUNET_PeerIdentity));
231       //s->peer_id = id;
232       s->peer_id_hash = other_hash;
233     }
234     else if ( 0 > hash_cmp(other_hash, s->peer_id_hash) )
235     {
236       LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER:            Got PeerID %s\n",
237           GNUNET_i2s(id));
238       LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Discarding old PeerID %s\n",
239           GNUNET_i2s(s->peer_id));
240
241       if ( NULL != del_cb )
242       {
243         LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Removing old PeerID %s with the delete callback.\n",
244             GNUNET_i2s(s->peer_id));
245         del_cb(cb_cls, s->peer_id, s->peer_id_hash);
246       }
247
248       memcpy(s->peer_id, id, sizeof(struct GNUNET_PeerIdentity));
249       //s->peer_id = id;
250       s->peer_id_hash = other_hash;
251     }
252     else
253     {
254       LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER:         Got PeerID %s\n",
255           GNUNET_i2s(id), id);
256       LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Keeping old PeerID %s\n",
257           GNUNET_i2s(s->peer_id), s->peer_id);
258     }
259   }
260 }
261
262
263 /**
264  * Initialise a tuple of samplers.
265  */
266 struct Samplers *
267 SAMPLER_samplers_init(size_t init_size)
268 {
269   struct Samplers *samplers;
270   struct Sampler *s;
271   uint64_t i;
272
273   samplers = GNUNET_new(struct Samplers);
274   samplers->size = 0;
275   samplers->head = samplers->tail = NULL;
276   samplers->peer_ids = NULL;
277   //samplers->peer_ids = GNUNET_new_array(init_size, struct GNUNET_PeerIdentity);
278
279   for ( i = 0 ; i < init_size ; i++ )
280   {
281     GNUNET_array_append(samplers->peer_ids,
282         samplers->size,
283         *own_identity);
284     //samplers->size++;
285     s = SAMPLER_init(&samplers->peer_ids[i]);
286     GNUNET_CONTAINER_DLL_insert_tail(samplers->head,
287         samplers->tail,
288         s);
289   }
290   //samplers->size = init_size;
291   GNUNET_assert(init_size == samplers->size);
292   return samplers;
293 }
294
295
296 /**
297  * A fuction to update every sampler in the given list
298  */
299   static void
300 SAMPLER_update_list(struct Samplers *samplers, const struct GNUNET_PeerIdentity *id,
301                     SAMPLER_deleteCB del_cb, void *cb_cls)
302 {
303   struct Sampler *iter;
304
305   iter = samplers->head;
306   while ( NULL != iter->next )
307   {
308     SAMPLER_next(iter, id, del_cb, cb_cls);
309     iter = iter->next;
310   }
311   
312 }
313
314 /**
315  * Get one random peer out of the sampled peers.
316  *
317  * We might want to reinitialise this sampler after giving the
318  * corrsponding peer to the client.
319  */
320   const struct GNUNET_PeerIdentity* 
321 SAMPLER_get_rand_peer (struct Samplers *samplers)
322 {
323   LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER_get_rand_peer:\n");
324
325   if ( 0 == samplers->size )
326   {
327     LOG(GNUNET_ERROR_TYPE_DEBUG, "Sgrp: List empty - Returning own PeerID %s\n", GNUNET_i2s(own_identity));
328     return own_identity;
329   }
330   else
331   {
332     uint64_t index;
333     struct Sampler *iter;
334     uint64_t i;
335     const struct GNUNET_PeerIdentity *peer;
336
337     /**
338      * Choose the index of the peer we want to give back
339      * at random from the interval of the sampler list
340      */
341     index = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_STRONG,
342                                      samplers->size);
343                                      // TODO check that it does not overflow
344     LOG(GNUNET_ERROR_TYPE_DEBUG, "Sgrp: Length of Slist: %" PRIu64 ", index: %" PRIu64 "\n", samplers->size, index);
345
346     iter = samplers->head;
347     for ( i = 0 ; i < index ; i++ )
348     {
349       if ( NULL == iter->next )
350       { // Maybe unneeded
351         iter = samplers->head;
352       }
353     }
354     
355     // TODO something missing?
356
357     peer = iter->peer_id;
358     LOG(GNUNET_ERROR_TYPE_DEBUG, "Sgrp: Returning PeerID %s\n", GNUNET_i2s(peer));
359     LOG(GNUNET_ERROR_TYPE_DEBUG, "Sgrp: (own ID: %s)\n", GNUNET_i2s(own_identity));
360
361     return peer;
362   }
363 }
364
365 /**
366  * Get n random peers out of the sampled peers.
367  *
368  * We might want to reinitialise this sampler after giving the
369  * corrsponding peer to the client.
370  * Random with or without consumption?
371  */
372   const struct GNUNET_PeerIdentity*  // TODO give back simple array
373 SAMPLER_get_n_rand_peers (struct Samplers *samplers, uint64_t n)
374 {
375   // TODO check if we have too much (distinct) sampled peers
376   // If we are not ready yet maybe schedule for later
377   struct GNUNET_PeerIdentity *peers;
378   uint64_t i;
379   
380   peers = GNUNET_malloc(n * sizeof(struct GNUNET_PeerIdentity));
381
382   for ( i = 0 ; i < n ; i++ ) {
383     //peers[i] = SAMPLER_get_rand_peer(samplers);
384     memcpy(&peers[i], SAMPLER_get_rand_peer(samplers), sizeof(struct GNUNET_PeerIdentity));
385   }
386
387   // TODO something else missing?
388   return peers;
389 }
390
391 /**
392  * Counts how many Samplers currently hold a given PeerID.
393  */
394   uint64_t
395 SAMPLER_count_id ( struct Samplers *samplers, struct GNUNET_PeerIdentity *id )
396 {
397   struct Sampler *iter;
398   uint64_t count;
399
400   iter = samplers->head;
401   count = 0;
402   while ( NULL != iter )
403   {
404     if ( peer_id_cmp( iter->peer_id, id) )
405       count++;
406     iter = iter->next;
407   }
408   return count;
409 }
410
411 /**
412  * Gow the size of the tuple of samplers.
413  */
414   void
415 SAMPLER_samplers_grow (struct Samplers * samplers, size_t new_size)
416 {
417   uint64_t i;
418   struct Sampler *sampler;
419
420   if ( new_size > samplers->size )
421   {
422     GNUNET_array_grow(samplers->peer_ids, samplers->size, new_size);
423     for ( i = 0 ; i < new_size - samplers->size ; i++ )
424     {
425       sampler = SAMPLER_init(&samplers->peer_ids[samplers->size + i]);
426       GNUNET_CONTAINER_DLL_insert_tail(samplers->head, samplers->tail, sampler);
427     }
428   }
429   else if ( new_size < samplers->size )
430   {
431     for ( i = 0 ; i < samplers->size - new_size ; i++)
432     {
433       // TODO call delCB on elem?
434       sampler = samplers->tail;
435       GNUNET_CONTAINER_DLL_remove(samplers->head, samplers->tail, sampler);
436     }
437     GNUNET_array_grow(samplers->peer_ids, samplers->size, new_size);
438   }
439
440   samplers->size = new_size;
441 }
442
443 /***********************************************************************
444  * /Sampler
445 ***********************************************************************/
446
447
448
449 /***********************************************************************
450  * Housekeeping with peers
451 ***********************************************************************/
452
453 /**
454  * Struct used to store the context of a connected client.
455  */
456 struct client_ctx
457 {
458   /**
459    * The message queue to communicate with the client.
460    */
461   struct GNUNET_MQ_Handle *mq;
462 };
463
464 /**
465  * Used to keep track in what lists single peerIDs are.
466  */
467 enum in_list_flag // probably unneeded
468 {
469   in_other_sampler_list = 0x1,
470   in_other_gossip_list  = 0x2, // unneeded?
471   in_own_sampler_list   = 0x4,
472   in_own_gossip_list    = 0x8 // unneeded?
473 };
474
475 /**
476  * Struct used to keep track of other peer's status
477  *
478  * This is stored in a multipeermap.
479  */
480 struct peer_context
481 {
482   /**
483    * In own gossip/sampler list, in other's gossip/sampler list
484    */
485   uint32_t in_flags; // unneeded?
486
487   /**
488    * Message queue open to client
489    */
490   struct GNUNET_MQ_Handle *mq;
491
492   /**
493    * Channel open to client.
494    */
495   struct GNUNET_CADET_Channel *to_channel;
496
497   /**
498    * Channel open from client.
499    */
500   struct GNUNET_CADET_Channel *from_channel; // unneeded
501
502   /**
503    * This is pobably followed by 'statistical' data (when we first saw
504    * him, how did we get his ID, how many pushes (in a timeinterval),
505    * ...)
506    */
507 };
508
509 /***********************************************************************
510  * /Housekeeping with peers
511 ***********************************************************************/
512
513 /**
514  * Set of all peers to keep track of them.
515  */
516 static struct GNUNET_CONTAINER_MultiPeerMap *peer_map;
517
518
519 // -- gossip list length --
520 // Depends on the (estimated) size of the
521 // network. - Initial size might be the
522 // number of peers cadet provides.
523 // TODO other events to grow/shrink size?
524
525 /**
526  * List of samplers.
527  */
528 struct Samplers *sampler_list; // TODO rename to sampler_list
529
530 /**
531  * Sampler list size // TODO get rid of that
532  *
533  * Adapts to the nse. Size should be in BigTheta(network_size)^(1/3).
534  */
535 size_t sampler_list_size;
536
537
538 /**
539  * The gossiped list of peers.
540  */
541 struct GNUNET_PeerIdentity *gossip_list;
542
543 /**
544  * Size of the gossiped list
545  */
546 unsigned int gossip_list_size;
547
548 /**
549  * Min size of the gossip list
550  */
551 uint64_t gossip_list_min_size;
552
553 ///**
554 // * Max size of the gossip list
555 // * 
556 // * This will probably be left to be set by the client.
557 // */
558 //uint64_t gossip_list_max_size;
559
560
561 /**
562  * The estimated size of the network.
563  *
564  * Influenced by the stdev.
565  */
566 size_t est_size;
567
568
569
570 /**
571  * Percentage of total peer number in the gossip list
572  * to send random PUSHes to
573  */
574 float alpha;
575
576 /**
577  * Percentage of total peer number in the gossip list
578  * to send random PULLs to
579  */
580 float beta;
581
582 /**
583  * The percentage gamma of history updates.
584  * Simply 1 - alpha - beta
585  */
586
587
588
589
590 /**
591  * Identifier for the main task that runs periodically.
592  */
593 GNUNET_SCHEDULER_TaskIdentifier do_round_task;
594
595 /**
596  * Time inverval the do_round task runs in.
597  */
598 struct GNUNET_TIME_Relative round_interval;
599
600
601
602 /**
603  * List to store peers received through pushes temporary.
604  */
605 struct GNUNET_PeerIdentity *push_list;
606
607 /**
608  * Size of the push_list;
609  */
610 unsigned int push_list_size;
611 //size_t push_list_size;
612
613 /**
614  * List to store peers received through pulls temporary.
615  */
616 struct GNUNET_PeerIdentity *pull_list;
617
618 /**
619  * Size of the pull_list;
620  */
621 unsigned int pull_list_size;
622 //size_t pull_list_size;
623
624
625 /**
626  * Handler to NSE.
627  */
628 struct GNUNET_NSE_Handle *nse;
629
630 /**
631  * Handler to CADET.
632  */
633 struct GNUNET_CADET_Handle *cadet_handle;
634
635
636 /***********************************************************************
637  * Util functions
638 ***********************************************************************/
639
640 /**
641  * Get random peer from the gossip list.
642  */
643   struct GNUNET_PeerIdentity *
644 get_rand_gossip_peer()
645 {
646   uint64_t index;
647   struct GNUNET_PeerIdentity *peer;
648
649   // TODO find a better solution.
650   // FIXME if we have only own ID in gossip list this will block
651   // but then we might have a problem nevertheless ?
652
653   do {
654
655     /**;
656      * Choose the index of the peer we want to return
657      * at random from the interval of the gossip list
658      */
659     index = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_STRONG,
660                                      gossip_list_size);
661
662     peer = &(gossip_list[index]);
663   } while ( own_identity == peer || NULL == peer );
664
665   return peer;
666 }
667
668 /**
669  * Get the message queue of a specific peer.
670  *
671  * If we already have a message queue open to this client,
672  * simply return it, otherways create one.
673  */
674   struct GNUNET_MQ_Handle *
675 get_mq (struct GNUNET_CONTAINER_MultiPeerMap *peer_map, struct GNUNET_PeerIdentity *peer_id)
676 {
677   struct peer_context *ctx;
678   struct GNUNET_MQ_Handle * mq;
679   struct GNUNET_CADET_Channel *channel;
680
681   if ( GNUNET_OK != GNUNET_CONTAINER_multipeermap_contains( peer_map, peer_id ) ) {
682
683     channel = GNUNET_CADET_channel_create(cadet_handle, NULL, peer_id,
684                                   GNUNET_RPS_CADET_PORT,
685                                   GNUNET_CADET_OPTION_RELIABLE);
686     mq = GNUNET_CADET_mq_create(channel);
687
688     ctx = GNUNET_malloc(sizeof(struct peer_context));
689     ctx->in_flags = 0;
690     ctx->to_channel = channel;
691     ctx->mq = mq;
692
693     GNUNET_CONTAINER_multipeermap_put(peer_map, peer_id, ctx,
694                                       GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
695   } else {
696     ctx = GNUNET_CONTAINER_multipeermap_get(peer_map, peer_id);
697     if ( NULL == ctx->mq ) {
698       if ( NULL == ctx->to_channel ) {
699         channel = GNUNET_CADET_channel_create(cadet_handle, NULL, peer_id,
700                                       GNUNET_RPS_CADET_PORT,
701                                       GNUNET_CADET_OPTION_RELIABLE);
702         ctx->to_channel = channel;
703       }
704
705       mq = GNUNET_CADET_mq_create(ctx->to_channel);
706       ctx->mq = mq;
707     }
708   }
709
710   return ctx->mq;
711 }
712
713
714 /***********************************************************************
715  * /Util functions
716 ***********************************************************************/
717
718 /**
719  * Function called by NSE.
720  *
721  * Updates sizes of sampler list and gossip list and adapt those lists
722  * accordingly.
723  */
724   void
725 nse_callback(void *cls, struct GNUNET_TIME_Absolute timestamp, double logestimate, double std_dev)
726 {
727   double estimate;
728   //double scale; // TODO this might go gloabal/config
729
730   LOG(GNUNET_ERROR_TYPE_DEBUG, "Received a ns estimate - logest: %f, std_dev: %f\n", logestimate, std_dev);
731   //scale = .01;
732   estimate = 1 << (uint64_t) round(logestimate);
733   // GNUNET_NSE_log_estimate_to_n (logestimate);
734   estimate = pow(estimate, 1./3);// * (std_dev * scale); // TODO add
735   if ( 0 < estimate ) {
736     LOG(GNUNET_ERROR_TYPE_DEBUG, "Changing estimate to %f\n", estimate);
737     est_size = estimate;
738   } else {
739     LOG(GNUNET_ERROR_TYPE_DEBUG, "Not using estimate %f\n", estimate);
740   }
741 }
742
743 /**
744  * Handle RPS request from the client.
745  *
746  * @param cls closure
747  * @param client identification of the client
748  * @param message the actual message
749  */
750 static void
751 // TODO rename
752 handle_cs_request (void *cls,
753             struct GNUNET_SERVER_Client *client,
754             const struct GNUNET_MessageHeader *message)
755 {
756   LOG(GNUNET_ERROR_TYPE_DEBUG, "Client requested (a) random peer(s).\n");
757
758   struct GNUNET_RPS_CS_RequestMessage *msg;
759   //unsigned int n_arr[sampler_list_size];// =
760     //GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_STRONG, (unsigned int) sampler_list_size);
761   //struct GNUNET_MQ_Handle *mq;
762   struct client_ctx *cli_ctx;
763   struct GNUNET_MQ_Envelope *ev;
764   struct GNUNET_RPS_CS_ReplyMessage *out_msg;
765   uint64_t num_peers;
766   uint64_t i;
767
768   // TODO
769   msg = (struct GNUNET_RPS_CS_RequestMessage *) message;
770   // Does not work because the compiler seems not to find it.
771   cli_ctx = GNUNET_SERVER_client_get_user_context(client, struct client_ctx);
772   if ( NULL == cli_ctx ) {
773     cli_ctx = GNUNET_new(struct client_ctx);
774     cli_ctx->mq = GNUNET_MQ_queue_for_server_client(client);
775     GNUNET_SERVER_client_set_user_context(client, cli_ctx);
776   }
777   
778   //mq = GNUNET_MQ_queue_for_server_client(client);
779     
780   // TODO How many peers do we give back?
781   // Wait until we have enough random peers?
782
783   ev = GNUNET_MQ_msg_extra(out_msg,
784                            GNUNET_ntohll(msg->num_peers) * sizeof(struct GNUNET_PeerIdentity),
785                            GNUNET_MESSAGE_TYPE_RPS_CS_REPLY);
786   out_msg->num_peers = GNUNET_ntohll(msg->num_peers);
787
788   num_peers = GNUNET_ntohll(msg->num_peers);
789   //&out_msg[1] = SAMPLER_get_n_rand_peers(sampler_list, num_peers);
790   for ( i = 0 ; i < num_peers ; i++ ) {
791     memcpy(&out_msg[1] + i * sizeof(struct GNUNET_PeerIdentity),
792            SAMPLER_get_rand_peer(sampler_list),
793            sizeof(struct GNUNET_PeerIdentity));
794   }
795   
796   GNUNET_MQ_send(cli_ctx->mq, ev);
797   //GNUNET_MQ_send(mq, ev);
798   //GNUNET_MQ_destroy(mq);
799
800   GNUNET_SERVER_receive_done (client,
801                               GNUNET_OK);
802 }
803
804 /**
805  * Handle a PUSH message from another peer.
806  *
807  * Check the proof of work and store the PeerID
808  * in the temporary list for pushed PeerIDs.
809  *
810  * @param cls Closure
811  * @param channel The channel the PUSH was received over
812  * @param channel_ctx The context associated with this channel
813  * @param msg The message header
814  */
815 static int
816 handle_peer_push (void *cls,
817     struct GNUNET_CADET_Channel *channel,
818     void **channel_ctx,
819     const struct GNUNET_MessageHeader *msg)
820 {
821   LOG(GNUNET_ERROR_TYPE_DEBUG, "PUSH received\n");
822
823   struct GNUNET_PeerIdentity *peer;
824
825   // TODO check the proof of work
826   // and check limit for PUSHes
827   // IF we count per peer PUSHes
828   // maybe remove from gossip/sampler list
829   
830   peer = (struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info( channel, GNUNET_CADET_OPTION_PEER );
831   
832   /* Add the sending peer to the push_list */
833   GNUNET_array_append(push_list, push_list_size, *peer);
834   push_list_size ++;
835
836   return GNUNET_OK;
837 }
838
839 /**
840  * Handle PULL REQUEST request message from another peer.
841  *
842  * Reply with the gossip list of PeerIDs.
843  *
844  * @param cls Closure
845  * @param channel The channel the PUSH was received over
846  * @param channel_ctx The context associated with this channel
847  * @param msg The message header
848  */
849 static int
850 handle_peer_pull_request (void *cls,
851     struct GNUNET_CADET_Channel *channel,
852     void **channel_ctx,
853     const struct GNUNET_MessageHeader *msg)
854 {
855
856   struct GNUNET_PeerIdentity *peer;
857   struct GNUNET_MQ_Handle *mq;
858   //struct GNUNET_RPS_P2P_PullRequestMessage *in_msg;
859   struct GNUNET_MQ_Envelope *ev;
860   struct GNUNET_RPS_P2P_PullReplyMessage *out_msg;
861
862   // TODO find some way to keep one peer from spamming with pull requests
863   // allow only one request per time interval ?
864   // otherwise remove from peerlist?
865
866   peer = (struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info(channel, GNUNET_CADET_OPTION_PEER);
867   LOG(GNUNET_ERROR_TYPE_DEBUG, "PULL REQUEST from peer %s received\n", GNUNET_i2s(peer));
868
869   mq = GNUNET_CADET_mq_create(channel); // TODO without mq?
870   //mq = get_mq(peer_map, peer);
871
872   //in_msg = (struct GNUNET_RPS_P2P_PullRequestMessage *) msg;
873   // TODO how many peers do we actually send?
874   // GNUNET_ntohll(in_msg->num_peers)
875   ev = GNUNET_MQ_msg_extra(out_msg,
876                            gossip_list_size * sizeof(struct GNUNET_PeerIdentity),
877                            GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY);
878   out_msg->num_peers = GNUNET_htonll(gossip_list_size);
879   memcpy(&out_msg[1], gossip_list,
880          gossip_list_size * sizeof(struct GNUNET_PeerIdentity));
881
882   GNUNET_MQ_send(mq, ev);
883
884   GNUNET_MQ_destroy(mq);
885
886
887   return GNUNET_OK;
888 }
889
890 /**
891  * Handle PULL REPLY message from another peer.
892  *
893  * Check whether we sent a corresponding request and
894  * whether this reply is the first one.
895  *
896  * @param cls Closure
897  * @param channel The channel the PUSH was received over
898  * @param channel_ctx The context associated with this channel
899  * @param msg The message header
900  */
901 static int
902 handle_peer_pull_reply (void *cls,
903     struct GNUNET_CADET_Channel *channel,
904     void **channel_ctx,
905     const struct GNUNET_MessageHeader *msg)
906 {
907   LOG(GNUNET_ERROR_TYPE_DEBUG, "PULL REPLY received\n");
908
909   struct GNUNET_RPS_P2P_PullReplyMessage *in_msg;
910   struct GNUNET_PeerIdentity *peers;
911   uint64_t i;
912
913   // TODO check that we sent a request and that it is the first reply
914
915   in_msg = (struct GNUNET_RPS_P2P_PullReplyMessage *) msg;
916   peers = (struct GNUNET_PeerIdentity *) &msg[1];
917   for ( i = 0 ; i < GNUNET_ntohll(in_msg->num_peers) ; i++ ) {
918     GNUNET_array_append(pull_list, pull_list_size, peers[i]);
919     pull_list_size++;
920   }
921
922   // TODO maybe a disconnect happens here
923   
924   return GNUNET_OK;
925 }
926
927
928 /**
929  * Callback called when a Sampler is updated.
930  */
931   void
932 delete_cb (void *cls, struct GNUNET_PeerIdentity *id, struct GNUNET_HashCode hash)
933 {
934   size_t s;
935
936   s = SAMPLER_count_id(sampler_list, id);
937   if ( 1 >= s ) {
938     // TODO cleanup peer
939     GNUNET_CONTAINER_multipeermap_remove_all( peer_map, id);
940   }
941 }
942
943
944 /**
945  * Send out PUSHes and PULLs.
946  *
947  * This is executed regylary.
948  */
949 static void
950 do_round(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
951 {
952   LOG(GNUNET_ERROR_TYPE_DEBUG, "Going to execute next round\n");
953
954   uint64_t i;
955   //unsigned int *n_arr;
956   struct GNUNET_RPS_P2P_PushMessage        *push_msg;
957   struct GNUNET_RPS_P2P_PullRequestMessage *pull_msg; // FIXME Send empty message
958   struct GNUNET_MQ_Envelope *ev;
959   struct GNUNET_PeerIdentity *peer;
960
961   // TODO print lists, ...
962   // TODO cleanup peer_map
963
964
965   /* If the NSE has changed adapt the lists accordingly */
966   // TODO check nse == 0!
967   LOG(GNUNET_ERROR_TYPE_DEBUG, "Checking size estimate.\n");
968   SAMPLER_samplers_grow(sampler_list, est_size);
969
970   GNUNET_array_grow(gossip_list, gossip_list_size, est_size); // FIXME Do conversion correct or change type
971
972   gossip_list_size = sampler_list_size = est_size;
973
974  
975
976
977   /* Would it make sense to have one shuffeled gossip list and then
978    * to send PUSHes to first alpha peers, PULL requests to next beta peers and
979    * use the rest to update sampler? */
980
981   /* Send PUSHes */
982   //n_arr = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_STRONG, (unsigned int) gossip_list_size);
983   LOG(GNUNET_ERROR_TYPE_DEBUG, "Going to send pushes to %f (%f * %" PRIu64 ") peers.\n",
984       alpha * gossip_list_size, alpha, gossip_list_size);
985   for ( i = 0 ; i < alpha * gossip_list_size ; i++ ) { // TODO compute length
986     peer = get_rand_gossip_peer();
987     // TODO check NULL == peer
988     LOG(GNUNET_ERROR_TYPE_DEBUG, "Sending PUSH to peer %s of gossiped list.\n", GNUNET_i2s(peer));
989
990     ev = GNUNET_MQ_msg(push_msg, GNUNET_MESSAGE_TYPE_RPS_PP_PUSH);
991     //ev = GNUNET_MQ_msg_extra();
992     /* TODO Compute proof of work here
993     push_msg; */
994     push_msg->placeholder = 0;
995     GNUNET_MQ_send( get_mq(peer_map, peer), ev );
996
997     // TODO modify in_flags of respective peer?
998   }
999
1000
1001   /* Send PULL requests */
1002   // TODO
1003   //n_arr = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_STRONG, (unsigned int) sampler_list_size);
1004   LOG(GNUNET_ERROR_TYPE_DEBUG, "Going to send pulls to %f (%f * %" PRIu64 ") peers.\n",
1005       beta * gossip_list_size, beta, gossip_list_size);
1006   for ( i = 0 ; i < beta * gossip_list_size ; i++ ){ // TODO compute length
1007     peer = get_rand_gossip_peer();
1008     // TODO check NULL == peer
1009     LOG(GNUNET_ERROR_TYPE_DEBUG, "Sending PULL request to peer %s of gossiped list.\n", GNUNET_i2s(peer));
1010
1011     ev = GNUNET_MQ_msg(pull_msg, GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST);
1012     //ev = GNUNET_MQ_msg_extra();
1013     pull_msg->placeholder = 0;
1014     GNUNET_MQ_send( get_mq(peer_map, peer), ev );
1015     // TODO modify in_flags of respective peer?
1016   }
1017
1018
1019
1020
1021   /* Update gossip list */
1022   uint64_t tmp_index;
1023   uint64_t index;
1024
1025   if ( push_list_size <= alpha * gossip_list_size &&
1026        push_list_size != 0 &&
1027        pull_list_size != 0 ) {
1028     LOG(GNUNET_ERROR_TYPE_DEBUG, "Update of the gossip list. ()\n");
1029
1030     for ( i = 0 ; i < alpha * gossip_list_size ; i++ ) { // TODO use SAMPLER_get_n_rand_peers
1031       /* Update gossip list with peers received through PUSHes */
1032       index = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_STRONG,
1033                                        push_list_size);
1034       gossip_list[i] = push_list[index];
1035       // TODO change the in_flags accordingly
1036     }
1037
1038     for ( i = 0 ; i < beta * gossip_list_size ; i++ ) {
1039       /* Update gossip list with peers received through PULLs */
1040       tmp_index = i + round(alpha * gossip_list_size);
1041       index = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_STRONG,
1042                                        pull_list_size);
1043       gossip_list[tmp_index] = pull_list[index];
1044       // TODO change the in_flags accordingly
1045     }
1046
1047     for ( i = 0 ; i < (1 - (alpha + beta)) * gossip_list_size ; i++ ) {
1048       /* Update gossip list with peers from history */
1049       tmp_index = i + round((alpha + beta) * gossip_list_size);
1050       index = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_STRONG,
1051                                        sampler_list->size);
1052       gossip_list[tmp_index] = sampler_list->peer_ids[index];
1053       // TODO change the in_flags accordingly
1054     }
1055
1056   } else {
1057     LOG(GNUNET_ERROR_TYPE_DEBUG, "No update of the gossip list. ()\n");
1058   }
1059   // TODO independent of that also get some peers from CADET_get_peers()?
1060
1061
1062
1063   /* Update samplers */
1064
1065   for ( i = 0 ; i < push_list_size ; i++ )
1066   {
1067     SAMPLER_update_list(sampler_list, &push_list[i], NULL, NULL);
1068     // TODO set in_flag?
1069   }
1070
1071   for ( i = 0 ; i < pull_list_size ; i++ )
1072   {
1073     SAMPLER_update_list(sampler_list, &pull_list[i], NULL, NULL);
1074     // TODO set in_flag?
1075   }
1076
1077
1078   // TODO go over whole peer_map and do cleanups
1079   // delete unneeded peers, set in_flags, check channel/mq
1080
1081
1082   /* Empty push/pull lists */
1083   GNUNET_array_grow(push_list, push_list_size, 0);
1084   push_list_size = 0;
1085   GNUNET_array_grow(pull_list, pull_list_size, 0);
1086   pull_list_size = 0;
1087
1088
1089   /* Schedule next round */
1090   // TODO
1091   do_round_task = GNUNET_SCHEDULER_add_delayed( round_interval, &do_round, NULL );
1092   LOG(GNUNET_ERROR_TYPE_DEBUG, "Finished round\n");
1093 }
1094
1095 static void
1096 rps_start (struct GNUNET_SERVER_Handle *server);
1097
1098 /**
1099  * This is called from GNUNET_CADET_get_peers().
1100  *
1101  * It is called on every peer(ID) that cadet somehow has contact with.
1102  * We use those to initialise the sampler.
1103  */
1104 void
1105 init_peer_cb (void *cls,
1106               const struct GNUNET_PeerIdentity *peer,
1107               int tunnel, // "Do we have a tunnel towards this peer?"
1108               unsigned int n_paths, // "Number of known paths towards this peer"
1109               unsigned int best_path) // "How long is the best path?
1110                                       // (0 = unknown, 1 = ourselves, 2 = neighbor)"
1111 {
1112   // FIXME use the magic 0000 PeerID
1113   if ( NULL != peer ) {
1114     LOG(GNUNET_ERROR_TYPE_DEBUG, "Got peer %s (at %p) from CADET\n", GNUNET_i2s(peer), peer);
1115     SAMPLER_update_list(sampler_list, peer, NULL, NULL);
1116     if ( GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains( peer_map, peer ) ) {
1117     } else {
1118       struct peer_context *ctx;
1119
1120       ctx = GNUNET_malloc(sizeof(struct peer_context));
1121       ctx->in_flags = 0;
1122       ctx->mq = NULL;
1123       ctx->to_channel = NULL;
1124       ctx->from_channel = NULL;
1125       GNUNET_CONTAINER_multipeermap_put( peer_map, peer, ctx, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1126     }
1127
1128     uint64_t i;
1129     i = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_STRONG, gossip_list_size);
1130     gossip_list[i] = *peer;
1131     // TODO send push/pull to each of those peers?
1132   } else {
1133     rps_start( (struct GNUNET_SERVER_Handle *) cls);
1134   }
1135 }
1136
1137
1138
1139
1140 /**
1141  * Task run during shutdown.
1142  *
1143  * @param cls unused
1144  * @param tc unused
1145  */
1146 static void
1147 shutdown_task (void *cls,
1148                const struct GNUNET_SCHEDULER_TaskContext *tc)
1149 {
1150   LOG(GNUNET_ERROR_TYPE_DEBUG, "RPS is going down\n");
1151
1152   if ( GNUNET_SCHEDULER_NO_TASK != do_round_task )
1153   {
1154     GNUNET_SCHEDULER_cancel (do_round_task);
1155     do_round_task = GNUNET_SCHEDULER_NO_TASK;
1156   }
1157
1158   GNUNET_NSE_disconnect(nse);
1159   GNUNET_CADET_disconnect(cadet_handle);
1160   GNUNET_free(own_identity);
1161   //GNUNET_free(round_interval);
1162   //GNUNET_free(est_size);
1163   //GNUNET_free(gossip_list_size);
1164   //GNUNET_free(sampler_list_size);
1165   GNUNET_free(gossip_list);
1166   // TODO for i in sampler_list free sampler
1167   // TODO destroy sampler_list
1168   // TODO destroy push/pull_list
1169   // TODO delete global data
1170 }
1171
1172
1173 /**
1174  * A client disconnected.  Remove all of its data structure entries.
1175  *
1176  * @param cls closure, NULL
1177  * @param client identification of the client
1178  */
1179 static void
1180 handle_client_disconnect (void *cls,
1181                           struct GNUNET_SERVER_Client * client)
1182 {
1183   // TODO reinitialise that sampler
1184 }
1185
1186 /**
1187  * Handle the channel a peer opens to us.
1188  *
1189  * @param cls The closure
1190  * @param channel The channel the peer wants to establish
1191  * @param initiator The peer's peer ID
1192  * @param port The port the channel is being established over
1193  * @param options Further options
1194  */
1195   static void *
1196 handle_inbound_channel (void *cls,
1197                         struct GNUNET_CADET_Channel *channel,
1198                         const struct GNUNET_PeerIdentity *initiator,
1199                         uint32_t port,
1200                         enum GNUNET_CADET_ChannelOption options)
1201 {
1202   LOG(GNUNET_ERROR_TYPE_DEBUG, "New channel was established to us.\n");
1203
1204   GNUNET_assert( NULL != channel );
1205
1206   // TODO we might even not store the from_channel
1207
1208   if ( GNUNET_CONTAINER_multipeermap_contains( peer_map, initiator ) ) {
1209     ((struct peer_context *) GNUNET_CONTAINER_multipeermap_get( peer_map, initiator ))->from_channel = channel;
1210     // FIXME there might already be an established channel
1211   } else {
1212     struct peer_context *ctx;
1213
1214     ctx = GNUNET_malloc( sizeof(struct peer_context));
1215     ctx->in_flags = in_other_gossip_list;
1216     ctx->mq = NULL; // TODO create mq?
1217     ctx->from_channel = channel;
1218
1219     GNUNET_CONTAINER_multipeermap_put( peer_map, initiator, ctx, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1220   }
1221   return NULL; // TODO
1222 }
1223
1224 /**
1225  * This is called when a remote peer destroys a channel.
1226  *
1227  * @param cls The closure
1228  * @param channel The channel being closed
1229  * @param channel_ctx The context associated with this channel
1230  */
1231 static void
1232 cleanup_channel(void *cls,
1233                 const struct GNUNET_CADET_Channel *channel,
1234                 void *channel_ctx)
1235 {
1236   LOG(GNUNET_ERROR_TYPE_DEBUG, "Channel was destroyed by remote peer.\n");
1237 }
1238
1239 /**
1240  * Actually start the service.
1241  */
1242 static void
1243 rps_start (struct GNUNET_SERVER_Handle *server)
1244 {
1245   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1246     {&handle_cs_request, NULL, GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST, 0},
1247     {NULL, NULL, 0, 0}
1248   };
1249
1250   GNUNET_SERVER_add_handlers (server, handlers);
1251   GNUNET_SERVER_disconnect_notify (server,
1252                                    &handle_client_disconnect,
1253                                    NULL);
1254   LOG(GNUNET_ERROR_TYPE_DEBUG, "Ready to receive requests from clients\n");
1255
1256
1257
1258   do_round_task = GNUNET_SCHEDULER_add_delayed( round_interval, &do_round, NULL);
1259   LOG(GNUNET_ERROR_TYPE_DEBUG, "Scheduled first round\n");
1260
1261   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1262                                 &shutdown_task,
1263                                 NULL);
1264 }
1265
1266
1267
1268 /**
1269  * Process statistics requests.
1270  *
1271  * @param cls closure
1272  * @param server the initialized server
1273  * @param c configuration to use
1274  */
1275 static void
1276 run (void *cls,
1277      struct GNUNET_SERVER_Handle *server,
1278      const struct GNUNET_CONFIGURATION_Handle *c)
1279 {
1280   // TODO check what this does -- copied from gnunet-boss
1281   // - seems to work as expected
1282   GNUNET_log_setup("rps", GNUNET_error_type_to_string(GNUNET_ERROR_TYPE_DEBUG), NULL);
1283
1284   LOG(GNUNET_ERROR_TYPE_DEBUG, "RPS started\n");
1285
1286   cfg = c;
1287
1288
1289   own_identity = GNUNET_new(struct GNUNET_PeerIdentity);
1290
1291   GNUNET_CRYPTO_get_peer_identity(cfg, own_identity); // TODO check return value
1292
1293   LOG(GNUNET_ERROR_TYPE_DEBUG, "Own identity is %s (at %p).\n", GNUNET_i2s(own_identity), own_identity);
1294
1295
1296
1297   /* Get time interval from the configuration */
1298   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time (cfg, "RPS",
1299                                                         "ROUNDINTERVAL",
1300                                                         &round_interval))
1301   {
1302     LOG(GNUNET_ERROR_TYPE_DEBUG, "Failed to read ROUNDINTERVAL from config\n");
1303     GNUNET_SCHEDULER_shutdown();
1304     return;
1305   }
1306
1307   /* Get initial size of sampler/gossip list from the configuration */
1308   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg, "RPS",
1309                                                          "INITSIZE",
1310                                                          (long long unsigned int *) &est_size))
1311   {
1312     LOG(GNUNET_ERROR_TYPE_DEBUG, "Failed to read INITSIZE from config\n");
1313     GNUNET_SCHEDULER_shutdown();
1314     return;
1315   }
1316   LOG(GNUNET_ERROR_TYPE_DEBUG, "INITSIZE is %" PRIu64 "\n", est_size);
1317
1318   gossip_list_size = sampler_list_size = est_size; // TODO rename est_size
1319
1320
1321   gossip_list = NULL;
1322
1323   static unsigned int tmp = 0;
1324
1325   GNUNET_array_grow(gossip_list, tmp, gossip_list_size);
1326
1327
1328
1329   /* connect to NSE */
1330   nse = GNUNET_NSE_connect(cfg, nse_callback, NULL);
1331   // TODO check whether that was successful
1332   // TODO disconnect on shutdown
1333   LOG(GNUNET_ERROR_TYPE_DEBUG, "Connected to NSE\n");
1334
1335
1336   alpha = 0.45;
1337   beta  = 0.45;
1338   // TODO initialise thresholds - ?
1339
1340   ///* Get alpha from the configuration */
1341   //if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_float (cfg, "RPS",
1342   //                                                       "ALPHA",
1343   //                                                       &alpha))
1344   //{
1345   //  LOG(GNUNET_ERROR_TYPE_DEBUG, "No ALPHA specified in the config\n");
1346   //}
1347   //LOG(GNUNET_ERROR_TYPE_DEBUG, "ALPHA is %f\n", alpha);
1348  
1349   ///* Get beta from the configuration */
1350   //if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_float (cfg, "RPS",
1351   //                                                       "BETA",
1352   //                                                       &beta))
1353   //{
1354   //  LOG(GNUNET_ERROR_TYPE_DEBUG, "No BETA specified in the config\n");
1355   //}
1356   //LOG(GNUNET_ERROR_TYPE_DEBUG, "BETA is %f\n", beta);
1357
1358
1359
1360
1361   peer_map = GNUNET_CONTAINER_multipeermap_create(est_size, GNUNET_NO);
1362
1363
1364   /* Initialise sampler and gossip list */
1365
1366   sampler_list = SAMPLER_samplers_init(est_size);
1367
1368   push_list = NULL;
1369   push_list_size = 0;
1370   pull_list = NULL;
1371   pull_list_size = 0;
1372
1373   static const struct GNUNET_CADET_MessageHandler cadet_handlers[] = {
1374     {&handle_peer_push        , GNUNET_MESSAGE_TYPE_RPS_PP_PUSH        , 0},
1375     {&handle_peer_pull_request, GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST, 0},
1376     {&handle_peer_pull_reply  , GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY  , 0},
1377     {NULL, 0, 0}
1378   };
1379
1380   const uint32_t ports[] = {GNUNET_RPS_CADET_PORT, 0}; // _PORT specified in src/rps/rps.h
1381   cadet_handle = GNUNET_CADET_connect(cfg,
1382                                     cls,
1383                                     &handle_inbound_channel,
1384                                     &cleanup_channel,
1385                                     cadet_handlers,
1386                                     ports);
1387   LOG(GNUNET_ERROR_TYPE_DEBUG, "Connected to CADET\n");
1388
1389
1390   LOG(GNUNET_ERROR_TYPE_DEBUG, "Requesting peers from CADET\n");
1391   GNUNET_CADET_get_peers(cadet_handle, &init_peer_cb, server);
1392   // FIXME use magic 0000 PeerID to _start_ the service
1393
1394   // TODO send push/pull to each of those peers?
1395 }
1396
1397
1398 /**
1399  * The main function for the rps service.
1400  *
1401  * @param argc number of arguments from the command line
1402  * @param argv command line arguments
1403  * @return 0 ok, 1 on error
1404  */
1405 int
1406 main (int argc, char *const *argv)
1407 {
1408   return (GNUNET_OK ==
1409           GNUNET_SERVICE_run (argc,
1410                               argv,
1411                               "rps",
1412                               GNUNET_SERVICE_OPTION_NONE,
1413                               &run, NULL)) ? 0 : 1;
1414 }
1415
1416 /* end of gnunet-service-rps.c */