gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] r34033 - in gnunet/src: include sensor sensordashboard


From: gnunet
Subject: [GNUnet-SVN] r34033 - in gnunet/src: include sensor sensordashboard
Date: Thu, 24 Jul 2014 13:55:37 +0200

Author: otarabai
Date: 2014-07-24 13:55:36 +0200 (Thu, 24 Jul 2014)
New Revision: 34033

Modified:
   gnunet/src/include/gnunet_protocols.h
   gnunet/src/sensor/gnunet-service-sensor-update.c
   gnunet/src/sensordashboard/gnunet-service-sensordashboard.c
Log:
sensor: towards update functionality


Modified: gnunet/src/include/gnunet_protocols.h
===================================================================
--- gnunet/src/include/gnunet_protocols.h       2014-07-24 10:01:56 UTC (rev 
34032)
+++ gnunet/src/include/gnunet_protocols.h       2014-07-24 11:55:36 UTC (rev 
34033)
@@ -2428,7 +2428,17 @@
  */
 #define GNUNET_MESSAGE_TYPE_SENSOR_READING 804
 
+/**
+ * Request for sensor list from update point
+ */
+#define GNUNET_MESSAGE_TYPE_SENSOR_LIST_REQ 805
 
+/**
+ * Sensor list sent from update point to requesting peer
+ */
+#define GNUNET_MESSAGE_TYPE_SENSOR_LIST 806
+
+
 
/*******************************************************************************
  * PEERSTORE message types
  
******************************************************************************/

Modified: gnunet/src/sensor/gnunet-service-sensor-update.c
===================================================================
--- gnunet/src/sensor/gnunet-service-sensor-update.c    2014-07-24 10:01:56 UTC 
(rev 34032)
+++ gnunet/src/sensor/gnunet-service-sensor-update.c    2014-07-24 11:55:36 UTC 
(rev 34033)
@@ -74,6 +74,11 @@
    */
   struct GNUNET_CADET_TransmitHandle *sensor_list_req_th;
 
+  /**
+   * Are we waiting for a sensor list?
+   */
+  int expecting_sensor_list;
+
 };
 
 
@@ -132,6 +137,7 @@
 {
   struct UpdatePoint *up;
 
+  up_default = NULL;
   up = up_head;
   while (NULL != up)
   {
@@ -178,10 +184,22 @@
 static size_t
 do_send_sensor_list_req (void *cls, size_t size, void *buf)
 {
+  struct GNUNET_MessageHeader *msg;
+  size_t msg_size;
+
   up_default->sensor_list_req_th = NULL;
-  //TODO
-
-  return 0; //FIXME
+  if (NULL == buf)
+  {
+    fail ();
+    return 0;
+  }
+  msg = GNUNET_new (struct GNUNET_MessageHeader);
+  msg_size = sizeof (struct GNUNET_MessageHeader);
+  msg->size = htons (msg_size);
+  msg->type = htons (GNUNET_MESSAGE_TYPE_SENSOR_LIST_REQ);
+  memcpy (buf, msg, msg_size);
+  up_default->expecting_sensor_list = GNUNET_YES;
+  return msg_size;
 }
 
 
@@ -221,7 +239,6 @@
                                           GNUNET_TIME_UNIT_FOREVER_REL,
                                           sizeof (struct GNUNET_MessageHeader),
                                           &do_send_sensor_list_req, NULL);
-  //TODO
 }
 
 
@@ -278,6 +295,7 @@
     up->peer_id.public_key = public_key;
     up->ch = NULL;
     up->sensor_list_req_th = NULL;
+    up->expecting_sensor_list = GNUNET_NO;
     GNUNET_CONTAINER_DLL_insert (up_head, up_tail, up);
     LOG (GNUNET_ERROR_TYPE_DEBUG,
          "Loaded update point `%s'.\n",
@@ -288,6 +306,30 @@
 
 
 /**
+ * Handler of a sensor list message received from an update point.
+ *
+ * @param cls Closure (unused).
+ * @param channel Connection to the other end.
+ * @param channel_ctx Place to store local state associated with the channel.
+ * @param message The actual message.
+ * @return #GNUNET_OK to keep the channel open,
+ *         #GNUNET_SYSERR to close it (signal serious error).
+ */
+static int
+handle_sensor_list (void *cls,
+                    struct GNUNET_CADET_Channel *channel,
+                    void **channel_ctx,
+                    const struct GNUNET_MessageHeader *message)
+{
+  GNUNET_assert (*channel_ctx == up_default);
+  GNUNET_assert (GNUNET_YES == up_default->expecting_sensor_list);
+  up_default->expecting_sensor_list = GNUNET_NO;
+  //TODO
+  return GNUNET_OK;
+}
+
+
+/**
  * Start the sensor update module
  *
  * @param c our service configuration
@@ -299,6 +341,7 @@
                      struct GNUNET_CONTAINER_MultiHashMap *sensors)
 {
   static struct GNUNET_CADET_MessageHandler cadet_handlers[] = {
+      {&handle_sensor_list, GNUNET_MESSAGE_TYPE_SENSOR_LIST, 0},
       {NULL, 0, 0}
   };
 

Modified: gnunet/src/sensordashboard/gnunet-service-sensordashboard.c
===================================================================
--- gnunet/src/sensordashboard/gnunet-service-sensordashboard.c 2014-07-24 
10:01:56 UTC (rev 34032)
+++ gnunet/src/sensordashboard/gnunet-service-sensordashboard.c 2014-07-24 
11:55:36 UTC (rev 34033)
@@ -32,7 +32,37 @@
 #include "gnunet_sensor_util_lib.h"
 #include "gnunet_peerstore_service.h"
 
+
 /**
+ * Context of a connected client peer
+ */
+struct ClientPeerContext
+{
+
+  /**
+   * DLL
+   */
+  struct ClientPeerContext *prev;
+
+  /*
+   * DLL
+   */
+  struct ClientPeerContext *next;
+
+  /**
+   * GNUnet Peer identity
+   */
+  struct GNUNET_PeerIdentity peerid;
+
+  /**
+   * Handle to the cadet channel
+   */
+  struct GNUNET_CADET_Channel *ch;
+
+};
+
+
+/**
  * Handle to CADET service
  */
 static struct GNUNET_CADET_Handle *cadet;
@@ -53,6 +83,33 @@
 static char *subsystem = "sensordashboard";
 
 /**
+ * Head of a DLL of all connected client peers
+ */
+static struct ClientPeerContext *cp_head;
+
+/**
+ * Tail of a DLL of all connected client peers
+ */
+static struct ClientPeerContext *cp_tail;
+
+
+/**
+ * Destroy a given client peer context
+ *
+ * @param cp client peer context
+ */
+static void
+destroy_clientpeer (struct ClientPeerContext *cp)
+{
+  if (NULL != cp->ch)
+  {
+    GNUNET_CADET_channel_destroy (cp->ch);
+    cp->ch = NULL;
+  }
+  GNUNET_free (cp);
+}
+
+/**
  * Task run during shutdown.
  *
  * @param cls unused
@@ -61,9 +118,18 @@
 static void
 cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
+  struct ClientPeerContext *cp;
+
+  cp = cp_head;
+  while (NULL != cp)
+  {
+    GNUNET_CONTAINER_DLL_remove (cp_head, cp_tail, cp);
+    destroy_clientpeer (cp);
+    cp = cp_head;
+  }
   if (NULL != cadet)
   {
-    GNUNET_CADET_disconnect(cadet);
+    GNUNET_CADET_disconnect (cadet);
     cadet = NULL;
   }
   if (NULL != peerstore)
@@ -86,13 +152,16 @@
  * @param channel_ctx place where local state associated
  *                   with the channel is stored
  */
-static void cadet_channel_destroyed (void *cls,
-    const struct GNUNET_CADET_Channel *channel,
-    void *channel_ctx)
+static void
+cadet_channel_destroyed (void *cls,
+                         const struct GNUNET_CADET_Channel *channel,
+                         void *channel_ctx)
 {
-  struct GNUNET_PeerIdentity *peer = channel_ctx;
+  struct ClientPeerContext *cp = channel_ctx;
 
-  GNUNET_free (peer);
+  cp->ch = NULL;
+  GNUNET_CONTAINER_DLL_remove (cp_head, cp_tail, cp);
+  destroy_clientpeer (cp);
 }
 
 /**
@@ -113,16 +182,19 @@
  * @return initial channel context for the channel
  *         (can be NULL -- that's not an error)
  */
-static void *cadet_channel_created (void *cls,
-    struct GNUNET_CADET_Channel *channel,
-    const struct GNUNET_PeerIdentity *initiator,
-    uint32_t port, enum GNUNET_CADET_ChannelOption options)
+static void *
+cadet_channel_created (void *cls,
+                       struct GNUNET_CADET_Channel *channel,
+                       const struct GNUNET_PeerIdentity *initiator,
+                       uint32_t port, enum GNUNET_CADET_ChannelOption options)
 {
-  struct GNUNET_PeerIdentity *peer;
+  struct ClientPeerContext *cp;
 
-  peer = GNUNET_new (struct GNUNET_PeerIdentity);
-  memcpy (peer, initiator, sizeof (struct GNUNET_PeerIdentity));
-  return peer;
+  cp = GNUNET_new (struct ClientPeerContext);
+  cp->peerid = *initiator;
+  cp->ch = channel;
+  GNUNET_CONTAINER_DLL_insert (cp_head, cp_tail, cp);
+  return cp;
 }
 
 /**
@@ -139,8 +211,11 @@
  * @return #GNUNET_OK to keep the channel open,
  *         #GNUNET_SYSERR to close it (signal serious error).
  */
-int sensor_reading_receiver (void *cls, struct GNUNET_CADET_Channel *channel,
-    void **channel_ctx, const struct GNUNET_MessageHeader *message)
+static int
+handle_sensor_reading (void *cls,
+                       struct GNUNET_CADET_Channel *channel,
+                       void **channel_ctx,
+                       const struct GNUNET_MessageHeader *message)
 {
   struct GNUNET_PeerIdentity *peer = *channel_ctx;
   struct GNUNET_SENSOR_Reading *reading;
@@ -149,28 +224,57 @@
   if (NULL == reading)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-        "Received an invalid sensor reading from peer `%s'\n",
-        GNUNET_i2s (peer));
+                "Received an invalid sensor reading from peer `%s'\n",
+                GNUNET_i2s (peer));
     return GNUNET_SYSERR;
   }
   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-      "Received a sensor reading from peer `%s':\n"
-      "# Sensor name: `%s'\n"
-      "# Timestamp: %" PRIu64 "\n"
-      "# Value size: %" PRIu64 ".\n",
-      GNUNET_i2s (peer),
-      reading->sensor->name,
-      reading->timestamp,
-      reading->value_size);
+              "Received a sensor reading from peer `%s':\n"
+              "# Sensor name: `%s'\n"
+              "# Timestamp: %" PRIu64 "\n"
+              "# Value size: %" PRIu64 ".\n",
+              GNUNET_i2s (peer),
+              reading->sensor->name,
+              reading->timestamp,
+              reading->value_size);
   GNUNET_PEERSTORE_store (peerstore, subsystem, peer, reading->sensor->name,
-      reading->value, reading->value_size, GNUNET_TIME_UNIT_FOREVER_ABS,
-      GNUNET_PEERSTORE_STOREOPTION_MULTIPLE, NULL, NULL);
+                          reading->value, reading->value_size,
+                          GNUNET_TIME_UNIT_FOREVER_ABS,
+                          GNUNET_PEERSTORE_STOREOPTION_MULTIPLE, NULL, NULL);
   GNUNET_free (reading->value);
   GNUNET_free (reading);
+  GNUNET_CADET_receive_done (channel);
   return GNUNET_OK;
 }
 
+
 /**
+ * Called with any sensor list request received.
+ *
+ * Each time the function must call #GNUNET_CADET_receive_done on the channel
+ * in order to receive the next message. This doesn't need to be immediate:
+ * can be delayed if some processing is done on the message.
+ *
+ * @param cls Closure (set from #GNUNET_CADET_connect).
+ * @param channel Connection to the other end.
+ * @param channel_ctx Place to store local state associated with the channel.
+ * @param message The actual message.
+ * @return #GNUNET_OK to keep the channel open,
+ *         #GNUNET_SYSERR to close it (signal serious error).
+ */
+static int
+handle_sensor_list_req (void *cls,
+                        struct GNUNET_CADET_Channel *channel,
+                        void **channel_ctx,
+                        const struct GNUNET_MessageHeader *message)
+{
+  //TODO
+  GNUNET_CADET_receive_done (channel);
+  return GNUNET_OK;
+}
+
+
+/**
  * Process sensordashboard requests.
  *
  * @param cls closure
@@ -185,25 +289,30 @@
     {NULL, NULL, 0, 0}
   };
   static struct GNUNET_CADET_MessageHandler cadet_handlers[] = {
-      {&sensor_reading_receiver, GNUNET_MESSAGE_TYPE_SENSOR_READING, 0},
+      {&handle_sensor_reading,
+       GNUNET_MESSAGE_TYPE_SENSOR_READING, 0},
+      {&handle_sensor_list_req,
+       GNUNET_MESSAGE_TYPE_SENSOR_LIST_REQ,
+       sizeof (struct GNUNET_MessageHeader)},
       {NULL, 0, 0}
   };
   static uint32_t cadet_ports[] = {
       GNUNET_APPLICATION_TYPE_SENSORDASHBOARD,
+      GNUNET_APPLICATION_TYPE_SENSORUPDATE,
       GNUNET_APPLICATION_TYPE_END
   };
   sensors = GNUNET_SENSOR_load_all_sensors ();
   GNUNET_assert (NULL != sensors);
   cadet = GNUNET_CADET_connect(cfg,
-      NULL,
-      &cadet_channel_created,
-      &cadet_channel_destroyed,
-      cadet_handlers,
-      cadet_ports);
+                               NULL,
+                               &cadet_channel_created,
+                               &cadet_channel_destroyed,
+                               cadet_handlers,
+                               cadet_ports);
   if(NULL == cadet)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-        _("Failed to connect to `%s' service.\n"), "CADET");
+                _("Failed to connect to `%s' service.\n"), "CADET");
     GNUNET_SCHEDULER_add_now (&cleanup_task, NULL);
     return;
   }
@@ -211,7 +320,7 @@
   if (NULL == peerstore)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-        _("Failed to connect to `%s' service.\n"), "PEERSTORE");
+                _("Failed to connect to `%s' service.\n"), "PEERSTORE");
     GNUNET_SCHEDULER_add_now (&cleanup_task, NULL);
     return;
   }




reply via email to

[Prev in Thread] Current Thread [Next in Thread]