gnunet-svn
[Top][All Lists]
Advanced

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

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


From: gnunet
Subject: [GNUnet-SVN] r34059 - in gnunet/src: include sensor sensordashboard
Date: Sun, 27 Jul 2014 03:23:58 +0200

Author: otarabai
Date: 2014-07-27 03:23:58 +0200 (Sun, 27 Jul 2014)
New Revision: 34059

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


Modified: gnunet/src/include/gnunet_sensor_util_lib.h
===================================================================
--- gnunet/src/include/gnunet_sensor_util_lib.h 2014-07-26 14:39:53 UTC (rev 
34058)
+++ gnunet/src/include/gnunet_sensor_util_lib.h 2014-07-27 01:23:58 UTC (rev 
34059)
@@ -270,24 +270,29 @@
   struct GNUNET_MessageHeader header;
 
   /**
-   * Size of the config file carrying full sensor information.
-   * Allocated at position 0 after this struct.
+   * Size of sensor name.
+   * Name allocated at position 0 after this struct.
    */
-  uint16_t cfg_size;
+  uint16_t sensorname_size;
 
   /**
+   * Size of the sensor definition file carrying full sensor information.
+   * The file content allocated at position 1 after this struct.
+   */
+  uint16_t sensorfile_size;
+
+  /**
    * Name of the file (usually script) associated with this sensor.
-   * At the moment we only support having one file since that's all our sensors
-   * need at the moment.
-   * The file name is allocated at position 1 after this struct.
+   * At the moment we only support having one file per sensor.
+   * The file name is allocated at position 2 after this struct.
    */
   uint16_t scriptname_size;
 
   /**
    * Size of the file (usually script) associated with this sensor.
-   * The file binary is allocated at position 2 after this struct.
+   * The file content is allocated at position 3 after this struct.
    */
-  uint16_t script_size;
+  uint16_t scriptfile_size;
 
 };
 

Modified: gnunet/src/sensor/gnunet-service-sensor-update.c
===================================================================
--- gnunet/src/sensor/gnunet-service-sensor-update.c    2014-07-26 14:39:53 UTC 
(rev 34058)
+++ gnunet/src/sensor/gnunet-service-sensor-update.c    2014-07-27 01:23:58 UTC 
(rev 34059)
@@ -160,8 +160,18 @@
  */
 static int updating;
 
+/**
+ * GNUnet scheduler task that starts the update check process.
+ */
+GNUNET_SCHEDULER_TaskIdentifier update_task;
 
 /**
+ * Pointer to service reset function called when we have new sensor updates.
+ */
+void (*reset_cb)();
+
+
+/**
  * Contact update points to check for new updates
  *
  * @param cls unused
@@ -222,6 +232,11 @@
 
   up_default = NULL;
   up = up_head;
+  if (GNUNET_SCHEDULER_NO_TASK != update_task)
+  {
+    GNUNET_SCHEDULER_cancel (update_task);
+    update_task = GNUNET_SCHEDULER_NO_TASK;
+  }
   while (NULL != up)
   {
     GNUNET_CONTAINER_DLL_remove (up_head, up_tail, up);
@@ -262,15 +277,15 @@
       up->failed = GNUNET_NO;
       up = up->next;
     }
-    GNUNET_SCHEDULER_add_delayed (SENSOR_UPDATE_CHECK_INTERVAL,
-                                  &check_for_updates, NULL);
+    update_task = GNUNET_SCHEDULER_add_delayed (SENSOR_UPDATE_CHECK_INTERVAL,
+                                                &check_for_updates, NULL);
     return;
   }
   LOG (GNUNET_ERROR_TYPE_WARNING,
        "Update point `%s' failed, trying next one now.\n",
        GNUNET_i2s (&up_default->peer_id));
   up_default = up_default->next;
-  GNUNET_SCHEDULER_add_now (&check_for_updates, NULL);
+  update_task = GNUNET_SCHEDULER_add_now (&check_for_updates, NULL);
 }
 
 
@@ -368,6 +383,7 @@
   struct GNUNET_MessageHeader *msg;
   size_t msg_size;
 
+  update_task = GNUNET_SCHEDULER_NO_TASK;
   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
     return;
   if (GNUNET_YES == updating)
@@ -377,8 +393,8 @@
          "Retrying in %s.\n",
          GNUNET_STRINGS_relative_time_to_string (SENSOR_UPDATE_CHECK_RETRY,
                                                  GNUNET_NO));
-    GNUNET_SCHEDULER_add_delayed (SENSOR_UPDATE_CHECK_RETRY,
-                                  &check_for_updates, NULL);
+    update_task = GNUNET_SCHEDULER_add_delayed (SENSOR_UPDATE_CHECK_RETRY,
+                                                &check_for_updates, NULL);
     return;
   }
   updating = GNUNET_YES;
@@ -406,8 +422,8 @@
   msg->size = htons (msg_size);
   msg->type = htons (GNUNET_MESSAGE_TYPE_SENSOR_LIST_REQ);
   queue_msg (msg);
-  GNUNET_SCHEDULER_add_delayed (SENSOR_UPDATE_CHECK_INTERVAL,
-                                &check_for_updates, NULL);
+  update_task = GNUNET_SCHEDULER_add_delayed (SENSOR_UPDATE_CHECK_INTERVAL,
+                                              &check_for_updates, NULL);
 }
 
 
@@ -539,6 +555,9 @@
   }
   if (GNUNET_MESSAGE_TYPE_SENSOR_END == ntohs (message->type))
   {
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "Received end of sensor list msg. We already requested %d updates.\n",
+         up_default->expected_sensor_updates);
     up_default->expecting_sensor_list = GNUNET_NO;
     if (0 == up_default->expected_sensor_updates)
     {
@@ -575,6 +594,64 @@
 
 
 /**
+ * Update local sensor definitions with a sensor retrieved from an update 
point.
+ *
+ * @param sensorname Sensor name
+ * @param sensorfile Buffer containing the sensor definition file
+ * @param sensorfile_size Size of @e sensorfile
+ * @param scriptname Name of associated script file, NULL if no script
+ * @param scriptfile Buffer containing the script file, NULL if no script
+ * @param scriptfile_size Size of @e scriptfile, 0 if no script
+ * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
+ */
+static int
+update_sensor (char *sensorname,
+               void *sensorfile, uint16_t sensorfile_size,
+               char *scriptname,
+               void *scriptfile, uint16_t scriptfile_size)
+{
+  char *sensors_dir;
+  char *sensor_path;
+  char *script_path;
+
+  LOG (GNUNET_ERROR_TYPE_INFO,
+       "Received new sensor information:\n"
+       "Name: %s\n"
+       "Sensor file size: %d\n"
+       "Script name: %s\n"
+       "Script file size: %d.\n",
+       sensorname,
+       sensorfile_size,
+       (NULL == scriptname) ? "None" : scriptname,
+       scriptfile_size);
+  sensors_dir = GNUNET_SENSOR_get_sensor_dir ();
+  GNUNET_asprintf (&sensor_path, "%s%s", sensors_dir, sensorname);
+  GNUNET_DISK_fn_write (sensor_path, sensorfile, sensorfile_size,
+                        GNUNET_DISK_PERM_USER_READ |
+                        GNUNET_DISK_PERM_GROUP_READ |
+                        GNUNET_DISK_PERM_OTHER_READ |
+                        GNUNET_DISK_PERM_USER_WRITE);
+  if (NULL != scriptname)
+  {
+    GNUNET_asprintf (&script_path,
+                     "%s-files%s%s",
+                     sensor_path,
+                     DIR_SEPARATOR_STR,
+                     scriptname);
+    GNUNET_DISK_fn_write (script_path, scriptfile, scriptfile_size,
+                          GNUNET_DISK_PERM_USER_READ |
+                          GNUNET_DISK_PERM_GROUP_READ |
+                          GNUNET_DISK_PERM_OTHER_READ |
+                          GNUNET_DISK_PERM_USER_WRITE |
+                          GNUNET_DISK_PERM_GROUP_WRITE |
+                          GNUNET_DISK_PERM_USER_EXEC |
+                          GNUNET_DISK_PERM_GROUP_EXEC);
+  }
+  return GNUNET_OK;
+}
+
+
+/**
  * Handler of a sensor list message received from an update point.
  *
  * @param cls Closure (unused).
@@ -592,6 +669,12 @@
 {
   struct GNUNET_SENSOR_SensorFullMessage *sfm;
   uint16_t msg_size;
+  uint16_t sensorfile_size;
+  uint16_t scriptfile_size;
+  char *sensorname_ptr;
+  void *sensorfile_ptr;
+  char *scriptname_ptr;
+  void *scriptfile_ptr;
 
   /* error check */
   GNUNET_assert (*channel_ctx == up_default);
@@ -605,20 +688,32 @@
   }
   /* parse received msg */
   sfm = (struct GNUNET_SENSOR_SensorFullMessage *)message;
-  LOG (GNUNET_ERROR_TYPE_INFO,
-       "Received full sensor info:\n"
-       "File size: %d\n"
-       "Script name size: %d\n"
-       "Script size: %d.\n",
-       ntohs (sfm->cfg_size),
-       ntohs (sfm->scriptname_size),
-       ntohs (sfm->script_size));
-  //TODO: do the actual update
+  sensorname_ptr = (char *)&sfm[1];
+  sensorfile_ptr = sensorname_ptr + ntohs (sfm->sensorname_size);
+  sensorfile_size = ntohs (sfm->sensorfile_size);
+  scriptfile_size = ntohs (sfm->scriptfile_size);
+  if (scriptfile_size > 0)
+  {
+    scriptname_ptr = sensorfile_ptr + sensorfile_size;
+    scriptfile_ptr = scriptname_ptr + ntohs (sfm->scriptname_size);
+  }
+  else
+  {
+    scriptname_ptr = NULL;
+    scriptfile_ptr = NULL;
+  }
+  update_sensor ((char *)&sfm[1],
+                 sensorfile_ptr,
+                 sensorfile_size,
+                 scriptname_ptr,
+                 scriptfile_ptr,
+                 scriptfile_size);
   up_default->expected_sensor_updates --;
   if (0 == up_default->expected_sensor_updates)
   {
     updating = GNUNET_NO;
     cleanup_updatepoint (up_default);
+    reset_cb ();
   }
   else
     GNUNET_CADET_receive_done (channel);
@@ -659,11 +754,13 @@
  *
  * @param c our service configuration
  * @param sensors multihashmap of loaded sensors
+ * @param cb callback to reset service components when we have new updates
  * @return #GNUNET_OK if started successfully, #GNUNET_SYSERR otherwise
  */
 int
 SENSOR_update_start (const struct GNUNET_CONFIGURATION_Handle *c,
-                     struct GNUNET_CONTAINER_MultiHashMap *s)
+                     struct GNUNET_CONTAINER_MultiHashMap *s,
+                     void (*cb)())
 {
   static struct GNUNET_CADET_MessageHandler cadet_handlers[] = {
       {&handle_sensor_brief, GNUNET_MESSAGE_TYPE_SENSOR_BRIEF, 0},
@@ -675,6 +772,7 @@
   GNUNET_assert(NULL != s);
   cfg = c;
   sensors = s;
+  reset_cb = cb;
   cadet = GNUNET_CADET_connect(cfg,
                                NULL,
                                NULL,
@@ -696,7 +794,8 @@
   }
   up_default = up_head;
   updating = GNUNET_NO;
-  GNUNET_SCHEDULER_add_now (&check_for_updates, NULL);
+  update_task = GNUNET_SCHEDULER_add_delayed (SENSOR_UPDATE_CHECK_INTERVAL,
+                                              &check_for_updates, NULL);
   LOG (GNUNET_ERROR_TYPE_DEBUG,
        "Sensor update module started.\n");
   return GNUNET_OK;

Modified: gnunet/src/sensor/gnunet-service-sensor.c
===================================================================
--- gnunet/src/sensor/gnunet-service-sensor.c   2014-07-26 14:39:53 UTC (rev 
34058)
+++ gnunet/src/sensor/gnunet-service-sensor.c   2014-07-27 01:23:58 UTC (rev 
34059)
@@ -60,7 +60,16 @@
  */
 struct GNUNET_PeerIdentity peerid;
 
+
 /**
+ * Resets the service by stopping components, reloading sensors and starting
+ * components. This is needed when we receive new sensor updates.
+ */
+static void
+reset ();
+
+
+/**
  * Change the state of the sensor.
  * Write the change to file to make it persistent.
  *
@@ -82,7 +91,21 @@
   GNUNET_CONFIGURATION_write (sensor->cfg, sensor->def_file);
 }
 
+
 /**
+ * Stops components and destroys sensors
+ */
+static void
+stop ()
+{
+  SENSOR_update_stop ();
+  SENSOR_reporting_stop ();
+  SENSOR_analysis_stop ();
+  GNUNET_SENSOR_destroy_sensors (sensors);
+}
+
+
+/**
  * Task run during shutdown.
  *
  * @param cls unused
@@ -92,10 +115,7 @@
 shutdown_task (void *cls,
                const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
-  SENSOR_update_stop ();
-  SENSOR_reporting_stop ();
-  SENSOR_analysis_stop ();
-  GNUNET_SENSOR_destroy_sensors (sensors);
+  stop ();
   if (NULL != statistics)
   {
     GNUNET_STATISTICS_destroy (statistics, GNUNET_YES);
@@ -575,7 +595,22 @@
   GNUNET_CONTAINER_multihashmap_iterate(sensors, &schedule_sensor, NULL);
 }
 
+
 /**
+ * Loads sensors and starts different service components
+ */
+static void
+start ()
+{
+  sensors = GNUNET_SENSOR_load_all_sensors ();
+  schedule_all_sensors();
+  SENSOR_analysis_start(cfg, sensors);
+  SENSOR_reporting_start(cfg, sensors);
+  SENSOR_update_start (cfg, sensors, &reset);
+}
+
+
+/**
  * Process statistics requests.
  *
  * @param cls closure
@@ -596,11 +631,6 @@
   };
 
   cfg = c;
-  sensors = GNUNET_SENSOR_load_all_sensors ();
-  schedule_all_sensors();
-  SENSOR_analysis_start(c, sensors);
-  SENSOR_reporting_start(c, sensors);
-  SENSOR_update_start (c, sensors);
   statistics = GNUNET_STATISTICS_create("sensor", cfg);
   GNUNET_CRYPTO_get_peer_identity(cfg, &peerid);
   peerstore = GNUNET_PEERSTORE_connect(cfg);
@@ -611,10 +641,23 @@
   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
         &shutdown_task,
         NULL);
+  start ();
 }
 
 
 /**
+ * Resets the service by stopping components, reloading sensors and starting
+ * components. This is needed when we receive new sensor updates.
+ */
+static void
+reset ()
+{
+  stop ();
+  start ();
+}
+
+
+/**
  * The main function for the sensor service.
  *
  * @param argc number of arguments from the command line

Modified: gnunet/src/sensor/sensor.h
===================================================================
--- gnunet/src/sensor/sensor.h  2014-07-26 14:39:53 UTC (rev 34058)
+++ gnunet/src/sensor/sensor.h  2014-07-27 01:23:58 UTC (rev 34059)
@@ -106,9 +106,11 @@
  *
  * @param c our service configuration
  * @param sensors multihashmap of loaded sensors
+ * @param reset_cb callback to reset service components when we have new 
updates
  * @return #GNUNET_OK if started successfully, #GNUNET_SYSERR otherwise
  */
 int
 SENSOR_update_start (const struct GNUNET_CONFIGURATION_Handle *c,
-                     struct GNUNET_CONTAINER_MultiHashMap *sensors);
+                     struct GNUNET_CONTAINER_MultiHashMap *s,
+                     void (*reset_cb)());
 

Modified: gnunet/src/sensordashboard/gnunet-service-sensordashboard.c
===================================================================
--- gnunet/src/sensordashboard/gnunet-service-sensordashboard.c 2014-07-26 
14:39:53 UTC (rev 34058)
+++ gnunet/src/sensordashboard/gnunet-service-sensordashboard.c 2014-07-27 
01:23:58 UTC (rev 34059)
@@ -597,6 +597,7 @@
   char *sensor_dir;
   char *sensor_path;
   char *sensorscript_path;
+  uint64_t sensorname_size;
   uint64_t sensorfile_size;
   uint64_t sensorscriptname_size;
   uint64_t sensorscript_size;
@@ -619,6 +620,7 @@
     GNUNET_free (sensor_path);
     return NULL;
   }
+  sensorname_size = strlen (sensorname) + 1;
   sensorscript_size = 0;
   sensorscriptname_size = 0;
   /* Test if there is an associated script */
@@ -633,18 +635,26 @@
                                             &sensorscript_size,
                                             GNUNET_NO,
                                             GNUNET_YES))
+    {
       sensorscriptname_size = strlen (sensor->ext_process) + 1;
+    }
   }
   /* Construct the msg */
   total_size = sizeof (struct GNUNET_SENSOR_SensorFullMessage) +
-               sensorfile_size + sensorscriptname_size + sensorscript_size;
+               sensorname_size +
+               sensorfile_size +
+               sensorscriptname_size +
+               sensorscript_size;
   msg = GNUNET_malloc (total_size);
   msg->header.size = htons (total_size);
   msg->header.type = htons (GNUNET_MESSAGE_TYPE_SENSOR_FULL);
-  msg->cfg_size = htons (sensorfile_size);
+  msg->sensorname_size = htons (sensorname_size);
+  msg->sensorfile_size = htons (sensorfile_size);
   msg->scriptname_size = htons (sensorscriptname_size);
-  msg->script_size = htons (sensorscript_size);
+  msg->scriptfile_size = htons (sensorscript_size);
   dummy = &msg[1];
+  memcpy (dummy, sensorname, sensorname_size);
+  dummy += sensorname_size;
   GNUNET_DISK_fn_read (sensor_path, dummy, sensorfile_size);
   dummy += sensorfile_size;
   if (sensorscriptname_size > 0)
@@ -652,10 +662,10 @@
     memcpy (dummy, sensor->ext_process, sensorscriptname_size);
     dummy += sensorscriptname_size;
     GNUNET_DISK_fn_read (sensorscript_path, dummy, sensorscript_size);
+    GNUNET_free (sensorscript_path);
   }
   GNUNET_free (sensor_dir);
   GNUNET_free (sensor_path);
-  GNUNET_free (sensorscript_path);
   return msg;
 }
 




reply via email to

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