sensor dashboard (collection point) initial commit
[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 "gnunet_cadet_service.h"
30
31 static struct GNUNET_CADET_Handle *cadet;
32
33 /**
34  * Task run during shutdown.
35  *
36  * @param cls unused
37  * @param tc unused
38  */
39 static void
40 cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
41 {
42   if (NULL != cadet)
43   {
44     GNUNET_CADET_disconnect(cadet);
45     cadet = NULL;
46   }
47   GNUNET_SCHEDULER_shutdown();
48 }
49
50 /**
51  * Function called whenever a channel is destroyed.  Should clean up
52  * any associated state.
53  *
54  * It must NOT call #GNUNET_CADET_channel_destroy on the channel.
55  *
56  * @param cls closure (set from #GNUNET_CADET_connect)
57  * @param channel connection to the other end (henceforth invalid)
58  * @param channel_ctx place where local state associated
59  *                   with the channel is stored
60  */
61 static void cadet_channel_destroyed (void *cls,
62     const struct GNUNET_CADET_Channel *channel,
63     void *channel_ctx)
64 {
65
66 }
67
68 /**
69  * Method called whenever another peer has added us to a channel
70  * the other peer initiated.
71  * Only called (once) upon reception of data with a message type which was
72  * subscribed to in #GNUNET_CADET_connect.
73  *
74  * A call to #GNUNET_CADET_channel_destroy causes the channel to be ignored. In
75  * this case the handler MUST return NULL.
76  *
77  * @param cls closure
78  * @param channel new handle to the channel
79  * @param initiator peer that started the channel
80  * @param port Port this channel is for.
81  * @param options CadetOption flag field, with all active option bits set to 1.
82  *
83  * @return initial channel context for the channel
84  *         (can be NULL -- that's not an error)
85  */
86 static void *cadet_channel_created (void *cls,
87     struct GNUNET_CADET_Channel *channel,
88     const struct GNUNET_PeerIdentity *initiator,
89     uint32_t port, enum GNUNET_CADET_ChannelOption options)
90 {
91   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
92       "CADET channel opened by remote peer `%s'.\n",
93       GNUNET_i2s(initiator));
94   return NULL; /* FIXME */
95 }
96
97 /**
98  * Process sensordashboard requests.
99  *
100  * @param cls closure
101  * @param server the initialized server
102  * @param cfg configuration to use
103  */
104 static void
105 run (void *cls, struct GNUNET_SERVER_Handle *server,
106      const struct GNUNET_CONFIGURATION_Handle *cfg)
107 {
108   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
109     {NULL, NULL, 0, 0}
110   };
111   static struct GNUNET_CADET_MessageHandler cadet_handlers[] = {
112       {NULL, 0, 0}
113   };
114   static uint32_t cadet_ports[] = {
115       GNUNET_APPLICATION_TYPE_SENSORDASHBOARD,
116       GNUNET_APPLICATION_TYPE_END
117   };
118   cadet = GNUNET_CADET_connect(cfg,
119       NULL,
120       &cadet_channel_created,
121       &cadet_channel_destroyed,
122       cadet_handlers,
123       cadet_ports);
124   if(NULL == cadet)
125   {
126     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
127         _("Failed to connect to CADET service.\n"));
128     GNUNET_SCHEDULER_add_now (&cleanup_task, NULL);
129     return;
130   }
131   GNUNET_SERVER_add_handlers (server, handlers);
132   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup_task,
133                                 NULL);
134 }
135
136
137 /**
138  * The main function for the sensordashboard service.
139  *
140  * @param argc number of arguments from the command line
141  * @param argv command line arguments
142  * @return 0 ok, 1 on error
143  */
144 int
145 main (int argc, char *const *argv)
146 {
147   return (GNUNET_OK ==
148           GNUNET_SERVICE_run (argc, argv, "sensordashboard",
149                               GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1;
150 }
151
152 /* end of gnunet-service-sensordashboard.c */