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