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"
31 #define LOG(kind,...) GNUNET_log_from (kind, "sensor-reporting",__VA_ARGS__)
34 * Context of reporting to collection
37 struct CollectionReportingContext
43 struct SensorInfo *sensor;
46 * Reporting task (OR GNUNET_SCHEDULER_NO_TASK)
48 GNUNET_SCHEDULER_TaskIdentifier task;
55 static const struct GNUNET_CONFIGURATION_Handle *cfg;
58 * Handle to peerstore service
60 static struct GNUNET_PEERSTORE_Handle *peerstore;
65 struct GNUNET_PeerIdentity peerid;
69 * Stop sensor reporting module
71 void SENSOR_reporting_stop()
73 LOG (GNUNET_ERROR_TYPE_DEBUG, "Stopping sensor reporting module.\n");
74 if (NULL != peerstore)
76 GNUNET_PEERSTORE_disconnect(peerstore);
82 * Task scheduled to send values to collection point
84 * @param cls closure, a 'struct CollectionReportingContext *'
87 void report_collection_point
88 (void *cls, const struct GNUNET_SCHEDULER_TaskContext* tc)
90 struct CollectionReportingContext *crc = cls;
92 crc->task = GNUNET_SCHEDULER_NO_TASK;
96 * Iterator for defined sensors
97 * Watches sensors for readings to report
101 * @param value a 'struct SensorInfo *' with sensor information
102 * @return #GNUNET_YES to continue iterations
105 init_sensor_reporting (void *cls,
106 const struct GNUNET_HashCode *key,
109 struct SensorInfo *sensor = value;
110 struct CollectionReportingContext *crc;
112 if (NULL != sensor->collection_point)
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;
121 GNUNET_SCHEDULER_add_delayed(sensor->collection_interval,
122 &report_collection_point,
125 if (GNUNET_YES == sensor->p2p_report)
127 LOG (GNUNET_ERROR_TYPE_INFO,
128 "Will start reporting sensor `%s' values to p2p network every %s.\n",
130 GNUNET_STRINGS_relative_time_to_string(sensor->p2p_interval, GNUNET_YES));
136 * Start the sensor reporting module
138 * @param c our service configuration
139 * @param sensors multihashmap of loaded sensors
140 * @return #GNUNET_OK if started successfully, #GNUNET_SYSERR otherwise
143 SENSOR_reporting_start(const struct GNUNET_CONFIGURATION_Handle *c,
144 struct GNUNET_CONTAINER_MultiHashMap *sensors)
147 GNUNET_assert(NULL != sensors);
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)
154 LOG (GNUNET_ERROR_TYPE_ERROR, _("Could not connect to peerstore service.\n"));
155 SENSOR_reporting_stop();
156 return GNUNET_SYSERR;
161 /* end of gnunet-service-sensor-reporting.c */