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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#![feature(generic_associated_types)]
#![doc = include_str!("../README.markdown")]

use crystalorb::{
    clocksync::ClockSyncMessage,
    network_resource::{Connection, ConnectionHandleType, NetworkResource},
    timestamp::Timestamped,
    world::World,
};
use serde::{de::DeserializeOwned, Serialize};
use std::{
    any::{type_name, Any, TypeId},
    cell::{Cell, RefCell},
    collections::{HashMap, VecDeque},
    fmt::Debug,
    rc::Rc,
};

#[derive(Default)]
pub struct MockNetwork {
    pub connections: HashMap<ConnectionHandleType, MockConnection>,
}

impl MockNetwork {
    pub fn new_mock_network<WorldType: World>() -> (MockNetwork, (MockNetwork, MockNetwork)) {
        let mut client_1_net = MockNetwork::default();
        let mut client_2_net = MockNetwork::default();
        let mut server_net = MockNetwork::default();

        let (mut client_1_connection, mut server_1_connection) = MockConnection::new_pair();
        let (mut client_2_connection, mut server_2_connection) = MockConnection::new_pair();

        MockConnection::register_channel::<ClockSyncMessage>(
            &mut client_1_connection,
            &mut server_1_connection,
        );
        MockConnection::register_channel::<ClockSyncMessage>(
            &mut client_2_connection,
            &mut server_2_connection,
        );

        MockConnection::register_channel::<Timestamped<WorldType::SnapshotType>>(
            &mut client_1_connection,
            &mut server_1_connection,
        );
        MockConnection::register_channel::<Timestamped<WorldType::SnapshotType>>(
            &mut client_2_connection,
            &mut server_2_connection,
        );

        MockConnection::register_channel::<Timestamped<WorldType::CommandType>>(
            &mut client_1_connection,
            &mut server_1_connection,
        );
        MockConnection::register_channel::<Timestamped<WorldType::CommandType>>(
            &mut client_2_connection,
            &mut server_2_connection,
        );

        client_1_net.connections.insert(0, client_1_connection);
        client_2_net.connections.insert(0, client_2_connection);
        server_net.connections.insert(0, server_1_connection);
        server_net.connections.insert(1, server_2_connection);

        (server_net, (client_1_net, client_2_net))
    }

    pub fn connect(&mut self) {
        for connection in self.connections.values_mut() {
            connection.is_connected.set(true);
        }
    }

    pub fn disconnect(&mut self) {
        for connection in self.connections.values_mut() {
            connection.is_connected.set(false);
        }
    }

    pub fn set_delay(&mut self, delay: f64) {
        for connection in self.connections.values_mut() {
            connection.set_delay(delay);
        }
    }

    pub fn tick(&mut self, delta_seconds: f64) {
        for connection in self.connections.values_mut() {
            connection.tick(delta_seconds);
        }
    }
}

pub struct MockConnection {
    pub channels: HashMap<TypeId, Box<dyn DelayedChannel>>,
    pub is_connected: Rc<Cell<bool>>,
}

impl MockConnection {
    pub fn new_pair() -> (MockConnection, MockConnection) {
        let connection_1 = MockConnection {
            channels: Default::default(),
            is_connected: Rc::new(Cell::new(false)),
        };
        let connection_2 = MockConnection {
            channels: Default::default(),
            is_connected: connection_1.is_connected.clone(),
        };
        (connection_1, connection_2)
    }

    pub fn register_channel<T: 'static>(
        connection_1: &mut MockConnection,
        connection_2: &mut MockConnection,
    ) {
        let (channel_1, channel_2) = MockChannel::<T>::new_pair();
        let key = TypeId::of::<T>();
        connection_1.channels.insert(key, Box::new(channel_1));
        connection_2.channels.insert(key, Box::new(channel_2));
    }

    pub fn get_mut<T: 'static>(&mut self) -> &mut MockChannel<T> {
        let key = TypeId::of::<T>();
        self.channels
            .get_mut(&key)
            .unwrap_or_else(|| {
                panic!(
                    "Message of type {:?} should be registered",
                    type_name::<T>()
                )
            })
            .as_any()
            .downcast_mut()
            .expect("Channel is of the right type")
    }

    pub fn set_delay(&mut self, delay: f64) {
        for channel in self.channels.values_mut() {
            channel.set_delay(delay);
        }
    }

    pub fn tick(&mut self, delta_seconds: f64) {
        for channel in self.channels.values_mut() {
            channel.tick(delta_seconds);
        }
    }
}

pub struct MockChannel<T> {
    pub inbox: Rc<RefCell<DelayedQueue<T>>>,
    pub outbox: Rc<RefCell<DelayedQueue<T>>>,
}

pub trait DelayedChannel {
    fn tick(&mut self, delta_seconds: f64);
    fn set_delay(&mut self, delay: f64);
    fn as_any(&mut self) -> &mut dyn Any;
}

impl<T> MockChannel<T> {
    pub fn new_pair() -> (MockChannel<T>, MockChannel<T>) {
        let channel_1 = MockChannel {
            inbox: Rc::new(RefCell::new(DelayedQueue::new())),
            outbox: Rc::new(RefCell::new(DelayedQueue::new())),
        };
        let channel_2 = MockChannel {
            inbox: channel_1.outbox.clone(),
            outbox: channel_1.inbox.clone(),
        };
        (channel_1, channel_2)
    }

    pub fn send(&mut self, message: T) -> Option<T> {
        self.outbox.borrow_mut().send(message)
    }

    pub fn recv(&mut self) -> Option<T> {
        self.inbox.borrow_mut().recv()
    }

    pub fn new_outgoing_activity_count(&mut self) -> usize {
        self.outbox.borrow_mut().new_activity_count()
    }

    pub fn new_incoming_activity_count(&mut self) -> usize {
        self.inbox.borrow_mut().new_activity_count()
    }
}

impl<T: 'static> DelayedChannel for MockChannel<T> {
    fn tick(&mut self, delta_seconds: f64) {
        self.inbox.borrow_mut().tick(delta_seconds);
    }

    fn set_delay(&mut self, delay: f64) {
        self.inbox.borrow_mut().set_delay(delay);
        self.outbox.borrow_mut().set_delay(delay);
    }

    fn as_any(&mut self) -> &mut dyn Any {
        self
    }
}

#[derive(Default)]
pub struct DelayedQueue<T> {
    new_activity_count: usize,
    incoming: VecDeque<(T, f64)>,
    outgoing: VecDeque<T>,
    delay: f64,
}

impl<T> DelayedQueue<T> {
    pub fn new() -> Self {
        Self {
            new_activity_count: 0,
            incoming: VecDeque::new(),
            outgoing: VecDeque::new(),
            delay: 0.0,
        }
    }

    pub fn set_delay(&mut self, delay: f64) {
        self.delay = delay;
    }

    pub fn tick(&mut self, delta_seconds: f64) {
        while self
            .incoming
            .front()
            .map_or(false, |(_, age)| *age >= self.delay)
        {
            self.outgoing
                .push_back(self.incoming.pop_front().unwrap().0);
        }

        for (_, age) in &mut self.incoming {
            // Note: Never decrement age. The network is immune to timeskips for our
            // demo.
            *age += delta_seconds.max(0.0);
        }
    }

    pub fn send(&mut self, message: T) -> Option<T> {
        self.new_activity_count += 1;
        self.incoming.push_back((message, 0.0));
        None
    }

    pub fn recv(&mut self) -> Option<T> {
        self.outgoing.pop_front()
    }

    pub fn new_activity_count(&mut self) -> usize {
        let count = self.new_activity_count;
        self.new_activity_count = 0;
        count
    }
}

pub struct MockConnectionRef<'a>(&'a mut MockConnection);

impl<WorldType: World> NetworkResource<WorldType> for MockNetwork {
    type ConnectionType<'a>
    where
        Self: 'a,
        WorldType: 'a,
    = MockConnectionRef<'a>;

    fn get_connection(&mut self, handle: ConnectionHandleType) -> Option<Self::ConnectionType<'_>> {
        self.connections
            .get_mut(&handle)
            .filter(|connection| connection.is_connected.get())
            .map(MockConnectionRef)
    }

    fn connections<'a>(
        &'a mut self,
    ) -> Box<dyn Iterator<Item = (ConnectionHandleType, Self::ConnectionType<'a>)> + 'a> {
        Box::new(
            self.connections
                .iter_mut()
                .filter(|(_handle, connection)| connection.is_connected.get())
                .map(|(handle, connection)| (*handle, MockConnectionRef(connection))),
        )
    }
}

impl<'a, WorldType: World> Connection<WorldType> for MockConnectionRef<'a> {
    fn recv_command(&mut self) -> Option<Timestamped<WorldType::CommandType>> {
        assert!(self.0.is_connected.get());
        self.0
            .get_mut::<Timestamped<WorldType::CommandType>>()
            .recv()
    }

    fn recv_snapshot(&mut self) -> Option<Timestamped<WorldType::SnapshotType>> {
        assert!(self.0.is_connected.get());
        self.0
            .get_mut::<Timestamped<WorldType::SnapshotType>>()
            .recv()
    }

    fn recv_clock_sync(&mut self) -> Option<ClockSyncMessage> {
        assert!(self.0.is_connected.get());
        self.0.get_mut::<ClockSyncMessage>().recv()
    }

    fn send<MessageType>(&mut self, message: MessageType) -> Option<MessageType>
    where
        MessageType: Debug + Clone + Serialize + DeserializeOwned + Send + Sync + 'static,
    {
        assert!(self.0.is_connected.get());
        self.0.get_mut::<MessageType>().send(message)
    }
    fn flush<MessageType>(&mut self)
    where
        MessageType: Debug + Clone + Serialize + DeserializeOwned + Send + Sync + 'static,
    {
        assert!(self.0.is_connected.get());
        // No-op in our mock network. You may want to forward this call to your network library.
    }
}