      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

    use std::sync::Arc;
    use std::net::{SocketAddr, IpAddr};
    use v1::traits::Network as NetworkRpc;
    use v1::types::{AddNodeOperation, NodeInfo};
    use jsonrpc_core::Error;
    use jsonrpc_macros::Trailing;
    use v1::helpers::errors;
    use p2p;

    pub trait NetworkApi : Send + Sync + 'static {
    	fn add_node(&self, socket_addr: SocketAddr) -> Result<(), p2p::NodeTableError>;
    	fn remove_node(&self, socket_addr: SocketAddr) -> Result<(), p2p::NodeTableError>;
    	fn connect(&self, socket_addr: SocketAddr);
    	fn node_info(&self, node_addr: IpAddr) -> Result<NodeInfo, p2p::NodeTableError>;
    	fn nodes_info(&self) -> Vec<NodeInfo>;
    	fn connection_count(&self) -> usize;
    }

    impl<T> NetworkRpc for NetworkClient<T> where T: NetworkApi {
    	fn add_node(&self, node: String, operation: AddNodeOperation) -> Result<(), Error> {
    		let addr = try!(node.parse().map_err(
    			|_| errors::invalid_params("node", "Invalid socket address format, should be ip:port (127.0.0.1:8008)")));
    		match operation {
    			AddNodeOperation::Add => {
    				self.api.add_node(addr).map_err(|_| errors::node_already_added())
    			},
    			AddNodeOperation::Remove => {
    				self.api.remove_node(addr).map_err(|_| errors::node_not_added())
    			},
    			AddNodeOperation::OneTry => {
    				self.api.connect(addr);
    				Ok(())
    			}
    		}
    	}

    	fn node_info(&self, _dns: bool, node_addr: Trailing<String>) -> Result<Vec<NodeInfo>, Error> {
    		let node_addr: Option<String> = node_addr.into();
    		Ok(
    			match node_addr {
    				None => self.api.nodes_info(),
    				Some(node_addr) => {
    					let addr = try!(node_addr.parse().map_err(
    						|_| errors::invalid_params("node", "Invalid ip address format, should be ip address (127.0.0.1)")));
    					let node_info = try!(
    						self.api.node_info(addr)
    							.map_err(|_| errors::node_not_added())
    					);
    					vec![node_info]
    				},
    			}
    		)
    	}

    	fn connection_count(&self) -> Result<usize, Error> {
    		Ok(self.api.connection_count())
    	}
    }

    pub struct NetworkClient<T: NetworkApi> {
    	api: T,
    }

    impl<T> NetworkClient<T> where T: NetworkApi {
    	pub fn new(api: T) -> Self {
    		NetworkClient {
    			api: api,
    		}
    	}
    }

    pub struct NetworkClientCore {
    	p2p: Arc<p2p::Context>,
    }

    impl NetworkClientCore {
    	pub fn new(p2p: Arc<p2p::Context>) -> Self {
    		NetworkClientCore { p2p: p2p }
    	}
    }

    impl NetworkApi for NetworkClientCore {
    	fn add_node(&self, socket_addr: SocketAddr) -> Result<(), p2p::NodeTableError> {
    		self.p2p.add_node(socket_addr)
    	}

    	fn remove_node(&self, socket_addr: SocketAddr) -> Result<(), p2p::NodeTableError> {
    		self.p2p.remove_node(socket_addr)
    	}

    	fn connect(&self, socket_addr: SocketAddr) {
    		p2p::Context::connect_normal(self.p2p.clone(), socket_addr);
    	}

    	fn node_info(&self, node_addr: IpAddr) -> Result<NodeInfo, p2p::NodeTableError> {
    		let exact_node = try!(
    			self.p2p.nodes()
    				.iter()
    				.find(|n| n.address().ip() == node_addr)
    				.cloned()
    				.ok_or(p2p::NodeTableError::NoAddressInTable)
    		);

    		let peers: Vec<p2p::PeerInfo> = self.p2p.connections().info()
    			.into_iter()
    			.filter(|p| p.address == exact_node.address()).collect();

    		Ok(
    			NodeInfo {
    				addednode: format!("{}", exact_node.address()),
    				connected: !peers.is_empty(),
    				addresses: peers.into_iter().map(|p| p.into()).collect(),
    			}
    		)
    	}

    	fn nodes_info(&self) -> Vec<NodeInfo> {
    		let peers: Vec<p2p::PeerInfo> = self.p2p.connections().info();

    		self.p2p.nodes().iter().map(|n| {
    			let node_peers: Vec<p2p::PeerInfo> = peers.iter().filter(|p| p.address == n.address()).cloned().collect();
    			NodeInfo {
    				addednode: format!("{}", n.address()),
    				connected: !node_peers.is_empty(),
    				addresses: node_peers.into_iter().map(|p| p.into()).collect(),
    			}
    		}).collect()
    	}

    	fn connection_count(&self) -> usize {
    		self.p2p.connections().count()
    	}
    }

# Help

## Keyboard Shortcuts

`?`
  Show this help dialog
`S`
  Focus the search field
`↑`
  Move up in search results
`↓`
  Move down in search results
`↹`
  Switch tab
`⏎`
  Go to active search result
`+`
  Expand all sections
`-`
  Collapse all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g. `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g. `vec -> usize` or `* -> vec`)

Search multiple things at once by splitting your query with comma (e.g. `str,u8` or `String,struct:Vec,test`)