sensor dashboard (collection point) initial commit
[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 #include "gnunet_cadet_service.h"
31
32 #define LOG(kind,...) GNUNET_log_from (kind, "sensor-reporting",__VA_ARGS__)
33
34 /**
35  * Context of reporting to collection
36  * point
37  */
38 struct CollectionReportingContext
39 {
40
41   /**
42    * Sensor information
43    */
44   struct SensorInfo *sensor;
45
46   /**
47    * Reporting task (OR GNUNET_SCHEDULER_NO_TASK)
48    */
49   GNUNET_SCHEDULER_TaskIdentifier task;
50
51 };
52
53 /**
54  * Our configuration.
55  */
56 static const struct GNUNET_CONFIGURATION_Handle *cfg;
57
58 /**
59  * Handle to peerstore service
60  */
61 static struct GNUNET_PEERSTORE_Handle *peerstore;
62
63 /**
64  * My peer id
65  */
66 static struct GNUNET_PeerIdentity peerid;
67
68 /**
69  * Handle to CADET service
70  */
71 static struct GNUNET_CADET_Handle *cadet;
72
73
74 /**
75  * Stop sensor reporting module
76  */
77 void SENSOR_reporting_stop ()
78 {
79   LOG (GNUNET_ERROR_TYPE_DEBUG, "Stopping sensor reporting module.\n");
80   if (NULL != peerstore)
81   {
82     GNUNET_PEERSTORE_disconnect (peerstore);
83     peerstore = NULL;
84   }
85 }
86
87 /**
88  * Task scheduled to send values to collection point
89  *
90  * @param cls closure, a 'struct CollectionReportingContext *'
91  * @param tc unused
92  */
93 void report_collection_point
94 (void *cls, const struct GNUNET_SCHEDULER_TaskContext* tc)
95 {
96   struct CollectionReportingContext *crc = cls;
97
98   crc->task = GNUNET_SCHEDULER_NO_TASK;
99 }
100
101 /**
102  * Iterator for defined sensors
103  * Watches sensors for readings to report
104  *
105  * @param cls unused
106  * @param key unused
107  * @param value a 'struct SensorInfo *' with sensor information
108  * @return #GNUNET_YES to continue iterations
109  */
110 static int
111 init_sensor_reporting (void *cls,
112     const struct GNUNET_HashCode *key,
113     void *value)
114 {
115   struct SensorInfo *sensor = value;
116   struct CollectionReportingContext *crc;
117
118   if (NULL != sensor->collection_point)
119   {
120     LOG (GNUNET_ERROR_TYPE_INFO,
121         "Will start reporting sensor `%s' values to collection point `%s' every %s.\n",
122         sensor->name, GNUNET_i2s_full(sensor->collection_point),
123         GNUNET_STRINGS_relative_time_to_string(sensor->collection_interval, GNUNET_YES));
124     crc = GNUNET_new (struct CollectionReportingContext);
125     crc->sensor = sensor;
126     crc->task =
127         GNUNET_SCHEDULER_add_delayed (sensor->collection_interval,
128             &report_collection_point,
129             crc);
130   }
131   if (GNUNET_YES == sensor->p2p_report)
132   {
133     LOG (GNUNET_ERROR_TYPE_INFO,
134         "Will start reporting sensor `%s' values to p2p network every %s.\n",
135         sensor->name,
136         GNUNET_STRINGS_relative_time_to_string(sensor->p2p_interval, GNUNET_YES));
137   }
138   return GNUNET_YES;
139 }
140
141 /**
142  * Function called whenever a channel is destroyed.  Should clean up
143  * any associated state.
144  *
145  * It must NOT call #GNUNET_CADET_channel_destroy on the channel.
146  *
147  * @param cls closure (set from #GNUNET_CADET_connect)
148  * @param channel connection to the other end (henceforth invalid)
149  * @param channel_ctx place where local state associated
150  *                   with the channel is stored
151  */
152 static void cadet_channel_destroyed (void *cls,
153     const struct GNUNET_CADET_Channel *channel,
154     void *channel_ctx)
155 {
156
157 }
158
159 /**
160  * Start the sensor reporting module
161  *
162  * @param c our service configuration
163  * @param sensors multihashmap of loaded sensors
164  * @return #GNUNET_OK if started successfully, #GNUNET_SYSERR otherwise
165  */
166 int
167 SENSOR_reporting_start (const struct GNUNET_CONFIGURATION_Handle *c,
168     struct GNUNET_CONTAINER_MultiHashMap *sensors)
169 {
170   static struct GNUNET_CADET_MessageHandler cadet_handlers[] = {
171       {NULL, 0, 0}
172   };
173
174   GNUNET_assert(NULL != sensors);
175   cfg = c;
176   GNUNET_CRYPTO_get_peer_identity(cfg, &peerid);
177   GNUNET_CONTAINER_multihashmap_iterate(sensors, &init_sensor_reporting, NULL);
178   peerstore = GNUNET_PEERSTORE_connect(cfg);
179   if (NULL == peerstore)
180   {
181     LOG (GNUNET_ERROR_TYPE_ERROR,
182         _("Failed to connect to peerstore service.\n"));
183     SENSOR_reporting_stop ();
184     return GNUNET_SYSERR;
185   }
186   cadet = GNUNET_CADET_connect(cfg,
187       NULL,
188       NULL,
189       &cadet_channel_destroyed,
190       cadet_handlers,
191       NULL);
192   if (NULL == cadet)
193   {
194     LOG (GNUNET_ERROR_TYPE_ERROR,
195         _("Failed to connect to CADET service.\n"));
196     SENSOR_reporting_stop ();
197     return GNUNET_SYSERR;
198   }
199
200   return GNUNET_OK;
201 }
202
203 /* end of gnunet-service-sensor-reporting.c */