2019-01-07 11:33:07 +01:00
|
|
|
// Copyright 2015-2019 Parity Technologies (UK) Ltd.
|
|
|
|
// This file is part of Parity Ethereum.
|
2017-05-24 12:24:07 +02:00
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is free software: you can redistribute it and/or modify
|
2017-05-24 12:24:07 +02:00
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is distributed in the hope that it will be useful,
|
2017-05-24 12:24:07 +02:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
2019-01-07 11:33:07 +01:00
|
|
|
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
|
2017-05-24 12:24:07 +02:00
|
|
|
|
2020-08-05 06:08:03 +02:00
|
|
|
use std::{
|
|
|
|
ops::{Deref, DerefMut},
|
|
|
|
path::PathBuf,
|
|
|
|
};
|
2018-01-19 17:32:53 +01:00
|
|
|
use tempdir::TempDir;
|
2017-05-24 12:24:07 +02:00
|
|
|
|
2018-10-22 09:40:50 +02:00
|
|
|
use parity_runtime::{Runtime, TaskExecutor};
|
2017-05-24 12:24:07 +02:00
|
|
|
|
|
|
|
use authcodes::AuthCodes;
|
|
|
|
|
|
|
|
/// Server with event loop
|
|
|
|
pub struct Server<T> {
|
2020-08-05 06:08:03 +02:00
|
|
|
/// Server
|
|
|
|
pub server: T,
|
|
|
|
/// RPC Event Loop
|
|
|
|
pub event_loop: Runtime,
|
2017-05-24 12:24:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Server<T> {
|
2020-08-05 06:08:03 +02:00
|
|
|
pub fn new<F>(f: F) -> Server<T>
|
|
|
|
where
|
|
|
|
F: FnOnce(TaskExecutor) -> T,
|
|
|
|
{
|
|
|
|
let event_loop = Runtime::with_thread_count(1);
|
|
|
|
let remote = event_loop.raw_executor();
|
|
|
|
|
|
|
|
Server {
|
|
|
|
server: f(remote),
|
|
|
|
event_loop,
|
|
|
|
}
|
|
|
|
}
|
2017-05-24 12:24:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Deref for Server<T> {
|
2020-08-05 06:08:03 +02:00
|
|
|
type Target = T;
|
2017-05-24 12:24:07 +02:00
|
|
|
|
2020-08-05 06:08:03 +02:00
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.server
|
|
|
|
}
|
2017-05-24 12:24:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Struct representing authcodes
|
|
|
|
pub struct GuardedAuthCodes {
|
2020-08-05 06:08:03 +02:00
|
|
|
authcodes: AuthCodes,
|
|
|
|
_tempdir: TempDir,
|
|
|
|
/// The path to the mock authcodes
|
|
|
|
pub path: PathBuf,
|
2017-05-24 12:24:07 +02:00
|
|
|
}
|
|
|
|
|
2019-03-22 12:01:11 +01:00
|
|
|
impl Default for GuardedAuthCodes {
|
2020-08-05 06:08:03 +02:00
|
|
|
fn default() -> Self {
|
|
|
|
let tempdir = TempDir::new("").unwrap();
|
|
|
|
let path = tempdir.path().join("file");
|
|
|
|
|
|
|
|
GuardedAuthCodes {
|
|
|
|
authcodes: AuthCodes::from_file(&path).unwrap(),
|
|
|
|
_tempdir: tempdir,
|
|
|
|
path,
|
|
|
|
}
|
|
|
|
}
|
2017-05-24 12:24:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Deref for GuardedAuthCodes {
|
2020-08-05 06:08:03 +02:00
|
|
|
type Target = AuthCodes;
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.authcodes
|
|
|
|
}
|
2017-05-24 12:24:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl DerefMut for GuardedAuthCodes {
|
2020-08-05 06:08:03 +02:00
|
|
|
fn deref_mut(&mut self) -> &mut AuthCodes {
|
|
|
|
&mut self.authcodes
|
|
|
|
}
|
2017-05-24 12:24:07 +02:00
|
|
|
}
|