Adding a function pick_random_friend ()
[oweals/gnunet.git] / src / dht / gnunet-service-wdht_neighbours.c
index b3a45c44d4496e6cbea228bb81e198ad90e8c66f..4e6ccf4324b0c94beb77b40a383e1d0572a411cb 100644 (file)
@@ -717,6 +717,68 @@ delete_trail (struct Trail *trail,
 }
 
 
+/**
+ * Blah.
+ */
+static void
+forward_message_on_trail (struct FriendInfo *next_target,
+                          const struct GNUNET_HashCode *trail_id,
+                          int have_path,
+                          const struct GNUNET_PeerIdentity *predecessor,
+                          const struct GNUNET_PeerIdentity *path,
+                          uint16_t path_length,
+                          const struct GNUNET_MessageHeader *payload)
+{
+  struct GNUNET_MQ_Envelope *env;
+  struct TrailRouteMessage *trm;
+  struct GNUNET_PeerIdentity *new_path;
+  unsigned int plen;
+  uint16_t payload_len;
+
+  payload_len = ntohs (payload->size);
+  if (have_path)
+  {
+    plen = path_length + 1;
+    if (plen >= (GNUNET_SERVER_MAX_MESSAGE_SIZE
+                 - payload_len
+                 - sizeof (struct TrailRouteMessage))
+        / sizeof (struct GNUNET_PeerIdentity))
+    {
+      /* Should really not have paths this long... */
+      GNUNET_break_op (0);
+      plen = 0;
+      have_path = 0;
+    }
+  }
+  else
+  {
+    GNUNET_break_op (0 == path_length);
+    path_length = 0;
+    plen = 0;
+  }
+  env = GNUNET_MQ_msg_extra (trm,
+                             payload_len +
+                             plen * sizeof (struct GNUNET_PeerIdentity),
+                             GNUNET_MESSAGE_TYPE_WDHT_TRAIL_ROUTE);
+  trm->record_path = htons (have_path);
+  trm->path_length = htons (plen);
+  trm->trail_id = *trail_id;
+  new_path = (struct GNUNET_PeerIdentity *) &trm[1];
+  if (have_path)
+  {
+    memcpy (new_path,
+            path,
+            path_length * sizeof (struct GNUNET_PeerIdentity));
+    new_path[path_length] = *predecessor;
+  }
+  memcpy (&new_path[plen],
+          payload,
+          payload_len);
+  GNUNET_MQ_send (next_target->mq,
+                  env);
+}
+
+
 /**
  * Send the get result to requesting client.
  *
@@ -741,8 +803,19 @@ GDS_NEIGHBOURS_send_get_result (const struct GNUNET_HashCode *trail_id,
                                 const void *data,
                                 size_t data_size)
 {
-  // TRICKY: need to introduce some context to remember trail from
-  // the lookup...
+  struct GNUNET_MessageHeader *payload;
+
+  payload = GNUNET_malloc (sizeof (struct GNUNET_MessageHeader) + data_size);
+  payload->size = data_size;
+  payload->type = GNUNET_MESSAGE_TYPE_WDHT_GET_RESULT;
+
+  forward_message_on_trail (NULL /* FIXME: put something right */,
+                            trail_id,
+                            0 /* FIXME: put something right */,
+                            &my_identity,
+                            put_path,
+                            put_path_length,
+                            payload);
 }
 
 
@@ -802,9 +875,24 @@ handle_core_disconnect (void *cls,
 static struct FriendInfo *
 pick_random_friend ()
 {
-  // TODO: need to extend peermap API to return random entry...
-  // (Note: same extension exists for hashmap API).
-  return NULL; // FIXME...
+  GNUNET_CONTAINER_PeerMapIterator *it;
+  if (0 != GNUNET_CONTAINER_multipeermap_get_random (friends_peermap,
+                                                     *it,
+                                                     NULL) ){
+    static struct FriendInfo **friend;
+    struct GNUNET_PeerIdentity *key;
+
+    /* FIXME: i am not sure of this one */
+    key = NULL;
+
+    if(GNUNET_YES == GNUNET_CONTAINER_multipeermap_iterator_next (it,
+                                                                  key,
+                                                                  (void *) friend))
+    {
+      return *friend;
+    }
+  }
+  return NULL;
 }
 
 
@@ -904,7 +992,7 @@ do_random_walk (void *cls,
   if (ft->finger_array_size < 42)
   {
     // FIXME: must have finger array of the right size here,
-    // FIXME: growing / shrinking are tricy -- with pointers
+    // FIXME: growing / shrinking are tricky -- with pointers
     // from Trails!!!
   }
 
@@ -1131,7 +1219,8 @@ handle_dht_p2p_random_walk_response (void *cls,
   const struct RandomWalkResponseMessage *rwrm;
 
   rwrm = (const struct RandomWalkResponseMessage *) message;
-  // 1) lookup trail => find Finger entry => fill in 'destination' and mark valid, move to end of sorted array, mark unsorted, update links from 'trails'
+  // 1) lookup trail => find Finger entry => fill in 'destination' and mark valid, move to end of sorted array,
+  //mark unsorted, update links from 'trails'
   /*
    * Steps :
    *  1 check if we are the correct layer
@@ -1357,68 +1446,6 @@ struct TrailHandler
 };
 
 
-/**
- * Blah.
- */
-static void
-forward_message_on_trail (struct FriendInfo *next_target,
-                          const struct GNUNET_HashCode *trail_id,
-                          int have_path,
-                          const struct GNUNET_PeerIdentity *predecessor,
-                          const struct GNUNET_PeerIdentity *path,
-                          uint16_t path_length,
-                          const struct GNUNET_MessageHeader *payload)
-{
-  struct GNUNET_MQ_Envelope *env;
-  struct TrailRouteMessage *trm;
-  struct GNUNET_PeerIdentity *new_path;
-  unsigned int plen;
-  uint16_t payload_len;
-
-  payload_len = ntohs (payload->size);
-  if (have_path)
-  {
-    plen = path_length + 1;
-    if (plen >= (GNUNET_SERVER_MAX_MESSAGE_SIZE
-                 - payload_len
-                 - sizeof (struct TrailRouteMessage))
-        / sizeof (struct GNUNET_PeerIdentity))
-    {
-      /* Should really not have paths this long... */
-      GNUNET_break_op (0);
-      plen = 0;
-      have_path = 0;
-    }
-  }
-  else
-  {
-    GNUNET_break_op (0 == path_length);
-    path_length = 0;
-    plen = 0;
-  }
-  env = GNUNET_MQ_msg_extra (trm,
-                             payload_len +
-                             plen * sizeof (struct GNUNET_PeerIdentity),
-                             GNUNET_MESSAGE_TYPE_WDHT_TRAIL_ROUTE);
-  trm->record_path = htons (have_path);
-  trm->path_length = htons (plen);
-  trm->trail_id = *trail_id;
-  new_path = (struct GNUNET_PeerIdentity *) &trm[1];
-  if (have_path)
-  {
-    memcpy (new_path,
-            path,
-            path_length * sizeof (struct GNUNET_PeerIdentity));
-    new_path[path_length] = *predecessor;
-  }
-  memcpy (&new_path[plen],
-          payload,
-          payload_len);
-  GNUNET_MQ_send (next_target->mq,
-                  env);
-}
-
-
 /**
  * Handle a `struct TrailRouteMessage`.
  *