plugin datastore mysql
[oweals/gnunet.git] / src / rps / gnunet-service-rps.c
index c7cdccdfb7f61f5b8ff79c080ace1a9c8fcd7bd4..4e24c62315efb9288f9f259e4839dfd2247946e4 100644 (file)
@@ -252,11 +252,6 @@ static struct GNUNET_TIME_Relative request_deltas[REQUEST_DELTAS_SIZE];
  */
 static struct GNUNET_TIME_Relative request_rate;
 
-/**
- * Number of history update tasks.
- */
-static uint32_t num_hist_update_tasks;
-
 
 #ifdef ENABLE_MALICIOUS
 /**
@@ -441,6 +436,45 @@ T_relative_avg (const struct GNUNET_TIME_Relative *rel_array,
 }
 
 
+/**
+ * Insert PeerID in #view
+ *
+ * Called once we know a peer is live.
+ * Implements #PeerOp
+ *
+ * @return GNUNET_OK if peer was actually inserted
+ *         GNUNET_NO if peer was not inserted
+ */
+static void
+insert_in_view_op (void *cls,
+               const struct GNUNET_PeerIdentity *peer);
+
+/**
+ * Insert PeerID in #view
+ *
+ * Called once we know a peer is live.
+ *
+ * @return GNUNET_OK if peer was actually inserted
+ *         GNUNET_NO if peer was not inserted
+ */
+static int
+insert_in_view (const struct GNUNET_PeerIdentity *peer)
+{
+  int online;
+
+  online = Peers_check_peer_flag (peer, Peers_ONLINE);
+  if ( (GNUNET_NO == online) ||
+       (GNUNET_SYSERR == online) ) /* peer is not even known */
+  {
+    (void) Peers_issue_peer_liveliness_check (peer);
+    (void) Peers_schedule_operation (peer, insert_in_view_op);
+    return GNUNET_NO;
+  }
+  /* Open channel towards peer to keep connection open */
+  Peers_indicate_sending_intention (peer);
+  return View_put (peer);
+}
+
 /**
  * Put random peer from sampler into the view as history update.
  */
@@ -453,13 +487,11 @@ hist_update (void *cls,
 
   for (i = 0; i < num_peers; i++)
   {
-    View_put (&ids[i]);
+    (void) insert_in_view (&ids[i]);
     to_file (file_name_view_log,
              "+%s\t(hist)",
              GNUNET_i2s_full (ids));
   }
-  if (0 < num_hist_update_tasks)
-    num_hist_update_tasks--;
 }
 
 
@@ -649,11 +681,10 @@ insert_in_pull_map (void *cls,
  * Implements #PeerOp
  */
 static void
-insert_in_view (void *cls,
+insert_in_view_op (void *cls,
                const struct GNUNET_PeerIdentity *peer)
 {
-  GNUNET_assert (GNUNET_YES == Peers_check_peer_flag (peer, Peers_ONLINE));
-  View_put (peer);
+  (void) insert_in_view (peer);
 }
 
 
@@ -673,11 +704,10 @@ insert_in_sampler (void *cls,
   if (0 < RPS_sampler_count_id (prot_sampler, peer))
   {
     /* Make sure we 'know' about this peer */
-    (void) Peers_insert_peer_check_liveliness (peer);
+    (void) Peers_issue_peer_liveliness_check (peer);
     /* Establish a channel towards that peer to indicate we are going to send
      * messages to it */
-    Peers_indicate_sending_intention (peer);
-    //Peers_issue_peer_liveliness_check (peer);
+    //Peers_indicate_sending_intention (peer);
   }
 }
 
@@ -691,14 +721,13 @@ static void
 got_peer (const struct GNUNET_PeerIdentity *peer)
 {
   /* If we did not know this peer already, insert it into sampler and view */
-  if (GNUNET_YES == Peers_insert_peer_check_liveliness (peer))
+  if (GNUNET_YES == Peers_issue_peer_liveliness_check (peer))
   {
     Peers_schedule_operation (peer, insert_in_sampler);
-    Peers_schedule_operation (peer, insert_in_view);
+    Peers_schedule_operation (peer, insert_in_view_op);
   }
 }
 
-
 /**
  * @brief Checks if there is a sending channel and if it is needed
  *
@@ -1387,7 +1416,7 @@ handle_peer_pull_reply (void *cls,
                                               &peers[i]))
     {
       /* Make sure we 'know' about this peer */
-      (void) Peers_insert_peer_check_liveliness (&peers[i]);
+      (void) Peers_insert_peer (&peers[i]);
 
       if (GNUNET_YES == Peers_check_peer_valid (&peers[i]))
       {
@@ -1396,7 +1425,7 @@ handle_peer_pull_reply (void *cls,
       else
       {
         Peers_schedule_operation (&peers[i], insert_in_pull_map);
-        Peers_issue_peer_liveliness_check (&peers[i]);
+        (void) Peers_issue_peer_liveliness_check (&peers[i]);
       }
     }
   }
@@ -1606,8 +1635,7 @@ handle_client_act_malicious (void *cls,
     /* Set the flag of the attacked peer to valid to avoid problems */
     if (GNUNET_NO == Peers_check_peer_known (&attacked_peer))
     {
-      Peers_insert_peer_check_liveliness (&attacked_peer);
-      Peers_issue_peer_liveliness_check (&attacked_peer);
+      (void) Peers_issue_peer_liveliness_check (&attacked_peer);
     }
 
     LOG (GNUNET_ERROR_TYPE_DEBUG,
@@ -1697,7 +1725,7 @@ do_mal_round (void *cls)
      * Send as many pushes to the attacked peer as possible
      * That is one push per round as it will ignore more.
      */
-    Peers_insert_peer_check_liveliness (&attacked_peer);
+    (void) Peers_issue_peer_liveliness_check (&attacked_peer);
     if (GNUNET_YES == Peers_check_peer_valid (&attacked_peer))
       send_push (&attacked_peer);
   }
@@ -1709,7 +1737,7 @@ do_mal_round (void *cls)
     /* Send PUSH to attacked peers */
     if (GNUNET_YES == Peers_check_peer_known (&attacked_peer))
     {
-      Peers_insert_peer_check_liveliness (&attacked_peer);
+      (void) Peers_issue_peer_liveliness_check (&attacked_peer);
       if (GNUNET_YES == Peers_check_peer_valid (&attacked_peer))
       {
         LOG (GNUNET_ERROR_TYPE_DEBUG,
@@ -1717,12 +1745,8 @@ do_mal_round (void *cls)
             GNUNET_i2s (&attacked_peer));
         send_push (&attacked_peer);
       }
-      else
-        Peers_issue_peer_liveliness_check (&attacked_peer);
     }
-    else
-      Peers_insert_peer_check_liveliness (&attacked_peer);
-    Peers_issue_peer_liveliness_check (&attacked_peer);
+    (void) Peers_issue_peer_liveliness_check (&attacked_peer);
 
     /* The maximum of pushes we're going to send this round */
     num_pushes = GNUNET_MIN (GNUNET_MIN (push_limit - 1,
@@ -1891,7 +1915,8 @@ do_round (void *cls)
                                            CustomPeerMap_size (push_map));
     for (i = 0; i < first_border; i++)
     {
-      View_put (CustomPeerMap_get_peer_by_index (push_map, permut[i]));
+      (void) insert_in_view (CustomPeerMap_get_peer_by_index (push_map,
+                                                              permut[i]));
       to_file (file_name_view_log,
                "+%s\t(push list)",
                GNUNET_i2s_full (&view_array[i]));
@@ -1905,8 +1930,8 @@ do_round (void *cls)
                                            CustomPeerMap_size (pull_map));
     for (i = first_border; i < second_border; i++)
     {
-      View_put (CustomPeerMap_get_peer_by_index (pull_map,
-                                                 permut[i - first_border]));
+      (void) insert_in_view (CustomPeerMap_get_peer_by_index (pull_map,
+            permut[i - first_border]));
       to_file (file_name_view_log,
                "+%s\t(pull list)",
                GNUNET_i2s_full (&view_array[i]));
@@ -1920,7 +1945,6 @@ do_round (void *cls)
                                   hist_update,
                                   NULL,
                                   final_size - second_border);
-    num_hist_update_tasks = final_size - second_border;
     // TODO change the peer_flags accordingly
 
     for (i = 0; i < View_size (); i++)
@@ -2019,6 +2043,31 @@ init_peer_cb (void *cls,
   }
 }
 
+/**
+ * @brief Iterator function over stored, valid peers.
+ *
+ * We initialise the sampler with those.
+ *
+ * @param cls the closure
+ * @param peer the peer id
+ * @return #GNUNET_YES if we should continue to
+ *         iterate,
+ *         #GNUNET_NO if not.
+ */
+static int
+valid_peers_iterator (void *cls,
+                      const struct GNUNET_PeerIdentity *peer)
+{
+  if (NULL != peer)
+  {
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "Got stored, valid peer %s\n",
+         GNUNET_i2s (peer));
+    got_peer (peer);
+  }
+  return GNUNET_YES;
+}
+
 
 /**
  * Iterator over peers from peerinfo.
@@ -2309,13 +2358,12 @@ run (void *cls,
   pull_map = CustomPeerMap_create (4);
 
 
-  num_hist_update_tasks = 0;
-
-
   LOG (GNUNET_ERROR_TYPE_DEBUG, "Requesting peers from CADET\n");
   GNUNET_CADET_get_peers (cadet_handle, &init_peer_cb, NULL);
   // TODO send push/pull to each of those peers?
   // TODO read stored valid peers from last run
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "Requesting stored valid peers\n");
+  Peers_get_valid_peers (valid_peers_iterator, NULL);
 
   peerinfo_notify_handle = GNUNET_PEERINFO_notify (cfg,
                                                    GNUNET_NO,