aboutsummaryrefslogtreecommitdiff
path: root/src/core/core_api_iterate_peers.c
diff options
context:
space:
mode:
authorNathan S. Evans <evans@in.tum.de>2010-11-16 15:21:20 +0000
committerNathan S. Evans <evans@in.tum.de>2010-11-16 15:21:20 +0000
commit03b104dab570998b1f791994a8134d35b4b4dc0a (patch)
tree98851c89adfbdeef8e0f1a3eff3d1854160ebd48 /src/core/core_api_iterate_peers.c
parent7f8e018e0b12b0ecc1a6021020af9e5f84846b02 (diff)
downloadgnunet-03b104dab570998b1f791994a8134d35b4b4dc0a.tar.gz
gnunet-03b104dab570998b1f791994a8134d35b4b4dc0a.zip
Testing changes required to work with new core api. Revert GNUNET_CORE_iterate_peers call to work properly.
Diffstat (limited to 'src/core/core_api_iterate_peers.c')
-rw-r--r--src/core/core_api_iterate_peers.c169
1 files changed, 169 insertions, 0 deletions
diff --git a/src/core/core_api_iterate_peers.c b/src/core/core_api_iterate_peers.c
new file mode 100644
index 000000000..657768363
--- /dev/null
+++ b/src/core/core_api_iterate_peers.c
@@ -0,0 +1,169 @@
1/*
2 This file is part of GNUnet.
3 (C) 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 core/core_api_iterate_peers.c
23 * @brief implementation of the peer_iterate function
24 * @author Christian Grothoff
25 * @author Nathan Evans
26 */
27#include "platform.h"
28#include "gnunet_core_service.h"
29#include "core.h"
30
31
32struct GNUNET_CORE_RequestContext
33{
34
35 /**
36 * Our connection to the service.
37 */
38 struct GNUNET_CLIENT_Connection *client;
39
40 /**
41 * Handle for transmitting a request.
42 */
43 struct GNUNET_CLIENT_TransmitHandle *th;
44
45 /**
46 * Function called with the peer.
47 */
48 GNUNET_CORE_ConnectEventHandler peer_cb;
49
50 /**
51 * Closure for peer_cb.
52 */
53 void *cb_cls;
54
55};
56
57
58/**
59 * Receive reply from core service with information about a peer.
60 *
61 * @param cls our 'struct GNUNET_CORE_RequestContext *'
62 * @param msg NULL on error or last entry
63 */
64static void
65receive_info (void *cls,
66 const struct GNUNET_MessageHeader *msg)
67{
68 struct GNUNET_CORE_RequestContext *request_context = cls;
69 const struct ConnectNotifyMessage *connect_message;
70
71
72 /* Handle last message or error case, disconnect and clean up */
73 if ( (msg == NULL) ||
74 ((ntohs (msg->type) == GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT) &&
75 (ntohs (msg->size) == sizeof (struct GNUNET_MessageHeader))) )
76 {
77 if (request_context->peer_cb != NULL)
78 request_context->peer_cb (request_context->cb_cls,
79 NULL, NULL);
80 GNUNET_CLIENT_disconnect (request_context->client, GNUNET_NO);
81 GNUNET_free (request_context);
82 return;
83 }
84
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)) )
88 {
89 GNUNET_break (0);
90 if (request_context->peer_cb != NULL)
91 request_context->peer_cb (request_context->cb_cls,
92 NULL, NULL);
93 GNUNET_CLIENT_disconnect (request_context->client, GNUNET_NO);
94 GNUNET_free (request_context);
95 return;
96 }
97
98 /* Normal case */
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 NULL);
104
105 GNUNET_CLIENT_receive(request_context->client, &receive_info, request_context, GNUNET_TIME_relative_get_forever());
106}
107
108/**
109 * Function called to notify a client about the socket
110 * begin ready to queue more data. "buf" will be
111 * NULL and "size" zero if the socket was closed for
112 * writing in the meantime.
113 *
114 * @param cls closure
115 * @param size number of bytes available in buf
116 * @param buf where the callee should write the message
117 * @return number of bytes written to buf
118 */
119static size_t
120transmit_request(void *cls,
121 size_t size, void *buf)
122{
123 struct GNUNET_MessageHeader *msg;
124 if ((size < sizeof(struct GNUNET_MessageHeader)) || (buf == NULL))
125 return 0;
126
127 msg = (struct GNUNET_MessageHeader *)buf;
128 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
129 msg->type = htons (GNUNET_MESSAGE_TYPE_CORE_ITERATE_PEERS);
130 return sizeof(struct GNUNET_MessageHeader);
131}
132
133/**
134 * Obtain statistics and/or change preferences for the given peer.
135 *
136 * @param sched scheduler to use
137 * @param cfg configuration to use
138 * @param peer_cb function to call with the peer information
139 * @param cb_cls closure for peer_cb
140 * @return GNUNET_OK if iterating, GNUNET_SYSERR on error
141 */
142int
143GNUNET_CORE_iterate_peers (const struct GNUNET_CONFIGURATION_Handle *cfg,
144 GNUNET_CORE_ConnectEventHandler peer_cb,
145 void *cb_cls)
146{
147 struct GNUNET_CORE_RequestContext *request_context;
148 struct GNUNET_CLIENT_Connection *client;
149
150 client = GNUNET_CLIENT_connect ("core", cfg);
151 if (client == NULL)
152 return GNUNET_SYSERR;
153 request_context = GNUNET_malloc (sizeof (struct GNUNET_CORE_RequestContext));
154 request_context->client = client;
155 request_context->peer_cb = peer_cb;
156 request_context->cb_cls = cb_cls;
157
158 request_context->th = GNUNET_CLIENT_notify_transmit_ready(client,
159 sizeof(struct GNUNET_MessageHeader),
160 GNUNET_TIME_relative_get_forever(),
161 GNUNET_YES,
162 &transmit_request,
163 NULL);
164
165 GNUNET_CLIENT_receive(client, &receive_info, request_context, GNUNET_TIME_relative_get_forever());
166 return GNUNET_OK;
167}
168
169/* end of core_api_iterate_peers.c */