initial sensor reporting component code
[oweals/gnunet.git] / src / sensor / gnunet-service-sensor-reporting.c
1 /*
2      This file is part of GNUnet.
3      (C)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file sensor/gnunet-service-sensor-reporting.c
23  * @brief sensor service reporting functionality
24  * @author Omar Tarabai
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "sensor.h"
29 #include "gnunet_peerstore_service.h"
30
31 #define LOG(kind,...) GNUNET_log_from (kind, "sensor-reporting",__VA_ARGS__)
32
33 /**
34  * Context of reporting to collection
35  * point
36  */
37 struct CollectionReportingContext
38 {
39
40   /**
41    * Sensor information
42    */
43   struct SensorInfo *sensor;
44
45   /**
46    * Reporting task (OR GNUNET_SCHEDULER_NO_TASK)
47    */
48   GNUNET_SCHEDULER_TaskIdentifier task;
49
50 };
51
52 /**
53  * Our configuration.
54  */
55 static const struct GNUNET_CONFIGURATION_Handle *cfg;
56
57 /**
58  * Handle to peerstore service
59  */
60 static struct GNUNET_PEERSTORE_Handle *peerstore;
61
62 /**
63  * My peer id
64  */
65 struct GNUNET_PeerIdentity peerid;
66
67
68 /**
69  * Stop sensor reporting module
70  */
71 void SENSOR_reporting_stop()
72 {
73   LOG (GNUNET_ERROR_TYPE_DEBUG, "Stopping sensor reporting module.\n");
74   if (NULL != peerstore)
75   {
76     GNUNET_PEERSTORE_disconnect(peerstore);
77     peerstore = NULL;
78   }
79 }
80
81 /**
82  * Task scheduled to send values to collection point
83  *
84  * @param cls closure, a 'struct CollectionReportingContext *'
85  * @param tc unused
86  */
87 void report_collection_point
88 (void *cls, const struct GNUNET_SCHEDULER_TaskContext* tc)
89 {
90   struct CollectionReportingContext *crc = cls;
91
92   crc->task = GNUNET_SCHEDULER_NO_TASK;
93 }
94
95 /**
96  * Iterator for defined sensors
97  * Watches sensors for readings to report
98  *
99  * @param cls unused
100  * @param key unused
101  * @param value a 'struct SensorInfo *' with sensor information
102  * @return #GNUNET_YES to continue iterations
103  */
104 static int
105 init_sensor_reporting (void *cls,
106     const struct GNUNET_HashCode *key,
107     void *value)
108 {
109   struct SensorInfo *sensor = value;
110   struct CollectionReportingContext *crc;
111
112   if (NULL != sensor->collection_point)
113   {
114     LOG (GNUNET_ERROR_TYPE_INFO,
115         "Will start reporting sensor `%s' values to collection point `%s' every %s.\n",
116         sensor->name, GNUNET_i2s_full(sensor->collection_point),
117         GNUNET_STRINGS_relative_time_to_string(sensor->collection_interval, GNUNET_YES));
118     crc = GNUNET_new (struct CollectionReportingContext);
119     crc->sensor = sensor;
120     crc->task =
121         GNUNET_SCHEDULER_add_delayed(sensor->collection_interval,
122             &report_collection_point,
123             crc);
124   }
125   if (GNUNET_YES == sensor->p2p_report)
126   {
127     LOG (GNUNET_ERROR_TYPE_INFO,
128         "Will start reporting sensor `%s' values to p2p network every %s.\n",
129         sensor->name,
130         GNUNET_STRINGS_relative_time_to_string(sensor->p2p_interval, GNUNET_YES));
131   }
132   return GNUNET_YES;
133 }
134
135 /**
136  * Start the sensor reporting module
137  *
138  * @param c our service configuration
139  * @param sensors multihashmap of loaded sensors
140  * @return #GNUNET_OK if started successfully, #GNUNET_SYSERR otherwise
141  */
142 int
143 SENSOR_reporting_start(const struct GNUNET_CONFIGURATION_Handle *c,
144     struct GNUNET_CONTAINER_MultiHashMap *sensors)
145 {
146
147   GNUNET_assert(NULL != sensors);
148   cfg = c;
149   GNUNET_CRYPTO_get_peer_identity(cfg, &peerid);
150   GNUNET_CONTAINER_multihashmap_iterate(sensors, &init_sensor_reporting, NULL);
151   peerstore = GNUNET_PEERSTORE_connect(cfg);
152   if (NULL == peerstore)
153   {
154     LOG (GNUNET_ERROR_TYPE_ERROR, _("Could not connect to peerstore service.\n"));
155     SENSOR_reporting_stop();
156     return GNUNET_SYSERR;
157   }
158   return GNUNET_OK;
159 }
160
161 /* end of gnunet-service-sensor-reporting.c */