plugin datastore mysql
[oweals/gnunet.git] / src / rps / rps_api.c
index e06f73b9ca7c06960c8e5a324e384c69c8d90b27..16a0c6c5414e92d42a7b574bedf971c7877ece3e 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     Copyright (C) 
+     Copyright (C)
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
@@ -40,17 +40,22 @@ struct GNUNET_RPS_Handle
   /**
    * The handle to the client configuration.
    */
-  struct GNUNET_CONFIGURATION_Handle *cfg;
+  const struct GNUNET_CONFIGURATION_Handle *cfg;
 
   /**
-   * The connection to the client.
+   * The message queue to the client.
    */
-  struct GNUNET_CLIENT_Connection *conn;
+  struct GNUNET_MQ_Handle *mq;
 
   /**
-   * The message queue to the client.
+   * Array of Request_Handles.
    */
-  struct GNUNET_MQ_Handle *mq;
+  struct GNUNET_CONTAINER_MultiHashMap32 *req_handlers;
+
+  /**
+   * The id of the last request.
+   */
+  uint32_t current_request_id;
 };
 
 
@@ -81,17 +86,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.
@@ -115,35 +109,69 @@ struct cb_cls_pack
 };
 
 
+
+/**
+ * This function is called, when the service replies to our request.
+ * 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_reply (void *cls,
+             const struct GNUNET_RPS_CS_ReplyMessage *msg)
+{
+  uint16_t msize = ntohs (msg->header.size);
+  uint32_t num_peers = ntohl (msg->num_peers);
+
+  msize -= sizeof (struct GNUNET_RPS_CS_ReplyMessage);
+  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 replies to our request.
  * It calls the callback the caller gave us with the provided closure
  * and disconnects afterwards.
  *
  * @param cls the closure
- * @param message the message
+ * @param msg the message
  */
-  static void
+static void
 handle_reply (void *cls,
-              const struct GNUNET_MessageHeader *message)
+              const struct GNUNET_RPS_CS_ReplyMessage *msg)
 {
-  struct GNUNET_RPS_CS_ReplyMessage *msg;
+  struct GNUNET_RPS_Handle *h = cls;
   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)];
-  rh->ready_cb((rh)->ready_cb_cls, ntohl (msg->num_peers), peers);
+  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
  */
@@ -160,8 +188,9 @@ reconnect (struct GNUNET_RPS_Handle *h);
  * @param cls the closure
  * @param error error code without specyfied meaning
  */
-  static void
-mq_error_handler (void *cls, enum GNUNET_MQ_Error error)
+static void
+mq_error_handler (void *cls,
+                  enum GNUNET_MQ_Error error)
 {
   struct GNUNET_RPS_Handle *h = cls;
   //TODO LOG
@@ -173,44 +202,53 @@ mq_error_handler (void *cls, enum GNUNET_MQ_Error 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
+  struct GNUNET_CLIENT_Connection *conn;
+
+  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_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, // TODO implement
-                                                h);
+  h->mq = GNUNET_CLIENT_connecT (h->cfg,
+                                 "rps",
+                                 mq_handlers,
+                                 &mq_error_handler,
+                                 h);
 }
 
+
 /**
  * Connect to the rps service
  *
  * @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->cfg = cfg;
   reconnect (h);
+  if (NULL == h->mq)
+  {
+    GNUNET_free (h);
+    return NULL;
+  }
+  h->req_handlers = GNUNET_CONTAINER_multihashmap32_create (4);
   return h;
 }
 
@@ -224,7 +262,7 @@ 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,
@@ -237,7 +275,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;
 
@@ -246,8 +284,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);
@@ -264,7 +302,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)
@@ -335,7 +373,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,
@@ -416,9 +454,10 @@ 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;
   struct GNUNET_MQ_Envelope *ev;
   struct GNUNET_RPS_CS_RequestCancelMessage*msg;
 
@@ -426,6 +465,10 @@ GNUNET_RPS_request_cancel (struct GNUNET_RPS_Request_Handle *rh)
        "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);
@@ -437,13 +480,14 @@ 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,
+        "Still waiting for requests\n");
+  GNUNET_CONTAINER_multihashmap32_destroy (h->req_handlers);
   GNUNET_free (h);
 }