diff options
author | Bart Polot <bart@net.in.tum.de> | 2013-10-08 10:30:26 +0000 |
---|---|---|
committer | Bart Polot <bart@net.in.tum.de> | 2013-10-08 10:30:26 +0000 |
commit | d83640d04d57f6388b37d19a814eb6770ea4c485 (patch) | |
tree | 09bcf5d5702aced1c504031dbfbaf787f84a0890 /src/mesh/gnunet-service-mesh_peer.c | |
parent | 45072973ee81ffa01a2fc3450a5a7dcbf57c0464 (diff) | |
download | gnunet-d83640d04d57f6388b37d19a814eb6770ea4c485.tar.gz gnunet-d83640d04d57f6388b37d19a814eb6770ea4c485.zip |
- move all core/PeerIdentity interacting code to _peer, remove GNUNET_PeerIdentity from _connection
Diffstat (limited to 'src/mesh/gnunet-service-mesh_peer.c')
-rw-r--r-- | src/mesh/gnunet-service-mesh_peer.c | 175 |
1 files changed, 174 insertions, 1 deletions
diff --git a/src/mesh/gnunet-service-mesh_peer.c b/src/mesh/gnunet-service-mesh_peer.c index 43c6259e3..0de74bb12 100644 --- a/src/mesh/gnunet-service-mesh_peer.c +++ b/src/mesh/gnunet-service-mesh_peer.c | |||
@@ -22,9 +22,12 @@ | |||
22 | #include "platform.h" | 22 | #include "platform.h" |
23 | #include "gnunet_util_lib.h" | 23 | #include "gnunet_util_lib.h" |
24 | 24 | ||
25 | #include "gnunet_core_service.h" | ||
26 | |||
25 | #include "gnunet-service-mesh_peer.h" | 27 | #include "gnunet-service-mesh_peer.h" |
26 | #include "gnunet-service-mesh_dht.h" | 28 | #include "gnunet-service-mesh_dht.h" |
27 | #include "gnunet-service-mesh_connection.h" | 29 | #include "gnunet-service-mesh_connection.h" |
30 | #include "gnunet-service-mesh_local.h" | ||
28 | #include "mesh_path.h" | 31 | #include "mesh_path.h" |
29 | 32 | ||
30 | /******************************************************************************/ | 33 | /******************************************************************************/ |
@@ -112,12 +115,162 @@ static unsigned long long max_peers; | |||
112 | */ | 115 | */ |
113 | static unsigned long long drop_percent; | 116 | static unsigned long long drop_percent; |
114 | 117 | ||
118 | /** | ||
119 | * Handle to communicate with core. | ||
120 | */ | ||
121 | static struct GNUNET_CORE_Handle *core_handle; | ||
122 | |||
123 | /** | ||
124 | * Local peer own ID (full value). | ||
125 | */ | ||
126 | const static struct GNUNET_PeerIdentity *my_full_id; | ||
127 | |||
115 | /******************************************************************************/ | 128 | /******************************************************************************/ |
116 | /***************************** CORE CALLBACKS *********************************/ | 129 | /***************************** CORE CALLBACKS *********************************/ |
117 | /******************************************************************************/ | 130 | /******************************************************************************/ |
118 | 131 | ||
119 | 132 | ||
120 | /** | 133 | /** |
134 | * Iterator to notify all connections of a broken link. Mark connections | ||
135 | * to destroy after all traffic has been sent. | ||
136 | * | ||
137 | * @param cls Closure (peer disconnected). | ||
138 | * @param key Current key code (peer id). | ||
139 | * @param value Value in the hash map (connection). | ||
140 | * | ||
141 | * @return GNUNET_YES if we should continue to iterate, | ||
142 | * GNUNET_NO if not. | ||
143 | */ | ||
144 | static int | ||
145 | notify_broken (void *cls, | ||
146 | const struct GNUNET_HashCode *key, | ||
147 | void *value) | ||
148 | { | ||
149 | struct MeshPeer *peer = cls; | ||
150 | struct MeshConnection *c = value; | ||
151 | |||
152 | GMC_notify_broken (c, peer, my_full_id); | ||
153 | |||
154 | return GNUNET_YES; | ||
155 | } | ||
156 | |||
157 | /** | ||
158 | * Method called whenever a given peer connects. | ||
159 | * | ||
160 | * @param cls closure | ||
161 | * @param peer peer identity this notification is about | ||
162 | */ | ||
163 | static void | ||
164 | core_connect (void *cls, const struct GNUNET_PeerIdentity *peer) | ||
165 | { | ||
166 | struct MeshPeer *pi; | ||
167 | struct MeshPeerPath *path; | ||
168 | |||
169 | LOG ("Peer connected\n"); | ||
170 | LOG (" %s\n", GNUNET_i2s (&my_full_id)); | ||
171 | pi = peer_get (peer); | ||
172 | if (myid == pi->id) | ||
173 | { | ||
174 | DEBUG_CONN (" (self)\n"); | ||
175 | path = path_new (1); | ||
176 | } | ||
177 | else | ||
178 | { | ||
179 | DEBUG_CONN (" %s\n", GNUNET_i2s (peer)); | ||
180 | path = path_new (2); | ||
181 | path->peers[1] = pi->id; | ||
182 | GNUNET_PEER_change_rc (pi->id, 1); | ||
183 | GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO); | ||
184 | } | ||
185 | path->peers[0] = myid; | ||
186 | GNUNET_PEER_change_rc (myid, 1); | ||
187 | peer_add_path (pi, path, GNUNET_YES); | ||
188 | |||
189 | pi->connections = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_YES); | ||
190 | return; | ||
191 | } | ||
192 | |||
193 | |||
194 | /** | ||
195 | * Method called whenever a peer disconnects. | ||
196 | * | ||
197 | * @param cls closure | ||
198 | * @param peer peer identity this notification is about | ||
199 | */ | ||
200 | static void | ||
201 | core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer) | ||
202 | { | ||
203 | struct MeshPeer *pi; | ||
204 | |||
205 | DEBUG_CONN ("Peer disconnected\n"); | ||
206 | pi = GNUNET_CONTAINER_multipeermap_get (peers, peer); | ||
207 | if (NULL == pi) | ||
208 | { | ||
209 | GNUNET_break (0); | ||
210 | return; | ||
211 | } | ||
212 | |||
213 | GNUNET_CONTAINER_multihashmap_iterate (pi->connections, ¬ify_broken, pi); | ||
214 | GNUNET_CONTAINER_multihashmap_destroy (pi->connections); | ||
215 | pi->connections = NULL; | ||
216 | if (NULL != pi->core_transmit) | ||
217 | { | ||
218 | GNUNET_CORE_notify_transmit_ready_cancel (pi->core_transmit); | ||
219 | pi->core_transmit = NULL; | ||
220 | } | ||
221 | if (myid == pi->id) | ||
222 | { | ||
223 | DEBUG_CONN (" (self)\n"); | ||
224 | } | ||
225 | GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO); | ||
226 | |||
227 | return; | ||
228 | } | ||
229 | |||
230 | |||
231 | |||
232 | /** | ||
233 | * To be called on core init/fail. | ||
234 | * | ||
235 | * @param cls Closure (config) | ||
236 | * @param identity the public identity of this peer | ||
237 | */ | ||
238 | static void | ||
239 | core_init (void *cls, | ||
240 | const struct GNUNET_PeerIdentity *identity) | ||
241 | { | ||
242 | const struct GNUNET_CONFIGURATION_Handle *c = cls; | ||
243 | static int i = 0; | ||
244 | |||
245 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n"); | ||
246 | if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id))) | ||
247 | { | ||
248 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n")); | ||
249 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
250 | " core id %s\n", | ||
251 | GNUNET_i2s (identity)); | ||
252 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
253 | " my id %s\n", | ||
254 | GNUNET_i2s (&my_full_id)); | ||
255 | GNUNET_CORE_disconnect (core_handle); | ||
256 | core_handle = GNUNET_CORE_connect (c, /* Main configuration */ | ||
257 | NULL, /* Closure passed to MESH functions */ | ||
258 | &core_init, /* Call core_init once connected */ | ||
259 | &core_connect, /* Handle connects */ | ||
260 | &core_disconnect, /* remove peers on disconnects */ | ||
261 | NULL, /* Don't notify about all incoming messages */ | ||
262 | GNUNET_NO, /* For header only in notification */ | ||
263 | NULL, /* Don't notify about all outbound messages */ | ||
264 | GNUNET_NO, /* For header-only out notification */ | ||
265 | core_handlers); /* Register these handlers */ | ||
266 | if (10 < i++) | ||
267 | GNUNET_abort(); | ||
268 | } | ||
269 | GML_start (); | ||
270 | return; | ||
271 | } | ||
272 | |||
273 | /** | ||
121 | * Core callback to write a pre-constructed data packet to core buffer | 274 | * Core callback to write a pre-constructed data packet to core buffer |
122 | * | 275 | * |
123 | * @param cls Closure (MeshTransmissionDescriptor with data in "data" member). | 276 | * @param cls Closure (MeshTransmissionDescriptor with data in "data" member). |
@@ -1145,10 +1298,13 @@ GMP_queue_add (void *cls, uint16_t type, size_t size, | |||
1145 | * Initialize the peer subsystem. | 1298 | * Initialize the peer subsystem. |
1146 | * | 1299 | * |
1147 | * @param c Configuration. | 1300 | * @param c Configuration. |
1301 | * @param id Peer identity | ||
1148 | */ | 1302 | */ |
1149 | void | 1303 | void |
1150 | GMP_init (const struct GNUNET_CONFIGURATION_Handle *c) | 1304 | GMP_init (const struct GNUNET_CONFIGURATION_Handle *c, |
1305 | const struct GNUNET_PeerIdentity *id) | ||
1151 | { | 1306 | { |
1307 | my_full_id = id; | ||
1152 | peers = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO); | 1308 | peers = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO); |
1153 | if (GNUNET_OK != | 1309 | if (GNUNET_OK != |
1154 | GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS", | 1310 | GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS", |
@@ -1174,6 +1330,23 @@ GMP_init (const struct GNUNET_CONFIGURATION_Handle *c) | |||
1174 | "Remove the DROP_PERCENT option from your configuration.\n" | 1330 | "Remove the DROP_PERCENT option from your configuration.\n" |
1175 | "***************************************\n"); | 1331 | "***************************************\n"); |
1176 | } | 1332 | } |
1333 | |||
1334 | core_handle = GNUNET_CORE_connect (c, /* Main configuration */ | ||
1335 | NULL, /* Closure passed to MESH functions */ | ||
1336 | &core_init, /* Call core_init once connected */ | ||
1337 | &core_connect, /* Handle connects */ | ||
1338 | &core_disconnect, /* remove peers on disconnects */ | ||
1339 | NULL, /* Don't notify about all incoming messages */ | ||
1340 | GNUNET_NO, /* For header only in notification */ | ||
1341 | NULL, /* Don't notify about all outbound messages */ | ||
1342 | GNUNET_NO, /* For header-only out notification */ | ||
1343 | core_handlers); /* Register these handlers */ | ||
1344 | if (NULL == core_handle) | ||
1345 | { | ||
1346 | GNUNET_break (0); | ||
1347 | GNUNET_SCHEDULER_shutdown (); | ||
1348 | return; | ||
1349 | } | ||
1177 | } | 1350 | } |
1178 | 1351 | ||
1179 | /** | 1352 | /** |