Removed obsolete code and added documentation
This commit is contained in:
parent
6728690109
commit
ccf1cc4d54
@ -20,6 +20,7 @@ time = "0.1"
|
|||||||
evmjit = { path = "rust-evmjit", optional = true }
|
evmjit = { path = "rust-evmjit", optional = true }
|
||||||
ethash = { path = "ethash" }
|
ethash = { path = "ethash" }
|
||||||
num_cpus = "0.2"
|
num_cpus = "0.2"
|
||||||
|
ctrlc = "1.0"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
jit = ["evmjit"]
|
jit = ["evmjit"]
|
||||||
|
@ -3,11 +3,12 @@ extern crate ethcore;
|
|||||||
extern crate rustc_serialize;
|
extern crate rustc_serialize;
|
||||||
extern crate log;
|
extern crate log;
|
||||||
extern crate env_logger;
|
extern crate env_logger;
|
||||||
|
extern crate ctrlc;
|
||||||
|
|
||||||
use std::io::stdin;
|
|
||||||
use std::env;
|
use std::env;
|
||||||
use log::{LogLevelFilter};
|
use log::{LogLevelFilter};
|
||||||
use env_logger::LogBuilder;
|
use env_logger::LogBuilder;
|
||||||
|
use ctrlc::CtrlC;
|
||||||
use util::*;
|
use util::*;
|
||||||
use ethcore::client::*;
|
use ethcore::client::*;
|
||||||
use ethcore::service::{ClientService, NetSyncMessage};
|
use ethcore::service::{ClientService, NetSyncMessage};
|
||||||
@ -31,13 +32,15 @@ fn main() {
|
|||||||
let mut service = ClientService::start(spec).unwrap();
|
let mut service = ClientService::start(spec).unwrap();
|
||||||
let io_handler = Arc::new(ClientIoHandler { client: service.client(), info: Default::default() });
|
let io_handler = Arc::new(ClientIoHandler { client: service.client(), info: Default::default() });
|
||||||
service.io().register_handler(io_handler).expect("Error registering IO handler");
|
service.io().register_handler(io_handler).expect("Error registering IO handler");
|
||||||
loop {
|
|
||||||
let mut cmd = String::new();
|
let exit = Arc::new(Condvar::new());
|
||||||
stdin().read_line(&mut cmd).unwrap();
|
let e = exit.clone();
|
||||||
if cmd == "quit\n" || cmd == "exit\n" || cmd == "q\n" {
|
CtrlC::set_handler(move || {
|
||||||
break;
|
e.notify_all();
|
||||||
}
|
});
|
||||||
}
|
|
||||||
|
let mutex = Mutex::new(());
|
||||||
|
let _ = exit.wait(mutex.lock().unwrap()).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Informant {
|
struct Informant {
|
||||||
|
@ -10,11 +10,14 @@ use arrayvec::*;
|
|||||||
use crossbeam::sync::chase_lev;
|
use crossbeam::sync::chase_lev;
|
||||||
use io::worker::{Worker, Work, WorkType};
|
use io::worker::{Worker, Work, WorkType};
|
||||||
|
|
||||||
|
/// Timer ID
|
||||||
pub type TimerToken = usize;
|
pub type TimerToken = usize;
|
||||||
|
/// Timer ID
|
||||||
pub type StreamToken = usize;
|
pub type StreamToken = usize;
|
||||||
|
/// IO Hadndler ID
|
||||||
pub type HandlerId = usize;
|
pub type HandlerId = usize;
|
||||||
|
|
||||||
// Tokens
|
/// Maximum number of tokens a handler can use
|
||||||
pub const TOKENS_PER_HANDLER: usize = 16384;
|
pub const TOKENS_PER_HANDLER: usize = 16384;
|
||||||
|
|
||||||
/// Messages used to communicate with the event loop from other threads.
|
/// Messages used to communicate with the event loop from other threads.
|
||||||
@ -49,8 +52,8 @@ pub enum IoMessage<Message> where Message: Send + Clone + Sized {
|
|||||||
|
|
||||||
/// IO access point. This is passed to all IO handlers and provides an interface to the IO subsystem.
|
/// IO access point. This is passed to all IO handlers and provides an interface to the IO subsystem.
|
||||||
pub struct IoContext<Message> where Message: Send + Clone + 'static {
|
pub struct IoContext<Message> where Message: Send + Clone + 'static {
|
||||||
pub channel: IoChannel<Message>,
|
channel: IoChannel<Message>,
|
||||||
pub handler: HandlerId,
|
handler: HandlerId,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Message> IoContext<Message> where Message: Send + Clone + 'static {
|
impl<Message> IoContext<Message> where Message: Send + Clone + 'static {
|
||||||
@ -249,6 +252,7 @@ impl<Message> IoChannel<Message> where Message: Send + Clone {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Send low level io message
|
||||||
pub fn send_io(&self, message: IoMessage<Message>) -> Result<(), IoError> {
|
pub fn send_io(&self, message: IoMessage<Message>) -> Result<(), IoError> {
|
||||||
if let Some(ref channel) = self.channel {
|
if let Some(ref channel) = self.channel {
|
||||||
try!(channel.send(message))
|
try!(channel.send(message))
|
||||||
|
@ -19,11 +19,17 @@ pub enum DisconnectReason
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
/// Network error.
|
||||||
pub enum NetworkError {
|
pub enum NetworkError {
|
||||||
|
/// Authentication error.
|
||||||
Auth,
|
Auth,
|
||||||
|
/// Unrecognised protocol.
|
||||||
BadProtocol,
|
BadProtocol,
|
||||||
|
/// Peer not found.
|
||||||
PeerNotFound,
|
PeerNotFound,
|
||||||
|
/// Peer is diconnected.
|
||||||
Disconnect(DisconnectReason),
|
Disconnect(DisconnectReason),
|
||||||
|
/// Socket IO error.
|
||||||
Io(IoError),
|
Io(IoError),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -69,22 +69,22 @@ pub type ProtocolId = &'static str;
|
|||||||
pub enum NetworkIoMessage<Message> where Message: Send + Sync + Clone {
|
pub enum NetworkIoMessage<Message> where Message: Send + Sync + Clone {
|
||||||
/// Register a new protocol handler.
|
/// Register a new protocol handler.
|
||||||
AddHandler {
|
AddHandler {
|
||||||
|
/// Handler shared instance.
|
||||||
handler: Arc<NetworkProtocolHandler<Message> + Sync>,
|
handler: Arc<NetworkProtocolHandler<Message> + Sync>,
|
||||||
|
/// Protocol Id.
|
||||||
protocol: ProtocolId,
|
protocol: ProtocolId,
|
||||||
|
/// Supported protocol versions.
|
||||||
versions: Vec<u8>,
|
versions: Vec<u8>,
|
||||||
},
|
},
|
||||||
|
/// Register a new protocol timer
|
||||||
AddTimer {
|
AddTimer {
|
||||||
|
/// Protocol Id.
|
||||||
protocol: ProtocolId,
|
protocol: ProtocolId,
|
||||||
|
/// Timer token.
|
||||||
token: TimerToken,
|
token: TimerToken,
|
||||||
|
/// Timer delay in milliseconds.
|
||||||
delay: u64,
|
delay: u64,
|
||||||
},
|
},
|
||||||
/// Send data over the network. // TODO: remove this
|
|
||||||
Send {
|
|
||||||
peer: PeerId,
|
|
||||||
packet_id: PacketId,
|
|
||||||
protocol: ProtocolId,
|
|
||||||
data: Vec<u8>,
|
|
||||||
},
|
|
||||||
/// User message
|
/// User message
|
||||||
User(Message),
|
User(Message),
|
||||||
}
|
}
|
||||||
@ -644,23 +644,6 @@ impl<Message> IoHandler<NetworkIoMessage<Message>> for Host<Message> where Messa
|
|||||||
self.timers.write().unwrap().insert(handler_token, ProtocolTimer { protocol: protocol, token: *token });
|
self.timers.write().unwrap().insert(handler_token, ProtocolTimer { protocol: protocol, token: *token });
|
||||||
io.register_timer(handler_token, *delay).expect("Error registering timer");
|
io.register_timer(handler_token, *delay).expect("Error registering timer");
|
||||||
},
|
},
|
||||||
&NetworkIoMessage::Send {
|
|
||||||
ref peer,
|
|
||||||
ref packet_id,
|
|
||||||
ref protocol,
|
|
||||||
ref data,
|
|
||||||
} => {
|
|
||||||
if let Some(connection) = self.connections.read().unwrap().get(*peer).map(|c| c.clone()) {
|
|
||||||
match connection.lock().unwrap().deref_mut() {
|
|
||||||
&mut ConnectionEntry::Session(ref mut s) => {
|
|
||||||
s.send_packet(protocol, *packet_id as u8, &data).unwrap_or_else(|e| {
|
|
||||||
warn!(target: "net", "Send error: {:?}", e);
|
|
||||||
}); //TODO: don't copy vector data
|
|
||||||
},
|
|
||||||
_ => { warn!(target: "net", "Send: Peer session not exist"); }
|
|
||||||
}
|
|
||||||
} else { warn!(target: "net", "Send: Peer does not exist"); }
|
|
||||||
},
|
|
||||||
&NetworkIoMessage::User(ref message) => {
|
&NetworkIoMessage::User(ref message) => {
|
||||||
for (p, h) in self.handlers.read().unwrap().iter() {
|
for (p, h) in self.handlers.read().unwrap().iter() {
|
||||||
h.message(&mut NetworkContext::new(io, p, None, self.connections.clone()), &message);
|
h.message(&mut NetworkContext::new(io, p, None, self.connections.clone()), &message);
|
||||||
|
@ -2,7 +2,7 @@ use std::sync::*;
|
|||||||
use error::*;
|
use error::*;
|
||||||
use network::{NetworkProtocolHandler};
|
use network::{NetworkProtocolHandler};
|
||||||
use network::error::{NetworkError};
|
use network::error::{NetworkError};
|
||||||
use network::host::{Host, NetworkIoMessage, PeerId, PacketId, ProtocolId};
|
use network::host::{Host, NetworkIoMessage, ProtocolId};
|
||||||
use io::*;
|
use io::*;
|
||||||
|
|
||||||
/// IO Service with networking
|
/// IO Service with networking
|
||||||
@ -26,17 +26,6 @@ impl<Message> NetworkService<Message> where Message: Send + Sync + Clone + 'stat
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Send a message over the network. Normaly `HostIo::send` should be used. This can be used from non-io threads.
|
|
||||||
pub fn send(&mut self, peer: &PeerId, packet_id: PacketId, protocol: ProtocolId, data: &[u8]) -> Result<(), NetworkError> {
|
|
||||||
try!(self.io_service.send_message(NetworkIoMessage::Send {
|
|
||||||
peer: *peer,
|
|
||||||
packet_id: packet_id,
|
|
||||||
protocol: protocol,
|
|
||||||
data: data.to_vec()
|
|
||||||
}));
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Regiter a new protocol handler with the event loop.
|
/// Regiter a new protocol handler with the event loop.
|
||||||
pub fn register_protocol(&mut self, handler: Arc<NetworkProtocolHandler<Message>+Send + Sync>, protocol: ProtocolId, versions: &[u8]) -> Result<(), NetworkError> {
|
pub fn register_protocol(&mut self, handler: Arc<NetworkProtocolHandler<Message>+Send + Sync>, protocol: ProtocolId, versions: &[u8]) -> Result<(), NetworkError> {
|
||||||
try!(self.io_service.send_message(NetworkIoMessage::AddHandler {
|
try!(self.io_service.send_message(NetworkIoMessage::AddHandler {
|
||||||
|
Loading…
Reference in New Issue
Block a user