2016-10-24 00:02:02 +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.
|
|
|
|
|
|
|
|
//! Grin server implementation, accepts incoming connections and connects to
|
2016-10-25 07:35:10 +03:00
|
|
|
//! other peers in the network.
|
2016-10-24 00:02:02 +03:00
|
|
|
|
2016-10-29 22:36:45 +03:00
|
|
|
use std::cell::RefCell;
|
2016-10-26 08:06:13 +03:00
|
|
|
use std::io;
|
|
|
|
use std::net::SocketAddr;
|
|
|
|
use std::str::FromStr;
|
2016-10-29 22:36:45 +03:00
|
|
|
use std::sync::{Arc, RwLock};
|
2016-10-26 08:06:13 +03:00
|
|
|
|
|
|
|
use mioco;
|
2016-10-28 00:28:02 +03:00
|
|
|
use mioco::tcp::{TcpListener, TcpStream};
|
2016-10-24 00:02:02 +03:00
|
|
|
|
2016-10-28 00:28:02 +03:00
|
|
|
use core::ser::Error;
|
2016-10-26 08:06:13 +03:00
|
|
|
use handshake::Handshake;
|
2016-10-29 22:36:45 +03:00
|
|
|
use peer::Peer;
|
2016-10-26 08:06:13 +03:00
|
|
|
use types::*;
|
2016-10-24 00:02:02 +03:00
|
|
|
|
2016-10-28 00:28:02 +03:00
|
|
|
pub const DEFAULT_LISTEN_ADDR: &'static str = "127.0.0.1:3414";
|
2016-10-24 00:02:02 +03:00
|
|
|
|
|
|
|
// replace with some config lookup or something
|
|
|
|
fn listen_addr() -> SocketAddr {
|
|
|
|
FromStr::from_str(DEFAULT_LISTEN_ADDR).unwrap()
|
|
|
|
}
|
|
|
|
|
2016-10-26 08:06:13 +03:00
|
|
|
struct DummyAdapter {}
|
|
|
|
impl NetAdapter for DummyAdapter {}
|
|
|
|
|
2016-10-29 22:36:45 +03:00
|
|
|
/// P2P server implementation, handling bootstrapping to find and connect to
|
|
|
|
/// peers, receiving connections from other peers and keep track of all of them.
|
2016-10-24 00:02:02 +03:00
|
|
|
pub struct Server {
|
2016-10-29 22:36:45 +03:00
|
|
|
peers: RwLock<Vec<Arc<Peer>>>,
|
2016-10-24 00:02:02 +03:00
|
|
|
}
|
|
|
|
|
2016-10-29 22:36:45 +03:00
|
|
|
// TODO TLS
|
2016-10-24 00:02:02 +03:00
|
|
|
impl Server {
|
2016-10-29 22:36:45 +03:00
|
|
|
pub fn new() -> Server {
|
|
|
|
Server{peers: RwLock::new(Vec::new())}
|
|
|
|
}
|
2016-10-24 00:02:02 +03:00
|
|
|
/// Creates a new p2p server. Opens a TCP port to allow incoming
|
|
|
|
/// connections and starts the bootstrapping process to find peers.
|
2016-10-29 22:36:45 +03:00
|
|
|
pub fn start(&'static self) -> Result<(), Error> {
|
2016-10-28 00:28:02 +03:00
|
|
|
mioco::spawn(move || -> io::Result<()> {
|
|
|
|
let addr = DEFAULT_LISTEN_ADDR.parse().unwrap();
|
|
|
|
let listener = try!(TcpListener::bind(&addr));
|
|
|
|
warn!("P2P server started on {}", addr);
|
2016-10-24 00:02:02 +03:00
|
|
|
|
2016-10-28 00:28:02 +03:00
|
|
|
let hs = Arc::new(Handshake::new());
|
2016-10-26 08:06:13 +03:00
|
|
|
|
2016-10-28 00:28:02 +03:00
|
|
|
loop {
|
|
|
|
let conn = try!(listener.accept());
|
2016-10-29 22:36:45 +03:00
|
|
|
let hs = hs.clone();
|
2016-10-26 08:06:13 +03:00
|
|
|
|
2016-10-28 00:28:02 +03:00
|
|
|
mioco::spawn(move || -> io::Result<()> {
|
2016-10-29 22:36:45 +03:00
|
|
|
let peer = try!(Peer::connect(conn, &hs).map_err(|_| io::Error::last_os_error()));
|
|
|
|
let wpeer = Arc::new(peer);
|
|
|
|
{
|
|
|
|
let mut peers = self.peers.write().unwrap();
|
|
|
|
peers.push(wpeer.clone());
|
|
|
|
}
|
|
|
|
if let Some(err) = wpeer.run(&DummyAdapter{}) {
|
2016-10-28 00:28:02 +03:00
|
|
|
error!("{:?}", err);
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
});
|
2016-10-29 22:36:45 +03:00
|
|
|
Ok(())
|
2016-10-28 00:28:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Simulates an unrelated client connecting to our server. Mostly used for
|
|
|
|
/// tests.
|
2016-10-29 22:36:45 +03:00
|
|
|
pub fn connect_as_client(addr: SocketAddr) -> Result<Peer, Error> {
|
2016-10-28 00:28:02 +03:00
|
|
|
let tcp_client = TcpStream::connect(&addr).unwrap();
|
2016-10-29 22:36:45 +03:00
|
|
|
Peer::accept(tcp_client, &Handshake::new())
|
2016-10-24 00:02:02 +03:00
|
|
|
}
|
|
|
|
}
|