Skip to content

Commit 6e87279

Browse files
committed
Break host module into local and remote submodules
1 parent 14f8417 commit 6e87279

File tree

5 files changed

+98
-58
lines changed

5 files changed

+98
-58
lines changed

agent/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ mod errors;
1919
use errors::*;
2020
use futures::{future, Future};
2121
use intecture_api::{Executable, Runnable};
22-
use intecture_api::host::JsonProto;
22+
use intecture_api::host::remote::JsonProto;
2323
use std::io;
2424
use tokio_proto::TcpServer;
2525
use tokio_service::Service;

core/src/host/local.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright 2015-2017 Intecture Developers.
2+
//
3+
// Licensed under the Mozilla Public License 2.0 <LICENSE or
4+
// https://www.tldrlegal.com/l/mpl-2.0>. This file may not be copied,
5+
// modified, or distributed except according to those terms.
6+
7+
use errors::*;
8+
use futures::{future, Future};
9+
use {Executable, Runnable};
10+
use serde::Deserialize;
11+
use serde_json;
12+
use std::sync::Arc;
13+
use super::Host;
14+
use telemetry::{self, Telemetry};
15+
16+
pub struct Local {
17+
telemetry: Option<Telemetry>,
18+
}
19+
20+
impl Local {
21+
/// Create a new Host targeting the local machine.
22+
pub fn new() -> Box<Future<Item = Arc<Local>, Error = Error>> {
23+
let mut host = Arc::new(Local {
24+
telemetry: None,
25+
});
26+
27+
Box::new(telemetry::load(&host).map(|t| {
28+
Arc::get_mut(&mut host).unwrap().telemetry = Some(t);
29+
host
30+
}))
31+
}
32+
}
33+
34+
impl Host for Local {
35+
fn telemetry(&self) -> &Telemetry {
36+
self.telemetry.as_ref().unwrap()
37+
}
38+
39+
fn run<D: 'static>(&self, provider: Runnable) -> Box<Future<Item = D, Error = Error>>
40+
where for<'de> D: Deserialize<'de>
41+
{
42+
Box::new(provider.exec()
43+
.chain_err(|| "Could not run provider")
44+
.and_then(|s| {
45+
match serde_json::to_value(s).chain_err(|| "Could not run provider") {
46+
Ok(v) => match serde_json::from_value::<D>(v).chain_err(|| "Could not run provider") {
47+
Ok(d) => future::ok(d),
48+
Err(e) => future::err(e),
49+
},
50+
Err(e) => future::err(e),
51+
}
52+
}))
53+
}
54+
}

core/src/host/mod.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2015-2017 Intecture Developers.
2+
//
3+
// Licensed under the Mozilla Public License 2.0 <LICENSE or
4+
// https://www.tldrlegal.com/l/mpl-2.0>. This file may not be copied,
5+
// modified, or distributed except according to those terms.
6+
7+
//! Manages the connection between the API and your servers.
8+
9+
pub mod local;
10+
pub mod remote;
11+
12+
use errors::*;
13+
use Runnable;
14+
use futures::Future;
15+
use serde::Deserialize;
16+
use telemetry::Telemetry;
17+
18+
pub trait Host {
19+
/// Retrieve Telemetry
20+
fn telemetry(&self) -> &Telemetry;
21+
22+
#[doc(hidden)]
23+
fn run<D: 'static>(&self, Runnable) -> Box<Future<Item = D, Error = Error>>
24+
where for<'de> D: Deserialize<'de>;
25+
}

core/src/host.rs renamed to core/src/host/remote.rs

Lines changed: 15 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,16 @@
44
// https://www.tldrlegal.com/l/mpl-2.0>. This file may not be copied,
55
// modified, or distributed except according to those terms.
66

7-
//! Manages the connection between the API and your host.
8-
97
use bytes::{BufMut, BytesMut};
108
use errors::*;
119
use futures::{future, Future};
12-
use {Executable, Runnable};
10+
use Runnable;
1311
use serde::Deserialize;
1412
use serde_json;
1513
use std::{io, result};
1614
use std::sync::Arc;
1715
use std::net::SocketAddr;
16+
use super::Host;
1817
use telemetry::{self, Telemetry};
1918
use tokio_core::net::TcpStream;
2019
use tokio_core::reactor::Handle;
@@ -24,56 +23,16 @@ use tokio_proto::pipeline::{ClientProto, ClientService, ServerProto};
2423
use tokio_proto::TcpClient;
2524
use tokio_service::Service;
2625

27-
pub trait Host {
28-
/// Retrieve Telemetry
29-
fn telemetry(&self) -> &Telemetry;
30-
31-
#[doc(hidden)]
32-
fn run<D: 'static>(&self, Runnable) -> Box<Future<Item = D, Error = Error>>
33-
where for<'de> D: Deserialize<'de>;
34-
}
35-
36-
pub struct LocalHost {
37-
telemetry: Option<Telemetry>,
38-
}
39-
40-
impl LocalHost {
41-
/// Create a new Host targeting the local machine.
42-
pub fn new() -> Box<Future<Item = Arc<LocalHost>, Error = Error>> {
43-
let mut host = Arc::new(LocalHost {
44-
telemetry: None,
45-
});
46-
47-
Box::new(telemetry::load(&host).map(|t| {
48-
Arc::get_mut(&mut host).unwrap().telemetry = Some(t);
49-
host
50-
}))
51-
}
52-
}
53-
54-
impl Host for LocalHost {
55-
fn telemetry(&self) -> &Telemetry {
56-
self.telemetry.as_ref().unwrap()
57-
}
58-
59-
fn run<D: 'static>(&self, provider: Runnable) -> Box<Future<Item = D, Error = Error>>
60-
where for<'de> D: Deserialize<'de>
61-
{
62-
Box::new(provider.exec()
63-
.chain_err(|| "Could not run provider")
64-
.and_then(|s| {
65-
match serde_json::to_value(s).chain_err(|| "Could not run provider") {
66-
Ok(v) => match serde_json::from_value::<D>(v).chain_err(|| "Could not run provider") {
67-
Ok(d) => future::ok(d),
68-
Err(e) => future::err(e),
69-
},
70-
Err(e) => future::err(e),
71-
}
72-
}))
73-
}
26+
pub trait RemoteHost: Host {
27+
fn connect(addr: &str, handle: &Handle) -> Box<Future<Item = Arc<Self>, Error = Error>>;
7428
}
7529

76-
pub struct RemoteHost {
30+
/// A Host type that uses an unencrypted socket.
31+
///
32+
/// *Warning! This Host type is susceptible to eavesdropping and MITM
33+
/// attacks, and ideally should only be used for testing on secure private
34+
/// networks.*
35+
pub struct Plain {
7736
inner: ClientService<TcpStream, JsonProto>,
7837
telemetry: Option<Telemetry>,
7938
}
@@ -83,9 +42,9 @@ pub struct JsonCodec;
8342
#[doc(hidden)]
8443
pub struct JsonProto;
8544

86-
impl RemoteHost {
45+
impl Plain {
8746
/// Create a new Host connected to addr.
88-
pub fn connect(addr: &str, handle: &Handle) -> Box<Future<Item = Arc<RemoteHost>, Error = Error>> {
47+
pub fn connect(addr: &str, handle: &Handle) -> Box<Future<Item = Arc<Plain>, Error = Error>> {
8948
let addr: SocketAddr = match addr.parse().chain_err(|| "Invalid host address") {
9049
Ok(addr) => addr,
9150
Err(e) => return Box::new(future::err(e)),
@@ -99,7 +58,7 @@ impl RemoteHost {
9958
.and_then(|client_service| {
10059
info!("Connected!");
10160

102-
let mut host = Arc::new(RemoteHost {
61+
let mut host = Arc::new(Plain {
10362
inner: client_service,
10463
telemetry: None,
10564
});
@@ -114,7 +73,7 @@ impl RemoteHost {
11473
}
11574
}
11675

117-
impl Host for RemoteHost {
76+
impl Host for Plain {
11877
fn telemetry(&self) -> &Telemetry {
11978
self.telemetry.as_ref().unwrap()
12079
}
@@ -135,7 +94,7 @@ impl Host for RemoteHost {
13594
}
13695
}
13796

138-
impl Service for RemoteHost {
97+
impl Service for Plain {
13998
type Request = serde_json::Value;
14099
type Response = serde_json::Value;
141100
type Error = io::Error;

core/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ pub mod errors;
3333
pub mod host;
3434
pub mod prelude {
3535
pub use command;
36-
pub use host::{Host, LocalHost, RemoteHost};
36+
pub use host::Host;
37+
pub use host::remote::{self, RemoteHost};
38+
pub use host::local::{self, Local};
3739
pub use telemetry::{self, Cpu, FsMount, Os, OsFamily, OsPlatform, Telemetry};
3840
}
3941
mod target;

0 commit comments

Comments
 (0)