Struct thrust::reactor::Reactor [] [src]

pub struct Reactor {
    // some fields omitted
}

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 Messages. 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 — 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.

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));
  1. Connecting to a remote TCP server and establishing a new non-blocking TcpStream.
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));
  1. 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.
reactor_sender.send(Message::Rpc(Token(1), vec![0, 1, 3, 4]));

Methods

impl Reactor

fn new() -> Reactor

fn run() -> JoinHandle<()>

fn incoming_timeout(&mut self, event_loop: &mut EventLoop<Self>, timeout: Timeout) -> ThrustResult<()>

fn incoming_msg(&mut self, event_loop: &mut EventLoop<Self>, msg: Message) -> ThrustResult<()>

fn accept_connection(&mut self, event_loop: &mut EventLoop<Self>, token: Token)

Trait Implementations

impl Handler for Reactor

type Timeout = Timeout

type Message = Message

fn ready(&mut self, event_loop: &mut EventLoop<Self>, token: Token, events: EventSet)

fn timeout(&mut self, event_loop: &mut EventLoop<Self>, timeout: Timeout)

fn notify(&mut self, event_loop: &mut EventLoop<Self>, msg: Message)

fn interrupted(&mut self, event_loop: &mut EventLoop<Self>)

fn tick(&mut self, event_loop: &mut EventLoop<Self>)