update velocity always at the end of iteration
[oweals/gnunet.git] / src / rps / rps_api.c
index b055f6265304cfb0059c18ef072ebd4cb3e00506..0da3bd4a4a43b3c5a8f45ba56f3a0d7011069d0d 100644 (file)
@@ -40,12 +40,7 @@ struct GNUNET_RPS_Handle
   /**
    * The handle to the client configuration.
    */
-  struct GNUNET_CONFIGURATION_Handle *cfg;
-
-  /**
-   * The connection to the client.
-   */
-  struct GNUNET_CLIENT_Connection *conn;
+  const struct GNUNET_CONFIGURATION_Handle *cfg;
 
   /**
    * The message queue to the client.
@@ -61,6 +56,16 @@ struct GNUNET_RPS_Handle
    * The id of the last request.
    */
   uint32_t current_request_id;
+
+  /**
+   * @brief Callback called on each update of the view
+   */
+  GNUNET_RPS_ViewUpdateCB view_update_cb;
+
+  /**
+   * @brief Callback called on each update of the view
+   */
+  void *view_update_cls;
 };
 
 
@@ -79,6 +84,11 @@ struct GNUNET_RPS_Request_Handle
    */
   uint32_t id;
 
+  /**
+   * The number of requested peers.
+   */
+  uint32_t num_peers;
+
   /**
    * The callback to be called when we receive an answer.
    */
@@ -113,6 +123,64 @@ struct cb_cls_pack
  struct GNUNET_CLIENT_Connection *service_conn;
 };
 
+/**
+ * @brief Send a request to the service.
+ *
+ * @param h rps handle
+ * @param id id of the request
+ * @param num_req_peers number of peers
+ */
+void
+send_request (const struct GNUNET_RPS_Handle *h,
+              uint32_t id,
+              uint32_t num_req_peers)
+{
+  struct GNUNET_MQ_Envelope *ev;
+  struct GNUNET_RPS_CS_RequestMessage *msg;
+
+  ev = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST);
+  msg->num_peers = htonl (num_req_peers);
+  msg->id = htonl (id);
+  GNUNET_MQ_send (h->mq, ev);
+}
+
+/**
+ * @brief Iterator function over pending requests
+ *
+ * Implements #GNUNET_CONTAINER_HashMapIterator32
+ *
+ * @param cls rps handle
+ * @param key id of the request
+ * @param value request handle
+ *
+ * @return GNUNET_YES to continue iteration
+ */
+int
+resend_requests_iterator (void *cls, uint32_t key, void *value)
+{
+  const struct GNUNET_RPS_Handle *h = cls;
+  const struct GNUNET_RPS_Request_Handle *req_handle = value;
+
+  send_request (h, req_handle->id, req_handle->num_peers);
+  return GNUNET_YES; /* continue iterating */
+}
+
+/**
+ * @brief Resend all pending requests
+ *
+ * This is used to resend all pending requests after the client
+ * reconnected to the service, because the service cancels all
+ * pending requests after reconnection.
+ *
+ * @param h rps handle
+ */
+void
+resend_requests (struct GNUNET_RPS_Handle *h)
+{
+  GNUNET_CONTAINER_multihashmap32_iterate (h->req_handlers,
+                                           resend_requests_iterator,
+                                           h);
+}
 
 
 /**
@@ -170,6 +238,7 @@ handle_reply (void *cls,
       GNUNET_CONTAINER_multihashmap32_contains (h->req_handlers, id));
   rh = GNUNET_CONTAINER_multihashmap32_get (h->req_handlers, id);
   GNUNET_assert (NULL != rh);
+  GNUNET_assert (rh->num_peers == ntohl (msg->num_peers));
   GNUNET_CONTAINER_multihashmap32_remove_all (h->req_handlers, id);
   rh->ready_cb (rh->ready_cb_cls,
                 ntohl (msg->num_peers),
@@ -177,6 +246,89 @@ handle_reply (void *cls,
 }
 
 
+/* Get internals for debugging/profiling purposes */
+
+/**
+ * Request updates of view
+ *
+ * @param rps_handle handle to the rps service
+ * @param num_req_peers number of peers we want to receive
+ *        (0 for infinite updates)
+ * @param cls a closure that will be given to the callback
+ * @param ready_cb the callback called when the peers are available
+ */
+void
+GNUNET_RPS_view_request (struct GNUNET_RPS_Handle *rps_handle,
+                         uint32_t num_updates,
+                         GNUNET_RPS_ViewUpdateCB view_update_cb,
+                         void *cls)
+{
+  struct GNUNET_MQ_Envelope *ev;
+  struct GNUNET_RPS_CS_DEBUG_ViewRequest *msg;
+
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "Client requests %" PRIu32 " view updates\n",
+       num_updates);
+  rps_handle->view_update_cb = view_update_cb;
+  rps_handle->view_update_cls = cls;
+
+  ev = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_VIEW_REQUEST);
+  msg->num_updates = htonl (num_updates);
+  GNUNET_MQ_send (rps_handle->mq, ev);
+}
+
+/**
+ * This function is called, when the service updates the view.
+ * It verifies that @a msg is well-formed.
+ *
+ * @param cls the closure
+ * @param msg the message
+ * @return #GNUNET_OK if @a msg is well-formed
+ */
+static int
+check_view_update (void *cls,
+                   const struct GNUNET_RPS_CS_DEBUG_ViewReply *msg)
+{
+  uint16_t msize = ntohs (msg->header.size);
+  uint32_t num_peers = ntohl (msg->num_peers);
+
+  msize -= sizeof (struct GNUNET_RPS_CS_DEBUG_ViewReply);
+  if ( (msize / sizeof (struct GNUNET_PeerIdentity) != num_peers) ||
+       (msize % sizeof (struct GNUNET_PeerIdentity) != 0) )
+  {
+    GNUNET_break (0);
+    return GNUNET_SYSERR;
+  }
+  return GNUNET_OK;
+}
+
+/**
+ * This function is called, when the service updated its view.
+ * It calls the callback the caller provided
+ * and disconnects afterwards.
+ *
+ * @param msg the message
+ */
+static void
+handle_view_update (void *cls,
+                    const struct GNUNET_RPS_CS_DEBUG_ViewReply *msg)
+{
+  struct GNUNET_RPS_Handle *h = cls;
+  struct GNUNET_PeerIdentity *peers;
+
+  /* Give the peers back */
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "New view of %" PRIu32 " peers:\n",
+       ntohl (msg->num_peers));
+
+  peers = (struct GNUNET_PeerIdentity *) &msg[1];
+  GNUNET_assert (NULL != h);
+  GNUNET_assert (NULL != h->view_update_cb);
+  h->view_update_cb (h->view_update_cls, ntohl (msg->num_peers), peers);
+}
+
+
+
 /**
  * Reconnect to the service
  */
@@ -205,6 +357,9 @@ mq_error_handler (void *cls,
        4: TIMEOUT\n",
        error);
   reconnect (h);
+  /* Resend all pending request as the service destroyed its knowledge
+   * about them */
+  resend_requests (h);
 }
 
 
@@ -214,24 +369,25 @@ mq_error_handler (void *cls,
 static void
 reconnect (struct GNUNET_RPS_Handle *h)
 {
-  GNUNET_MQ_hd_var_size (reply,
-                         GNUNET_MESSAGE_TYPE_RPS_CS_REPLY,
-                         struct GNUNET_RPS_CS_ReplyMessage);
   struct GNUNET_MQ_MessageHandler mq_handlers[] = {
-    make_reply_handler (h),
+    GNUNET_MQ_hd_var_size (reply,
+                           GNUNET_MESSAGE_TYPE_RPS_CS_REPLY,
+                           struct GNUNET_RPS_CS_ReplyMessage,
+                           h),
+    GNUNET_MQ_hd_var_size (view_update,
+                           GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_VIEW_REPLY,
+                           struct GNUNET_RPS_CS_DEBUG_ViewReply,
+                           h),
     GNUNET_MQ_handler_end ()
   };
 
   if (NULL != h->mq)
     GNUNET_MQ_destroy (h->mq);
-  if (NULL != h->conn)
-    GNUNET_CLIENT_disconnect (h->conn);
-  h->conn = GNUNET_CLIENT_connect ("rps", h->cfg);
-  GNUNET_assert (NULL != h->conn);
-  h->mq = GNUNET_MQ_queue_for_connection_client (h->conn,
-                                                 mq_handlers,
-                                                 &mq_error_handler,
-                                                 h);
+  h->mq = GNUNET_CLIENT_connect (h->cfg,
+                                 "rps",
+                                 mq_handlers,
+                                 &mq_error_handler,
+                                 h);
 }
 
 
@@ -241,15 +397,20 @@ reconnect (struct GNUNET_RPS_Handle *h)
  * @param cfg configuration to use
  * @return a handle to the service
  */
-  struct GNUNET_RPS_Handle *
+struct GNUNET_RPS_Handle *
 GNUNET_RPS_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
 {
   struct GNUNET_RPS_Handle *h;
-  //struct GNUNET_RPS_Request_Handle *rh;
 
-  h = GNUNET_new(struct GNUNET_RPS_Handle);
-  h->cfg = GNUNET_CONFIGURATION_dup (cfg);
+  h = GNUNET_new (struct GNUNET_RPS_Handle);
+  h->current_request_id = 0;
+  h->cfg = cfg;
   reconnect (h);
+  if (NULL == h->mq)
+  {
+    GNUNET_free (h);
+    return NULL;
+  }
   h->req_handlers = GNUNET_CONTAINER_multihashmap32_create (4);
   return h;
 }
@@ -264,20 +425,18 @@ GNUNET_RPS_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
  * @param cls closure given to the callback
  * @return a handle to cancel this request
  */
-  struct GNUNET_RPS_Request_Handle *
+struct GNUNET_RPS_Request_Handle *
 GNUNET_RPS_request_peers (struct GNUNET_RPS_Handle *rps_handle,
                           uint32_t num_req_peers,
                           GNUNET_RPS_NotifyReadyCB ready_cb,
                           void *cls)
 {
   struct GNUNET_RPS_Request_Handle *rh;
-  struct GNUNET_MQ_Envelope *ev;
-  struct GNUNET_RPS_CS_RequestMessage *msg;
 
-  // assert func != NULL
   rh = GNUNET_new (struct GNUNET_RPS_Request_Handle);
   rh->rps_handle = rps_handle;
   rh->id = rps_handle->current_request_id++;
+  rh->num_peers = num_req_peers;
   rh->ready_cb = ready_cb;
   rh->ready_cb_cls = cls;
 
@@ -289,10 +448,7 @@ GNUNET_RPS_request_peers (struct GNUNET_RPS_Handle *rps_handle,
   GNUNET_CONTAINER_multihashmap32_put (rps_handle->req_handlers, rh->id, rh,
       GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
 
-  ev = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST);
-  msg->num_peers = htonl (num_req_peers);
-  msg->id = htonl (rh->id);
-  GNUNET_MQ_send (rps_handle->mq, ev);
+  send_request (rps_handle, rh->id, num_req_peers);
   return rh;
 }
 
@@ -304,7 +460,7 @@ GNUNET_RPS_request_peers (struct GNUNET_RPS_Handle *rps_handle,
  * @param n number of peers to seed
  * @param ids the ids of the peers seeded
  */
-  void
+void
 GNUNET_RPS_seed_ids (struct GNUNET_RPS_Handle *h,
                      uint32_t n,
                      const struct GNUNET_PeerIdentity *ids)
@@ -331,17 +487,17 @@ GNUNET_RPS_seed_ids (struct GNUNET_RPS_Handle *h,
     n * sizeof (struct GNUNET_PeerIdentity);
   /* The number of peers that fits in one message together with
    * the respective header */
-  num_peers_max = (GNUNET_SERVER_MAX_MESSAGE_SIZE -
+  num_peers_max = (GNUNET_MAX_MESSAGE_SIZE -
       sizeof (struct GNUNET_RPS_CS_SeedMessage)) /
     sizeof (struct GNUNET_PeerIdentity);
   tmp_peer_pointer = ids;
 
-  while (GNUNET_SERVER_MAX_MESSAGE_SIZE < size_needed)
+  while (GNUNET_MAX_MESSAGE_SIZE < size_needed)
   {
     ev = GNUNET_MQ_msg_extra (msg, num_peers_max * sizeof (struct GNUNET_PeerIdentity),
         GNUNET_MESSAGE_TYPE_RPS_CS_SEED);
     msg->num_peers = htonl (num_peers_max);
-    memcpy (&msg[1], tmp_peer_pointer, num_peers_max * sizeof (struct GNUNET_PeerIdentity));
+    GNUNET_memcpy (&msg[1], tmp_peer_pointer, num_peers_max * sizeof (struct GNUNET_PeerIdentity));
     GNUNET_MQ_send (h->mq, ev);
 
     n -= num_peers_max;
@@ -354,7 +510,7 @@ GNUNET_RPS_seed_ids (struct GNUNET_RPS_Handle *h,
   ev = GNUNET_MQ_msg_extra (msg, n * sizeof (struct GNUNET_PeerIdentity),
                             GNUNET_MESSAGE_TYPE_RPS_CS_SEED);
   msg->num_peers = htonl (n);
-  memcpy (&msg[1], tmp_peer_pointer, n * sizeof (struct GNUNET_PeerIdentity));
+  GNUNET_memcpy (&msg[1], tmp_peer_pointer, n * sizeof (struct GNUNET_PeerIdentity));
 
   GNUNET_MQ_send (h->mq, ev);
 }
@@ -375,7 +531,7 @@ GNUNET_RPS_seed_ids (struct GNUNET_RPS_Handle *h,
  *            if @type is 2 the last id is the id of the
  *            peer to be isolated from the rest
  */
-  void
+void
 GNUNET_RPS_act_malicious (struct GNUNET_RPS_Handle *h,
                           uint32_t type,
                           uint32_t num_peers,
@@ -405,12 +561,12 @@ GNUNET_RPS_act_malicious (struct GNUNET_RPS_Handle *h,
     num_peers * sizeof (struct GNUNET_PeerIdentity);
   /* The number of peers that fit in one message together with
    * the respective header */
-  num_peers_max = (GNUNET_SERVER_MAX_MESSAGE_SIZE -
+  num_peers_max = (GNUNET_MAX_MESSAGE_SIZE -
       sizeof (struct GNUNET_RPS_CS_SeedMessage)) /
     sizeof (struct GNUNET_PeerIdentity);
   tmp_peer_pointer = peer_ids;
 
-  while (GNUNET_SERVER_MAX_MESSAGE_SIZE < size_needed)
+  while (GNUNET_MAX_MESSAGE_SIZE < size_needed)
   {
     LOG (GNUNET_ERROR_TYPE_DEBUG,
          "Too many peers to send at once, sending %" PRIu32 " (all we can so far)\n",
@@ -423,7 +579,7 @@ GNUNET_RPS_act_malicious (struct GNUNET_RPS_Handle *h,
     if ( (2 == type) ||
          (3 == type) )
       msg->attacked_peer = peer_ids[num_peers];
-    memcpy (&msg[1],
+    GNUNET_memcpy (&msg[1],
             tmp_peer_pointer,
             num_peers_max * sizeof (struct GNUNET_PeerIdentity));
 
@@ -444,7 +600,7 @@ GNUNET_RPS_act_malicious (struct GNUNET_RPS_Handle *h,
   if ( (2 == type) ||
        (3 == type) )
     msg->attacked_peer = *target_peer;
-  memcpy (&msg[1], tmp_peer_pointer, num_peers * sizeof (struct GNUNET_PeerIdentity));
+  GNUNET_memcpy (&msg[1], tmp_peer_pointer, num_peers * sizeof (struct GNUNET_PeerIdentity));
 
   GNUNET_MQ_send (h->mq, ev);
 }
@@ -456,7 +612,7 @@ GNUNET_RPS_act_malicious (struct GNUNET_RPS_Handle *h,
  *
  * @param rh request handle of request to cancle
  */
-  void
+void
 GNUNET_RPS_request_cancel (struct GNUNET_RPS_Request_Handle *rh)
 {
   struct GNUNET_RPS_Handle *h;
@@ -482,12 +638,9 @@ GNUNET_RPS_request_cancel (struct GNUNET_RPS_Request_Handle *rh)
  *
  * @param h the handle to the rps service
  */
-  void
+void
 GNUNET_RPS_disconnect (struct GNUNET_RPS_Handle *h)
 {
-  if (NULL != h->conn)
-    GNUNET_CLIENT_disconnect (h->conn);
-  GNUNET_CONFIGURATION_destroy (h->cfg);
   GNUNET_MQ_destroy (h->mq);
   if (0 < GNUNET_CONTAINER_multihashmap32_size (h->req_handlers))
     LOG (GNUNET_ERROR_TYPE_WARNING,