poa-bridge/bridge/src/app.rs

79 lines
2.0 KiB
Rust
Raw Normal View History

2017-08-01 02:36:48 -07:00
use std::path::{Path, PathBuf};
2017-08-03 15:48:12 -07:00
use tokio_core::reactor::{Handle};
2017-08-31 08:32:34 -07:00
use tokio_timer::Timer;
2017-08-13 04:09:50 -07:00
use web3::Transport;
2017-08-01 02:36:48 -07:00
use web3::transports::ipc::Ipc;
2017-08-23 02:59:25 -07:00
use error::{Error, ResultExt, ErrorKind};
2017-08-01 02:36:48 -07:00
use config::Config;
2017-10-10 02:02:46 -07:00
use contracts::{home, foreign};
2017-08-04 08:57:09 -07:00
2017-08-01 02:36:48 -07:00
pub struct App<T> where T: Transport {
2017-08-12 07:57:07 -07:00
pub config: Config,
pub database_path: PathBuf,
pub connections: Connections<T>,
2017-10-10 02:02:46 -07:00
pub home_bridge: home::HomeBridge,
pub foreign_bridge: foreign::ForeignBridge,
2017-08-31 08:32:34 -07:00
pub timer: Timer,
2017-08-01 02:36:48 -07:00
}
pub struct Connections<T> where T: Transport {
2017-10-10 02:02:46 -07:00
pub home: T,
pub foreign: T,
2017-08-01 02:36:48 -07:00
}
impl Connections<Ipc> {
2017-10-10 02:02:46 -07:00
pub fn new_ipc<P: AsRef<Path>>(handle: &Handle, home: P, foreign: P) -> Result<Self, Error> {
let home = Ipc::with_event_loop(home, handle)
2017-08-23 02:59:25 -07:00
.map_err(ErrorKind::Web3)
.map_err(Error::from)
2017-10-10 02:02:46 -07:00
.chain_err(|| "Cannot connect to home node ipc")?;
let foreign = Ipc::with_event_loop(foreign, handle)
2017-08-23 02:59:25 -07:00
.map_err(ErrorKind::Web3)
.map_err(Error::from)
2017-10-10 02:02:46 -07:00
.chain_err(|| "Cannot connect to foreign node ipc")?;
2017-08-02 03:45:15 -07:00
2017-08-01 02:36:48 -07:00
let result = Connections {
2017-10-10 02:02:46 -07:00
home,
foreign,
2017-08-01 02:36:48 -07:00
};
Ok(result)
}
}
2017-08-13 11:44:41 -07:00
impl<T: Transport> Connections<T> {
pub fn as_ref(&self) -> Connections<&T> {
Connections {
2017-10-10 02:02:46 -07:00
home: &self.home,
foreign: &self.foreign,
2017-08-13 11:44:41 -07:00
}
}
}
2017-08-01 02:36:48 -07:00
impl App<Ipc> {
2017-08-03 15:48:12 -07:00
pub fn new_ipc<P: AsRef<Path>>(config: Config, database_path: P, handle: &Handle) -> Result<Self, Error> {
2017-10-10 02:02:46 -07:00
let connections = Connections::new_ipc(handle, &config.home.ipc, &config.foreign.ipc)?;
2017-08-01 02:36:48 -07:00
let result = App {
config,
database_path: database_path.as_ref().to_path_buf(),
connections,
2017-10-10 02:02:46 -07:00
home_bridge: home::HomeBridge::default(),
foreign_bridge: foreign::ForeignBridge::default(),
2017-08-31 08:32:34 -07:00
timer: Timer::default(),
2017-08-01 02:36:48 -07:00
};
Ok(result)
}
2017-08-12 07:57:07 -07:00
}
2017-08-01 02:36:48 -07:00
2017-08-12 07:57:07 -07:00
impl<T: Transport> App<T> {
2017-08-13 11:44:41 -07:00
pub fn as_ref(&self) -> App<&T> {
App {
config: self.config.clone(),
connections: self.connections.as_ref(),
database_path: self.database_path.clone(),
2017-10-10 02:02:46 -07:00
home_bridge: home::HomeBridge::default(),
foreign_bridge: foreign::ForeignBridge::default(),
2017-08-31 08:32:34 -07:00
timer: self.timer.clone(),
2017-08-01 02:36:48 -07:00
}
}
}