From 0e6acdbff4961f00d1fba76f5c526a04fc75ce58 Mon Sep 17 00:00:00 2001 From: Joscha Date: Fri, 11 Feb 2022 22:11:20 +0100 Subject: [PATCH] Fix and simplify packets --- cove-core/src/message.rs | 11 +-- cove-core/src/packets.rs | 17 ++--- cove-core/src/user.rs | 8 +- cove-server/src/main.rs | 161 ++++++++++++++++++++++++++++++++++++++- 4 files changed, 173 insertions(+), 24 deletions(-) diff --git a/cove-core/src/message.rs b/cove-core/src/message.rs index 5751d91..764ce0e 100644 --- a/cove-core/src/message.rs +++ b/cove-core/src/message.rs @@ -1,10 +1,10 @@ use serde::{Deserialize, Serialize}; -use crate::{ Identity, MessageId}; +use crate::{Identity, MessageId}; -#[derive(Debug, Deserialize, Serialize)] +#[derive(Debug, Clone, Deserialize, Serialize)] pub struct Message { - pub pred: Option, + pub pred: MessageId, pub parent: Option, pub identity: Identity, pub nick: String, @@ -13,10 +13,7 @@ pub struct Message { impl Message { pub fn id(&self) -> MessageId { - let pred = match self.pred { - Some(id) => format!("{id}"), - None => "none".to_string(), - }; + let pred = self.pred; let parent = match self.parent { Some(id) => format!("{id}"), None => "none".to_string(), diff --git a/cove-core/src/packets.rs b/cove-core/src/packets.rs index da6e36e..bcf67d8 100644 --- a/cove-core/src/packets.rs +++ b/cove-core/src/packets.rs @@ -29,14 +29,13 @@ pub struct NickCmd { #[derive(Debug, Deserialize, Serialize)] #[serde(tag = "type")] pub enum NickRpl { - Success { you: User }, + Success, NickTooLong, } #[derive(Debug, Deserialize, Serialize)] pub struct SendCmd { pub parent: Option, - pub nick: Option, pub content: String, } @@ -49,32 +48,32 @@ pub enum SendRpl { } #[derive(Debug, Deserialize, Serialize)] -pub struct WhoCmd; +pub struct WhoCmd {} #[derive(Debug, Deserialize, Serialize)] pub struct WhoRpl { - you: User, - others: Vec, + pub you: User, + pub others: Vec, } #[derive(Debug, Deserialize, Serialize)] pub struct JoinNtf { - user: User, + pub user: User, } #[derive(Debug, Deserialize, Serialize)] pub struct NickNtf { - user: User, + pub user: User, } #[derive(Debug, Deserialize, Serialize)] pub struct PartNtf { - user: User, + pub user: User, } #[derive(Debug, Deserialize, Serialize)] pub struct SendNtf { - message: Message, + pub message: Message, } // Create a Cmd enum for all commands, a Rpl enum for all replies and a Ntf enum diff --git a/cove-core/src/user.rs b/cove-core/src/user.rs index e3e0aa1..94d4090 100644 --- a/cove-core/src/user.rs +++ b/cove-core/src/user.rs @@ -2,9 +2,9 @@ use serde::{Deserialize, Serialize}; use crate::{Identity, SessionId}; -#[derive(Debug, Deserialize, Serialize)] +#[derive(Debug, Clone, Deserialize, Serialize)] pub struct User { - nick: String, - identity: Identity, - sid: SessionId, + pub nick: String, + pub identity: Identity, + pub sid: SessionId, } diff --git a/cove-server/src/main.rs b/cove-server/src/main.rs index ddbc658..fc8fec7 100644 --- a/cove-server/src/main.rs +++ b/cove-server/src/main.rs @@ -1,12 +1,165 @@ +use cove_core::packets::{ + Cmd, HelloCmd, HelloRpl, JoinNtf, NickCmd, NickNtf, NickRpl, Ntf, Packet, PartNtf, Rpl, + SendCmd, SendNtf, SendRpl, WhoCmd, WhoRpl, +}; +use cove_core::{Identity, Message, MessageId, SessionId, User}; use futures::{future, StreamExt, TryStreamExt}; use tokio::net::{TcpListener, TcpStream}; #[tokio::main] async fn main() { - let listener = TcpListener::bind(("::0", 40080)).await.unwrap(); - while let Ok((stream, _)) = listener.accept().await { - tokio::spawn(conn(stream)); - } + let user = User { + nick: "Garmy".to_string(), + identity: Identity::of("random garbage"), + sid: SessionId::of("12345"), + }; + let message = Message { + pred: MessageId::of("pred"), + parent: None, + identity: Identity::of("asd"), + nick: "Foo".to_string(), + content: "Bar".to_string(), + }; + println!( + "{}", + serde_json::to_string_pretty(&Packet::Cmd { + id: 12345, + cmd: Cmd::Hello(HelloCmd { + nick: "Garmy".to_string(), + identity: "random garbage".to_string() + }) + }) + .unwrap() + ); + println!( + "{}", + serde_json::to_string_pretty(&Packet::Rpl { + id: 67890, + rpl: Rpl::Hello(HelloRpl::Success { + you: user.clone(), + others: vec![], + last_message: MessageId::of("Blarg") + }) + }) + .unwrap() + ); + println!( + "{}", + serde_json::to_string_pretty(&Packet::Rpl { + id: 67890, + rpl: Rpl::Hello(HelloRpl::NickTooLong) + }) + .unwrap() + ); + println!( + "{}", + serde_json::to_string_pretty(&Packet::Cmd { + id: 12345, + cmd: Cmd::Nick(NickCmd { + nick: "Garmelon".to_string() + }) + }) + .unwrap() + ); + println!( + "{}", + serde_json::to_string_pretty(&Packet::Rpl { + id: 67890, + rpl: Rpl::Nick(NickRpl::Success) + }) + .unwrap() + ); + println!( + "{}", + serde_json::to_string_pretty(&Packet::Rpl { + id: 67890, + rpl: Rpl::Nick(NickRpl::NickTooLong) + }) + .unwrap() + ); + println!( + "{}", + serde_json::to_string_pretty(&Packet::Cmd { + id: 12345, + cmd: Cmd::Send(SendCmd { + parent: None, + // parent: Some(MessageId::of("Booh!")), + content: "Hello world!".to_string() + }) + }) + .unwrap() + ); + println!( + "{}", + serde_json::to_string_pretty(&Packet::Rpl { + id: 67890, + rpl: Rpl::Send(SendRpl::Success { + message: message.clone() + }) + }) + .unwrap() + ); + println!( + "{}", + serde_json::to_string_pretty(&Packet::Rpl { + id: 67890, + rpl: Rpl::Send(SendRpl::ContentTooLong) + }) + .unwrap() + ); + println!( + "{}", + serde_json::to_string_pretty(&Packet::Cmd { + id: 12345, + cmd: Cmd::Who(WhoCmd {}) + }) + .unwrap() + ); + println!( + "{}", + serde_json::to_string_pretty(&Packet::Rpl { + id: 67890, + rpl: Rpl::Who(WhoRpl { + you: user.clone(), + others: vec![] + }) + }) + .unwrap() + ); + println!( + "{}", + serde_json::to_string_pretty(&Packet::Ntf { + ntf: Ntf::Join(JoinNtf { user: user.clone() }) + }) + .unwrap() + ); + println!( + "{}", + serde_json::to_string_pretty(&Packet::Ntf { + ntf: Ntf::Nick(NickNtf { user: user.clone() }) + }) + .unwrap() + ); + println!( + "{}", + serde_json::to_string_pretty(&Packet::Ntf { + ntf: Ntf::Part(PartNtf { user: user.clone() }) + }) + .unwrap() + ); + println!( + "{}", + serde_json::to_string_pretty(&Packet::Ntf { + ntf: Ntf::Send(SendNtf { + message: message.clone() + }) + }) + .unwrap() + ); + // let listener = TcpListener::bind(("::0", 40080)).await.unwrap(); + // while let Ok((stream, _)) = listener.accept().await { + // tokio::spawn(conn(stream)); + // } } async fn conn(stream: TcpStream) {