moved common sensor functionality to a util lib
[oweals/gnunet.git] / src / sensordashboard / gnunet-service-sensordashboard.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009 Christian Grothoff (and other contributing authors)
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 sensordashboard/gnunet-service-sensordashboard.c
23  * @brief Service collecting sensor readings from peers
24  * @author Omar Tarabai
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_applications.h"
29 #include "sensordashboard.h"
30 #include "gnunet_cadet_service.h"
31 #include "gnunet_sensor_util_lib.h"
32
33 /**
34  * Handle to CADET service
35  */
36 static struct GNUNET_CADET_Handle *cadet;
37
38 static struct GNUNET_CONTAINER_MultiHashMap *sensors;
39
40 /**
41  * Task run during shutdown.
42  *
43  * @param cls unused
44  * @param tc unused
45  */
46 static void
47 cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
48 {
49   if (NULL != cadet)
50   {
51     GNUNET_CADET_disconnect(cadet);
52     cadet = NULL;
53   }
54   GNUNET_SCHEDULER_shutdown();
55 }
56
57 /**
58  * Function called whenever a channel is destroyed.  Should clean up
59  * any associated state.
60  *
61  * It must NOT call #GNUNET_CADET_channel_destroy on the channel.
62  *
63  * @param cls closure (set from #GNUNET_CADET_connect)
64  * @param channel connection to the other end (henceforth invalid)
65  * @param channel_ctx place where local state associated
66  *                   with the channel is stored
67  */
68 static void cadet_channel_destroyed (void *cls,
69     const struct GNUNET_CADET_Channel *channel,
70     void *channel_ctx)
71 {
72
73 }
74
75 /**
76  * Method called whenever another peer has added us to a channel
77  * the other peer initiated.
78  * Only called (once) upon reception of data with a message type which was
79  * subscribed to in #GNUNET_CADET_connect.
80  *
81  * A call to #GNUNET_CADET_channel_destroy causes the channel to be ignored. In
82  * this case the handler MUST return NULL.
83  *
84  * @param cls closure
85  * @param channel new handle to the channel
86  * @param initiator peer that started the channel
87  * @param port Port this channel is for.
88  * @param options CadetOption flag field, with all active option bits set to 1.
89  *
90  * @return initial channel context for the channel
91  *         (can be NULL -- that's not an error)
92  */
93 static void *cadet_channel_created (void *cls,
94     struct GNUNET_CADET_Channel *channel,
95     const struct GNUNET_PeerIdentity *initiator,
96     uint32_t port, enum GNUNET_CADET_ChannelOption options)
97 {
98   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
99       "CADET channel opened by remote peer `%s'.\n",
100       GNUNET_i2s(initiator));
101   return NULL; /* FIXME */
102 }
103
104 /**
105  * Called with any sensor reading messages received from CADET.
106  *
107  * Each time the function must call #GNUNET_CADET_receive_done on the channel
108  * in order to receive the next message. This doesn't need to be immediate:
109  * can be delayed if some processing is done on the message.
110  *
111  * @param cls Closure (set from #GNUNET_CADET_connect).
112  * @param channel Connection to the other end.
113  * @param channel_ctx Place to store local state associated with the channel.
114  * @param message The actual message.
115  * @return #GNUNET_OK to keep the channel open,
116  *         #GNUNET_SYSERR to close it (signal serious error).
117  */
118 int sensor_reading_receiver (void *cls, struct GNUNET_CADET_Channel *channel,
119     void **channel_ctx, const struct GNUNET_MessageHeader *message)
120 {
121
122 }
123
124 /**
125  * Process sensordashboard requests.
126  *
127  * @param cls closure
128  * @param server the initialized server
129  * @param cfg configuration to use
130  */
131 static void
132 run (void *cls, struct GNUNET_SERVER_Handle *server,
133      const struct GNUNET_CONFIGURATION_Handle *cfg)
134 {
135   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
136     {NULL, NULL, 0, 0}
137   };
138   static struct GNUNET_CADET_MessageHandler cadet_handlers[] = {
139       {&sensor_reading_receiver, GNUNET_MESSAGE_TYPE_SENSOR_READING, 0},
140       {NULL, 0, 0}
141   };
142   static uint32_t cadet_ports[] = {
143       GNUNET_APPLICATION_TYPE_SENSORDASHBOARD,
144       GNUNET_APPLICATION_TYPE_END
145   };
146   sensors = GNUNET_SENSOR_load_all_sensors ();
147   GNUNET_assert (NULL != sensors);
148   cadet = GNUNET_CADET_connect(cfg,
149       NULL,
150       &cadet_channel_created,
151       &cadet_channel_destroyed,
152       cadet_handlers,
153       cadet_ports);
154   if(NULL == cadet)
155   {
156     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
157         _("Failed to connect to CADET service.\n"));
158     GNUNET_SCHEDULER_add_now (&cleanup_task, NULL);
159     return;
160   }
161   GNUNET_SERVER_add_handlers (server, handlers);
162   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup_task,
163                                 NULL);
164 }
165
166
167 /**
168  * The main function for the sensordashboard service.
169  *
170  * @param argc number of arguments from the command line
171  * @param argv command line arguments
172  * @return 0 ok, 1 on error
173  */
174 int
175 main (int argc, char *const *argv)
176 {
177   return (GNUNET_OK ==
178           GNUNET_SERVICE_run (argc, argv, "sensordashboard",
179                               GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1;
180 }
181
182 /* end of gnunet-service-sensordashboard.c */