make peerinfo notify api more robust to serious disconnects
[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,
101            const struct GNUNET_SCHEDULER_TaskContext *tc)
102 {
103   struct GNUNET_PEERINFO_NotifyContext *nc = cls;
104
105   nc->task = GNUNET_SCHEDULER_NO_TASK;
106   nc->client = GNUNET_CLIENT_connect ("peerinfo", nc->cfg);
107   if (NULL == nc->client)
108     {
109       /* ugh */
110       nc->task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
111                                                &reconnect,
112                                                nc);
113       return; 
114     }
115   request_notifications (nc);
116 }
117
118
119 /**
120  * Receive a peerinfo information message, process it and
121  * go for more.
122  *
123  * @param cls closure
124  * @param msg message received, NULL on timeout or fatal error
125  */
126 static void
127 process_notification (void *cls,
128                       const struct
129                       GNUNET_MessageHeader * msg)
130 {
131   struct GNUNET_PEERINFO_NotifyContext *nc = cls;
132   const struct InfoMessage *im;
133   const struct GNUNET_HELLO_Message *hello;
134   uint16_t ms;
135
136   if (msg == NULL)
137     {
138       GNUNET_CLIENT_disconnect (nc->client, GNUNET_NO);
139       reconnect (nc, NULL);
140       return;
141     }
142   ms = ntohs (msg->size);
143   if ((ms < sizeof (struct InfoMessage)) ||
144       (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_PEERINFO_INFO))
145     {
146       GNUNET_break (0);
147       GNUNET_CLIENT_disconnect (nc->client, GNUNET_NO);
148       nc->client = GNUNET_CLIENT_connect ("peerinfo", nc->cfg);
149       request_notifications (nc);
150       return;
151     }
152   im = (const struct InfoMessage *) msg;
153   hello = NULL;
154   if (ms > sizeof (struct InfoMessage) + sizeof (struct GNUNET_MessageHeader))
155     {
156       hello = (const struct GNUNET_HELLO_Message *) &im[1];
157       if (ms != sizeof (struct InfoMessage) + GNUNET_HELLO_size (hello))
158         {
159           GNUNET_break (0);
160           GNUNET_CLIENT_disconnect (nc->client, GNUNET_NO);
161           nc->client = GNUNET_CLIENT_connect ("peerinfo", nc->cfg);
162           request_notifications (nc);
163           return;
164         }
165     }
166 #if DEBUG_PEERINFO
167   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
168               "Received information about peer `%s' from peerinfo database\n",
169               GNUNET_i2s (&im->peer));
170 #endif
171   nc->callback (nc->callback_cls, &im->peer, hello, NULL);
172   receive_notifications (nc);
173 }
174
175
176 /**
177  * Read notifications from the client handle and pass them
178  * to the callback.
179  *
180  * @param nc our context
181  */
182 static void
183 receive_notifications (struct GNUNET_PEERINFO_NotifyContext *nc)
184 {
185   GNUNET_CLIENT_receive (nc->client,
186                          &process_notification,
187                          nc,
188                          GNUNET_TIME_UNIT_FOREVER_REL);
189 }
190
191
192 /**
193  * Transmit our init-notify request, start receiving.
194  *
195  * @param cls closure (our 'struct GNUNET_PEERINFO_NotifyContext')
196  * @param size number of bytes available in buf
197  * @param buf where the callee should write the message
198  * @return number of bytes written to buf
199  */
200 static size_t 
201 transmit_notify_request (void *cls,
202                          size_t size, 
203                          void *buf)
204 {
205   struct GNUNET_PEERINFO_NotifyContext *nc = cls;
206   struct GNUNET_MessageHeader hdr;
207
208   nc->init = NULL;
209   if (buf == NULL)
210     {
211       GNUNET_CLIENT_disconnect (nc->client, GNUNET_NO);
212       nc->client = GNUNET_CLIENT_connect ("peerinfo", nc->cfg);
213       request_notifications (nc);
214       return 0;
215     }
216   GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
217   hdr.size = htons (sizeof (struct GNUNET_MessageHeader));
218   hdr.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_NOTIFY);
219   memcpy (buf, &hdr, sizeof (struct GNUNET_MessageHeader));
220   receive_notifications (nc);
221   return sizeof (struct GNUNET_MessageHeader);
222 }
223
224
225 /**
226  * Send a request to the peerinfo service to start being
227  * notified about all changes to peer information.
228  *
229  * @param nc our context
230  */
231 static void
232 request_notifications (struct GNUNET_PEERINFO_NotifyContext *nc)
233 {
234   GNUNET_assert (NULL == nc->init);
235   nc->init =GNUNET_CLIENT_notify_transmit_ready (nc->client,
236                                                  sizeof (struct GNUNET_MessageHeader),
237                                                  GNUNET_TIME_UNIT_FOREVER_REL,
238                                                  GNUNET_YES,
239                                                  &transmit_notify_request,
240                                                  nc);
241 }
242
243
244 /**
245  * Call a method whenever our known information about peers
246  * changes.  Initially calls the given function for all known
247  * peers and then only signals changes.
248  *
249  * @param cfg configuration to use
250  * @param callback the method to call for each peer
251  * @param callback_cls closure for callback
252  * @return NULL on error
253  */
254 struct GNUNET_PEERINFO_NotifyContext *
255 GNUNET_PEERINFO_notify (const struct GNUNET_CONFIGURATION_Handle *cfg,
256                         GNUNET_PEERINFO_Processor callback,
257                         void *callback_cls)
258 {
259   struct GNUNET_PEERINFO_NotifyContext *nc;
260   struct GNUNET_CLIENT_Connection *client;
261
262   client = GNUNET_CLIENT_connect ("peerinfo", cfg);
263   if (client == NULL)
264     {      
265       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
266                   _("Could not connect to `%s' service.\n"), "peerinfo");
267       return NULL;
268     }
269   nc = GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_NotifyContext));
270   nc->cfg = cfg;
271   nc->client = client;
272   nc->callback = callback;
273   nc->callback_cls = callback_cls; 
274   request_notifications (nc);
275   return nc;
276 }
277
278
279 /**
280  * Stop notifying about changes.
281  *
282  * @param nc context to stop notifying
283  */
284 void
285 GNUNET_PEERINFO_notify_cancel (struct GNUNET_PEERINFO_NotifyContext *nc)
286 {
287   if (NULL != nc->init)
288     {
289       GNUNET_CLIENT_notify_transmit_ready_cancel (nc->init);
290       nc->init = NULL;
291     }
292   if (NULL != nc->client)
293     GNUNET_CLIENT_disconnect (nc->client, GNUNET_NO);
294   if (GNUNET_SCHEDULER_NO_TASK != nc->task)
295     GNUNET_SCHEDULER_cancel (nc->task);
296   GNUNET_free (nc);
297 }
298
299 /* end of peerinfo_api_notify.c */