Pass functions that can use bridgedb/lox_auth to server_net
This commit is contained in:
parent
cb26f44d68
commit
266f53cb31
|
@ -19,7 +19,7 @@ async fn main() {
|
|||
|
||||
// If bridgedb has already been created, recreate it from file.
|
||||
// Otherwise, create new bridgedb.
|
||||
let bridgedb = if Path::new(bridgedb_filename).exists() {
|
||||
let mut bridgedb = if Path::new(bridgedb_filename).exists() {
|
||||
// read in file
|
||||
let bridgedb_infile = File::open(bridgedb_filename).unwrap();
|
||||
serde_json::from_reader(bridgedb_infile).unwrap()
|
||||
|
@ -49,11 +49,16 @@ async fn main() {
|
|||
.expect("Failed to write to bridgedb pubkey file");
|
||||
}
|
||||
|
||||
listen(addr, to_uppercase).await;
|
||||
let mut cmd_lambda = |cmd: Vec<u8>| perform_command(cmd, &bridgedb);
|
||||
|
||||
listen(addr, &mut cmd_lambda).await;
|
||||
}
|
||||
|
||||
// This function assumes the byte vector is a valid string.
|
||||
fn to_uppercase(str_vec: Vec<u8>) -> Vec<u8> {
|
||||
let str = std::str::from_utf8(&str_vec).unwrap();
|
||||
str.to_uppercase().into()
|
||||
fn perform_command(cmd: Vec<u8>, bridgedb: &BridgeDb) -> Vec<u8> {
|
||||
if cmd[0] == 1 {
|
||||
// note, the bridgedb currently has no buckets
|
||||
return bridgedb.invite().to_vec();
|
||||
}
|
||||
// default response, to deal with later
|
||||
[].to_vec()
|
||||
}
|
||||
|
|
|
@ -25,7 +25,7 @@ async fn main() {
|
|||
|
||||
// If lox_auth has already been created, recreate it from file.
|
||||
// Otherwise, create new lox authority.
|
||||
let lox_auth = if Path::new(lox_auth_filename).exists() {
|
||||
let mut lox_auth = if Path::new(lox_auth_filename).exists() {
|
||||
// read in file
|
||||
let lox_auth_infile = File::open(lox_auth_filename).unwrap();
|
||||
serde_json::from_reader(lox_auth_infile).unwrap()
|
||||
|
@ -65,11 +65,12 @@ async fn main() {
|
|||
.expect("Failed to write to lox_auth pubkeys file");
|
||||
}
|
||||
|
||||
listen(addr, reverse_string).await;
|
||||
let mut cmd_lambda = |cmd: Vec<u8>| perform_command(cmd, &lox_auth);
|
||||
|
||||
listen(addr, &mut cmd_lambda).await;
|
||||
}
|
||||
|
||||
// This function assumes the byte vector is a valid string.
|
||||
fn reverse_string(str_vec: Vec<u8>) -> Vec<u8> {
|
||||
let str = std::str::from_utf8(&str_vec).unwrap();
|
||||
str.trim().chars().rev().collect::<String>().into()
|
||||
fn perform_command(cmd: Vec<u8>, lox_auth: &BridgeAuth) -> Vec<u8> {
|
||||
// just send something back for now
|
||||
"ACK".into()
|
||||
}
|
||||
|
|
|
@ -19,6 +19,7 @@ async fn main() {
|
|||
let addr = args().nth(1).unwrap();
|
||||
|
||||
// message to send
|
||||
// TODO: determine commands so the client sends something meaningful
|
||||
let msg = args().nth(2).unwrap();
|
||||
|
||||
// import bridgedb pubkey
|
||||
|
@ -37,5 +38,5 @@ async fn main() {
|
|||
|
||||
let s = send(addr, msg.into()).await;
|
||||
|
||||
println!("{}", std::str::from_utf8(&s).unwrap());
|
||||
println!("{}", serde_json::to_string(&s).unwrap());
|
||||
}
|
||||
|
|
|
@ -9,7 +9,7 @@ these work. */
|
|||
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
||||
use tokio::net::TcpListener;
|
||||
|
||||
pub async fn listen(addr: String, fun: fn(Vec<u8>) -> Vec<u8>) {
|
||||
pub async fn listen(addr: String, fun: &mut dyn FnMut(Vec<u8>) -> Vec<u8>) {
|
||||
let listener = TcpListener::bind(&addr)
|
||||
.await
|
||||
.expect("Failed to create TcpListener");
|
||||
|
@ -19,7 +19,7 @@ pub async fn listen(addr: String, fun: fn(Vec<u8>) -> Vec<u8>) {
|
|||
// asynchronously wait for an inbound socket
|
||||
let (mut socket, _) = listener.accept().await.expect("Failed to create socket");
|
||||
|
||||
tokio::spawn(async move {
|
||||
// tokio::spawn(async move {
|
||||
loop {
|
||||
// get number of bytes to receive
|
||||
let mut nbuf: [u8; 8] = [0; 8];
|
||||
|
@ -30,7 +30,8 @@ pub async fn listen(addr: String, fun: fn(Vec<u8>) -> Vec<u8>) {
|
|||
let n = usize::from_be_bytes(nbuf);
|
||||
|
||||
if n == 0 {
|
||||
return;
|
||||
break;
|
||||
// return;
|
||||
}
|
||||
|
||||
let mut buf = vec![0; n];
|
||||
|
@ -42,7 +43,8 @@ pub async fn listen(addr: String, fun: fn(Vec<u8>) -> Vec<u8>) {
|
|||
.expect("Failed to read data from socket");
|
||||
|
||||
if n == 0 {
|
||||
return;
|
||||
break;
|
||||
// return;
|
||||
}
|
||||
|
||||
let response = fun(buf);
|
||||
|
@ -60,6 +62,6 @@ pub async fn listen(addr: String, fun: fn(Vec<u8>) -> Vec<u8>) {
|
|||
.await
|
||||
.expect("Failed to write data to socket");
|
||||
}
|
||||
});
|
||||
// });
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue