Fix for #4553
[oweals/gnunet.git] / src / rps / rps_api.c
index 16bcb9ec0c86b9b8bb46143aa0ddca3307b31964..6971fb7165ac59bf86dd069c2af8577cfc88dffb 100644 (file)
@@ -14,8 +14,8 @@
 
      You should have received a copy of the GNU General Public License
      along with GNUnet; see the file COPYING.  If not, write to the
-     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-     Boston, MA 02111-1307, USA.
+     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+     Boston, MA 02110-1301, USA.
 */
 
 /**
@@ -40,7 +40,7 @@ struct GNUNET_RPS_Handle
   /**
    * The handle to the client configuration.
    */
-  const struct GNUNET_CONFIGURATION_Handle *cfg;
+  struct GNUNET_CONFIGURATION_Handle *cfg;
 
   /**
    * The connection to the client.
@@ -51,6 +51,16 @@ struct GNUNET_RPS_Handle
    * The message queue to the client.
    */
   struct GNUNET_MQ_Handle *mq;
+
+  /**
+   * Array of Request_Handles.
+   */
+  struct GNUNET_CONTAINER_MultiHashMap32 *req_handlers;
+
+  /**
+   * The id of the last request.
+   */
+  uint32_t current_request_id;
 };
 
 
@@ -81,17 +91,6 @@ struct GNUNET_RPS_Request_Handle
 };
 
 
-/**
- * Array of Request_Handles.
- */
-struct GNUNET_RPS_Request_Handle *req_handlers = NULL;
-
-/**
- * Current length of req_handlers.
- */
-unsigned int req_handlers_size = 0;
-
-
 /**
  * Struct used to pack the callback, its closure (provided by the caller)
  * and the connection handler to the service to pass it to a callback function.
@@ -127,23 +126,36 @@ struct cb_cls_pack
 handle_reply (void *cls,
               const struct GNUNET_MessageHeader *message)
 {
+  struct GNUNET_RPS_Handle *h = (struct GNUNET_RPS_Handle *) cls;
   struct GNUNET_RPS_CS_ReplyMessage *msg;
   struct GNUNET_PeerIdentity *peers;
   struct GNUNET_RPS_Request_Handle *rh;
+  uint32_t id;
 
   /* Give the peers back */
   msg = (struct GNUNET_RPS_CS_ReplyMessage *) message;
+  id = ntohl (msg->id);
 
   LOG (GNUNET_ERROR_TYPE_DEBUG,
        "Service replied with %" PRIu32 " peers for id %" PRIu32 "\n",
        ntohl (msg->num_peers),
-       ntohl (msg->id));
+       id);
 
   peers = (struct GNUNET_PeerIdentity *) &msg[1];
-  rh = &req_handlers[ntohl (msg->id)];
+  GNUNET_assert (GNUNET_YES ==
+      GNUNET_CONTAINER_multihashmap32_contains (h->req_handlers, id));
+  rh = GNUNET_CONTAINER_multihashmap32_get (h->req_handlers, id);
+  GNUNET_assert (NULL != rh);
+  GNUNET_CONTAINER_multihashmap32_remove_all (h->req_handlers, id);
   rh->ready_cb((rh)->ready_cb_cls, ntohl (msg->num_peers), peers);
 }
 
+/**
+ * Reconnect to the service
+ */
+static void
+reconnect (struct GNUNET_RPS_Handle *h);
+
 
 /**
  * Error handler for mq.
@@ -157,13 +169,37 @@ handle_reply (void *cls,
   static void
 mq_error_handler (void *cls, enum GNUNET_MQ_Error error)
 {
+  struct GNUNET_RPS_Handle *h = cls;
   //TODO LOG
-  LOG (GNUNET_ERROR_TYPE_WARNING, "Some problem with the message queue. error: %i\n\
+  LOG (GNUNET_ERROR_TYPE_WARNING, "Problem with message queue. error: %i\n\
        1: READ,\n\
        2: WRITE,\n\
        4: TIMEOUT\n",
        error);
+  reconnect (h);
+}
+
+/**
+ * Reconnect to the service
+ */
+static void
+reconnect (struct GNUNET_RPS_Handle *h)
+{
+  static const struct GNUNET_MQ_MessageHandler mq_handlers[] = {
+    {&handle_reply, GNUNET_MESSAGE_TYPE_RPS_CS_REPLY, 0},
+    GNUNET_MQ_HANDLERS_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, // TODO implement
+                                                h);
 }
 
 /**
@@ -177,21 +213,11 @@ GNUNET_RPS_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
 {
   struct GNUNET_RPS_Handle *h;
   //struct GNUNET_RPS_Request_Handle *rh;
-  static const struct GNUNET_MQ_MessageHandler mq_handlers[] = {
-    {&handle_reply, GNUNET_MESSAGE_TYPE_RPS_CS_REPLY, 0},
-    GNUNET_MQ_HANDLERS_END
-  };
 
   h = GNUNET_new(struct GNUNET_RPS_Handle);
-  //h->cfg = GNUNET_new(struct GNUNET_CONFIGURATION_Handle);
-  //*h->cfg = *cfg;
-  h->cfg = cfg; // FIXME |^
-  h->conn = GNUNET_CLIENT_connect("rps", cfg);
-  h->mq = GNUNET_MQ_queue_for_connection_client(h->conn,
-                                                mq_handlers,
-                                                mq_error_handler, // TODO implement
-                                                h);
-
+  h->cfg = GNUNET_CONFIGURATION_dup (cfg);
+  reconnect (h);
+  h->req_handlers = GNUNET_CONTAINER_multihashmap32_create (4);
   return h;
 }
 
@@ -218,7 +244,7 @@ GNUNET_RPS_request_peers (struct GNUNET_RPS_Handle *rps_handle,
   // assert func != NULL
   rh = GNUNET_new (struct GNUNET_RPS_Request_Handle);
   rh->rps_handle = rps_handle;
-  rh->id = req_handlers_size; // TODO ntoh
+  rh->id = rps_handle->current_request_id++;
   rh->ready_cb = ready_cb;
   rh->ready_cb_cls = cls;
 
@@ -227,8 +253,8 @@ GNUNET_RPS_request_peers (struct GNUNET_RPS_Handle *rps_handle,
        num_req_peers,
        rh->id);
 
-  GNUNET_array_append (req_handlers, req_handlers_size, *rh);
-  //memcpy(&req_handlers[req_handlers_size-1], rh, sizeof(struct GNUNET_RPS_Request_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);
@@ -320,7 +346,8 @@ GNUNET_RPS_seed_ids (struct GNUNET_RPS_Handle *h,
 GNUNET_RPS_act_malicious (struct GNUNET_RPS_Handle *h,
                           uint32_t type,
                           uint32_t num_peers,
-                          const struct GNUNET_PeerIdentity *peer_ids)
+                          const struct GNUNET_PeerIdentity *peer_ids,
+                          const struct GNUNET_PeerIdentity *target_peer)
 {
   size_t size_needed;
   uint32_t num_peers_max;
@@ -360,7 +387,8 @@ GNUNET_RPS_act_malicious (struct GNUNET_RPS_Handle *h,
                               GNUNET_MESSAGE_TYPE_RPS_ACT_MALICIOUS);
     msg->type = htonl (type);
     msg->num_peers = htonl (num_peers_max);
-    if (2 == type)
+    if ( (2 == type) ||
+         (3 == type) )
       msg->attacked_peer = peer_ids[num_peers];
     memcpy (&msg[1],
             tmp_peer_pointer,
@@ -380,8 +408,9 @@ GNUNET_RPS_act_malicious (struct GNUNET_RPS_Handle *h,
                             GNUNET_MESSAGE_TYPE_RPS_ACT_MALICIOUS);
   msg->type = htonl (type);
   msg->num_peers = htonl (num_peers);
-  if (2 == type)
-    msg->attacked_peer = peer_ids[num_peers];
+  if ( (2 == type) ||
+       (3 == type) )
+    msg->attacked_peer = *target_peer;
   memcpy (&msg[1], tmp_peer_pointer, num_peers * sizeof (struct GNUNET_PeerIdentity));
 
   GNUNET_MQ_send (h->mq, ev);
@@ -397,7 +426,21 @@ GNUNET_RPS_act_malicious (struct GNUNET_RPS_Handle *h,
   void
 GNUNET_RPS_request_cancel (struct GNUNET_RPS_Request_Handle *rh)
 {
-  // TODO
+  struct GNUNET_RPS_Handle *h;
+  struct GNUNET_MQ_Envelope *ev;
+  struct GNUNET_RPS_CS_RequestCancelMessage*msg;
+
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "Cancelling request with id %" PRIu32 "\n",
+       rh->id);
+
+  h = rh->rps_handle;
+  GNUNET_assert (GNUNET_CONTAINER_multihashmap32_contains (h->req_handlers,
+        rh->id));
+  GNUNET_CONTAINER_multihashmap32_remove_all (h->req_handlers, rh->id);
+  ev = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST_CANCEL);
+  msg->id = htonl (rh->id);
+  GNUNET_MQ_send (rh->rps_handle->mq, ev);
 }
 
 
@@ -411,6 +454,13 @@ 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,
+        "Still waiting for requests\n");
+  GNUNET_CONTAINER_multihashmap32_destroy (h->req_handlers);
+  GNUNET_free (h);
 }