Skip to content

Commit

Permalink
send client_version with mailbox bind message
Browse files Browse the repository at this point in the history
  • Loading branch information
wuan committed Dec 2, 2022
1 parent c666cdf commit e6973b9
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 9 deletions.
24 changes: 24 additions & 0 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,30 @@ impl<S: Into<String>> From<S> 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>);
Expand Down
9 changes: 6 additions & 3 deletions src/core/rendezvous.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
41 changes: 35 additions & 6 deletions src/core/server_messages.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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<String>) -> Self {
Expand Down Expand Up @@ -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")),
Expand All @@ -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;
Expand Down

0 comments on commit e6973b9

Please sign in to comment.