Ver código fonte

Move protocol-specific test to protocol::, move Packet to network::.

Mathias Gottschlag 5 anos atrás
pai
commit
51a7112558
4 arquivos alterados com 114 adições e 114 exclusões
  1. 1
    1
      src/network/client.rs
  2. 40
    1
      src/network/mod.rs
  3. 0
    112
      src/network/packet.rs
  4. 73
    0
      src/protocol.rs

+ 1
- 1
src/network/client.rs Ver arquivo

@@ -14,7 +14,7 @@ use tokio::sync::{mpsc, oneshot};
14 14
 use tokio::time::{delay_for, Delay};
15 15
 
16 16
 use super::idalloc::IdAllocator;
17
-use super::packet::{Packet, SERVER_EVENT_ID};
17
+use super::{Packet, SERVER_EVENT_ID};
18 18
 
19 19
 pub fn create<StreamType, ErrorType, PacketType>(
20 20
     stream: StreamType,

+ 40
- 1
src/network/mod.rs Ver arquivo

@@ -1,5 +1,44 @@
1
+//! Module which implements a low-level packet protocol over websockets.
2
+//!
3
+//! The low-level protocol needs to implement two basic pieces of functionality: It needs to be
4
+//! able to implement remote procedure calls from the client to the server, and it needs to
5
+//! provide a method for the server to send asynchronous events. A protocol for both cases is
6
+//! outlined below, where each packet is sent as one WebSocket message.
7
+//!
8
+//! Remote procedure call:
9
+//! - The client allocates an ID from the range between 0..2^31-1 that is not currently in use
10
+//!   for another RPC in progress.
11
+//! - The client prepares a packet with this ID, "response = false", and the appropriate
12
+//!   payload and sends the packet to the server.
13
+//! - The server receives the packet, processes the request and sends a packet back with the
14
+//!   identical ID and "response = true".
15
+//! - In case of an error, the payload has the type "Error" which in turn contains the error
16
+//!   type.
17
+//! - Unpon reception of the response, the client is free to reuse the ID for further RPCs.
18
+//! Note that this protocol does not allow the client to cancel an RPC that is currently in
19
+//! progress - implementing such functionality is difficult, error-prone, and most likely not
20
+//! worth the effort.
21
+//!
22
+//! Server-side event:
23
+//! - The server creates a packet with the ID 2^31 and the event content and sends it to the
24
+//!   client.
25
+//! Note that the client does not have any method to acknowledge event reception. As events are
26
+//! asynchronous compared to RPCs on the same connection, the event type needs to be designed in
27
+//! a way that no such temporal information is necessary to process the events.
28
+//! TODO: Example to demonstrate the problem.
29
+
1 30
 pub mod client;
2 31
 pub mod idalloc;
3
-pub mod packet;
4 32
 pub mod server;
5 33
 pub mod websocket;
34
+
35
+use serde::{Deserialize, Serialize};
36
+
37
+pub const SERVER_EVENT_ID: u32 = u32::max_value();
38
+
39
+#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
40
+pub struct Packet<Payload> {
41
+    pub id: u32,
42
+    pub response: bool, // TODO: Not really required.
43
+    pub payload: Payload,
44
+}

+ 0
- 112
src/network/packet.rs Ver arquivo

@@ -1,112 +0,0 @@
1
-//! Module which implements a low-level packet protocol over websockets.
2
-//!
3
-//! The low-level protocol needs to implement two basic pieces of functionality: It needs to be
4
-//! able to implement remote procedure calls from the client to the server, and it needs to
5
-//! provide a method for the server to send asynchronous events. A protocol for both cases is
6
-//! outlined below, where each packet is sent as one WebSocket message.
7
-//!
8
-//! Remote procedure call:
9
-//! - The client allocates an ID from the range between 0..2^31-1 that is not currently in use
10
-//!   for another RPC in progress.
11
-//! - The client prepares a packet with this ID, "response = false", and the appropriate
12
-//!   payload and sends the packet to the server.
13
-//! - The server receives the packet, processes the request and sends a packet back with the
14
-//!   identical ID and "response = true".
15
-//! - In case of an error, the payload has the type "Error" which in turn contains the error
16
-//!   type.
17
-//! - Unpon reception of the response, the client is free to reuse the ID for further RPCs.
18
-//! Note that this protocol does not allow the client to cancel an RPC that is currently in
19
-//! progress - implementing such functionality is difficult, error-prone, and most likely not
20
-//! worth the effort.
21
-//!
22
-//! Server-side event:
23
-//! - The server creates a packet with the ID 2^31 and the event content and sends it to the
24
-//!   client.
25
-//! Note that the client does not have any method to acknowledge event reception. As events are
26
-//! asynchronous compared to RPCs on the same connection, the event type needs to be designed in
27
-//! a way that no such temporal information is necessary to process the events.
28
-//! TODO: Example to demonstrate the problem.
29
-
30
-use serde::{Deserialize, Serialize};
31
-
32
-pub const SERVER_EVENT_ID: u32 = u32::max_value();
33
-
34
-#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
35
-pub struct Packet<Payload> {
36
-    pub id: u32,
37
-    pub response: bool, // TODO: Not really required.
38
-    pub payload: Payload,
39
-}
40
-
41
-#[cfg(test)]
42
-mod tests {
43
-    use rmps::Serializer;
44
-
45
-    use super::*;
46
-    use crate::protocol::{PacketPayload, ServerInfoResponse};
47
-
48
-    /// Test whether serialization of packets matches the expected values.
49
-    /// TODO: TGhese tests should be in protocol.rs.
50
-    #[test]
51
-    fn test_serde_compatibility() {
52
-        let tests = [
53
-            (
54
-                Packet {
55
-                    id: 42,
56
-                    response: false,
57
-                    payload: PacketPayload::ServerInfo,
58
-                },
59
-                vec![
60
-                    131, 162, 105, 100, 42, 168, 114, 101, 115, 112, 111, 110, 115, 101, 194, 167,
61
-                    112, 97, 121, 108, 111, 97, 100, 129, 170, 83, 101, 114, 118, 101, 114, 73,
62
-                    110, 102, 111, 192,
63
-                ],
64
-            ),
65
-            (
66
-                Packet {
67
-                    id: 42,
68
-                    response: true,
69
-                    payload: PacketPayload::ServerInfoResponse(ServerInfoResponse {
70
-                        version: "0.1".to_owned(),
71
-                    }),
72
-                },
73
-                vec![
74
-                    131, 162, 105, 100, 42, 168, 114, 101, 115, 112, 111, 110, 115, 101, 195, 167,
75
-                    112, 97, 121, 108, 111, 97, 100, 129, 178, 83, 101, 114, 118, 101, 114, 73,
76
-                    110, 102, 111, 82, 101, 115, 112, 111, 110, 115, 101, 129, 167, 118, 101, 114,
77
-                    115, 105, 111, 110, 163, 48, 46, 49,
78
-                ],
79
-            ),
80
-            (
81
-                Packet {
82
-                    id: 42,
83
-                    response: true,
84
-                    payload: PacketPayload::Error(crate::protocol::Error::InvalidParameter),
85
-                },
86
-                vec![
87
-                    131, 162, 105, 100, 42, 168, 114, 101, 115, 112, 111, 110, 115, 101, 195, 167,
88
-                    112, 97, 121, 108, 111, 97, 100, 129, 165, 69, 114, 114, 111, 114, 129, 176,
89
-                    73, 110, 118, 97, 108, 105, 100, 80, 97, 114, 97, 109, 101, 116, 101, 114, 192,
90
-                ],
91
-            ),
92
-        ];
93
-
94
-        for test in tests.iter() {
95
-            let mut serialized = Vec::new();
96
-            test.0
97
-                .serialize(
98
-                    &mut Serializer::new(&mut serialized)
99
-                        .with_struct_map()
100
-                        .with_string_variants(),
101
-                )
102
-                .unwrap();
103
-            let deserialized: Packet<PacketPayload> =
104
-                rmps::decode::from_slice(&serialized).unwrap();
105
-
106
-            // Test that serializing produces the expected binary results.
107
-            assert_eq!(serialized, test.1);
108
-            // Test that deserializing yields the original again.
109
-            assert_eq!(deserialized, test.0);
110
-        }
111
-    }
112
-}

+ 73
- 0
src/protocol.rs Ver arquivo

@@ -136,3 +136,76 @@ pub struct GetFileContent {
136 136
 pub struct GetFileContentResponse {
137 137
     // TODO
138 138
 }
139
+
140
+#[cfg(test)]
141
+mod tests {
142
+    use rmps::Serializer;
143
+
144
+    use super::*;
145
+    use crate::network::Packet;
146
+
147
+    /// Test whether serialization of packets matches the expected values.
148
+    /// TODO: These tests should be in protocol.rs.
149
+    #[test]
150
+    fn test_serde_compatibility() {
151
+        let tests = [
152
+            (
153
+                Packet {
154
+                    id: 42,
155
+                    response: false,
156
+                    payload: PacketPayload::ServerInfo,
157
+                },
158
+                vec![
159
+                    131, 162, 105, 100, 42, 168, 114, 101, 115, 112, 111, 110, 115, 101, 194, 167,
160
+                    112, 97, 121, 108, 111, 97, 100, 129, 170, 83, 101, 114, 118, 101, 114, 73,
161
+                    110, 102, 111, 192,
162
+                ],
163
+            ),
164
+            (
165
+                Packet {
166
+                    id: 42,
167
+                    response: true,
168
+                    payload: PacketPayload::ServerInfoResponse(ServerInfoResponse {
169
+                        version: "0.1".to_owned(),
170
+                    }),
171
+                },
172
+                vec![
173
+                    131, 162, 105, 100, 42, 168, 114, 101, 115, 112, 111, 110, 115, 101, 195, 167,
174
+                    112, 97, 121, 108, 111, 97, 100, 129, 178, 83, 101, 114, 118, 101, 114, 73,
175
+                    110, 102, 111, 82, 101, 115, 112, 111, 110, 115, 101, 129, 167, 118, 101, 114,
176
+                    115, 105, 111, 110, 163, 48, 46, 49,
177
+                ],
178
+            ),
179
+            (
180
+                Packet {
181
+                    id: 42,
182
+                    response: true,
183
+                    payload: PacketPayload::Error(crate::protocol::Error::InvalidParameter),
184
+                },
185
+                vec![
186
+                    131, 162, 105, 100, 42, 168, 114, 101, 115, 112, 111, 110, 115, 101, 195, 167,
187
+                    112, 97, 121, 108, 111, 97, 100, 129, 165, 69, 114, 114, 111, 114, 129, 176,
188
+                    73, 110, 118, 97, 108, 105, 100, 80, 97, 114, 97, 109, 101, 116, 101, 114, 192,
189
+                ],
190
+            ),
191
+        ];
192
+
193
+        for test in tests.iter() {
194
+            let mut serialized = Vec::new();
195
+            test.0
196
+                .serialize(
197
+                    &mut Serializer::new(&mut serialized)
198
+                        .with_struct_map()
199
+                        .with_string_variants(),
200
+                )
201
+                .unwrap();
202
+            let deserialized: Packet<PacketPayload> =
203
+                rmps::decode::from_slice(&serialized).unwrap();
204
+
205
+            // Test that serializing produces the expected binary results.
206
+            assert_eq!(serialized, test.1);
207
+            // Test that deserializing yields the original again.
208
+            assert_eq!(deserialized, test.0);
209
+        }
210
+    }
211
+}

Carregando…
Cancelar
Salvar