even nicer indentation, thanks to LRN's indent patch
[oweals/gnunet.git] / src / peerinfo / peerinfo_api_notify.c
1 /*
2      This file is part of GNUnet.
3      (C) 2001, 2002, 2004, 2005, 2007, 2009, 2010 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 peerinfo/peerinfo_api_notify.c
23  * @brief notify API to access peerinfo service
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_client_lib.h"
28 #include "gnunet_peerinfo_service.h"
29 #include "gnunet_protocols.h"
30 #include "gnunet_time_lib.h"
31 #include "peerinfo.h"
32
33 /**
34  * Context for the info handler.
35  */
36 struct GNUNET_PEERINFO_NotifyContext
37 {
38
39   /**
40    * Our connection to the PEERINFO service.
41    */
42   struct GNUNET_CLIENT_Connection *client;
43
44   /**
45    * Function to call with information.
46    */
47   GNUNET_PEERINFO_Processor callback;
48
49   /**
50    * Closure for callback.
51    */
52   void *callback_cls;
53
54   /**
55    * Handle to our initial request for message transmission to
56    * the peerinfo service.
57    */
58   struct GNUNET_CLIENT_TransmitHandle *init;
59
60   /**
61    * Configuration.
62    */
63   const struct GNUNET_CONFIGURATION_Handle *cfg;
64
65   /**
66    * Tasked used for delayed re-connection attempt.
67    */
68   GNUNET_SCHEDULER_TaskIdentifier task;
69
70 };
71
72
73 /**
74  * Send a request to the peerinfo service to start being
75  * notified about all changes to peer information.
76  *
77  * @param nc our context
78  */
79 static void
80 request_notifications (struct GNUNET_PEERINFO_NotifyContext *nc);
81
82
83 /**
84  * Read notifications from the client handle and pass them
85  * to the callback.
86  *
87  * @param nc our context
88  */
89 static void
90 receive_notifications (struct GNUNET_PEERINFO_NotifyContext *nc);
91
92
93 /**
94  * Task to re-try connecting to peerinfo.
95  *
96  * @param cls the 'struct GNUNET_PEERINFO_NotifyContext'
97  * @param tc scheduler context
98  */
99 static void
100 reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
101 {
102   struct GNUNET_PEERINFO_NotifyContext *nc = cls;
103
104   nc->task = GNUNET_SCHEDULER_NO_TASK;
105   nc->client = GNUNET_CLIENT_connect ("peerinfo", nc->cfg);
106   if (NULL == nc->client)
107   {
108     /* ugh */
109     nc->task =
110         GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &reconnect, nc);
111     return;
112   }
113   request_notifications (nc);
114 }
115
116
117 /**
118  * Receive a peerinfo information message, process it and
119  * go for more.
120  *
121  * @param cls closure
122  * @param msg message received, NULL on timeout or fatal error
123  */
124 static void
125 process_notification (void *cls, const struct GNUNET_MessageHeader *msg)
126 {
127   struct GNUNET_PEERINFO_NotifyContext *nc = cls;
128   const struct InfoMessage *im;
129   const struct GNUNET_HELLO_Message *hello;
130   uint16_t ms;
131
132   if (msg == NULL)
133   {
134     GNUNET_CLIENT_disconnect (nc->client, GNUNET_NO);
135     reconnect (nc, NULL);
136     return;
137   }
138   ms = ntohs (msg->size);
139   if ((ms < sizeof (struct InfoMessage)) ||
140       (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_PEERINFO_INFO))
141   {
142     GNUNET_break (0);
143     GNUNET_CLIENT_disconnect (nc->client, GNUNET_NO);
144     nc->client = GNUNET_CLIENT_connect ("peerinfo", nc->cfg);
145     request_notifications (nc);
146     return;
147   }
148   im = (const struct InfoMessage *) msg;
149   hello = NULL;
150   if (ms > sizeof (struct InfoMessage) + sizeof (struct GNUNET_MessageHeader))
151   {
152     hello = (const struct GNUNET_HELLO_Message *) &im[1];
153     if (ms != sizeof (struct InfoMessage) + GNUNET_HELLO_size (hello))
154     {
155       GNUNET_break (0);
156       GNUNET_CLIENT_disconnect (nc->client, GNUNET_NO);
157       nc->client = GNUNET_CLIENT_connect ("peerinfo", nc->cfg);
158       request_notifications (nc);
159       return;
160     }
161   }
162 #if DEBUG_PEERINFO
163   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
164               "Received information about peer `%s' from peerinfo database\n",
165               GNUNET_i2s (&im->peer));
166 #endif
167   nc->callback (nc->callback_cls, &im->peer, hello, NULL);
168   receive_notifications (nc);
169 }
170
171
172 /**
173  * Read notifications from the client handle and pass them
174  * to the callback.
175  *
176  * @param nc our context
177  */
178 static void
179 receive_notifications (struct GNUNET_PEERINFO_NotifyContext *nc)
180 {
181   GNUNET_CLIENT_receive (nc->client, &process_notification, nc,
182                          GNUNET_TIME_UNIT_FOREVER_REL);
183 }
184
185
186 /**
187  * Transmit our init-notify request, start receiving.
188  *
189  * @param cls closure (our 'struct GNUNET_PEERINFO_NotifyContext')
190  * @param size number of bytes available in buf
191  * @param buf where the callee should write the message
192  * @return number of bytes written to buf
193  */
194 static size_t
195 transmit_notify_request (void *cls, size_t size, void *buf)
196 {
197   struct GNUNET_PEERINFO_NotifyContext *nc = cls;
198   struct GNUNET_MessageHeader hdr;
199
200   nc->init = NULL;
201   if (buf == NULL)
202   {
203     GNUNET_CLIENT_disconnect (nc->client, GNUNET_NO);
204     nc->client = GNUNET_CLIENT_connect ("peerinfo", nc->cfg);
205     request_notifications (nc);
206     return 0;
207   }
208   GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
209   hdr.size = htons (sizeof (struct GNUNET_MessageHeader));
210   hdr.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_NOTIFY);
211   memcpy (buf, &hdr, sizeof (struct GNUNET_MessageHeader));
212   receive_notifications (nc);
213   return sizeof (struct GNUNET_MessageHeader);
214 }
215
216
217 /**
218  * Send a request to the peerinfo service to start being
219  * notified about all changes to peer information.
220  *
221  * @param nc our context
222  */
223 static void
224 request_notifications (struct GNUNET_PEERINFO_NotifyContext *nc)
225 {
226   GNUNET_assert (NULL == nc->init);
227   nc->init =
228       GNUNET_CLIENT_notify_transmit_ready (nc->client,
229                                            sizeof (struct GNUNET_MessageHeader),
230                                            GNUNET_TIME_UNIT_FOREVER_REL,
231                                            GNUNET_YES, &transmit_notify_request,
232                                            nc);
233 }
234
235
236 /**
237  * Call a method whenever our known information about peers
238  * changes.  Initially calls the given function for all known
239  * peers and then only signals changes.
240  *
241  * @param cfg configuration to use
242  * @param callback the method to call for each peer
243  * @param callback_cls closure for callback
244  * @return NULL on error
245  */
246 struct GNUNET_PEERINFO_NotifyContext *
247 GNUNET_PEERINFO_notify (const struct GNUNET_CONFIGURATION_Handle *cfg,
248                         GNUNET_PEERINFO_Processor callback, void *callback_cls)
249 {
250   struct GNUNET_PEERINFO_NotifyContext *nc;
251   struct GNUNET_CLIENT_Connection *client;
252
253   client = GNUNET_CLIENT_connect ("peerinfo", cfg);
254   if (client == NULL)
255   {
256     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
257                 _("Could not connect to `%s' service.\n"), "peerinfo");
258     return NULL;
259   }
260   nc = GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_NotifyContext));
261   nc->cfg = cfg;
262   nc->client = client;
263   nc->callback = callback;
264   nc->callback_cls = callback_cls;
265   request_notifications (nc);
266   return nc;
267 }
268
269
270 /**
271  * Stop notifying about changes.
272  *
273  * @param nc context to stop notifying
274  */
275 void
276 GNUNET_PEERINFO_notify_cancel (struct GNUNET_PEERINFO_NotifyContext *nc)
277 {
278   if (NULL != nc->init)
279   {
280     GNUNET_CLIENT_notify_transmit_ready_cancel (nc->init);
281     nc->init = NULL;
282   }
283   if (NULL != nc->client)
284     GNUNET_CLIENT_disconnect (nc->client, GNUNET_NO);
285   if (GNUNET_SCHEDULER_NO_TASK != nc->task)
286     GNUNET_SCHEDULER_cancel (nc->task);
287   GNUNET_free (nc);
288 }
289
290 /* end of peerinfo_api_notify.c */