AP-2024-25-D-R-O-N-E / drone

1 stars 0 forks source link

Query handling function #2

Open ZGako opened 2 days ago

ZGako commented 2 days ago

Although the query isn't part of the API so it won't work yet. Adding the issue for when it gets merged

ZGako commented 2 days ago
fn forward_discovery_query (&self, mut query: Query, packet: Packet) {

    // packet: Packet
    // the packet.packet_type was matched for PacketType::Query
    // query: Query

    // push node into path_trace, this assures that every edge can be reconstructed by the initiator node
    query.path_trace.push((self.id, NodeType::Drone));

    if (query.ttl == 0) {

        //only send back a result if the flooding is not a broadcast
        if !query.broadcasting {
            let query_result = QueryResult {
                path_trace: query.path_trace.clone(),
                flood_id: query.flood_id
            };

            let return_routing: Vec<u64> = query.path_trace.iter().rev().map(|(node_id, _node_type)| *node_id).collect();

            let packet = Packet {
                pack_type: PacketType::QueryResult(query_result),
                routing_header: SourceRoutingHeader {
                    hops: return_routing.clone()
                },
                session_id: 0, // it'll be whatever for now
            };

            let first_hop_channel = self.neighbors.get(&return_routing[0]).unwrap(); 
            // unwrap since given the construction of the hops we're guaranteed to not have any errors

            let _ = first_hop_channel.send(packet);
        }

    } else {

        //check if the flood was already managed on this node

        let floodVal = self.floods_tracker.get(&query.initiator_id).or(Some(&0));

        if let Some(&flood_id) = floodVal {
            if flood_id < query.flood_id {
                // this means that the query's flood id is a new flood, thus it needs to be processed

                query.ttl -= 1;

                for (neighbor_id, neighbor_channel) in &self.neighbors {

                    let mut route = packet.routing_header.hops.clone();

                    route.push(*neighbor_id);

                    let packet = Packet {
                        pack_type: PacketType::Query(query.clone()),
                        routing_header: SourceRoutingHeader {
                            hops: route
                        },
                        session_id: 0, // it'll be whatever for now
                    };
                }

            } else {
                //flood's already passed through this node, thus send back a QueryResult

                //only send back a result if the flooding is not a broadcast
                if !query.broadcasting {
                    let query_result = QueryResult {
                        path_trace: query.path_trace.clone(),
                        flood_id: query.flood_id
                    };

                    let mut return_routing: Vec<u64> = query.path_trace.iter().map_while(|(node_id, _node_type)| 
                    if *node_id != self.id {
                        Some(*node_id)
                    } else {
                        None
                    }).collect();

                    return_routing.reverse();

                    let packet = Packet {
                        pack_type: PacketType::QueryResult(query_result),
                        routing_header: SourceRoutingHeader {
                            hops: return_routing.clone()
                        },
                        session_id: 0, // it'll be whatever for now
                    };

                    let first_hop_channel = self.neighbors.get(&return_routing[0]).unwrap(); 
                    // unwrap since given the construction of the hops we're guaranteed to not have any errors

                    let _ = first_hop_channel.send(packet);
                }
            }
        }
    }
}

Idea for such an implementation

ZGako commented 2 days ago

Don't copy paste since it doesn't take into account the current project structure