Added message + listen commands
This commit is contained in:
Generated
+1
-1
@@ -510,7 +510,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "fedichat"
|
||||
version = "0.1.0"
|
||||
source = "git+https://git.firechicken.net/fedichat/fedichat-lib#3edf74d890232d09737ac9c343ebb24331fe7c68"
|
||||
source = "git+https://git.firechicken.net/fedichat/fedichat-lib#27cff2ced9f1879ddea03d2db3a0c5c5252b8cee"
|
||||
dependencies = [
|
||||
"ed25519",
|
||||
"rmp-serde",
|
||||
|
||||
+12
-2
@@ -30,7 +30,7 @@ pub struct Args {
|
||||
pub verbose: u8,
|
||||
}
|
||||
|
||||
#[derive(Subcommand,Debug)]
|
||||
#[derive(Subcommand,Debug,Clone)]
|
||||
pub enum Command {
|
||||
CreateUser {
|
||||
#[clap(short, long)]
|
||||
@@ -50,6 +50,12 @@ pub enum Command {
|
||||
#[clap(short, long, value_parser=Room::from_str)]
|
||||
room: Room,
|
||||
},
|
||||
GetMessages {
|
||||
#[clap(short, long, default_value_t = 100)]
|
||||
count: u64,
|
||||
#[clap(value_parser=Room::from_str)]
|
||||
room: Room
|
||||
},
|
||||
Join {
|
||||
#[clap(value_parser=Room::from_str)]
|
||||
room: Room,
|
||||
@@ -63,7 +69,7 @@ pub enum Command {
|
||||
room: Room,
|
||||
},
|
||||
Listen {
|
||||
#[clap(short, long, value_parser=Room::from_str)]
|
||||
#[clap(value_parser=Room::from_str)]
|
||||
room: Room,
|
||||
},
|
||||
Upload {
|
||||
@@ -123,6 +129,10 @@ impl Command {
|
||||
Listen {
|
||||
room,
|
||||
} => (ClientMessage::SubscribeMessages{room_id: room.get_coord()},room.get_server()),
|
||||
GetMessages {
|
||||
room,
|
||||
count
|
||||
} => (ClientMessage::FetchMessages{room_id: room.get_coord(), count, end: fedichat::message::MessageId(i64::MAX as u64)},room.get_server()),
|
||||
Join {
|
||||
room,
|
||||
} => (ClientMessage::RoomJoin{room_id: room.get_coord()},room.get_server()),
|
||||
|
||||
+28
-4
@@ -11,7 +11,10 @@ use hickory_resolver::Resolver;
|
||||
use std::net::{IpAddr,SocketAddr};
|
||||
use std::fs::File;
|
||||
use std::io::Write;
|
||||
use std::time::Duration;
|
||||
use std::sync::Arc;
|
||||
use quinn::Endpoint;
|
||||
use quinn::TransportConfig;
|
||||
use rmp_serde::encode::Serializer;
|
||||
use serde::Serialize;
|
||||
use tracing::{instrument,debug,Level};
|
||||
@@ -74,7 +77,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
.next().expect("DNS lookup of server failed");
|
||||
debug!("Found address {response}");
|
||||
// create connection
|
||||
let client_config = quinn::ClientConfig::try_with_platform_verifier().unwrap();
|
||||
let mut transport_config: TransportConfig = TransportConfig::default();
|
||||
transport_config.keep_alive_interval(Some(Duration::from_secs(1)));
|
||||
let mut client_config = quinn::ClientConfig::try_with_platform_verifier().unwrap();
|
||||
client_config.transport_config(Arc::new(transport_config));
|
||||
let endpoint = Endpoint::client(CLIENT_ADDR)?;
|
||||
let connection = endpoint.connect_with(
|
||||
client_config,
|
||||
@@ -89,6 +95,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
|
||||
let key = config.get_key(&target_server,&cli.username)?;
|
||||
|
||||
let command = cli.command.clone();
|
||||
|
||||
// send messages, each time waiting for a response
|
||||
let messages = cli.command.generate_messages(cli.username.clone(),token,key)?;
|
||||
|
||||
@@ -126,10 +134,26 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
},
|
||||
_ => {}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
println!("{}",format_response(response));
|
||||
|
||||
if let Command::Listen{..} = command {
|
||||
loop {
|
||||
debug!("Receiving post");
|
||||
let mut recv = connection.accept_uni().await?;
|
||||
debug!("Connection established");
|
||||
let received = recv.read_to_end(1000 * 1000).await?;
|
||||
debug!("Message read");
|
||||
debug!("BYTES: {:?}",received);
|
||||
|
||||
response = rmp_serde::from_slice(&received)?;
|
||||
debug!("Message processed");
|
||||
|
||||
println!("{}",format_response(response));
|
||||
}
|
||||
|
||||
} else {
|
||||
println!("{}",format_response(response));
|
||||
}
|
||||
|
||||
// On listen specifically we will stay connected
|
||||
|
||||
|
||||
Reference in New Issue
Block a user