1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
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 peer_key: Vec<u8>,
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: Vec<u8>,
pub peer2: Vec<u8>,
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",
"peerKey": [1],
"os": "macOS",
"isConnectedToDittoCloud": false,
"isCompatible": true,
"dittoSdkVersion": "1.0.0",
"meshRole": 0,
"queryOverlapGroup": 0,
"connections" : [
{
"id": "01<->02:Bluetooth",
"peer1": [1],
"peer2": [2],
"connectionType": "Bluetooth",
"approximateDistanceInMeters": 2.2963063716888428
},
{
"id": "01<->03:AccessPoint",
"peer1": [1],
"peer2": [3],
"connectionType": "AccessPoint",
"approximateDistanceInMeters": null
},
{
"id": "01<->04:WebSocket",
"peer1": [1],
"peer2": [4],
"connectionType": "WebSocket",
"approximateDistanceInMeters": null
}
]
},
"remotePeers": [
{
"address": {"siteId":2, "pubkey":[2]},
"peerKey": [2],
"deviceName": "device-2",
"os": "iOS",
"isConnectedToDittoCloud": false,
"isCompatible": true,
"dittoSdkVersion": null,
"meshRole": 0,
"queryOverlapGroup": 0,
"connections" : [
{
"id": "01<->02:Bluetooth",
"peer1": [1],
"peer2": [2],
"connectionType": "Bluetooth",
"approximateDistanceInMeters": 2.2963063716888428
}
]
},
{
"address": {"siteId":3, "pubkey":[3]},
"peerKey": [3],
"deviceName": "device-3",
"os": "Android",
"isConnectedToDittoCloud": false,
"isCompatible": true,
"dittoSdkVersion": "1.0.3",
"meshRole": 32,
"queryOverlapGroup": 32,
"connections" : [
{
"id": "01<->03:AccessPoint",
"peer1": [1],
"peer2": [3],
"connectionType": "AccessPoint",
"approximateDistanceInMeters": null
}
]
},
{
"address": {"siteId":4, "pubkey":[4]},
"peerKey": [4],
"deviceName": "device-4",
"os": "Linux",
"isConnectedToDittoCloud": false,
"isCompatible": true,
"dittoSdkVersion": null,
"connections" : [
{
"id": "01<->04:WebSocket",
"peer1": [1],
"peer2": [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.local_peer.peer_key, vec![1]);
assert_eq!(graph.remote_peers[0].address.pubkey, vec![2]);
assert_eq!(graph.remote_peers[0].peer_key, 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].peer_key, 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].peer_key, 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);
}
}