2 This file is part of GNUnet.
3 (C) 2001, 2002, 2004, 2005, 2007, 2009, 2010 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file peerinfo/peerinfo_api_notify.c
23 * @brief notify API to access peerinfo service
24 * @author Christian Grothoff
27 #include "gnunet_client_lib.h"
28 #include "gnunet_peerinfo_service.h"
29 #include "gnunet_protocols.h"
30 #include "gnunet_time_lib.h"
34 * Context for the info handler.
36 struct GNUNET_PEERINFO_NotifyContext
40 * Our connection to the PEERINFO service.
42 struct GNUNET_CLIENT_Connection *client;
45 * Function to call with information.
47 GNUNET_PEERINFO_Processor callback;
50 * Closure for callback.
55 * Handle to our initial request for message transmission to
56 * the peerinfo service.
58 struct GNUNET_CLIENT_TransmitHandle *init;
63 const struct GNUNET_CONFIGURATION_Handle *cfg;
66 * Tasked used for delayed re-connection attempt.
68 GNUNET_SCHEDULER_TaskIdentifier task;
74 * Send a request to the peerinfo service to start being
75 * notified about all changes to peer information.
77 * @param nc our context
80 request_notifications (struct GNUNET_PEERINFO_NotifyContext *nc);
84 * Read notifications from the client handle and pass them
87 * @param nc our context
90 receive_notifications (struct GNUNET_PEERINFO_NotifyContext *nc);
94 * Task to re-try connecting to peerinfo.
96 * @param cls the 'struct GNUNET_PEERINFO_NotifyContext'
97 * @param tc scheduler context
100 reconnect (void *cls,
101 const struct GNUNET_SCHEDULER_TaskContext *tc)
103 struct GNUNET_PEERINFO_NotifyContext *nc = cls;
105 nc->task = GNUNET_SCHEDULER_NO_TASK;
106 nc->client = GNUNET_CLIENT_connect ("peerinfo", nc->cfg);
107 if (NULL == nc->client)
110 nc->task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
115 request_notifications (nc);
120 * Receive a peerinfo information message, process it and
124 * @param msg message received, NULL on timeout or fatal error
127 process_notification (void *cls,
129 GNUNET_MessageHeader * msg)
131 struct GNUNET_PEERINFO_NotifyContext *nc = cls;
132 const struct InfoMessage *im;
133 const struct GNUNET_HELLO_Message *hello;
138 GNUNET_CLIENT_disconnect (nc->client, GNUNET_NO);
139 reconnect (nc, NULL);
142 ms = ntohs (msg->size);
143 if ((ms < sizeof (struct InfoMessage)) ||
144 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_PEERINFO_INFO))
147 GNUNET_CLIENT_disconnect (nc->client, GNUNET_NO);
148 nc->client = GNUNET_CLIENT_connect ("peerinfo", nc->cfg);
149 request_notifications (nc);
152 im = (const struct InfoMessage *) msg;
154 if (ms > sizeof (struct InfoMessage) + sizeof (struct GNUNET_MessageHeader))
156 hello = (const struct GNUNET_HELLO_Message *) &im[1];
157 if (ms != sizeof (struct InfoMessage) + GNUNET_HELLO_size (hello))
160 GNUNET_CLIENT_disconnect (nc->client, GNUNET_NO);
161 nc->client = GNUNET_CLIENT_connect ("peerinfo", nc->cfg);
162 request_notifications (nc);
167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
168 "Received information about peer `%s' from peerinfo database\n",
169 GNUNET_i2s (&im->peer));
171 nc->callback (nc->callback_cls, &im->peer, hello, NULL);
172 receive_notifications (nc);
177 * Read notifications from the client handle and pass them
180 * @param nc our context
183 receive_notifications (struct GNUNET_PEERINFO_NotifyContext *nc)
185 GNUNET_CLIENT_receive (nc->client,
186 &process_notification,
188 GNUNET_TIME_UNIT_FOREVER_REL);
193 * Transmit our init-notify request, start receiving.
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
201 transmit_notify_request (void *cls,
205 struct GNUNET_PEERINFO_NotifyContext *nc = cls;
206 struct GNUNET_MessageHeader hdr;
211 GNUNET_CLIENT_disconnect (nc->client, GNUNET_NO);
212 nc->client = GNUNET_CLIENT_connect ("peerinfo", nc->cfg);
213 request_notifications (nc);
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);
226 * Send a request to the peerinfo service to start being
227 * notified about all changes to peer information.
229 * @param nc our context
232 request_notifications (struct GNUNET_PEERINFO_NotifyContext *nc)
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,
239 &transmit_notify_request,
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.
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
254 struct GNUNET_PEERINFO_NotifyContext *
255 GNUNET_PEERINFO_notify (const struct GNUNET_CONFIGURATION_Handle *cfg,
256 GNUNET_PEERINFO_Processor callback,
259 struct GNUNET_PEERINFO_NotifyContext *nc;
260 struct GNUNET_CLIENT_Connection *client;
262 client = GNUNET_CLIENT_connect ("peerinfo", cfg);
265 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
266 _("Could not connect to `%s' service.\n"), "peerinfo");
269 nc = GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_NotifyContext));
272 nc->callback = callback;
273 nc->callback_cls = callback_cls;
274 request_notifications (nc);
280 * Stop notifying about changes.
282 * @param nc context to stop notifying
285 GNUNET_PEERINFO_notify_cancel (struct GNUNET_PEERINFO_NotifyContext *nc)
287 if (NULL != nc->init)
289 GNUNET_CLIENT_notify_transmit_ready_cancel (nc->init);
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);
299 /* end of peerinfo_api_notify.c */