From 78724607b0ea9aea18c70704090ac6ad8be0bb5d Mon Sep 17 00:00:00 2001 From: Paul Butler Date: Sat, 5 Oct 2024 09:49:38 -0700 Subject: [PATCH] Make examples work --- aper-stateroom/src/lib.rs | 14 ++-- aper/src/aper.rs | 3 +- aper/src/connection.rs | 4 +- examples/counter/common/src/lib.rs | 6 +- examples/drop-four/common/src/state.rs | 82 ++++++++++++------------ examples/drop-four/service/.cargo/config | 2 +- examples/timer/common/src/lib.rs | 12 ++-- 7 files changed, 62 insertions(+), 61 deletions(-) diff --git a/aper-stateroom/src/lib.rs b/aper-stateroom/src/lib.rs index fdf332e..152fb8e 100644 --- a/aper-stateroom/src/lib.rs +++ b/aper-stateroom/src/lib.rs @@ -1,6 +1,6 @@ use aper::connection::{MessageToClient, MessageToServer, ServerConnection, ServerHandle}; use aper::{Aper, IntentMetadata}; -use chrono::Utc; +use chrono::{DateTime, Utc}; pub use stateroom::ClientId; use stateroom::{MessagePayload, StateroomContext, StateroomService}; use std::collections::HashMap; @@ -11,7 +11,7 @@ where P::Intent: Unpin + 'static, { connection: ServerConnection

, - suspended_event: Option<(P::Intent, IntentMetadata)>, + suspended_event: Option<(DateTime, P::Intent)>, client_connections: HashMap>, /// Pseudo-connection for sending timer events. @@ -48,7 +48,7 @@ where } if let Some(ev) = &susp { - let dur = ev.1.timestamp.signed_duration_since(Utc::now()); + let dur = ev.0.signed_duration_since(Utc::now()); ctx.set_timer(dur.num_milliseconds().max(0) as u32); } @@ -114,13 +114,13 @@ where } fn timer(&mut self, ctx: &impl StateroomContext) { - if let Some(mut event) = self.suspended_event.take() { - event.1.timestamp = Utc::now(); - let event = bincode::serialize(&event).unwrap(); + if let Some((_timestamp, intent)) = self.suspended_event.take() { + let intent = bincode::serialize(&intent).unwrap(); self.process_message( MessageToServer::Intent { - intent: event, + intent, client_version: 0, + metadata: IntentMetadata::new(None, Utc::now()), }, None, ctx, diff --git a/aper/src/aper.rs b/aper/src/aper.rs index 3e9d0bb..7c880f9 100644 --- a/aper/src/aper.rs +++ b/aper/src/aper.rs @@ -3,6 +3,7 @@ use crate::{ store::{Store, StoreHandle}, IntentMetadata, Mutation, }; +use chrono::{DateTime, Utc}; use serde::{Deserialize, Serialize}; use std::{collections::VecDeque, fmt::Debug}; @@ -24,7 +25,7 @@ pub trait Aper: AperSync + 'static { metadata: &IntentMetadata, ) -> Result<(), Self::Error>; - fn suspended_event(&self) -> Option<(Self::Intent, IntentMetadata)> { + fn suspended_event(&self) -> Option<(DateTime, Self::Intent)> { None } } diff --git a/aper/src/connection.rs b/aper/src/connection.rs index bf48eb8..aa3010a 100644 --- a/aper/src/connection.rs +++ b/aper/src/connection.rs @@ -12,6 +12,7 @@ pub enum MessageToServer { Intent { intent: Vec, client_version: u64, + metadata: IntentMetadata, }, RequestState { latest_version: u64, @@ -82,6 +83,7 @@ impl ClientConnection { (self.message_callback)(MessageToServer::Intent { intent, client_version: version, + metadata, }); Ok(()) @@ -163,10 +165,10 @@ impl ServerHandle { MessageToServer::Intent { intent, client_version, + metadata, } => { let intent = bincode::deserialize(intent).unwrap(); let mut server_borrow = self.server.lock().unwrap(); - let metadata = IntentMetadata::new(Some(self.client_id), Utc::now()); let Ok(mutations) = server_borrow.apply(&intent, &metadata) else { // still need to ack the client. diff --git a/examples/counter/common/src/lib.rs b/examples/counter/common/src/lib.rs index 6ccb15e..2a4df6e 100644 --- a/examples/counter/common/src/lib.rs +++ b/examples/counter/common/src/lib.rs @@ -1,4 +1,4 @@ -use aper::{data_structures::atom::Atom, Aper, AperSync, IntentEvent}; +use aper::{data_structures::atom::Atom, Aper, AperSync, IntentMetadata}; use serde::{Deserialize, Serialize}; #[derive(AperSync, Clone)] @@ -23,10 +23,10 @@ impl Aper for Counter { type Intent = CounterIntent; type Error = (); - fn apply(&mut self, event: &IntentEvent) -> Result<(), ()> { + fn apply(&mut self, intent: &CounterIntent, _metadata: &IntentMetadata) -> Result<(), ()> { let value = self.value.get(); - match &event.intent { + match &intent { CounterIntent::Add(i) => { self.value.set(value + i); } diff --git a/examples/drop-four/common/src/state.rs b/examples/drop-four/common/src/state.rs index 8bed5ae..449c6f6 100644 --- a/examples/drop-four/common/src/state.rs +++ b/examples/drop-four/common/src/state.rs @@ -1,6 +1,6 @@ use aper::{ data_structures::{atom::Atom, fixed_array::FixedArray}, - Aper, AperSync, IntentEvent, + Aper, AperSync, IntentMetadata, }; use serde::{Deserialize, Serialize}; @@ -154,14 +154,14 @@ impl Aper for DropFourGame { type Intent = GameTransition; type Error = (); - fn apply(&mut self, event: &IntentEvent) -> Result<(), ()> { - match event.intent { + fn apply(&mut self, intent: &Self::Intent, metadata: &IntentMetadata) -> Result<(), ()> { + match intent { GameTransition::Join => { if PlayState::Waiting == self.play_state.get() { if self.player_map.teal_player.get().is_none() { - self.player_map.teal_player.set(event.client); + self.player_map.teal_player.set(metadata.client); } else if self.player_map.brown_player.get().is_none() { - self.player_map.brown_player.set(event.client); + self.player_map.brown_player.set(metadata.client); self.play_state.set(PlayState::Playing); } } @@ -171,15 +171,15 @@ impl Aper for DropFourGame { if self.winner.get().is_some() { return Ok(()); } // Someone has already won. - if self.player_map.id_of_color(self.next_player.get()) != event.client { + if self.player_map.id_of_color(self.next_player.get()) != metadata.client { return Ok(()); } // Play out of turn. - if let Some(insert_row) = self.board.lowest_open_row(c as u32) { + if let Some(insert_row) = self.board.lowest_open_row(*c as u32) { self.board - .set(insert_row, c as u32, Some(self.next_player.get())); + .set(insert_row, *c as u32, Some(self.next_player.get())); - let winner = self.board.check_winner_at(insert_row as i32, c as i32); + let winner = self.board.check_winner_at(insert_row as i32, *c as i32); self.winner.set(winner); self.next_player.set(self.next_player.get().other()); @@ -221,31 +221,36 @@ mod tests { #[test] fn test_game() { let mut game = DropFourGame::new(); - let dummy_timestamp = Utc.timestamp_millis_opt(0).unwrap(); let player1 = 1; let player2 = 2; + let player1_meta = IntentMetadata { + client: Some(player1), + timestamp: Utc.timestamp_millis_opt(0).unwrap(), + }; + let player2_meta = IntentMetadata { + client: Some(player2), + timestamp: Utc.timestamp_millis_opt(0).unwrap(), + }; + assert_eq!(Waiting, game.play_state.get()); - game.apply(&IntentEvent::new(Some(player1), dummy_timestamp, Join)) - .unwrap(); + game.apply(&Join, &player1_meta).unwrap(); assert_eq!(Waiting, game.play_state.get()); - assert_eq!(Some(player1), game.player_map.teal_player.get(),); + assert_eq!(Some(player1), game.player_map.teal_player.get()); - game.apply(&IntentEvent::new(Some(player2), dummy_timestamp, Join)) - .unwrap(); + game.apply(&Join, &player2_meta).unwrap(); assert_eq!(game.play_state.get(), Playing,); - assert_eq!(Some(player2), game.player_map.brown_player.get(),); - assert_eq!(Teal, game.next_player.get(),); + assert_eq!(Some(player2), game.player_map.brown_player.get()); + assert_eq!(Teal, game.next_player.get()); - game.apply(&IntentEvent::new(Some(player1), dummy_timestamp, Drop(4))) - .unwrap(); + game.apply(&Drop(4), &player1_meta).unwrap(); expect_disc(&game, 5, 4, Teal); - assert_eq!(Brown, game.next_player.get(),); + assert_eq!(Brown, game.next_player.get()); // v // ....... @@ -255,10 +260,9 @@ mod tests { // ....... // ....T.. - game.apply(&IntentEvent::new(Some(player2), dummy_timestamp, Drop(4))) - .unwrap(); + game.apply(&Drop(4), &player2_meta).unwrap(); - assert_eq!(Teal, game.next_player.get(),); + assert_eq!(Teal, game.next_player.get()); expect_disc(&game, 4, 4, Brown); // v @@ -269,10 +273,9 @@ mod tests { // ....B.. // ....T.. - game.apply(&IntentEvent::new(Some(player1), dummy_timestamp, Drop(3))) - .unwrap(); + game.apply(&Drop(3), &player1_meta).unwrap(); - assert_eq!(Brown, game.next_player.get(),); + assert_eq!(Brown, game.next_player.get()); expect_disc(&game, 5, 3, Teal); // v @@ -283,10 +286,9 @@ mod tests { // ....B.. // ...TT.. - game.apply(&IntentEvent::new(Some(player2), dummy_timestamp, Drop(5))) - .unwrap(); + game.apply(&Drop(5), &player2_meta).unwrap(); - assert_eq!(Teal, game.next_player.get(),); + assert_eq!(Teal, game.next_player.get()); expect_disc(&game, 5, 5, Brown); // v @@ -297,10 +299,9 @@ mod tests { // ....B.. // ...TTB. - game.apply(&IntentEvent::new(Some(player1), dummy_timestamp, Drop(2))) - .unwrap(); + game.apply(&Drop(2), &player1_meta).unwrap(); - assert_eq!(Brown, game.next_player.get(),); + assert_eq!(Brown, game.next_player.get()); expect_disc(&game, 5, 2, Teal); // v @@ -311,10 +312,9 @@ mod tests { // ....B.. // ..TTTB. - game.apply(&IntentEvent::new(Some(player2), dummy_timestamp, Drop(2))) - .unwrap(); + game.apply(&Drop(2), &player2_meta).unwrap(); - assert_eq!(Teal, game.next_player.get(),); + assert_eq!(Teal, game.next_player.get()); expect_disc(&game, 4, 2, Brown); // v @@ -325,12 +325,11 @@ mod tests { // ..B.B.. // ..TTTB. - game.apply(&IntentEvent::new(Some(player1), dummy_timestamp, Drop(1))) - .unwrap(); + game.apply(&Drop(1), &player1_meta).unwrap(); - assert_eq!(Brown, game.next_player.get(),); + assert_eq!(Brown, game.next_player.get()); expect_disc(&game, 5, 1, Teal); - assert_eq!(Some(Teal), game.winner.get(),); + assert_eq!(Some(Teal), game.winner.get()); // v // ....... @@ -340,9 +339,8 @@ mod tests { // ..B.B.. // .TTTTB. - game.apply(&IntentEvent::new(Some(player1), dummy_timestamp, Reset)) - .unwrap(); + game.apply(&Reset, &player1_meta).unwrap(); - assert_eq!(None, game.winner.get(),); + assert_eq!(None, game.winner.get()); } } diff --git a/examples/drop-four/service/.cargo/config b/examples/drop-four/service/.cargo/config index 6b77899..6b509f5 100644 --- a/examples/drop-four/service/.cargo/config +++ b/examples/drop-four/service/.cargo/config @@ -1,2 +1,2 @@ [build] -target = "wasm32-wasi" +target = "wasm32-wasip1" diff --git a/examples/timer/common/src/lib.rs b/examples/timer/common/src/lib.rs index 5f6cfd7..1013384 100644 --- a/examples/timer/common/src/lib.rs +++ b/examples/timer/common/src/lib.rs @@ -1,4 +1,4 @@ -use aper::{data_structures::atom::Atom, Aper, AperSync, IntentEvent}; +use aper::{data_structures::atom::Atom, Aper, AperSync, IntentMetadata}; use chrono::{DateTime, Duration, Utc}; use serde::{Deserialize, Serialize}; @@ -18,25 +18,25 @@ impl Aper for Timer { type Intent = TimerIntent; type Error = (); - fn apply(&mut self, event: &IntentEvent) -> Result<(), ()> { - match event.intent { + fn apply(&mut self, intent: &Self::Intent, metadata: &IntentMetadata) -> Result<(), ()> { + match intent { TimerIntent::Reset => self.value.set(0), TimerIntent::Increment => { self.value.set(self.value.get() + 1); - self.last_increment.set(event.timestamp); + self.last_increment.set(metadata.timestamp); } } Ok(()) } - fn suspended_event(&self) -> Option> { + fn suspended_event(&self) -> Option<(DateTime, TimerIntent)> { let next_event = self .last_increment .get() .checked_add_signed(Duration::seconds(1)) .unwrap(); - Some(IntentEvent::new(None, next_event, TimerIntent::Increment)) + Some((next_event, TimerIntent::Increment)) } }