renamed Services

This commit is contained in:
debris 2016-10-18 01:44:52 +02:00
parent f7bfa16195
commit e039068772
10 changed files with 52 additions and 28 deletions

View File

@ -3,11 +3,11 @@ use ser::{
Stream, Serializable,
Reader, Deserializable, Error as ReaderError, deserialize,
};
use common::{Port, IpAddress, ServiceFlags};
use common::{Port, IpAddress, Services};
#[derive(Debug, PartialEq)]
pub struct NetAddress {
pub services: ServiceFlags,
pub services: Services,
pub address: IpAddress,
pub port: Port,
}
@ -42,7 +42,7 @@ impl From<&'static str> for NetAddress {
#[cfg(test)]
mod tests {
use ser::{serialize, deserialize};
use common::ServiceFlags;
use common::Services;
use super::NetAddress;
#[test]
@ -54,7 +54,7 @@ mod tests {
].into();
let address = NetAddress {
services: ServiceFlags::default().with_network(true),
services: Services::default().with_network(true),
address: "::ffff:a00:1".into(),
port: 8333.into(),
};
@ -71,7 +71,7 @@ mod tests {
];
let expected = NetAddress {
services: ServiceFlags::default().with_network(true),
services: Services::default().with_network(true),
address: "::ffff:a00:1".into(),
port: 8333.into(),
};
@ -82,7 +82,7 @@ mod tests {
#[test]
fn test_net_address_from_static_str() {
let expected = NetAddress {
services: ServiceFlags::default().with_network(true),
services: Services::default().with_network(true),
address: "::ffff:a00:1".into(),
port: 8333.into(),

View File

@ -20,4 +20,4 @@ pub use self::ip::IpAddress;
pub use self::magic::Magic;
pub use self::port::Port;
pub use self::prefilled_transaction::PrefilledTransaction;
pub use self::service::ServiceFlags;
pub use self::service::Services;

View File

@ -4,22 +4,22 @@ use ser::{
};
#[derive(Debug, Default, PartialEq, Clone, Copy)]
pub struct ServiceFlags(u64);
pub struct Services(u64);
impl From<ServiceFlags> for u64 {
fn from(s: ServiceFlags) -> Self {
impl From<Services> for u64 {
fn from(s: Services) -> Self {
s.0
}
}
impl From<u64> for ServiceFlags {
impl From<u64> for Services {
fn from(v: u64) -> Self {
ServiceFlags(v)
Services(v)
}
}
impl ServiceFlags {
impl Services {
pub fn network(&self) -> bool {
self.bit_at(0)
}
@ -78,14 +78,14 @@ impl ServiceFlags {
}
}
impl Serializable for ServiceFlags {
impl Serializable for Services {
fn serialize(&self, stream: &mut Stream) {
stream.append(&self.0);
}
}
impl Deserializable for ServiceFlags {
impl Deserializable for Services {
fn deserialize(reader: &mut Reader) -> Result<Self, ReaderError> where Self: Sized {
reader.read().map(ServiceFlags)
reader.read().map(Services)
}
}

View File

@ -3,7 +3,7 @@ use ser::{
Serializable, Stream,
Deserializable, Reader, Error as ReaderError,
};
use common::{NetAddress, ServiceFlags};
use common::{NetAddress, Services};
use {Payload, MessageResult};
use serialization::deserialize_payload;
@ -70,7 +70,7 @@ impl Version {
}
}
pub fn services(&self) -> ServiceFlags {
pub fn services(&self) -> Services {
match *self {
Version::V0(ref s) => s.services,
Version::V106(ref s, _) => s.services,
@ -82,7 +82,7 @@ impl Version {
#[derive(Debug, PartialEq)]
pub struct V0 {
pub version: u32,
pub services: ServiceFlags,
pub services: Services,
pub timestamp: i64,
pub receiver: NetAddress,
}

View File

@ -3,10 +3,20 @@ use net::Config as NetConfig;
#[derive(Debug)]
pub struct Config {
/// Number of threads used by p2p thread pool.
pub threads: usize,
/// Lowest supported protocol version.
pub protocol_minimum: u32,
/// Highest supported protocol version.
pub protocol_maximum: u32,
/// Number of inbound connections.
pub inbound_connections: usize,
/// Number of outbound connections.
pub outbound_connections: usize,
/// Configuration for every connection.
pub connection: NetConfig,
/// Connect to these nodes to retrieve peer addresses, and disconnect.
pub seednodes: Vec<IpAddr>,
/// Connect only ot these nodes.
pub limited_connect: Option<Vec<IpAddr>>,
pub peers: Vec<IpAddr>,
/// Connect to these nodes to retrieve peer addresses, and disconnect.
pub seeds: Vec<IpAddr>,
}

View File

@ -1,5 +1,5 @@
use std::net::SocketAddr;
use message::common::{Magic, ServiceFlags, NetAddress};
use message::common::{Magic, Services, NetAddress};
use message::types::version::{Version, V0, V106, V70001};
use util::time::{Time, RealTime};
use util::nonce::{NonceGenerator, RandomNonce};
@ -9,7 +9,7 @@ use VERSION;
pub struct Config {
pub magic: Magic,
pub local_address: SocketAddr,
pub services: ServiceFlags,
pub services: Services,
pub user_agent: String,
pub start_height: i32,
pub relay: bool,

View File

@ -23,7 +23,7 @@ pub struct P2P {
impl P2P {
pub fn new(config: Config, handle: Handle) -> Self {
let pool = CpuPool::new(4);
let pool = CpuPool::new(config.threads);
P2P {
event_loop_handle: handle.clone(),
@ -35,7 +35,7 @@ impl P2P {
}
pub fn run(&self) -> Result<(), io::Error> {
for seednode in self.config.seednodes.iter() {
for seednode in self.config.peers.iter() {
self.connect(*seednode)
}

View File

@ -1,3 +1,11 @@
mod manual;
mod normal;
mod seednode;
pub enum SessionState {
Connected,
AcceptedConnection,
SentGetAddr,
Closing,
Idle,
}

View File

@ -1,2 +1,3 @@
pub mod nonce;
pub mod time;

View File

@ -28,6 +28,11 @@ fn run() -> Result<(), String> {
let mut el = event_loop();
let p2p_cfg = p2p::Config {
threads: 4,
protocol_minimum: 70001,
protocol_maximum: 70017,
inbound_connections: 10,
outbound_connections: 10,
connection: net::Config {
magic: cfg.magic,
local_address: SocketAddr::new("127.0.0.1".parse().unwrap(), cfg.port),
@ -36,8 +41,8 @@ fn run() -> Result<(), String> {
start_height: 0,
relay: false,
},
seednodes: cfg.seednode.map_or_else(|| vec![], |x| vec![x]),
limited_connect: cfg.connect.map_or(None, |x| Some(vec![x])),
peers: cfg.connect.map_or_else(|| vec![], |x| vec![x]),
seeds: cfg.seednode.map_or_else(|| vec![], |x| vec![x]),
};
let p2p = P2P::new(p2p_cfg, el.handle());