lox_client now connects to server, sends msg, receives response

This commit is contained in:
Vecna 2023-01-15 13:04:47 -05:00
parent 5216ede738
commit 1e8ff05425
2 changed files with 56 additions and 1 deletions

View File

@ -1,11 +1,26 @@
// This seems like probably not the best way to do this, but it works.
#[path = "../client_net.rs"]
mod client_net;
use crate::client_net::send;
use ed25519_dalek::PublicKey;
use lox::IssuerPubKey;
use std::env::args;
use std::fs::File;
fn main() {
#[tokio::main]
async fn main() {
let bridgedb_pubkey_filename = "bridgedb_pubkey.json";
let lox_auth_pubkeys_filename = "lox_auth_pubkeys.json";
// TODO: make argument passing nicer (maybe flags, config files, etc.)
// network address to connect to, e.g., localhost:8080
let addr = args().nth(1).unwrap();
// message to send
let msg = args().nth(2).unwrap();
// import bridgedb pubkey
let bridgedb_pubkey_infile = File::open(bridgedb_pubkey_filename).unwrap();
let bridgedb_pubkey: PublicKey = serde_json::from_reader(bridgedb_pubkey_infile).unwrap();
@ -19,4 +34,8 @@ fn main() {
let migrationkey_pub = &lox_auth_pubkeys[2];
let reachability_pub = &lox_auth_pubkeys[3];
let invitation_pub = &lox_auth_pubkeys[4];
let s = send(addr, msg).await;
println!("{}", s);
}

36
src/client_net.rs Normal file
View File

@ -0,0 +1,36 @@
/*! The networking methods for our client components to call. In
particular, this file provides a send() method to handle connecting
to a server process and sending it data. */
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::TcpStream;
use std::str;
// may need to change strings to byte vectors in the future
pub async fn send(addr: String, str: String) -> String {
let mut stream = TcpStream::connect(&addr)
.await
.expect("Failed to create TcpStream");
// send data
stream
.write_all(str.as_bytes())
.await
.expect("Failed to write data to stream");
// read response
let mut buf = vec![0; 1024];
let n = stream
.read(&mut buf)
.await
.expect("Failed to read data from socket");
if n == 0 {
return "".to_string();
}
str::from_utf8(&buf[0..n])
.expect("Invalid UTF-8 sequence")
.to_string()
}