2 This file is part of GNUnet.
3 (C) 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 core/core_api_iterate_peers.c
23 * @brief implementation of the peer_iterate function
24 * @author Christian Grothoff
25 * @author Nathan Evans
28 #include "gnunet_core_service.h"
32 struct GNUNET_CORE_RequestContext
36 * Our connection to the service.
38 struct GNUNET_CLIENT_Connection *client;
41 * Handle for transmitting a request.
43 struct GNUNET_CLIENT_TransmitHandle *th;
46 * Function called with the peer.
48 GNUNET_CORE_ConnectEventHandler peer_cb;
51 * Closure for peer_cb.
59 * Receive reply from core service with information about a peer.
61 * @param cls our 'struct GNUNET_CORE_RequestContext *'
62 * @param msg NULL on error or last entry
65 receive_info (void *cls,
66 const struct GNUNET_MessageHeader *msg)
68 struct GNUNET_CORE_RequestContext *request_context = cls;
69 const struct ConnectNotifyMessage *connect_message;
72 /* Handle last message or error case, disconnect and clean up */
74 ((ntohs (msg->type) == GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT) &&
75 (ntohs (msg->size) == sizeof (struct GNUNET_MessageHeader))) )
77 if (request_context->peer_cb != NULL)
78 request_context->peer_cb (request_context->cb_cls,
79 NULL, GNUNET_TIME_relative_get_zero(), 0);
80 GNUNET_CLIENT_disconnect (request_context->client, GNUNET_NO);
81 GNUNET_free (request_context);
85 /* Handle incorrect message type or size, disconnect and clean up */
86 if ( (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT) ||
87 (ntohs (msg->size) != sizeof (struct ConnectNotifyMessage)) )
90 if (request_context->peer_cb != NULL)
91 request_context->peer_cb (request_context->cb_cls,
92 NULL, GNUNET_TIME_relative_get_zero(), 0);
93 GNUNET_CLIENT_disconnect (request_context->client, GNUNET_NO);
94 GNUNET_free (request_context);
99 connect_message = (const struct ConnectNotifyMessage *) msg;
100 if (request_context->peer_cb != NULL)
101 request_context->peer_cb (request_context->cb_cls,
102 &connect_message->peer,
103 GNUNET_TIME_relative_ntoh(connect_message->latency),
104 ntohl (connect_message->distance));
106 GNUNET_CLIENT_receive(request_context->client, &receive_info, request_context, GNUNET_TIME_relative_get_forever());
110 * Function called to notify a client about the socket
111 * begin ready to queue more data. "buf" will be
112 * NULL and "size" zero if the socket was closed for
113 * writing in the meantime.
116 * @param size number of bytes available in buf
117 * @param buf where the callee should write the message
118 * @return number of bytes written to buf
121 transmit_request(void *cls,
122 size_t size, void *buf)
124 struct GNUNET_MessageHeader *msg;
125 if ((size < sizeof(struct GNUNET_MessageHeader)) || (buf == NULL))
128 msg = (struct GNUNET_MessageHeader *)buf;
129 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
130 msg->type = htons (GNUNET_MESSAGE_TYPE_CORE_ITERATE_PEERS);
131 return sizeof(struct GNUNET_MessageHeader);
135 * Obtain statistics and/or change preferences for the given peer.
137 * @param sched scheduler to use
138 * @param cfg configuration to use
139 * @param peer_cb function to call with the peer information
140 * @param cb_cls closure for peer_cb
141 * @return GNUNET_OK if iterating, GNUNET_SYSERR on error
144 GNUNET_CORE_iterate_peers (struct GNUNET_SCHEDULER_Handle *sched,
145 const struct GNUNET_CONFIGURATION_Handle *cfg,
146 GNUNET_CORE_ConnectEventHandler peer_cb,
149 struct GNUNET_CORE_RequestContext *request_context;
150 struct GNUNET_CLIENT_Connection *client;
152 client = GNUNET_CLIENT_connect (sched, "core", cfg);
154 return GNUNET_SYSERR;
155 request_context = GNUNET_malloc (sizeof (struct GNUNET_CORE_RequestContext));
156 request_context->client = client;
157 request_context->peer_cb = peer_cb;
158 request_context->cb_cls = cb_cls;
160 /*GNUNET_assert (GNUNET_OK == GNUNET_CLIENT_transmit_and_get_response (client,
162 GNUNET_TIME_relative_get_forever(),
166 request_context->th = GNUNET_CLIENT_notify_transmit_ready(client, sizeof(struct GNUNET_MessageHeader), GNUNET_TIME_relative_get_forever(), GNUNET_YES, &transmit_request, NULL);
167 GNUNET_CLIENT_receive(client, &receive_info, request_context, GNUNET_TIME_relative_get_forever());
171 /* end of core_api_iterate_peers.c */