Skip to main content

interstice_cli/
call_query.rs

1use crate::node_client::handshake_with_node;
2use crate::node_registry::NodeRegistry;
3use interstice_core::{
4    IntersticeError, NetworkPacket,
5    interstice_abi::IntersticeValue,
6    packet::{read_packet, write_packet},
7};
8
9pub async fn call_query(
10    node_ref: String,
11    module_name: String,
12    query_name: String,
13    input: IntersticeValue,
14) -> Result<(), IntersticeError> {
15    let registry = NodeRegistry::load()?;
16    let node_address = registry
17        .resolve_address(&node_ref)
18        .ok_or_else(|| IntersticeError::Internal("Unknown node".into()))?;
19    // connect to node
20    let (mut stream, _handshake) = handshake_with_node(&node_address).await?;
21
22    // Send call query packet to node
23    let request_id = uuid::Uuid::new_v4().to_string();
24    let packet = NetworkPacket::QueryCall {
25        module_name,
26        query_name,
27        input,
28        request_id: request_id.clone(),
29    };
30    write_packet(&mut stream, &packet).await?;
31
32    // Wait receiving query response packet from node
33    let response_packet = read_packet(&mut stream).await?;
34    match response_packet {
35        NetworkPacket::QueryResponse {
36            request_id: _response_request_id,
37            result,
38        } => {
39            println!("Query response: {}", result);
40        }
41        _ => {
42            println!("Unexpected packet received: {:?}", response_packet);
43        }
44    }
45
46    // Close connection properly
47    let packet = NetworkPacket::Close;
48    write_packet(&mut stream, &packet).await?;
49
50    Ok(())
51}