3 use crate::schema::hello_world::HelloWorld;
4 use connect::tls::rustls::ClientConfig;
5 use connect::{Connection, SinkExt, StreamExt};
7 use protobuf::well_known_types::Any;
11 async fn main() -> anyhow::Result<()> {
14 // get ip address and domain from cmd line args
15 let (ip_addr, domain, cafile_path) = parse_args();
17 // construct `rustls` client config
18 let cafile = std::fs::read(cafile_path)?;
20 let mut client_pem = std::io::Cursor::new(cafile);
22 let mut client_config = ClientConfig::new();
25 .add_pem_file(&mut client_pem)
26 .map_err(|_| std::io::Error::new(std::io::ErrorKind::InvalidInput, "invalid cert"))?;
28 // create a client connection to the server
29 let mut conn = Connection::tls_client(ip_addr, &domain, client_config.into()).await?;
31 // send a message to the server
32 let raw_msg = String::from("Hello world");
33 info!("Sending message: {}", raw_msg);
35 let mut msg = HelloWorld::new();
36 msg.set_message(raw_msg);
38 conn.writer().send(msg).await?;
40 // wait for the server to reply with an ack
41 while let Some(reply) = conn.reader().next().await {
42 info!("Received message");
44 let msg: HelloWorld = Any::unpack(&reply)?.unwrap();
46 info!("Unpacked reply: {}", msg.get_message());
52 fn parse_args() -> (String, String, String) {
53 let args: Vec<String> = env::args().collect();
55 let ip_address = match args.get(1) {
58 error!("Need to pass IP address to connect to as first command line argument");
63 let domain = match args.get(2) {
66 error!("Need to pass domain name as second command line argument");
71 let cafile_path = match args.get(3) {
74 error!("Need to pass path to cafile as third command line argument");
80 ip_address.to_string(),
82 cafile_path.to_string(),