@@ -117,11 +117,15 @@ impl RoutingHandler {
117117 Ok ( ( ) )
118118 }
119119
120+
121+ /// Tries to remove the neighbor from the neighbors map and network view
120122 pub fn remove_neighbor ( & mut self , node_id : NodeId ) {
121123 let _ = self . neighbors . remove ( & node_id) ;
122124 let _ = self . network_view . remove_node ( node_id) ;
123125 }
124126
127+
128+ /// Adds a new neighbor to the neighbors map and updates the network view
125129 pub fn add_neighbor ( & mut self , node_id : NodeId , sender : Sender < Packet > ) {
126130 let _ = self . neighbors . insert ( node_id, sender) ;
127131 let _ = self . network_view . update_node ( self . id , vec ! [ node_id] ) ;
@@ -209,18 +213,21 @@ impl RoutingHandler {
209213 Ok ( ( ) )
210214 }
211215
212- pub fn handle_nack ( & mut self , nack : Nack , sender_id : NodeId ) -> Result < ( ) , NetworkError > {
216+ pub fn handle_nack ( & mut self , nack : Nack , session_id : u64 , source_id : NodeId ) -> Result < ( ) , NetworkError > {
213217 match nack. nack_type {
214218 NackType :: ErrorInRouting ( id) => {
215- self . network_view . remove_node ( id) ;
216-
219+ self . remove_neighbor ( id) ;
217220 self . start_flood ( ) ?;
221+ if let Some ( packet) = self . buffer . get_fragment_by_id ( session_id, nack. fragment_index , source_id) {
222+ self . try_send ( packet) ?;
223+ }
224+
218225 } ,
219226 NackType :: Dropped => {
220- self . network_view . remove_node ( sender_id ) ;
227+ self . network_view . remove_node ( source_id ) ;
221228 self . start_flood ( ) ?;
222229 } ,
223- NackType :: DestinationIsDrone => self . network_view . change_node_type ( sender_id , NodeType :: Drone ) ,
230+ NackType :: DestinationIsDrone => self . network_view . change_node_type ( source_id , NodeType :: Drone ) ,
224231 _ => { }
225232 }
226233
@@ -240,38 +247,56 @@ impl RoutingHandler {
240247 Ok ( ( ) )
241248 }
242249
243- fn try_send ( & mut self , mut packet : Packet ) -> Result < ( ) , NetworkError > {
244- /// Tries to send a packet to next hop until it succeeds or there are no more neighbors.
245- /// If sending fails, it removes the first hop from the route and tries again.
246250
251+ /// Tries to send a packet to next hop until it succeeds or there are no more neighbors.
252+ /// If sending fails, it removes the neighbor, finds a new route and tries again.
253+ fn try_send ( & mut self , mut packet : Packet ) -> Result < ( ) , NetworkError > {
254+ // A packet must have a destination
247255 let destination = packet
248256 . routing_header
249257 . destination ( )
250258 . ok_or ( NetworkError :: NoDestination ) ?;
251259
252260 let mut packet_sent = false ;
253261 while !packet_sent && self . neighbors . len ( ) > 0 {
254- if let Ok ( _) = self . send_packet_to_first_hop ( packet. clone ( ) ) {
255- packet_sent = true ;
256- } else {
257- // If sending failed, remove the neighbor and try again
258- if let Some ( first_hop) = packet. routing_header . hops . get ( 1 ) {
259- self . remove_neighbor ( * first_hop) ;
260- let route = self . network_view . find_path ( destination) ?;
261- packet. routing_header = SourceRoutingHeader :: new ( route, 1 ) . without_loops ( ) ;
262- }
262+ match self . send_packet_to_first_hop ( packet. clone ( ) ) {
263+ Ok ( _) => {
264+ packet_sent = true ;
265+ } ,
266+ Err ( NetworkError :: SendError ( t) ) => {
267+ // If the first hop is not a neighbor, remove it and try again
268+ if let Some ( first_hop) = packet. routing_header . hops . get ( 1 ) {
269+ self . remove_neighbor ( * first_hop) ;
270+ let route = self . network_view
271+ . find_path ( destination)
272+ . ok_or ( NetworkError :: PathNotFound ( destination) ) ?;
273+ packet. routing_header = SourceRoutingHeader :: new ( route, 1 ) . without_loops ( ) ;
274+ }
275+ } ,
276+ Err ( e) => return Err ( e) ,
263277 }
264278 }
265279
280+ if self . neighbors . is_empty ( ) {
281+ return Err ( NetworkError :: NoNeighborAssigned ) ;
282+ }
283+
266284 Ok ( ( ) )
267285
268286 }
269287
288+
289+ /// Sends a message by fragmenting it into 128-byte chunks and sending each chunk as a separate packet.
270290 pub fn send_message ( & mut self , message : & Vec < u8 > , destination : NodeId ) -> Result < ( ) , NetworkError > {
271291 let chunks: Vec < & [ u8 ] > = message. chunks ( 128 ) . collect ( ) ;
272292 let total_n_fragments = chunks. len ( ) ;
273293
274294 self . session_counter += 1 ;
295+ let shr = SourceRoutingHeader :: new (
296+ self . network_view . find_path ( destination) . ok_or ( NetworkError :: PathNotFound ( destination) ) ?,
297+ 1 ,
298+ ) . without_loops ( ) ;
299+
275300 for ( i, chunk) in chunks. into_iter ( ) . enumerate ( ) {
276301 // Pad/truncate to exactly 128 bytes
277302 let mut arr = [ 0u8 ; 128 ] ;
@@ -283,12 +308,7 @@ impl RoutingHandler {
283308 arr,
284309 ) ;
285310
286- let shr = SourceRoutingHeader :: new (
287- self . network_view . find_path ( destination) ?,
288- 1 ,
289- ) . without_loops ( ) ;
290-
291- let packet = Packet :: new_fragment ( shr, self . session_counter , fragment) ;
311+ let packet = Packet :: new_fragment ( shr. clone ( ) , self . session_counter , fragment) ;
292312
293313 self . try_send ( packet. clone ( ) ) ?;
294314 self . buffer . insert ( packet, self . session_counter , self . id ) ;
0 commit comments