2016-02-05 13:40:41 +01:00
|
|
|
// Copyright 2015, 2016 Ethcore (UK) Ltd.
|
|
|
|
// This file is part of Parity.
|
|
|
|
|
|
|
|
// Parity is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// Parity is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2016-02-01 15:22:42 +01:00
|
|
|
//! General IO module.
|
|
|
|
//!
|
2016-02-01 16:19:59 +01:00
|
|
|
//! Example usage for creating a network service and adding an IO handler:
|
2016-02-01 15:22:42 +01:00
|
|
|
//!
|
|
|
|
//! ```rust
|
|
|
|
//! extern crate ethcore_util;
|
|
|
|
//! use ethcore_util::*;
|
|
|
|
//!
|
|
|
|
//! struct MyHandler;
|
|
|
|
//!
|
|
|
|
//! #[derive(Clone)]
|
|
|
|
//! struct MyMessage {
|
|
|
|
//! data: u32
|
|
|
|
//! }
|
|
|
|
//!
|
2016-02-01 16:20:46 +01:00
|
|
|
//! impl IoHandler<MyMessage> for MyHandler {
|
|
|
|
//! fn initialize(&self, io: &IoContext<MyMessage>) {
|
2016-02-10 12:50:27 +01:00
|
|
|
//! io.register_timer(0, 1000).unwrap();
|
|
|
|
//! }
|
2016-02-01 15:22:42 +01:00
|
|
|
//!
|
2016-02-10 12:50:27 +01:00
|
|
|
//! fn timeout(&self, _io: &IoContext<MyMessage>, timer: TimerToken) {
|
|
|
|
//! println!("Timeout {}", timer);
|
|
|
|
//! }
|
2016-02-01 16:20:46 +01:00
|
|
|
//!
|
2016-02-10 12:50:27 +01:00
|
|
|
//! fn message(&self, _io: &IoContext<MyMessage>, message: &MyMessage) {
|
|
|
|
//! println!("Message {}", message.data);
|
|
|
|
//! }
|
2016-02-01 16:20:46 +01:00
|
|
|
//! }
|
|
|
|
//!
|
2016-02-01 15:22:42 +01:00
|
|
|
//! fn main () {
|
|
|
|
//! let mut service = IoService::<MyMessage>::start().expect("Error creating network service");
|
|
|
|
//! service.register_handler(Arc::new(MyHandler)).unwrap();
|
|
|
|
//!
|
|
|
|
//! // Wait for quit condition
|
|
|
|
//! // ...
|
|
|
|
//! // Drop the service
|
|
|
|
//! }
|
|
|
|
//! ```
|
2016-01-12 17:33:40 +01:00
|
|
|
mod service;
|
2016-01-21 16:48:37 +01:00
|
|
|
mod worker;
|
|
|
|
|
|
|
|
use mio::{EventLoop, Token};
|
2016-05-21 00:12:51 +02:00
|
|
|
use std::fmt;
|
2016-01-12 17:33:40 +01:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
2016-02-03 16:43:48 +01:00
|
|
|
/// IO Error
|
2016-01-12 17:33:40 +01:00
|
|
|
pub enum IoError {
|
2016-02-03 16:43:48 +01:00
|
|
|
/// Low level error from mio crate
|
2016-01-12 17:33:40 +01:00
|
|
|
Mio(::std::io::Error),
|
|
|
|
}
|
|
|
|
|
2016-05-21 00:12:51 +02:00
|
|
|
impl fmt::Display for IoError {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
// just defer to the std implementation for now.
|
|
|
|
// we can refine the formatting when more variants are added.
|
|
|
|
match *self {
|
|
|
|
IoError::Mio(ref std_err) => std_err.fmt(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-21 16:48:37 +01:00
|
|
|
impl<Message> From<::mio::NotifyError<service::IoMessage<Message>>> for IoError where Message: Send + Clone {
|
2016-01-13 13:56:48 +01:00
|
|
|
fn from(_err: ::mio::NotifyError<service::IoMessage<Message>>) -> IoError {
|
2016-01-12 17:33:40 +01:00
|
|
|
IoError::Mio(::std::io::Error::new(::std::io::ErrorKind::ConnectionAborted, "Network IO notification error"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-10 12:50:27 +01:00
|
|
|
/// Generic IO handler.
|
2016-01-13 11:31:37 +01:00
|
|
|
/// All the handler function are called from within IO event loop.
|
|
|
|
/// `Message` type is used as notification data
|
2016-01-21 16:48:37 +01:00
|
|
|
pub trait IoHandler<Message>: Send + Sync where Message: Send + Sync + Clone + 'static {
|
2016-01-13 11:31:37 +01:00
|
|
|
/// Initialize the handler
|
2016-01-21 16:48:37 +01:00
|
|
|
fn initialize(&self, _io: &IoContext<Message>) {}
|
2016-01-13 11:31:37 +01:00
|
|
|
/// Timer function called after a timeout created with `HandlerIo::timeout`.
|
2016-01-21 16:48:37 +01:00
|
|
|
fn timeout(&self, _io: &IoContext<Message>, _timer: TimerToken) {}
|
2016-01-13 11:31:37 +01:00
|
|
|
/// Called when a broadcasted message is received. The message can only be sent from a different IO handler.
|
2016-01-21 16:48:37 +01:00
|
|
|
fn message(&self, _io: &IoContext<Message>, _message: &Message) {}
|
2016-01-13 11:31:37 +01:00
|
|
|
/// Called when an IO stream gets closed
|
2016-01-21 16:48:37 +01:00
|
|
|
fn stream_hup(&self, _io: &IoContext<Message>, _stream: StreamToken) {}
|
2016-02-10 12:50:27 +01:00
|
|
|
/// Called when an IO stream can be read from
|
2016-01-21 16:48:37 +01:00
|
|
|
fn stream_readable(&self, _io: &IoContext<Message>, _stream: StreamToken) {}
|
2016-01-13 11:31:37 +01:00
|
|
|
/// Called when an IO stream can be written to
|
2016-01-21 16:48:37 +01:00
|
|
|
fn stream_writable(&self, _io: &IoContext<Message>, _stream: StreamToken) {}
|
|
|
|
/// Register a new stream with the event loop
|
|
|
|
fn register_stream(&self, _stream: StreamToken, _reg: Token, _event_loop: &mut EventLoop<IoManager<Message>>) {}
|
|
|
|
/// Re-register a stream with the event loop
|
|
|
|
fn update_stream(&self, _stream: StreamToken, _reg: Token, _event_loop: &mut EventLoop<IoManager<Message>>) {}
|
2016-01-22 18:13:59 +01:00
|
|
|
/// Deregister a stream. Called whenstream is removed from event loop
|
|
|
|
fn deregister_stream(&self, _stream: StreamToken, _event_loop: &mut EventLoop<IoManager<Message>>) {}
|
2016-01-13 11:31:37 +01:00
|
|
|
}
|
|
|
|
|
2016-01-21 17:21:51 +01:00
|
|
|
pub use io::service::TimerToken;
|
|
|
|
pub use io::service::StreamToken;
|
2016-01-21 16:48:37 +01:00
|
|
|
pub use io::service::IoContext;
|
2016-01-21 17:21:51 +01:00
|
|
|
pub use io::service::IoService;
|
|
|
|
pub use io::service::IoChannel;
|
|
|
|
pub use io::service::IoManager;
|
|
|
|
pub use io::service::TOKENS_PER_HANDLER;
|
2016-01-12 17:33:40 +01:00
|
|
|
|
2016-01-13 13:56:48 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
|
2016-01-22 00:47:45 +01:00
|
|
|
use std::sync::Arc;
|
2016-01-13 13:56:48 +01:00
|
|
|
use io::*;
|
|
|
|
|
|
|
|
struct MyHandler;
|
|
|
|
|
2016-01-22 00:47:45 +01:00
|
|
|
#[derive(Clone)]
|
2016-01-13 13:56:48 +01:00
|
|
|
struct MyMessage {
|
|
|
|
data: u32
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IoHandler<MyMessage> for MyHandler {
|
2016-01-22 00:47:45 +01:00
|
|
|
fn initialize(&self, io: &IoContext<MyMessage>) {
|
|
|
|
io.register_timer(0, 1000).unwrap();
|
2016-01-13 13:56:48 +01:00
|
|
|
}
|
|
|
|
|
2016-01-22 00:47:45 +01:00
|
|
|
fn timeout(&self, _io: &IoContext<MyMessage>, timer: TimerToken) {
|
2016-01-13 13:56:48 +01:00
|
|
|
println!("Timeout {}", timer);
|
|
|
|
}
|
|
|
|
|
2016-01-22 00:47:45 +01:00
|
|
|
fn message(&self, _io: &IoContext<MyMessage>, message: &MyMessage) {
|
2016-01-13 13:56:48 +01:00
|
|
|
println!("Message {}", message.data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_service_register_handler () {
|
|
|
|
let mut service = IoService::<MyMessage>::start().expect("Error creating network service");
|
2016-01-22 00:47:45 +01:00
|
|
|
service.register_handler(Arc::new(MyHandler)).unwrap();
|
2016-01-13 13:56:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|