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;
68 struct GNUNET_SCHEDULER_Handle *sched;
73 * Send a request to the peerinfo service to start being
74 * notified about all changes to peer information.
76 * @param nc our context
79 request_notifications (struct GNUNET_PEERINFO_NotifyContext *nc);
83 * Read notifications from the client handle and pass them
86 * @param nc our context
89 receive_notifications (struct GNUNET_PEERINFO_NotifyContext *nc);
93 * Receive a peerinfo information message, process it and
97 * @param msg message received, NULL on timeout or fatal error
100 process_notification (void *cls,
102 GNUNET_MessageHeader * msg)
104 struct GNUNET_PEERINFO_NotifyContext *nc = cls;
105 const struct InfoMessage *im;
106 const struct GNUNET_HELLO_Message *hello;
111 GNUNET_CLIENT_disconnect (nc->client, GNUNET_NO);
112 nc->client = GNUNET_CLIENT_connect (nc->sched, "peerinfo", nc->cfg);
113 request_notifications (nc);
116 ms = ntohs (msg->size);
117 if ((ms < sizeof (struct InfoMessage)) ||
118 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_PEERINFO_INFO))
121 GNUNET_CLIENT_disconnect (nc->client, GNUNET_NO);
122 nc->client = GNUNET_CLIENT_connect (nc->sched, "peerinfo", nc->cfg);
123 request_notifications (nc);
126 im = (const struct InfoMessage *) msg;
128 if (ms > sizeof (struct InfoMessage) + sizeof (struct GNUNET_MessageHeader))
130 hello = (const struct GNUNET_HELLO_Message *) &im[1];
131 if (ms != sizeof (struct InfoMessage) + GNUNET_HELLO_size (hello))
134 GNUNET_CLIENT_disconnect (nc->client, GNUNET_NO);
135 nc->client = GNUNET_CLIENT_connect (nc->sched, "peerinfo", nc->cfg);
136 request_notifications (nc);
141 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
142 "Received information about peer `%s' from peerinfo database\n",
143 GNUNET_i2s (&im->peer));
145 nc->callback (nc->callback_cls, &im->peer, hello, ntohl (im->trust));
146 receive_notifications (nc);
151 * Read notifications from the client handle and pass them
154 * @param nc our context
157 receive_notifications (struct GNUNET_PEERINFO_NotifyContext *nc)
159 GNUNET_CLIENT_receive (nc->client,
160 &process_notification,
162 GNUNET_TIME_UNIT_FOREVER_REL);
167 * Transmit our init-notify request, start receiving.
169 * @param cls closure (our 'struct GNUNET_PEERINFO_NotifyContext')
170 * @param size number of bytes available in buf
171 * @param buf where the callee should write the message
172 * @return number of bytes written to buf
175 transmit_notify_request (void *cls,
179 struct GNUNET_PEERINFO_NotifyContext *nc = cls;
180 struct GNUNET_MessageHeader hdr;
185 GNUNET_CLIENT_disconnect (nc->client, GNUNET_NO);
186 nc->client = GNUNET_CLIENT_connect (nc->sched, "peerinfo", nc->cfg);
187 request_notifications (nc);
190 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
191 hdr.size = htons (sizeof (struct GNUNET_MessageHeader));
192 hdr.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_NOTIFY);
193 memcpy (buf, &hdr, sizeof (struct GNUNET_MessageHeader));
194 receive_notifications (nc);
195 return sizeof (struct GNUNET_MessageHeader);
200 * Send a request to the peerinfo service to start being
201 * notified about all changes to peer information.
203 * @param nc our context
206 request_notifications (struct GNUNET_PEERINFO_NotifyContext *nc)
208 GNUNET_assert (NULL == nc->init);
209 nc->init =GNUNET_CLIENT_notify_transmit_ready (nc->client,
210 sizeof (struct GNUNET_MessageHeader),
211 GNUNET_TIME_UNIT_FOREVER_REL,
213 &transmit_notify_request,
219 * Call a method whenever our known information about peers
220 * changes. Initially calls the given function for all known
221 * peers and then only signals changes.
223 * @param cfg configuration to use
224 * @param sched scheduler to use
225 * @param callback the method to call for each peer
226 * @param callback_cls closure for callback
227 * @return NULL on error
229 struct GNUNET_PEERINFO_NotifyContext *
230 GNUNET_PEERINFO_notify (const struct GNUNET_CONFIGURATION_Handle *cfg,
231 struct GNUNET_SCHEDULER_Handle *sched,
232 GNUNET_PEERINFO_Processor callback,
235 struct GNUNET_PEERINFO_NotifyContext *nc;
236 struct GNUNET_CLIENT_Connection *client;
238 client = GNUNET_CLIENT_connect (sched, "peerinfo", cfg);
241 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
242 _("Could not connect to `%s' service.\n"), "peerinfo");
245 nc = GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_NotifyContext));
249 nc->callback = callback;
250 nc->callback_cls = callback_cls;
251 request_notifications (nc);
257 * Stop notifying about changes.
259 * @param nc context to stop notifying
262 GNUNET_PEERINFO_notify_cancel (struct GNUNET_PEERINFO_NotifyContext *nc)
264 if (NULL != nc->init)
266 GNUNET_CLIENT_notify_transmit_ready_cancel (nc->init);
269 GNUNET_CLIENT_disconnect (nc->client, GNUNET_NO);
273 /* end of peerinfo_api_notify.c */