aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/mesh/mesh.h17
-rw-r--r--src/mesh/mesh_api_new.c93
2 files changed, 79 insertions, 31 deletions
diff --git a/src/mesh/mesh.h b/src/mesh/mesh.h
index 4e5f6e802..874e29a91 100644
--- a/src/mesh/mesh.h
+++ b/src/mesh/mesh.h
@@ -146,13 +146,6 @@ struct GNUNET_MESH_PeerControl
146 MESH_TunnelNumber tunnel_id GNUNET_PACKED; 146 MESH_TunnelNumber tunnel_id GNUNET_PACKED;
147 147
148 /** 148 /**
149 * When should this request time out (the service abort trying to find
150 * a matching peer)? The request should naturally also be aborted
151 * if the corresponding client disconnects.
152 */
153 struct GNUNET_TIME_AbsoluteNBO timeout;
154
155 /**
156 * Peer to connect/disconnect. 149 * Peer to connect/disconnect.
157 */ 150 */
158 struct GNUNET_PeerIdentity peer; 151 struct GNUNET_PeerIdentity peer;
@@ -165,7 +158,8 @@ struct GNUNET_MESH_PeerControl
165struct GNUNET_MESH_ConnectPeerByType 158struct GNUNET_MESH_ConnectPeerByType
166{ 159{
167 /** 160 /**
168 * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_BY_TYPE 161 * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_BY_TYPE |
162 * GNUNET_MESSAGE_TYPE_MESH_LOCAL_DISCONNECT_PEER_BY_TYPE
169 */ 163 */
170 struct GNUNET_MessageHeader header; 164 struct GNUNET_MessageHeader header;
171 165
@@ -175,13 +169,6 @@ struct GNUNET_MESH_ConnectPeerByType
175 MESH_TunnelNumber tunnel_id GNUNET_PACKED; 169 MESH_TunnelNumber tunnel_id GNUNET_PACKED;
176 170
177 /** 171 /**
178 * When should this request time out (the service abort trying to find
179 * a matching peer)? The request should naturally also be aborted
180 * if the corresponding client disconnects.
181 */
182 struct GNUNET_TIME_AbsoluteNBO timeout;
183
184 /**
185 * Type specification 172 * Type specification
186 */ 173 */
187 GNUNET_MESH_ApplicationType type GNUNET_PACKED; 174 GNUNET_MESH_ApplicationType type GNUNET_PACKED;
diff --git a/src/mesh/mesh_api_new.c b/src/mesh/mesh_api_new.c
index cd385b01d..5c11ab665 100644
--- a/src/mesh/mesh_api_new.c
+++ b/src/mesh/mesh_api_new.c
@@ -40,10 +40,8 @@ extern "C"
40#if 0 /* keep Emacsens' auto-indent happy */ 40#if 0 /* keep Emacsens' auto-indent happy */
41} 41}
42#endif 42#endif
43 /* 43#endif
44 */ 44
45#endif /*
46 */
47#include "platform.h" 45#include "platform.h"
48#include "gnunet_common.h" 46#include "gnunet_common.h"
49#include "gnunet_client_lib.h" 47#include "gnunet_client_lib.h"
@@ -204,6 +202,33 @@ struct GNUNET_MESH_Handle
204 202
205 203
206/** 204/**
205 * Description of a peer
206 */
207struct GNUNET_MESH_Peer
208{
209 /**
210 * ID of the peer in short form
211 */
212 GNUNET_PEER_Id id;
213
214 /**
215 * Tunnel this peer belongs to
216 */
217 struct GNUNET_MESH_Tunnel *t;
218
219 /**
220 * Flag indicating whether service has informed about its connection
221 */
222 int connected;
223
224 /**
225 * Task to cancel the connection request for this peer
226 */
227 GNUNET_SCHEDULER_TaskIdentifier cancel;
228};
229
230
231/**
207 * Opaque handle to a tunnel. 232 * Opaque handle to a tunnel.
208 */ 233 */
209struct GNUNET_MESH_Tunnel 234struct GNUNET_MESH_Tunnel
@@ -228,7 +253,7 @@ struct GNUNET_MESH_Tunnel
228 /** 253 /**
229 * All peers added to the tunnel 254 * All peers added to the tunnel
230 */ 255 */
231 GNUNET_PEER_Id *peers; 256 struct GNUNET_MESH_Peer **peers;
232 257
233 /** 258 /**
234 * Closure for the connect/disconnect handlers 259 * Closure for the connect/disconnect handlers
@@ -314,6 +339,39 @@ timeout_transmission (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
314 339
315 340
316/** 341/**
342 * Notify client that the transmission has timed out
343 * @param cls closure
344 * @param tc task context
345 */
346static void
347timeout_peer_request (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
348{
349 struct GNUNET_MESH_Peer *p = cls;
350 struct GNUNET_PeerIdentity id;
351 unsigned int i;
352
353 GNUNET_assert (0 == p->connected);
354 for (i = 0; i < p->t->npeers; i++)
355 {
356 if (p->t->peers[i] == p)
357 break;
358 }
359 if (i == p->t->npeers)
360 {
361 GNUNET_break (0);
362 return;
363 }
364 p->t->peers[i] = p->t->peers[p->t->npeers - 1];
365 GNUNET_array_grow (p->t->peers, p->t->npeers, p->t->npeers - 1);
366
367 if (NULL != p->t->connect_handler)
368 p->t->connect_handler (p->t->cls, 0, NULL);
369
370 GNUNET_PEER_resolve (p->id, &id);
371 GNUNET_MESH_peer_request_connect_del (p->t, &id);
372}
373
374/**
317 * Add a transmit handle to the transmission queue by priority and set the 375 * Add a transmit handle to the transmission queue by priority and set the
318 * timeout if needed. 376 * timeout if needed.
319 * 377 *
@@ -844,6 +902,8 @@ GNUNET_MESH_tunnel_destroy (struct GNUNET_MESH_Tunnel *tun)
844/** 902/**
845 * Request that a peer should be added to the tunnel. The existing 903 * Request that a peer should be added to the tunnel. The existing
846 * connect handler will be called ONCE with either success or failure. 904 * connect handler will be called ONCE with either success or failure.
905 * This function should NOT be called again with the same peer before the
906 * connect handler is called
847 * 907 *
848 * @param tunnel handle to existing tunnel 908 * @param tunnel handle to existing tunnel
849 * @param timeout how long to try to establish a connection 909 * @param timeout how long to try to establish a connection
@@ -855,29 +915,29 @@ GNUNET_MESH_peer_request_connect_add (struct GNUNET_MESH_Tunnel *tunnel,
855 const struct GNUNET_PeerIdentity *peer) 915 const struct GNUNET_PeerIdentity *peer)
856{ 916{
857 struct GNUNET_MESH_PeerControl msg; 917 struct GNUNET_MESH_PeerControl msg;
918 struct GNUNET_MESH_Peer *p;
858 GNUNET_PEER_Id peer_id; 919 GNUNET_PEER_Id peer_id;
859 unsigned int i; 920 unsigned int i;
860 921
861 peer_id = GNUNET_PEER_intern (peer); 922 peer_id = GNUNET_PEER_intern (peer);
862 for (i = 0; i < tunnel->npeers; i++) 923 for (i = 0; i < tunnel->npeers; i++)
863 { 924 {
864 if (tunnel->peers[i] == peer_id) 925 if (tunnel->peers[i]->id == peer_id)
865 { 926 {
866 GNUNET_PEER_change_rc (peer_id, -1); 927 GNUNET_PEER_change_rc (peer_id, -1);
867 /* FIXME: peer was already in the tunnel */ 928 /* FIXME: peer was already in the tunnel */
868 return; 929 return;
869 } 930 }
870 } 931 }
871 tunnel->npeers++; 932 p = GNUNET_malloc (sizeof (struct GNUNET_MESH_Peer));
872 tunnel->peers = 933 p->id = peer_id;
873 GNUNET_realloc (tunnel->peers, tunnel->npeers * sizeof (GNUNET_PEER_Id)); 934 p->t = tunnel;
874 tunnel->peers[tunnel->npeers - 1] = peer_id; 935 p->cancel = GNUNET_SCHEDULER_add_delayed (timeout, &timeout_peer_request, p);
936 GNUNET_array_append (tunnel->peers, tunnel->npeers, p);
875 937
876 msg.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl)); 938 msg.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
877 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_ADD); 939 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_ADD);
878 msg.tunnel_id = htonl (tunnel->tid); 940 msg.tunnel_id = htonl (tunnel->tid);
879 msg.timeout =
880 GNUNET_TIME_absolute_hton (GNUNET_TIME_relative_to_absolute (timeout));
881 msg.peer = *peer; 941 msg.peer = *peer;
882 send_packet (tunnel->mesh, &msg.header); 942 send_packet (tunnel->mesh, &msg.header);
883// tunnel->connect_handler (tunnel->cls, peer, NULL); FIXME call this later 943// tunnel->connect_handler (tunnel->cls, peer, NULL); FIXME call this later
@@ -908,20 +968,23 @@ GNUNET_MESH_peer_request_connect_del (struct GNUNET_MESH_Tunnel *tunnel,
908 return; 968 return;
909 } 969 }
910 for (i = 0; i < tunnel->npeers; i++) 970 for (i = 0; i < tunnel->npeers; i++)
911 if (tunnel->peers[i] == peer_id) 971 if (tunnel->peers[i]->id == peer_id)
912 break; 972 break;
913 if (i == tunnel->npeers) 973 if (i == tunnel->npeers)
914 { 974 {
915 GNUNET_break (0); 975 GNUNET_break (0);
916 return; 976 return;
917 } 977 }
978 if (NULL != tunnel->disconnect_handler && tunnel->peers[i]->connected == 1)
979 tunnel->disconnect_handler (tunnel->cls, peer);
918 GNUNET_PEER_change_rc (peer_id, -1); 980 GNUNET_PEER_change_rc (peer_id, -1);
981 GNUNET_free (tunnel->peers[i]);
919 tunnel->peers[i] = tunnel->peers[tunnel->npeers - 1]; 982 tunnel->peers[i] = tunnel->peers[tunnel->npeers - 1];
920 GNUNET_array_grow (tunnel->peers, tunnel->npeers, tunnel->npeers - 1); 983 GNUNET_array_grow (tunnel->peers, tunnel->npeers, tunnel->npeers - 1);
984
921 msg.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl)); 985 msg.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
922 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_DEL); 986 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_DEL);
923 msg.tunnel_id = htonl (tunnel->tid); 987 msg.tunnel_id = htonl (tunnel->tid);
924 msg.timeout = GNUNET_TIME_absolute_hton (GNUNET_TIME_UNIT_FOREVER_ABS);
925 memcpy (&msg.peer, peer, sizeof (struct GNUNET_PeerIdentity)); 988 memcpy (&msg.peer, peer, sizeof (struct GNUNET_PeerIdentity));
926 send_packet (tunnel->mesh, &msg.header); 989 send_packet (tunnel->mesh, &msg.header);
927} 990}
@@ -947,8 +1010,6 @@ GNUNET_MESH_peer_request_connect_by_type (struct GNUNET_MESH_Tunnel *tunnel,
947 msg.header.size = htons (sizeof (struct GNUNET_MESH_ConnectPeerByType)); 1010 msg.header.size = htons (sizeof (struct GNUNET_MESH_ConnectPeerByType));
948 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_BY_TYPE); 1011 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_BY_TYPE);
949 msg.tunnel_id = htonl (tunnel->tid); 1012 msg.tunnel_id = htonl (tunnel->tid);
950 msg.timeout =
951 GNUNET_TIME_absolute_hton (GNUNET_TIME_relative_to_absolute (timeout));
952 msg.type = htonl (app_type); 1013 msg.type = htonl (app_type);
953 send_packet (tunnel->mesh, &msg.header); 1014 send_packet (tunnel->mesh, &msg.header);
954} 1015}