Refactor the discovery module to remove usage of deprecated functions

This commit is contained in:
Thomas Bächler 2018-01-21 23:35:50 +01:00
parent d36017d6f0
commit 2465b0f57f
2 changed files with 19 additions and 36 deletions

View file

@ -3,8 +3,8 @@ use crypto::digest::Digest;
use crypto::mac::Mac; use crypto::mac::Mac;
use crypto; use crypto;
use futures::sync::mpsc; use futures::sync::mpsc;
use futures::{Future, Stream, Poll, Async}; use futures::{Future, Stream, Poll};
use hyper::server::{Service, NewService, Request, Response, Http}; use hyper::server::{Service, Request, Response, Http};
use hyper::{self, Get, Post, StatusCode}; use hyper::{self, Get, Post, StatusCode};
use mdns; use mdns;
use num_bigint::BigUint; use num_bigint::BigUint;
@ -12,7 +12,6 @@ use rand;
use std::collections::BTreeMap; use std::collections::BTreeMap;
use std::io; use std::io;
use std::sync::Arc; use std::sync::Arc;
use tokio_core::net::TcpListener;
use tokio_core::reactor::Handle; use tokio_core::reactor::Handle;
use url; use url;
@ -32,7 +31,7 @@ struct DiscoveryInner {
} }
impl Discovery { impl Discovery {
pub fn new(config: ConnectConfig, device_id: String) fn new(config: ConnectConfig, device_id: String)
-> (Discovery, mpsc::UnboundedReceiver<Credentials>) -> (Discovery, mpsc::UnboundedReceiver<Credentials>)
{ {
let (tx, rx) = mpsc::unbounded(); let (tx, rx) = mpsc::unbounded();
@ -190,21 +189,9 @@ impl Service for Discovery {
} }
} }
impl NewService for Discovery {
type Request = Request;
type Response = Response;
type Error = hyper::Error;
type Instance = Self;
fn new_service(&self) -> io::Result<Self::Instance> {
Ok(self.clone())
}
}
pub struct DiscoveryStream { pub struct DiscoveryStream {
credentials: mpsc::UnboundedReceiver<Credentials>, credentials: mpsc::UnboundedReceiver<Credentials>,
_svc: mdns::Service, _svc: mdns::Service,
task: Box<Future<Item=(), Error=io::Error>>,
} }
pub fn discovery(handle: &Handle, config: ConnectConfig, device_id: String) pub fn discovery(handle: &Handle, config: ConnectConfig, device_id: String)
@ -212,15 +199,20 @@ pub fn discovery(handle: &Handle, config: ConnectConfig, device_id: String)
{ {
let (discovery, creds_rx) = Discovery::new(config.clone(), device_id); let (discovery, creds_rx) = Discovery::new(config.clone(), device_id);
let listener = TcpListener::bind(&"0.0.0.0:0".parse().unwrap(), handle)?; let serve = {
let addr = listener.local_addr()?;
let http = Http::new(); let http = Http::new();
let handle_ = handle.clone(); http.serve_addr_handle(&"0.0.0.0:0".parse().unwrap(), &handle, move || Ok(discovery.clone())).unwrap()
let task = Box::new(listener.incoming().for_each(move |(socket, addr)| { };
http.bind_connection(&handle_, socket, addr, discovery.clone()); let addr = serve.incoming_ref().local_addr();
let server_future = {
let handle = handle.clone();
serve.for_each(move |connection| {
handle.spawn(connection.then(|_| Ok(())));
Ok(()) Ok(())
})); })
.then(|_| Ok(()))
};
handle.spawn(server_future);
let responder = mdns::Responder::spawn(&handle)?; let responder = mdns::Responder::spawn(&handle)?;
let svc = responder.register( let svc = responder.register(
@ -232,20 +224,14 @@ pub fn discovery(handle: &Handle, config: ConnectConfig, device_id: String)
Ok(DiscoveryStream { Ok(DiscoveryStream {
credentials: creds_rx, credentials: creds_rx,
_svc: svc, _svc: svc,
task: task,
}) })
} }
impl Stream for DiscoveryStream { impl Stream for DiscoveryStream {
type Item = Credentials; type Item = Credentials;
type Error = io::Error; type Error = ();
fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> { fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
match self.task.poll()? { self.credentials.poll()
Async::Ready(()) => unreachable!(),
Async::NotReady => (),
}
Ok(self.credentials.poll().unwrap())
} }
} }

View file

@ -2,9 +2,6 @@
#![cfg_attr(feature = "cargo-clippy", allow(unused_io_amount))] #![cfg_attr(feature = "cargo-clippy", allow(unused_io_amount))]
// TODO: many items from tokio-core::io have been deprecated in favour of tokio-io
#![allow(deprecated)]
#[macro_use] extern crate log; #[macro_use] extern crate log;
#[macro_use] extern crate serde_json; #[macro_use] extern crate serde_json;
#[macro_use] extern crate serde_derive; #[macro_use] extern crate serde_derive;