aboutsummaryrefslogtreecommitdiff
path: root/src/core
diff options
context:
space:
mode:
authorNathan S. Evans <evans@in.tum.de>2011-02-16 16:24:32 +0000
committerNathan S. Evans <evans@in.tum.de>2011-02-16 16:24:32 +0000
commit27a6cee72d7c479853acbeed0047534fd4566deb (patch)
treed932da0e35e14abb83480b508874fcc9d0efdab2 /src/core
parente054285d845aad88df678b60ee532d71d5115f4b (diff)
downloadgnunet-27a6cee72d7c479853acbeed0047534fd4566deb.tar.gz
gnunet-27a6cee72d7c479853acbeed0047534fd4566deb.zip
remove double connect notify, add is connected optimization for peer iterate call
Diffstat (limited to 'src/core')
-rw-r--r--src/core/core_api.c16
-rw-r--r--src/core/core_api_iterate_peers.c69
-rw-r--r--src/core/gnunet-service-core.c38
3 files changed, 98 insertions, 25 deletions
diff --git a/src/core/core_api.c b/src/core/core_api.c
index 57bf64ad7..e9a38271b 100644
--- a/src/core/core_api.c
+++ b/src/core/core_api.c
@@ -1730,25 +1730,11 @@ GNUNET_CORE_peer_request_connect (struct GNUNET_CORE_Handle *h,
1730 struct GNUNET_CORE_PeerRequestHandle *ret; 1730 struct GNUNET_CORE_PeerRequestHandle *ret;
1731 struct ControlMessage *cm; 1731 struct ControlMessage *cm;
1732 struct ConnectMessage *msg; 1732 struct ConnectMessage *msg;
1733 struct PeerRecord *pr;
1734 static struct GNUNET_TRANSPORT_ATS_Information distance[2];
1735 1733
1736 if (NULL != GNUNET_CONTAINER_multihashmap_get (h->peers, 1734 if (NULL != GNUNET_CONTAINER_multihashmap_get (h->peers,
1737 &peer->hashPubKey)) 1735 &peer->hashPubKey))
1738 { 1736 return NULL; /* Already connected, means callback should have happened already! */
1739 pr = GNUNET_CONTAINER_multihashmap_get(h->peers, &peer->hashPubKey);
1740 GNUNET_assert(pr != NULL);
1741 distance[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
1742 distance[0].value = htonl (1);
1743 distance[1].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
1744 distance[1].value = htonl (0);
1745 1737
1746 if (NULL != h->connects)
1747 h->connects (h->cls,
1748 &pr->peer,
1749 &distance[0]);
1750 return NULL;
1751 }
1752 1738
1753 cm = GNUNET_malloc (sizeof (struct ControlMessage) + 1739 cm = GNUNET_malloc (sizeof (struct ControlMessage) +
1754 sizeof (struct ConnectMessage)); 1740 sizeof (struct ConnectMessage));
diff --git a/src/core/core_api_iterate_peers.c b/src/core/core_api_iterate_peers.c
index fb9c01fee..8175b70c6 100644
--- a/src/core/core_api_iterate_peers.c
+++ b/src/core/core_api_iterate_peers.c
@@ -31,7 +31,6 @@
31 31
32struct GNUNET_CORE_RequestContext 32struct GNUNET_CORE_RequestContext
33{ 33{
34
35 /** 34 /**
36 * Our connection to the service. 35 * Our connection to the service.
37 */ 36 */
@@ -48,6 +47,11 @@ struct GNUNET_CORE_RequestContext
48 GNUNET_CORE_ConnectEventHandler peer_cb; 47 GNUNET_CORE_ConnectEventHandler peer_cb;
49 48
50 /** 49 /**
50 * Peer to check for.
51 */
52 struct GNUNET_PeerIdentity *peer;
53
54 /**
51 * Closure for peer_cb. 55 * Closure for peer_cb.
52 */ 56 */
53 void *cb_cls; 57 void *cb_cls;
@@ -136,21 +140,78 @@ transmit_request(void *cls,
136 size_t size, void *buf) 140 size_t size, void *buf)
137{ 141{
138 struct GNUNET_MessageHeader *msg; 142 struct GNUNET_MessageHeader *msg;
139 if ((size < sizeof(struct GNUNET_MessageHeader)) || (buf == NULL)) 143 struct GNUNET_PeerIdentity *peer = cls;
144 int msize;
145
146 if (peer == NULL)
147 msize = sizeof(struct GNUNET_MessageHeader);
148 else
149 msize = sizeof(struct GNUNET_MessageHeader) + sizeof(struct GNUNET_PeerIdentity);
150
151 if ((size < msize) || (buf == NULL))
140 return 0; 152 return 0;
141 153
142 msg = (struct GNUNET_MessageHeader *)buf; 154 msg = (struct GNUNET_MessageHeader *)buf;
143 msg->size = htons (sizeof (struct GNUNET_MessageHeader)); 155 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
144 msg->type = htons (GNUNET_MESSAGE_TYPE_CORE_ITERATE_PEERS); 156 msg->type = htons (GNUNET_MESSAGE_TYPE_CORE_ITERATE_PEERS);
145 return sizeof(struct GNUNET_MessageHeader); 157 memcpy(&msg[1], peer, sizeof(struct GNUNET_PeerIdentity));
158
159 return msize;
146} 160}
147 161
148/** 162/**
149 * Obtain statistics and/or change preferences for the given peer. 163 * Iterate over all currently connected peers.
164 * Calls peer_cb with each connected peer, and then
165 * once with NULL to indicate that all peers have
166 * been handled.
150 * 167 *
151 * @param cfg configuration to use 168 * @param cfg configuration to use
169 * @param peer the specific peer to check for
152 * @param peer_cb function to call with the peer information 170 * @param peer_cb function to call with the peer information
153 * @param cb_cls closure for peer_cb 171 * @param cb_cls closure for peer_cb
172 *
173 * @return GNUNET_OK if iterating, GNUNET_SYSERR on error
174 */
175int
176GNUNET_CORE_is_peer_connected (const struct GNUNET_CONFIGURATION_Handle *cfg,
177 struct GNUNET_PeerIdentity *peer,
178 GNUNET_CORE_ConnectEventHandler peer_cb,
179 void *cb_cls)
180{
181 struct GNUNET_CORE_RequestContext *request_context;
182 struct GNUNET_CLIENT_Connection *client;
183
184 client = GNUNET_CLIENT_connect ("core", cfg);
185 if (client == NULL)
186 return GNUNET_SYSERR;
187 GNUNET_assert(peer != NULL);
188 request_context = GNUNET_malloc (sizeof (struct GNUNET_CORE_RequestContext));
189 request_context->client = client;
190 request_context->peer_cb = peer_cb;
191 request_context->cb_cls = cb_cls;
192 request_context->peer = peer;
193
194 request_context->th = GNUNET_CLIENT_notify_transmit_ready(client,
195 sizeof(struct GNUNET_MessageHeader) + sizeof(struct GNUNET_PeerIdentity),
196 GNUNET_TIME_relative_get_forever(),
197 GNUNET_YES,
198 &transmit_request,
199 peer);
200
201 GNUNET_CLIENT_receive(client, &receive_info, request_context, GNUNET_TIME_relative_get_forever());
202 return GNUNET_OK;
203}
204
205/**
206 * Iterate over all currently connected peers.
207 * Calls peer_cb with each connected peer, and then
208 * once with NULL to indicate that all peers have
209 * been handled.
210 *
211 * @param cfg configuration to use
212 * @param peer_cb function to call with the peer information
213 * @param cb_cls closure for peer_cb
214 *
154 * @return GNUNET_OK if iterating, GNUNET_SYSERR on error 215 * @return GNUNET_OK if iterating, GNUNET_SYSERR on error
155 */ 216 */
156int 217int
diff --git a/src/core/gnunet-service-core.c b/src/core/gnunet-service-core.c
index 4519991b3..81d851265 100644
--- a/src/core/gnunet-service-core.c
+++ b/src/core/gnunet-service-core.c
@@ -1498,10 +1498,22 @@ handle_client_iterate_peers (void *cls,
1498{ 1498{
1499 struct GNUNET_MessageHeader done_msg; 1499 struct GNUNET_MessageHeader done_msg;
1500 struct GNUNET_SERVER_TransmitContext *tc; 1500 struct GNUNET_SERVER_TransmitContext *tc;
1501 1501 struct GNUNET_PeerIdentity *peer;
1502 int msize;
1502 /* notify new client about existing neighbours */ 1503 /* notify new client about existing neighbours */
1504
1505 msize = ntohs(message->size);
1503 tc = GNUNET_SERVER_transmit_context_create (client); 1506 tc = GNUNET_SERVER_transmit_context_create (client);
1504 GNUNET_CONTAINER_multihashmap_iterate (neighbours, &queue_connect_message, tc); 1507 if (msize == sizeof(struct GNUNET_MessageHeader))
1508 GNUNET_CONTAINER_multihashmap_iterate (neighbours, &queue_connect_message, tc);
1509 else if (msize == sizeof(struct GNUNET_MessageHeader) + sizeof(struct GNUNET_PeerIdentity))
1510 {
1511 peer = (struct GNUNET_PeerIdentity *)&message[1];
1512 GNUNET_CONTAINER_multihashmap_get_multiple(neighbours, &peer->hashPubKey, &queue_connect_message, tc);
1513 }
1514 else
1515 GNUNET_break(0);
1516
1505 done_msg.size = htons (sizeof (struct GNUNET_MessageHeader)); 1517 done_msg.size = htons (sizeof (struct GNUNET_MessageHeader));
1506 done_msg.type = htons (GNUNET_MESSAGE_TYPE_CORE_ITERATE_PEERS_END); 1518 done_msg.type = htons (GNUNET_MESSAGE_TYPE_CORE_ITERATE_PEERS_END);
1507 GNUNET_SERVER_transmit_context_append_message (tc, &done_msg); 1519 GNUNET_SERVER_transmit_context_append_message (tc, &done_msg);
@@ -2908,6 +2920,10 @@ notify_transport_connect_done (void *cls,
2908 } 2920 }
2909 if (buf == NULL) 2921 if (buf == NULL)
2910 { 2922 {
2923 GNUNET_STATISTICS_update (stats,
2924 gettext_noop ("# connection requests timed out in transport"),
2925 1,
2926 GNUNET_NO);
2911 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 2927 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2912 _("Failed to connect to `%4s': transport failed to connect\n"), 2928 _("Failed to connect to `%4s': transport failed to connect\n"),
2913 GNUNET_i2s (&n->peer)); 2929 GNUNET_i2s (&n->peer));
@@ -2966,10 +2982,20 @@ handle_client_request_connect (void *cls,
2966 1, 2982 1,
2967 GNUNET_NO); 2983 GNUNET_NO);
2968 else 2984 else
2969 GNUNET_STATISTICS_update (stats, 2985 {
2970 gettext_noop ("# connection requests ignored (already trying)"), 2986 GNUNET_TRANSPORT_notify_transmit_ready_cancel(n->th);
2971 1, 2987 n->th = GNUNET_TRANSPORT_notify_transmit_ready (transport,
2972 GNUNET_NO); 2988 &cm->peer,
2989 sizeof (struct GNUNET_MessageHeader), 0,
2990 timeout,
2991 &notify_transport_connect_done,
2992 n);
2993 GNUNET_break (NULL != n->th);
2994 GNUNET_STATISTICS_update (stats,
2995 gettext_noop ("# connection requests retried (due to repeat request connect)"),
2996 1,
2997 GNUNET_NO);
2998 }
2973 return; /* already connected, or at least trying */ 2999 return; /* already connected, or at least trying */
2974 } 3000 }
2975 GNUNET_STATISTICS_update (stats, 3001 GNUNET_STATISTICS_update (stats,