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