Skip to content

Commit

Permalink
feat(protocol): Tag server entries reply with client id
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanuppal committed Jan 5, 2025
1 parent 102f446 commit a1a7c13
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 8 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion client-connect/examples/simple_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ async fn main() -> ClientConnectionResult<()> {
chat_log_entry.text_content()
);
}
comms::ServerMessage::EntryRange(entries) => {
comms::ServerMessage::EntryRange {
client_id: _,
entries,
} => {
println!("got entry range: {:?}", entries);
}
}
Expand Down
1 change: 1 addition & 0 deletions client-tui/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,7 @@ impl App {
if self.messages_cursor == 0 {
self.tx
.send(comms::ClientMessage::Request {
client_id: comms::ClientId::unique_per_client(),
count: 50,
up_to_slot_number: Some(
messages
Expand Down
6 changes: 5 additions & 1 deletion client-tui/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ async fn main() -> Result<(), io::Error> {
let app_messages = messages.clone();

tx.send(comms::ClientMessage::Request {
client_id: comms::ClientId::unique_per_client(),
count: 50,
up_to_slot_number: None,
})
Expand All @@ -34,7 +35,10 @@ async fn main() -> Result<(), io::Error> {
break;
}
},
comms::ServerMessage::EntryRange(entries) => {
comms::ServerMessage::EntryRange {
client_id: _,
entries,
} => {
if !entries.is_empty() {
// since we don't have to worry about updates until
// v0.2, this is going to be
Expand Down
1 change: 1 addition & 0 deletions comms/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ edition.workspace = true
chat.workspace = true
serde.workspace = true
serde_json.workspace = true
chrono.workspace = true
23 changes: 22 additions & 1 deletion comms/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use chat::Entry;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
pub use serde_json::Error as CodingError;

Expand All @@ -18,13 +19,30 @@ pub trait Codable {
}
}

/// Opaque unique-per-client identifier.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ClientId {
timestamp: DateTime<Utc>,
}

impl ClientId {
/// Guaranteed to be unique for each client but not necessarily between
/// clients.
pub fn new_unique_per_client() -> Self {
Self {
timestamp: Utc::now(),
}
}
}

#[derive(Debug, Serialize, Deserialize)]
pub enum ClientMessage {
Post {
username: String,
content: String,
},
Request {
client_id: ClientId,
count: usize,
up_to_slot_number: Option<usize>,
},
Expand All @@ -35,7 +53,10 @@ impl Codable for ClientMessage {}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ServerMessage {
NewEntry(Entry),
EntryRange(Vec<chat::Entry>),
EntryRange {
client_id: ClientId,
entries: Vec<chat::Entry>,
},
}

impl Codable for ServerMessage {}
12 changes: 7 additions & 5 deletions server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,14 @@ async fn main() -> Result<(), Error> {
}
}
comms::ClientMessage::Request {
client_id,
count,
up_to_slot_number,
} => {
let entries = fake_chat_log.entries(count, up_to_slot_number);
sessions.read().await[&sender]
.send(comms::ServerMessage::EntryRange(entries));
sessions.read().await[&sender].send(
comms::ServerMessage::EntryRange { client_id, entries },
);
}
}
}
Expand Down Expand Up @@ -254,16 +256,16 @@ async fn new_client_connection(
match comms::ClientMessage::try_from_bytes(
&message_bytes,
) {
Ok(client_request) => {
Ok(client_message) => {
log::info!(
"Received {:?} from client address {}",
client_request,
client_message ,
client_address
);
message_tx
.send((
client_address,
client_request,
client_message,
))
.expect("Failed to queue client message for processing");
}
Expand Down

0 comments on commit a1a7c13

Please sign in to comment.