2018-11-03 17:09:14 +01:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::{fmt, io, net};
|
|
|
|
|
2018-12-09 19:15:49 +01:00
|
|
|
use actix_service::{IntoNewService, NewService};
|
2018-11-03 17:09:14 +01:00
|
|
|
use futures::future::{join_all, Future};
|
2018-12-06 23:04:42 +01:00
|
|
|
use log::error;
|
2018-11-03 17:09:14 +01:00
|
|
|
use tokio_tcp::TcpStream;
|
|
|
|
|
2018-12-06 23:04:42 +01:00
|
|
|
use crate::counter::CounterGuard;
|
2018-11-03 17:09:14 +01:00
|
|
|
|
2018-12-10 06:51:35 +01:00
|
|
|
use super::builder::bind_addr;
|
2018-11-03 17:09:14 +01:00
|
|
|
use super::services::{
|
|
|
|
BoxedServerService, InternalServiceFactory, ServerMessage, StreamService,
|
|
|
|
};
|
|
|
|
use super::Token;
|
|
|
|
|
|
|
|
pub struct ServiceConfig {
|
2018-12-12 23:16:16 +01:00
|
|
|
pub(crate) services: Vec<(String, net::TcpListener)>,
|
|
|
|
pub(crate) apply: Option<Box<ServiceRuntimeConfiguration>>,
|
|
|
|
pub(crate) threads: usize,
|
2018-11-03 17:09:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ServiceConfig {
|
2018-12-12 23:16:16 +01:00
|
|
|
pub(super) fn new(threads: usize) -> ServiceConfig {
|
2018-11-03 17:09:14 +01:00
|
|
|
ServiceConfig {
|
2018-12-12 23:16:16 +01:00
|
|
|
threads,
|
2018-11-03 17:09:14 +01:00
|
|
|
services: Vec::new(),
|
2018-12-12 23:16:16 +01:00
|
|
|
apply: None,
|
2018-11-03 17:09:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-12 23:16:16 +01:00
|
|
|
/// Set number of workers to start.
|
|
|
|
///
|
|
|
|
/// By default server uses number of available logical cpu as workers
|
|
|
|
/// count.
|
|
|
|
pub fn workers(&mut self, num: usize) {
|
|
|
|
self.threads = num;
|
|
|
|
}
|
|
|
|
|
2018-11-03 17:09:14 +01:00
|
|
|
/// Add new service to server
|
|
|
|
pub fn bind<U, N: AsRef<str>>(&mut self, name: N, addr: U) -> io::Result<&mut Self>
|
|
|
|
where
|
|
|
|
U: net::ToSocketAddrs,
|
|
|
|
{
|
|
|
|
let sockets = bind_addr(addr)?;
|
|
|
|
|
|
|
|
for lst in sockets {
|
|
|
|
self.listen(name.as_ref(), lst);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Add new service to server
|
|
|
|
pub fn listen<N: AsRef<str>>(&mut self, name: N, lst: net::TcpListener) -> &mut Self {
|
2018-12-12 23:16:16 +01:00
|
|
|
if self.apply.is_none() {
|
|
|
|
self.apply = Some(Box::new(not_configured));
|
|
|
|
}
|
2018-11-03 17:09:14 +01:00
|
|
|
self.services.push((name.as_ref().to_string(), lst));
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2018-12-12 23:16:16 +01:00
|
|
|
/// Register service configuration function. This function get called
|
|
|
|
/// during worker runtime configuration. It get executed in worker thread.
|
|
|
|
pub fn apply<F>(&mut self, f: F) -> io::Result<()>
|
2018-11-03 17:09:14 +01:00
|
|
|
where
|
|
|
|
F: Fn(&mut ServiceRuntime) + Send + Clone + 'static,
|
|
|
|
{
|
2018-12-12 23:16:16 +01:00
|
|
|
self.apply = Some(Box::new(f));
|
2018-11-03 17:09:14 +01:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) struct ConfiguredService {
|
|
|
|
rt: Box<ServiceRuntimeConfiguration>,
|
|
|
|
names: HashMap<Token, String>,
|
|
|
|
services: HashMap<String, Token>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ConfiguredService {
|
|
|
|
pub(super) fn new(rt: Box<ServiceRuntimeConfiguration>) -> Self {
|
|
|
|
ConfiguredService {
|
|
|
|
rt,
|
|
|
|
names: HashMap::new(),
|
|
|
|
services: HashMap::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) fn stream(&mut self, token: Token, name: String) {
|
|
|
|
self.names.insert(token, name.clone());
|
|
|
|
self.services.insert(name, token);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl InternalServiceFactory for ConfiguredService {
|
|
|
|
fn name(&self, token: Token) -> &str {
|
|
|
|
&self.names[&token]
|
|
|
|
}
|
|
|
|
|
|
|
|
fn clone_factory(&self) -> Box<InternalServiceFactory> {
|
|
|
|
Box::new(Self {
|
|
|
|
rt: self.rt.clone(),
|
|
|
|
names: self.names.clone(),
|
|
|
|
services: self.services.clone(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create(&self) -> Box<Future<Item = Vec<(Token, BoxedServerService)>, Error = ()>> {
|
|
|
|
// configure services
|
|
|
|
let mut rt = ServiceRuntime::new(self.services.clone());
|
|
|
|
self.rt.configure(&mut rt);
|
|
|
|
rt.validate();
|
|
|
|
|
|
|
|
// construct services
|
|
|
|
let mut fut = Vec::new();
|
|
|
|
for (token, ns) in rt.services {
|
|
|
|
fut.push(ns.new_service().map(move |service| (token, service)));
|
|
|
|
}
|
|
|
|
|
|
|
|
Box::new(join_all(fut).map_err(|e| {
|
|
|
|
error!("Can not construct service: {:?}", e);
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) trait ServiceRuntimeConfiguration: Send {
|
|
|
|
fn clone(&self) -> Box<ServiceRuntimeConfiguration>;
|
|
|
|
|
2018-12-06 23:04:42 +01:00
|
|
|
fn configure(&self, rt: &mut ServiceRuntime);
|
2018-11-03 17:09:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<F> ServiceRuntimeConfiguration for F
|
|
|
|
where
|
|
|
|
F: Fn(&mut ServiceRuntime) + Send + Clone + 'static,
|
|
|
|
{
|
|
|
|
fn clone(&self) -> Box<ServiceRuntimeConfiguration> {
|
|
|
|
Box::new(self.clone())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn configure(&self, rt: &mut ServiceRuntime) {
|
|
|
|
(self)(rt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn not_configured(_: &mut ServiceRuntime) {
|
|
|
|
error!("Service is not configured");
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct ServiceRuntime {
|
|
|
|
names: HashMap<String, Token>,
|
|
|
|
services: HashMap<Token, BoxedNewService>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ServiceRuntime {
|
|
|
|
fn new(names: HashMap<String, Token>) -> Self {
|
|
|
|
ServiceRuntime {
|
|
|
|
names,
|
|
|
|
services: HashMap::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn validate(&self) {
|
|
|
|
for (name, token) in &self.names {
|
|
|
|
if !self.services.contains_key(&token) {
|
|
|
|
error!("Service {:?} is not configured", name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn service<T, F>(&mut self, name: &str, service: F)
|
|
|
|
where
|
2019-02-02 04:53:13 +01:00
|
|
|
F: IntoNewService<T>,
|
|
|
|
T: NewService<Request = TcpStream, Response = ()> + 'static,
|
2018-11-03 17:09:14 +01:00
|
|
|
T::Future: 'static,
|
|
|
|
T::Service: 'static,
|
|
|
|
T::InitError: fmt::Debug,
|
|
|
|
{
|
|
|
|
// let name = name.to_owned();
|
|
|
|
if let Some(token) = self.names.get(name) {
|
|
|
|
self.services.insert(
|
|
|
|
token.clone(),
|
|
|
|
Box::new(ServiceFactory {
|
|
|
|
inner: service.into_new_service(),
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
panic!("Unknown service: {:?}", name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type BoxedNewService = Box<
|
|
|
|
NewService<
|
2019-02-02 04:53:13 +01:00
|
|
|
Request = (Option<CounterGuard>, ServerMessage),
|
2018-11-03 17:09:14 +01:00
|
|
|
Response = (),
|
|
|
|
Error = (),
|
|
|
|
InitError = (),
|
|
|
|
Service = BoxedServerService,
|
|
|
|
Future = Box<Future<Item = BoxedServerService, Error = ()>>,
|
|
|
|
>,
|
|
|
|
>;
|
|
|
|
|
|
|
|
struct ServiceFactory<T> {
|
|
|
|
inner: T,
|
|
|
|
}
|
|
|
|
|
2019-02-02 04:53:13 +01:00
|
|
|
impl<T> NewService for ServiceFactory<T>
|
2018-11-03 17:09:14 +01:00
|
|
|
where
|
2019-02-02 04:53:13 +01:00
|
|
|
T: NewService<Request = TcpStream, Response = ()>,
|
2018-11-03 17:09:14 +01:00
|
|
|
T::Future: 'static,
|
|
|
|
T::Service: 'static,
|
|
|
|
T::Error: 'static,
|
|
|
|
T::InitError: fmt::Debug + 'static,
|
|
|
|
{
|
2019-02-02 04:53:13 +01:00
|
|
|
type Request = (Option<CounterGuard>, ServerMessage);
|
2018-11-03 17:09:14 +01:00
|
|
|
type Response = ();
|
|
|
|
type Error = ();
|
|
|
|
type InitError = ();
|
|
|
|
type Service = BoxedServerService;
|
|
|
|
type Future = Box<Future<Item = BoxedServerService, Error = ()>>;
|
|
|
|
|
|
|
|
fn new_service(&self) -> Self::Future {
|
|
|
|
Box::new(self.inner.new_service().map_err(|_| ()).map(|s| {
|
|
|
|
let service: BoxedServerService = Box::new(StreamService::new(s));
|
|
|
|
service
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
}
|