use dfir_rs::dfir_syntax;
use dfir_rs::util::{bind_udp_bytes, ipv4_resolve};
use crate::Opts;
use crate::helpers::{parse_command, print_graph};
use crate::protocol::KvsMessage;
pub(crate) async fn run_client(opts: Opts) {
let client_addr = ipv4_resolve("localhost:0").unwrap();
let server_addr = opts.address;
assert_ne!(
0,
server_addr.port(),
"Client cannot connect to server port 0."
);
let (outbound, inbound, allocated_client_addr) = bind_udp_bytes(client_addr).await;
println!(
"Client is live! Listening on {:?} and talking to server on {:?}",
allocated_client_addr, server_addr
);
let mut flow = dfir_syntax! {
outbound_chan = dest_sink_serde(outbound);
inbound_chan = source_stream_serde(inbound) -> map(Result::unwrap);
source_stdin()
-> filter_map(|line| parse_command(line.unwrap()))
-> map(|msg| { (msg, server_addr) })
-> outbound_chan;
inbound_chan -> for_each(|(response, _addr): (KvsMessage, _)| println!("Got a Response: {:?}", response));
};
if let Some(graph) = opts.graph {
print_graph(&flow, graph, opts.write_config);
}
flow.run().await;
}