Skip to main content

interstice_cli/
call_reducer.rs

1use crate::node_client::handshake_with_node;
2use crate::node_registry::NodeRegistry;
3use interstice_core::{
4    IntersticeError, NetworkPacket, interstice_abi::IntersticeValue, packet::write_packet,
5};
6
7pub async fn call_reducer(
8    node_ref: String,
9    module_name: String,
10    reducer_name: String,
11    input: IntersticeValue,
12) -> Result<(), IntersticeError> {
13    let registry = NodeRegistry::load()?;
14    let node_address = registry
15        .resolve_address(&node_ref)
16        .ok_or_else(|| IntersticeError::Internal("Unknown node".into()))?;
17    // connect to node
18    let (mut stream, _handshake) = handshake_with_node(&node_address).await?;
19
20    // Send call reducer packet to node
21    let packet = NetworkPacket::ReducerCall {
22        module_name,
23        reducer_name,
24        input,
25    };
26    write_packet(&mut stream, &packet).await?;
27
28    // Close connection properly
29    let packet = NetworkPacket::Close;
30    write_packet(&mut stream, &packet).await?;
31
32    Ok(())
33}