2 This file is part of GNUnet.
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.
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.
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.
22 * @file sensor/gnunet-service-sensor-reporting.c
23 * @brief sensor service reporting functionality
24 * @author Omar Tarabai
27 #include "gnunet_util_lib.h"
29 #include "gnunet_peerstore_service.h"
30 #include "gnunet_cadet_service.h"
32 #define LOG(kind,...) GNUNET_log_from (kind, "sensor-reporting",__VA_ARGS__)
35 * Context of reporting to collection
38 struct CollectionReportingContext
44 struct SensorInfo *sensor;
47 * Reporting task (OR GNUNET_SCHEDULER_NO_TASK)
49 GNUNET_SCHEDULER_TaskIdentifier task;
56 static const struct GNUNET_CONFIGURATION_Handle *cfg;
59 * Handle to peerstore service
61 static struct GNUNET_PEERSTORE_Handle *peerstore;
66 static struct GNUNET_PeerIdentity peerid;
69 * Handle to CADET service
71 static struct GNUNET_CADET_Handle *cadet;
75 * Stop sensor reporting module
77 void SENSOR_reporting_stop ()
79 LOG (GNUNET_ERROR_TYPE_DEBUG, "Stopping sensor reporting module.\n");
80 if (NULL != peerstore)
82 GNUNET_PEERSTORE_disconnect (peerstore);
88 * Task scheduled to send values to collection point
90 * @param cls closure, a 'struct CollectionReportingContext *'
93 void report_collection_point
94 (void *cls, const struct GNUNET_SCHEDULER_TaskContext* tc)
96 struct CollectionReportingContext *crc = cls;
98 crc->task = GNUNET_SCHEDULER_NO_TASK;
102 * Iterator for defined sensors
103 * Watches sensors for readings to report
107 * @param value a 'struct SensorInfo *' with sensor information
108 * @return #GNUNET_YES to continue iterations
111 init_sensor_reporting (void *cls,
112 const struct GNUNET_HashCode *key,
115 struct SensorInfo *sensor = value;
116 struct CollectionReportingContext *crc;
118 if (NULL != sensor->collection_point)
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;
127 GNUNET_SCHEDULER_add_delayed (sensor->collection_interval,
128 &report_collection_point,
131 if (GNUNET_YES == sensor->p2p_report)
133 LOG (GNUNET_ERROR_TYPE_INFO,
134 "Will start reporting sensor `%s' values to p2p network every %s.\n",
136 GNUNET_STRINGS_relative_time_to_string(sensor->p2p_interval, GNUNET_YES));
142 * Function called whenever a channel is destroyed. Should clean up
143 * any associated state.
145 * It must NOT call #GNUNET_CADET_channel_destroy on the channel.
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
152 static void cadet_channel_destroyed (void *cls,
153 const struct GNUNET_CADET_Channel *channel,
160 * Start the sensor reporting module
162 * @param c our service configuration
163 * @param sensors multihashmap of loaded sensors
164 * @return #GNUNET_OK if started successfully, #GNUNET_SYSERR otherwise
167 SENSOR_reporting_start (const struct GNUNET_CONFIGURATION_Handle *c,
168 struct GNUNET_CONTAINER_MultiHashMap *sensors)
170 static struct GNUNET_CADET_MessageHandler cadet_handlers[] = {
174 GNUNET_assert(NULL != sensors);
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)
181 LOG (GNUNET_ERROR_TYPE_ERROR,
182 _("Failed to connect to peerstore service.\n"));
183 SENSOR_reporting_stop ();
184 return GNUNET_SYSERR;
186 cadet = GNUNET_CADET_connect(cfg,
189 &cadet_channel_destroyed,
194 LOG (GNUNET_ERROR_TYPE_ERROR,
195 _("Failed to connect to CADET service.\n"));
196 SENSOR_reporting_stop ();
197 return GNUNET_SYSERR;
203 /* end of gnunet-service-sensor-reporting.c */