sensor: fixes for proof-of-work, test passes now
[oweals/gnunet.git] / src / sensor / gnunet-service-sensor.c
index 047ad91837150794003144912bf515a84e0f81ae..e8b01ae33d5bfada5ec76fa2621c2a0eaa64dee9 100644 (file)
@@ -42,6 +42,26 @@ static char *sensor_dir;
  */
 static struct GNUNET_CONTAINER_MultiHashMap *sensors;
 
+/**
+ * Start the monitoring module ?
+ */
+static int start_monitoring;
+
+/**
+ * Start the analysis module ?
+ */
+static int start_analysis;
+
+/**
+ * Start the reporting module ?
+ */
+static int start_reporting;
+
+/**
+ * Start the update module ?
+ */
+static int start_update;
+
 
 /**
  * Resets the service by stopping components, reloading sensors and starting
@@ -57,10 +77,14 @@ reset ();
 static void
 stop ()
 {
-  SENSOR_update_stop ();
-  SENSOR_analysis_stop ();
-  SENSOR_reporting_stop ();
-  SENSOR_monitoring_stop ();
+  if (GNUNET_YES == start_update)
+    SENSOR_update_stop ();
+  if (GNUNET_YES == start_analysis)
+    SENSOR_analysis_stop ();
+  if (GNUNET_YES == start_reporting)
+    SENSOR_reporting_stop ();
+  if (GNUNET_YES == start_monitoring)
+    SENSOR_monitoring_stop ();
   GNUNET_SENSOR_destroy_sensors (sensors);
 }
 
@@ -84,6 +108,36 @@ shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 }
 
 
+/**
+ * Handle a force anomaly request from client.
+ *
+ * @param cls closure
+ * @param client identification of the client
+ * @param message the actual message
+ */
+static void
+handle_anomaly_force (void *cls, struct GNUNET_SERVER_Client *client,
+                      const struct GNUNET_MessageHeader *message)
+{
+  struct ForceAnomalyMessage *anomaly_msg;
+  struct GNUNET_SENSOR_SensorInfo *sensor;
+
+  anomaly_msg = (struct ForceAnomalyMessage *) message;
+  sensor =
+      GNUNET_CONTAINER_multihashmap_get (sensors,
+                                         &anomaly_msg->sensor_name_hash);
+  if (NULL == sensor)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+                "Force anomaly message received for a sensor we don't have.\n");
+    GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+    return;
+  }
+  SENSOR_reporting_anomaly_update (sensor, ntohs (anomaly_msg->anomalous));
+  GNUNET_SERVER_receive_done (client, GNUNET_YES);
+}
+
+
 /**
  * Creates a structure with basic sensor info to be sent to a client.
  *
@@ -230,10 +284,14 @@ static void
 start ()
 {
   sensors = GNUNET_SENSOR_load_all_sensors (sensor_dir);
-  SENSOR_monitoring_start (cfg, sensors);
-  SENSOR_reporting_start (cfg, sensors);
-  SENSOR_analysis_start (cfg, sensors);
-  SENSOR_update_start (cfg, sensors, &reset);
+  if (GNUNET_YES == start_monitoring)
+    SENSOR_monitoring_start (cfg, sensors);
+  if (GNUNET_YES == start_reporting)
+    SENSOR_reporting_start (cfg, sensors);
+  if (GNUNET_YES == start_analysis)
+    SENSOR_analysis_start (cfg, sensors);
+  if (GNUNET_YES == start_update)
+    SENSOR_update_start (cfg, sensors, &reset);
 }
 
 
@@ -253,16 +311,43 @@ run (void *cls, struct GNUNET_SERVER_Handle *server,
      0},
     {&handle_get_all_sensors, NULL, GNUNET_MESSAGE_TYPE_SENSOR_GETALL,
      sizeof (struct GNUNET_MessageHeader)},
+    {
+     &handle_anomaly_force, NULL, GNUNET_MESSAGE_TYPE_SENSOR_ANOMALY_FORCE,
+     sizeof (struct ForceAnomalyMessage)},
     {NULL, NULL, 0, 0}
   };
 
   cfg = c;
   if (GNUNET_OK !=
-      GNUNET_CONFIGURATION_get_value_filename (cfg, "SENSOR", "SENSOR_DIR",
+      GNUNET_CONFIGURATION_get_value_filename (cfg, "sensor", "SENSOR_DIR",
                                                &sensor_dir))
   {
     sensor_dir = GNUNET_SENSOR_get_default_sensor_dir ();
   }
+  start_monitoring = GNUNET_YES;
+  start_analysis = GNUNET_YES;
+  start_reporting = GNUNET_YES;
+  start_update = GNUNET_YES;
+  if (GNUNET_NO ==
+      GNUNET_CONFIGURATION_get_value_yesno (cfg, "sensor", "START_MONITORING"))
+  {
+    start_monitoring = GNUNET_NO;
+  }
+  if (GNUNET_NO ==
+      GNUNET_CONFIGURATION_get_value_yesno (cfg, "sensor", "START_REPORTING"))
+  {
+    start_reporting = GNUNET_NO;
+  }
+  if (GNUNET_NO ==
+      GNUNET_CONFIGURATION_get_value_yesno (cfg, "sensor", "START_ANALYSIS"))
+  {
+    start_analysis = GNUNET_NO;
+  }
+  if (GNUNET_NO ==
+      GNUNET_CONFIGURATION_get_value_yesno (cfg, "sensor", "START_UPDATE"))
+  {
+    start_update = GNUNET_NO;
+  }
   GNUNET_SERVER_add_handlers (server, handlers);
   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
                                 NULL);