2016-10-25 07:35:10 +03:00
|
|
|
// Copyright 2016 The Grin Developers
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2016-12-16 01:57:04 +03:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2016-12-11 06:11:49 +03:00
|
|
|
use futures::Future;
|
|
|
|
use tokio_core::net::TcpStream;
|
2016-10-25 07:35:10 +03:00
|
|
|
|
2016-11-01 20:42:33 +03:00
|
|
|
use core::core;
|
2016-10-26 21:21:45 +03:00
|
|
|
use core::ser::Error;
|
2016-10-29 22:36:45 +03:00
|
|
|
use handshake::Handshake;
|
2016-10-26 08:06:13 +03:00
|
|
|
use types::*;
|
2016-10-25 07:35:10 +03:00
|
|
|
|
2016-10-29 22:36:45 +03:00
|
|
|
pub struct Peer {
|
2016-10-31 04:23:52 +03:00
|
|
|
info: PeerInfo,
|
|
|
|
proto: Box<Protocol>,
|
2016-10-26 08:06:13 +03:00
|
|
|
}
|
2016-10-25 07:35:10 +03:00
|
|
|
|
2016-10-29 22:36:45 +03:00
|
|
|
unsafe impl Sync for Peer {}
|
|
|
|
unsafe impl Send for Peer {}
|
|
|
|
|
|
|
|
impl Peer {
|
2017-01-30 02:52:01 +03:00
|
|
|
/// Initiates the handshake with another peer.
|
2016-12-11 06:11:49 +03:00
|
|
|
pub fn connect(conn: TcpStream,
|
|
|
|
hs: &Handshake)
|
|
|
|
-> Box<Future<Item = (TcpStream, Peer), Error = Error>> {
|
|
|
|
let connect_peer = hs.connect(conn).and_then(|(conn, proto, info)| {
|
|
|
|
Ok((conn,
|
|
|
|
Peer {
|
|
|
|
info: info,
|
|
|
|
proto: Box::new(proto),
|
|
|
|
}))
|
|
|
|
});
|
|
|
|
Box::new(connect_peer)
|
2016-10-31 04:23:52 +03:00
|
|
|
}
|
2016-10-29 22:36:45 +03:00
|
|
|
|
2017-01-30 02:52:01 +03:00
|
|
|
/// Accept a handshake initiated by another peer.
|
2016-12-11 06:11:49 +03:00
|
|
|
pub fn accept(conn: TcpStream,
|
|
|
|
hs: &Handshake)
|
|
|
|
-> Box<Future<Item = (TcpStream, Peer), Error = Error>> {
|
|
|
|
let hs_peer = hs.handshake(conn).and_then(|(conn, proto, info)| {
|
|
|
|
Ok((conn,
|
|
|
|
Peer {
|
|
|
|
info: info,
|
|
|
|
proto: Box::new(proto),
|
|
|
|
}))
|
|
|
|
});
|
|
|
|
Box::new(hs_peer)
|
2016-10-31 04:23:52 +03:00
|
|
|
}
|
|
|
|
|
2017-01-30 02:52:01 +03:00
|
|
|
/// Main peer loop listening for messages and forwarding to the rest of the
|
|
|
|
/// system.
|
2016-12-19 02:51:54 +03:00
|
|
|
pub fn run(&self,
|
|
|
|
conn: TcpStream,
|
|
|
|
na: Arc<NetAdapter>)
|
|
|
|
-> Box<Future<Item = (), Error = Error>> {
|
2016-12-11 06:11:49 +03:00
|
|
|
self.proto.handle(conn, na)
|
2016-11-01 20:42:33 +03:00
|
|
|
}
|
|
|
|
|
2017-01-30 02:52:01 +03:00
|
|
|
/// Bytes sent and received by this peer to the remote peer.
|
2016-10-31 22:29:08 +03:00
|
|
|
pub fn transmitted_bytes(&self) -> (u64, u64) {
|
|
|
|
self.proto.transmitted_bytes()
|
2016-10-31 04:23:52 +03:00
|
|
|
}
|
|
|
|
|
2016-12-11 06:11:49 +03:00
|
|
|
pub fn send_ping(&self) -> Result<(), Error> {
|
|
|
|
self.proto.send_ping()
|
|
|
|
}
|
|
|
|
|
2016-12-21 04:33:20 +03:00
|
|
|
/// Sends the provided block to the remote peer. The request may be dropped
|
|
|
|
/// if the remote peer is known to already have the block.
|
|
|
|
pub fn send_block(&self, b: &core::Block) -> Result<(), Error> {
|
|
|
|
// TODO do not send if the peer sent us the block in the first place
|
|
|
|
self.proto.send_block(b)
|
|
|
|
}
|
|
|
|
|
2016-10-31 04:23:52 +03:00
|
|
|
pub fn stop(&self) {
|
2016-12-11 06:11:49 +03:00
|
|
|
self.proto.close();
|
2016-10-31 04:23:52 +03:00
|
|
|
}
|
2016-10-25 07:35:10 +03:00
|
|
|
}
|