From dbb63d1284299141530fa6ac95e26bd042d5b6eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20W=C3=BCrl?= Date: Fri, 2 Dec 2022 18:04:32 +0100 Subject: [PATCH] send client_version with mailbox bind message --- src/core.rs | 24 ++++++++++++++++++++++ src/core/rendezvous.rs | 9 +++++--- src/core/server_messages.rs | 41 +++++++++++++++++++++++++++++++------ 3 files changed, 65 insertions(+), 9 deletions(-) diff --git a/src/core.rs b/src/core.rs index 27bf0d3c..8b5456af 100644 --- a/src/core.rs +++ b/src/core.rs @@ -476,6 +476,30 @@ impl> From for EitherSide { } } +#[derive(PartialEq, Eq, Clone, Debug, Deserialize, Serialize, derive_more::Display)] +#[display(fmt = "{}-{}", "&*_0[0]", "&*_0[1]")] +pub struct ClientVersion<'a>(#[serde(borrow)] pub [&'a str; 2]); + +impl<'a> ClientVersion<'a> { + pub fn new(name: &'a str, version: &'a str) -> Self { + let array = [name, version]; + ClientVersion { 0: array } + } +} + +impl<'a> std::ops::Deref for ClientVersion<'a> { + type Target = [&'a str; 2]; + fn deref(&self) -> &[&'a str; 2] { + &self.0 + } +} + +impl<'a> std::ops::DerefMut for ClientVersion<'a> { + fn deref_mut(&mut self) -> &mut [&'a str; 2] { + &mut self.0 + } +} + #[derive(PartialEq, Eq, Clone, Debug, Hash, Deserialize, Serialize, derive_more::Display)] #[serde(transparent)] pub struct Phase(pub Cow<'static, str>); diff --git a/src/core/rendezvous.rs b/src/core/rendezvous.rs index ec999809..e509ff5c 100644 --- a/src/core/rendezvous.rs +++ b/src/core/rendezvous.rs @@ -70,9 +70,9 @@ struct WsConnection { } impl WsConnection { - async fn send_message( + async fn send_message<'a>( &mut self, - message: &OutboundMessage, + message: &OutboundMessage<'a>, queue: Option<&mut MessageQueue>, ) -> Result<(), RendezvousError> { log::debug!("Sending {}", message); @@ -322,7 +322,10 @@ impl RendezvousServer { &self.side } - async fn send_message(&mut self, message: &OutboundMessage) -> Result<(), RendezvousError> { + async fn send_message<'a>( + &mut self, + message: &OutboundMessage<'a>, + ) -> Result<(), RendezvousError> { self.connection .send_message(message, self.state.as_mut().map(|state| &mut state.queue)) .await diff --git a/src/core/server_messages.rs b/src/core/server_messages.rs index d0fa5264..9497d38e 100644 --- a/src/core/server_messages.rs +++ b/src/core/server_messages.rs @@ -1,4 +1,4 @@ -use super::{AppID, Mailbox, Mood, MySide, Nameplate, Phase, TheirSide}; +use super::{AppID, ClientVersion, Mailbox, Mood, MySide, Nameplate, Phase, TheirSide}; use serde_derive::{Deserialize, Serialize}; use std::collections::HashMap; @@ -132,13 +132,19 @@ impl EncryptedMessage { #[derive(Serialize, Debug, PartialEq, derive_more::Display)] #[serde(rename_all = "kebab-case")] #[serde(tag = "type")] -pub enum OutboundMessage { +pub enum OutboundMessage<'a> { #[display(fmt = "SubmitPermission({})", _0)] SubmitPermission(SubmitPermission), - #[display(fmt = "Bind {{ appid: {}, side: {} }}", appid, side)] + #[display( + fmt = "Bind {{ appid: {}, side: {}, client_version: {} }}", + appid, + side, + client_version + )] Bind { appid: AppID, side: MySide, + client_version: ClientVersion<'a>, }, List, Allocate, @@ -175,9 +181,16 @@ pub enum OutboundMessage { }, } -impl OutboundMessage { +const CLIENT_NAME: &str = "rust"; +impl<'a> OutboundMessage<'a> { pub fn bind(appid: AppID, side: MySide) -> Self { - OutboundMessage::Bind { appid, side } + let client_version_string: &str = env!("CARGO_PKG_VERSION"); + let client_version = ClientVersion::new(CLIENT_NAME, client_version_string); + OutboundMessage::Bind { + appid, + side, + client_version, + } } pub fn claim(nameplate: impl Into) -> Self { @@ -254,9 +267,11 @@ pub enum InboundMessage { mod test { use super::*; use serde_json::{from_str, json, Value}; + use std::ops::Deref; #[test] fn test_bind() { + let client_version_string: String = String::from(env!("CARGO_PKG_VERSION")); let m1 = OutboundMessage::bind( AppID::new("appid"), MySide::unchecked_from_string(String::from("side1")), @@ -266,10 +281,24 @@ mod test { assert_eq!( m2, json!({"type": "bind", "appid": "appid", - "side": "side1"}) + "side": "side1", "client_version": ["rust", client_version_string]}) ); } + #[test] + fn test_client_version_string_rep() { + let client_version = ClientVersion::new("foo", "1.0.2"); + + assert_eq!(client_version.to_string(), "foo-1.0.2") + } + + #[test] + fn test_client_version_deref() { + let client_version = ClientVersion::new("bar", "0.8.9"); + + assert_eq!(client_version.deref(), &["bar", "0.8.9"]) + } + #[test] fn test_list() { let m1 = OutboundMessage::List;