use std::cmp::Ordering;
use serde::{Deserialize, Serialize};
use super::SiteId;
#[derive(Deserialize, Serialize, PartialEq, Eq, PartialOrd, Ord, Debug, Clone, Hash)]
#[serde(rename_all = "camelCase")]
pub struct DittoAddress {
pub(crate) pubkey: Vec<u8>,
pub(crate) site_id: Option<SiteId>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PresenceGraph {
pub local_peer: Peer,
pub remote_peers: Vec<Peer>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Peer {
pub address: DittoAddress,
pub device_name: String,
pub os: Option<PresenceOs>,
#[serde(default)]
pub query_overlap_group: u8,
pub is_connected_to_ditto_cloud: bool,
pub is_compatible: Option<bool>,
pub ditto_sdk_version: Option<String>,
pub connections: Vec<UndirectedConnection>,
}
impl Eq for Peer {}
impl PartialEq<Self> for Peer {
fn eq(&self, other: &Self) -> bool {
self.address.eq(&other.address)
}
}
impl PartialOrd for Peer {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for Peer {
fn cmp(&self, other: &Self) -> Ordering {
self.address.cmp(&other.address)
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct UndirectedConnection {
pub id: String,
pub peer1: DittoAddress,
pub peer2: DittoAddress,
pub connection_type: ConnectionType,
pub approximate_distance_in_meters: Option<f32>,
}
impl Eq for UndirectedConnection {}
impl PartialEq<Self> for UndirectedConnection {
fn eq(&self, other: &Self) -> bool {
self.id == other.id
}
}
impl PartialOrd for UndirectedConnection {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for UndirectedConnection {
fn cmp(&self, other: &Self) -> Ordering {
self.id.cmp(&other.id)
}
}
#[derive(PartialEq, Eq, Hash, Clone, Debug, Serialize, Deserialize)]
pub enum PresenceOs {
#[serde(rename = "Generic")]
Generic,
#[serde(rename = "iOS")]
Ios,
#[serde(rename = "Android")]
Android,
#[serde(rename = "Linux")]
Linux,
#[serde(rename = "Windows")]
Windows,
#[serde(rename = "macOS")]
MacOS,
}
#[derive(PartialEq, Eq, Hash, Ord, PartialOrd, Clone, Copy, Debug, Serialize, Deserialize)]
pub enum ConnectionType {
Bluetooth,
AccessPoint,
P2PWiFi,
WebSocket,
}
#[cfg(test)]
mod tests {
use super::*;
const V3_PEER_JSON: &str = r#"
{
"localPeer": {
"address": {"siteId":1, "pubkey":[1]},
"deviceName": "local-peer",
"os": "macOS",
"isConnectedToDittoCloud": false,
"isCompatible": true,
"dittoSdkVersion": "1.0.0",
"meshRole": 0,
"queryOverlapGroup": 0,
"connections" : [
{
"id": "1<->2:Bluetooth",
"peer1": {"siteId":1, "pubkey":[1]},
"peer2": {"siteId":2, "pubkey":[2]},
"connectionType": "Bluetooth",
"approximateDistanceInMeters": 2.2963063716888428
},
{
"id": "1<->3:AccessPoint",
"peer1": {"siteId":1, "pubkey":[1]},
"peer2": {"siteId":3, "pubkey":[3]},
"connectionType": "AccessPoint",
"approximateDistanceInMeters": null
},
{
"id": "1<->4:WebSocket",
"peer1": {"siteId":1, "pubkey":[1]},
"peer2": {"siteId":4, "pubkey":[4]},
"connectionType": "WebSocket",
"approximateDistanceInMeters": null
}
]
},
"remotePeers": [
{
"address": {"siteId":2, "pubkey":[2]},
"deviceName": "device-2",
"os": "iOS",
"isConnectedToDittoCloud": false,
"isCompatible": true,
"dittoSdkVersion": null,
"meshRole": 0,
"queryOverlapGroup": 0,
"connections" : [
{
"id": "1<->2:Bluetooth",
"peer1": {"siteId":1, "pubkey":[1]},
"peer2": {"siteId":2, "pubkey":[2]},
"connectionType": "Bluetooth",
"approximateDistanceInMeters": 2.2963063716888428
}
]
},
{
"address": {"siteId":3, "pubkey":[3]},
"deviceName": "device-3",
"os": "Android",
"isConnectedToDittoCloud": false,
"isCompatible": true,
"dittoSdkVersion": "1.0.3",
"meshRole": 32,
"queryOverlapGroup": 32,
"connections" : [
{
"id": "1<->3:AccessPoint",
"peer1": {"siteId":1, "pubkey":[1]},
"peer2": {"siteId":3, "pubkey":[3]},
"connectionType": "AccessPoint",
"approximateDistanceInMeters": null
}
]
},
{
"address": {"siteId":4, "pubkey":[4]},
"deviceName": "device-4",
"os": "Linux",
"isConnectedToDittoCloud": false,
"isCompatible": true,
"dittoSdkVersion": null,
"connections" : [
{
"id": "1<->4:WebSocket",
"peer1": {"siteId":1, "pubkey":[1]},
"peer2": {"siteId":4, "pubkey":[4]},
"connectionType": "WebSocket",
"approximateDistanceInMeters": null
}
]
}
]
}
"#;
#[test]
fn test_json_parsing() {
let graph: PresenceGraph = serde_json::from_str(V3_PEER_JSON).unwrap();
assert_eq!(graph.local_peer.address.pubkey, vec![1]);
assert_eq!(graph.local_peer.device_name, "local-peer");
assert_eq!(graph.local_peer.os, Some(PresenceOs::MacOS));
assert_eq!(graph.local_peer.query_overlap_group, 0);
assert_eq!(graph.local_peer.query_overlap_group, 0);
assert_eq!(graph.local_peer.connections.len(), 3);
assert_eq!(graph.remote_peers[0].address.pubkey, vec![2]);
assert_eq!(graph.remote_peers[0].device_name, "device-2");
assert_eq!(graph.remote_peers[0].os, Some(PresenceOs::Ios));
assert_eq!(graph.remote_peers[0].query_overlap_group, 0);
assert_eq!(graph.remote_peers[1].address.pubkey, vec![3]);
assert_eq!(graph.remote_peers[1].device_name, "device-3");
assert_eq!(graph.remote_peers[1].os, Some(PresenceOs::Android));
assert_eq!(graph.remote_peers[1].query_overlap_group, 32);
assert_eq!(graph.remote_peers[2].address.pubkey, vec![4]);
assert_eq!(graph.remote_peers[2].device_name, "device-4");
assert_eq!(graph.remote_peers[2].os, Some(PresenceOs::Linux));
assert_eq!(graph.remote_peers[2].query_overlap_group, 0);
}
}