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 52bd2ab
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 4 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(pub [String; 2]);

impl ClientVersion {
pub fn new(name: String, version: String) -> Self {
let array = [name, version];
ClientVersion { 0: array }
}
}

impl std::ops::Deref for ClientVersion {
type Target = [String; 2];
fn deref(&self) -> &[String; 2] {
&self.0
}
}

impl std::ops::DerefMut for ClientVersion {
fn deref_mut(&mut self) -> &mut [String; 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
40 changes: 36 additions & 4 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 @@ -135,10 +135,16 @@ impl EncryptedMessage {
pub enum OutboundMessage {
#[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,
},
List,
Allocate,
Expand Down Expand Up @@ -177,7 +183,14 @@ pub enum OutboundMessage {

impl OutboundMessage {
pub fn bind(appid: AppID, side: MySide) -> Self {
OutboundMessage::Bind { appid, side }
let client_name: String = String::from("rust");
let client_version_string: String = String::from(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,27 @@ 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(String::from("foo"), String::from("1.0.2"));

assert_eq!(client_version.to_string(), "foo-1.0.2")
}

#[test]
fn test_client_version_deref() {
let client_version = ClientVersion::new(String::from("bar"), String::from("0.8.9"));

assert_eq!(
client_version.deref(),
&[String::from("bar"), String::from("0.8.9")]
)
}

#[test]
fn test_list() {
let m1 = OutboundMessage::List;
Expand Down

0 comments on commit 52bd2ab

Please sign in to comment.