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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
use mio::tcp::*;
use mio;
use mio::{Token, Handler, EventLoop, EventSet, PollOpt, TryRead, TryWrite, Evented};
use std::io::{self, Cursor, Write, Read};
use std::net::{self, SocketAddr};
use std::time::Duration;
use std::mem;
use std::iter;
use std::thread::{self, JoinHandle};
use event_loop::EVENT_LOOP;
use std::sync::mpsc::{Receiver, Sender, channel};
use result::{ThrustResult, ThrustError};
use tangle::{Future, Async};
use bytes::buf::Buf;
use std::collections::HashMap;
use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
use libc;
use std::os::unix::io::AsRawFd;

pub struct Id(pub Token);

/// Communication into the Mio event loop happens with a `Message`. For each new Mio
/// event loop, a mio-specific `Sender<Message>` is returned.
#[derive(Debug, Clone)]
pub enum Message {
    /// `Connect` establishes a new `TcpStream` with a specified remote. The
    /// `Sender` channel part is used to communicate back with the initiator on
    /// certain socket events.
    ///
    /// The first `Sender` is used to communicate back the assigned `Token`.
    Connect(SocketAddr, Sender<Id>, Sender<Dispatch>),
    /// To give a tighter feedback loop, a `Bind` message will accept a normal
    /// Rust blocking net::TcpListener. This allows the user to more easily handle
    /// binding errors before sending it into the event loop where you need to
    /// handle any errors asynchronously.
    Bind(SocketAddr, Sender<Id>, Sender<Dispatch>),
    /// Initiate an `Rpc` request. Each request needs to know which `Token` the respective
    /// `Connection` is associated with. The `Reactor` also knows nothing about Thrift
    /// and simply works at the binary level.
    ///
    /// An `Rpc` message is also used for replying to an RPC call.
    Rpc(Token, Vec<u8>),
    /// Completely shutdown the `Reactor` and event loop. All current listeners
    /// and connections will be dropped.
    Shutdown
}

/// Communication from the `Reactor` to outside components happens with a `Dispatch` message
/// and normal Rust channels instead of Mio's variant.
#[derive(Debug)]
pub enum Dispatch {
    /// When a socket has been read, the `Reactor` will send the `Dispatch::Data` message
    /// to the associating channel.
    ///
    /// We also associate any incoming data with the Token of the responsible socket.
    Data(Token, Vec<u8>)
}

pub enum Timeout {
    Reconnect(Token)
}

#[derive(Debug, PartialEq, Eq)]
pub enum State {
    /// The length that has been read so far.
    ReadingFrame(usize),
    Reading,
    Writing,
    Closed
}

#[derive(Debug)]
pub enum FrameState {
    Reading(u32),
    Writing
}

/// Wrap a `TcpStream` to handle reading and writing frames. Frames are simply some encoded thrift
/// protocol byte buffer preceeded by a 32-bit unsigned length.
pub struct FramedTransport {
    buffer: Vec<u8>,
    state: FrameState
}

impl FramedTransport {
    pub fn new() -> FramedTransport {
        FramedTransport {
            buffer: Vec::new(),
            state: FrameState::Reading(0)
        }
    }

    pub fn read<S>(&mut self, socket: &mut S) -> ThrustResult<Option<Vec<u8>>>
        where S: TryRead + TryWrite
    {
        match self.state {
            FrameState::Reading(total_len) if self.buffer.len() == 0 => {
                let mut buf = Vec::with_capacity(4);
                buf.extend(iter::repeat(0).take(4));

                // Try reading the first unsigned 32-bits for the length of the frame.
                let len = match socket.try_read_buf(&mut buf)? {
                    Some(n) if n == 4 => {
                        let mut buf = Cursor::new(buf);
                        buf.read_u32::<BigEndian>()?
                    },
                    Some(n) => {
                        return Err(ThrustError::Other);
                    },
                    None => panic!("err")
                };

                // Set our internal buffer.
                self.buffer = Vec::with_capacity(len as usize);
                self.buffer.extend(iter::repeat(0).take(len as usize));
                return self.read(socket);
            },
            FrameState::Reading(ref mut total_len) => {
                while let Some(n) = socket.try_read_buf(&mut self.buffer)? {
                    *total_len += n as u32;
                    if *total_len == self.buffer.len() as u32 {
                        return Ok(Some(mem::replace(&mut self.buffer, Vec::new())));
                    } else if n == 0 {
                        return Ok(None);
                    }
                }
            },
            FrameState::Writing => return Err(ThrustError::Other)
        }

        Ok(None)
    }
}


pub struct Connection {
    stream: TcpStream,
    addr: SocketAddr,
    pub token: Token,
    state: State,
    chan: Sender<Dispatch>,
    rbuffer: Vec<u8>,
    wbuffer: Cursor<Vec<u8>>
}

impl Connection {
    pub fn new(conn: (TcpStream, SocketAddr), token: Token, chan: Sender<Dispatch>) -> Self {
        Connection {
            stream: conn.0,
            addr: conn.1,
            token: token,
            state: State::Reading,
            chan: chan,
            rbuffer: vec![],
            wbuffer: Cursor::new(vec![])
        }
    }

    pub fn reset(&mut self, event_loop: &mut EventLoop<Reactor>) {
        event_loop.timeout(Timeout::Reconnect(self.token), Duration::from_millis(10));
    }

    pub fn ready(&mut self, event_loop: &mut EventLoop<Reactor>, events: EventSet) {
        match self.state {
            State::Reading if events.is_readable() => {
                self.readable();
                self.reregister(event_loop, self.token);
            },
            State::Writing if events.is_writable() => {
                self.writable();
                self.reregister(event_loop, self.token);
            },
            _ => {
                self.reregister(event_loop, self.token);
            }
        }
    }

    pub fn read(&mut self) -> ThrustResult<Option<Vec<u8>>> {
        match self.state {
            State::Reading => {
                let len = self.stream.read_u32::<BigEndian>()?;
                self.state = State::ReadingFrame(0);
                self.rbuffer = Vec::with_capacity(len as usize);
                self.read()
            },
            State::ReadingFrame(ref mut len) => {
                match self.stream.try_read_buf(&mut self.rbuffer) {
                    Ok(Some(n)) => {
                        if *len + n == self.rbuffer.len() {
                            let buf = mem::replace(&mut self.rbuffer, Vec::new());
                            Ok(Some(buf))
                        } else {
                            *len += n;
                            // We don't have a complete frame yet.
                            Ok(None)
                        }
                    },
                    Ok(None) => Err(ThrustError::NotReady),
                    Err(err) => Err(ThrustError::Other)
                }
            },
            _ => Err(ThrustError::Other)
        }
    }

    pub fn writable(&mut self) -> ThrustResult<()> {
        // Flush the whole buffer. The socket can, at any time, be unwritable. Thus, we
        // need to keep track of what we've written so far.
        while self.wbuffer.has_remaining() {
            self.flush();
        }

        self.state = State::Reading;

        Ok(())
    }

    pub fn readable(&mut self) -> ThrustResult<()> {
        while let Ok(op) = self.read() {
            match op {
                Some(buf) => {
                    self.state = State::Reading;
                    self.chan.send(Dispatch::Data(self.token, buf));
                },
                None => {}
            }
        }

        self.state = State::Writing;

        Ok(())
    }

    fn register(&mut self, event_loop: &mut EventLoop<Reactor>, token: Token) -> ThrustResult<()> {
        event_loop.register(&self.stream, token, EventSet::readable(), PollOpt::edge() | PollOpt::oneshot())?;
        Ok(())
    }

    pub fn reregister(&self, event_loop: &mut EventLoop<Reactor>, token: Token) -> ThrustResult<()> {
        let event_set = match self.state {
            State::Reading => EventSet::readable(),
            State::Writing => EventSet::writable(),
            _ => EventSet::none()
        };

        event_loop.reregister(&self.stream, self.token, event_set, PollOpt::oneshot())?;
        Ok(())
    }
}

impl Write for Connection {
    fn write(&mut self, data: &[u8]) -> io::Result<usize> {
        self.wbuffer.get_mut().write_u32::<BigEndian>(data.len() as u32)?;
        self.wbuffer.get_mut().write(data)?;
        self.flush()?;
        Ok(0)
    }

    fn flush(&mut self) -> io::Result<()> {
        match self.stream.try_write_buf(&mut self.wbuffer) {
            Ok(Some(_)) => Ok(()),
            Ok(None) => Ok(()),
            Err(err) => Err(err)
        }
    }
}

/// The `Reactor` is the component that interacts with networking. The reactor is
/// built around Mio's event loop and manages both TcpListeners and TcpStreams.
///
/// The reactor isn't responsible for anything Thrift related, so it doesn't know
/// about parsing, protocols, serialization, etc... All it's responsible for
/// is sending and receiving data from various connections and dispatching them
/// to the appropriate channels.
///
/// To communicate into the event loop, you use a copy of Mio's `Sender` channel
/// type to send `Message`s. These will be intercepted in the event loop and processed.
///
/// Things you might send to the `Reactor` through this mechanism:
///
/// 1. Binding a new TCP listener &mdash; Each reactor is capable of handling an unbounded
/// number of listeners, who will all be able to accept new sockets.
///
/// Binding a new listener requires that you have already established a blocking variant
/// through `net::TcpListener`. The listener will be converted to Mio's non-blocking variant.
///
/// ```notrust
/// use std::net::TcpListener;
/// use std::sync::mpsc::channel;
///
/// // The channel is used as a channel. Any socket being accepted
/// // by this listener will also use this channel (the sender part).
/// let (tx, rx) = channel();
/// let addr = "127.0.0.1:4566".parse().unwrap();
///
/// reactor_sender.send(Message::Bind(addr, tx));
/// ```
///
/// 2. Connecting to a remote TCP server and establishing a new non-blocking `TcpStream`.
///
/// ```notrust
/// use std::sync::mpsc::channel;
///
/// // The callback channel on the single socket.
/// let (tx, rx) = channel();
/// let addr = "127.0.0.1::4566".parse().unwrap();
/// reactor_sender.send(Message::Connect(addr, tx));
/// ```
///
///
/// 3. Sending RPC calls (initiating or reply) to a socket. Instead of writing or reading
/// primitives, we boil everything down to Rpc or Data messages, each with an associative Token
/// to mark the responsible `TcpStream` or `Connection`.
///
/// ```notrust
/// reactor_sender.send(Message::Rpc(Token(1), vec![0, 1, 3, 4]));
/// ```
pub struct Reactor {
    listeners: HashMap<Token, TcpListener>,
    connections: HashMap<Token, Connection>,
    /// Channels that are sent from `::Bind` messages to establish a listener will
    /// be appended in this map. All subsequent sockets being accepted from the listener
    /// will use the same sender channel to consolidate communications.
    servers: HashMap<Token, Sender<Dispatch>>,
    /// The `Reactor` manages a count of the number of tokens, the number being
    /// the token used for the next allocated resource. Tokens are used sequentially
    /// across both listeners and connections.
    current_token: usize
}

impl Reactor {
    pub fn new() -> Reactor {
        Reactor {
            listeners: HashMap::new(),
            connections: HashMap::new(),
            servers: HashMap::new(),
            current_token: 0
        }
    }

    pub fn run() -> JoinHandle<()> {
        thread::spawn(move || {
            let mut event_loop = EVENT_LOOP.lock().expect("Failed to take the `EVENT_LOOP` lock.");
            let mut reactor = Reactor::new();
            event_loop.run(&mut reactor);
        })
    }

    pub fn incoming_timeout(&mut self, event_loop: &mut EventLoop<Self>, timeout: Timeout) -> ThrustResult<()> {
        match timeout {
            Timeout::Reconnect(token) => {
                let mut conn = self.connections.get_mut(&token).expect("Could not find the connection.");
                let mut stream = TcpStream::connect(&conn.addr)?;
                conn.stream = stream;
                conn.register(event_loop, token);
            }
        }

        Ok(())
    }

    pub fn incoming_msg(&mut self, event_loop: &mut EventLoop<Self>, msg: Message) -> ThrustResult<()> {
        match msg {
            Message::Rpc(id, data) => {
                self.connections.get_mut(&id).expect("connection was not found #2").write(&*data);
            },
            Message::Shutdown => {
                println!("Shutting down...");
                event_loop.shutdown();
            },
            Message::Connect(addr, id_tx, tx) => {
                let mut mio_stream = TcpStream::connect(&addr)?;
                let new_token = Token(self.current_token);
                id_tx.send(Id(new_token));
                let mut conn = Connection::new((mio_stream, addr), new_token, tx);

                self.connections.insert(new_token, conn);

                self.connections.get_mut(&new_token)
                    .expect("Cannot find the connection from the token {:?}")
                    .register(event_loop, new_token);

                self.current_token += 1;
            },
            Message::Bind(addr, id_tx, tx) => {
                let token = Token(self.current_token);
                let mut lis = TcpListener::bind(&addr)?;

                id_tx.send(Id(token));
                self.servers.insert(token, tx);

                event_loop.register(&lis, token, EventSet::readable(), PollOpt::edge())?;
                self.listeners.insert(token, lis);
                self.current_token += 1;
            }
        }

        Ok(())
    }

    pub fn accept_connection(&mut self, event_loop: &mut EventLoop<Self>, token: Token) {
        let mut listener = self.listeners.get_mut(&token).expect("Listener was not found.");
        match listener.accept() {
            Ok(Some(socket)) => {
                let clone = self.servers[&token].clone();
                let new_token = Token(self.current_token);
                let mut conn = Connection::new(socket, new_token, clone);

                self.connections.insert(new_token, conn);
                self.connections.get_mut(&new_token)
                    .expect("Cannot find the connection in the cache.")
                    .register(event_loop, new_token);

                self.current_token += 1;
            },
            _ => {}
        }
    }
}

impl Handler for Reactor {
    type Timeout = Timeout;
    type Message = Message;

    fn ready(&mut self, event_loop: &mut EventLoop<Self>, token: Token, events: EventSet) {
        if events.is_hup() {
            if self.connections.contains_key(&token) {
                let mut conn = self.connections.get_mut(&token)
                    .expect("Cannot find the connection in the cache.")
                    .reset(event_loop);
            }

            return;
        }

        if events.is_error() {
            println!("Err: {:?}", events);
            return;
        }

        if events.is_readable() && self.listeners.contains_key(&token) {
            self.accept_connection(event_loop, token);
            return;
        }

        if self.connections.contains_key(&token) {
            self.connections.get_mut(&token).expect("connection was not found #1").ready(event_loop, events);
        }
    }

    /// XXX: Timeouts would be useful to implement.
    fn timeout(&mut self, event_loop: &mut EventLoop<Self>, timeout: Timeout) {
        match self.incoming_timeout(event_loop, timeout) {
            Ok(_) => {},
            Err(err) => panic!("An error occurred while handling a timeout")
        }
    }

    fn notify(&mut self, event_loop: &mut EventLoop<Self>, msg: Message) {
        match self.incoming_msg(event_loop, msg) {
            Ok(_) => {},
            Err(err) => panic!("Reactor failed to handle incoming msg")
        }
    }
}

#[cfg(test)]
mod tests {
    use mio::{EventLoop, Token};
    use super::*;
    use std::io::{Write, Cursor, Read};
    use std::sync::mpsc::{Receiver, Sender, channel};
    use tangle::{Future, Async};
    use std::thread;
    use std::time::Duration;
    use std::net::{TcpListener, TcpStream, SocketAddr};
    use byteorder::{ReadBytesExt, WriteBytesExt, BigEndian};

    // #[test]
    // fn should_read_frame() {
    //     let mut buf = vec![1, 2, 3];
    //     let mut source = Vec::new();
    //     source.write_u32::<BigEndian>(3);
    //     source.write(&mut buf);

    //     let mut source = Cursor::new(source);
    //     let mut framed = FramedTransport::new();
    //     let buf = match framed.read(&mut source) {
    //         Ok(Some(buf)) => buf,
    //         Ok(None) => panic!("Could not read the next frame from the socket."),
    //         Err(err) => panic!("Tests failed.")
    //     };

    //     assert_eq!(&buf[..], &[1, 2, 3]);
    // }

    // #[test]
    // fn should_error_on_incomplete_frame() {
    //     let mut buf = vec![1, 2];
    //     let mut source = Vec::new();
    //     source.write_u32::<BigEndian>(3);
    //     source.write(&mut buf);

    //     let mut source = Cursor::new(source);
    //     let mut framed = FramedTransport::new();
    //     match framed.read(&mut source) {
    //         Ok(Some(buf)) => panic!("We shouldn't have gotten a read frame back."),
    //         Ok(None) => {},
    //         Err(err) => panic!("Tests failed. {:?}", err)
    //     }
    // }

    // #[test]
    // fn should_eventually_read_delayed_frame() {
    //     let mut buf = vec![];
    //     let mut source = Vec::new();
    //     source.write_u32::<BigEndian>(3);
    //     source.write(&mut buf);

    //     let mut reader = Cursor::new(source);
    //     let mut framed = FramedTransport::new();
    //     match framed.read(&mut reader) {
    //         Ok(Some(buf)) => panic!("We shouldn't have gotten a read frame back."),
    //         Ok(None) => {},
    //         Err(err) => panic!("Tests failed. {:?}", err)
    //     }
    // }


    #[test]
    fn create_reactor() {
        let (assert_tx, assert_rx) = channel();
        let mut reactor = Reactor::new();
        let mut event_loop = EventLoop::new().expect("[test]: EventLoop failed to create.");
        let sender = event_loop.channel();

        let handle = thread::spawn(move || {
            event_loop.run(&mut reactor);
        });

        // Establish a local TcpListener.
        let addr: SocketAddr = "127.0.0.1:6543".parse().expect("[test]: Parsing into SocketAddr failed.");
        let (rpc_server_tx, rpc_server_rx) = channel();

        // Create a new non-blocking tcp server.
        let (id_tx, id_rx) = channel();
        sender.send(Message::Bind(addr.clone(), id_tx, rpc_server_tx.clone()));

        let (rpc_client_tx, rpc_client_rx) = channel();
        let (rpc_client_id_tx, rpc_client_id_rx) = channel();

        sender.send(Message::Connect(addr, rpc_client_id_tx, rpc_client_tx));

        let Id(client_id) = rpc_client_id_rx.recv().expect("[test]: Receiving from channel `rpc_client_id_rx` failed.");
        sender.send(Message::Rpc(client_id, b"abc".to_vec()));

        let server = thread::spawn(move || {
            for msg in rpc_server_rx.iter() {
                match msg {
                    Dispatch::Data(id, msg) => {
                        assert_tx.send((id, msg)).expect("Could not assert_tx");
                    }
                }
            }
        });

        let (new_id, v) = assert_rx.recv().expect("Error trying to assert reactor test.");
        assert_eq!(new_id, Token(2));
        assert_eq!(v.len(), 3);
        assert_eq!(v, b"abc");

        // Send a "response" back.
        sender.send(Message::Rpc(new_id, b"bbb".to_vec()));

        match rpc_client_rx.recv().expect("[test]: Receiving from channel `rpc_client_rx` failed.") {
            Dispatch::Data(id, v) => {
                assert_eq!(id, client_id);
                assert_eq!(v, b"bbb");
            }
        }
    }
}