added config manager support to server and removed old serverConfig references
This commit is contained in:
parent
8c167ad603
commit
14a8ed4dac
|
|
@ -1,70 +1,78 @@
|
|||
use actix::Addr;
|
||||
use mlua::{Error, UserData, UserDataFields, UserDataMethods};
|
||||
use crate::scripting::scriptable_client_manager::ScriptableClientManager;
|
||||
use crate::scripting::scriptable_network_manager::ScriptableNetworkManager;
|
||||
use actix::Addr;
|
||||
use mlua::{Error, UserData, UserDataMethods};
|
||||
|
||||
use crate::server::ServerDataResponse::{
|
||||
ClientManager, Name, NetworkManager, Owner,
|
||||
};
|
||||
use crate::server::*;
|
||||
use crate::server::ServerDataResponse::{ClientManager, Name, NetworkManager, Owner, Port};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub(crate) struct ScriptableServer {
|
||||
pub(super) addr: Addr<Server>
|
||||
pub(super) addr: Addr<Server>,
|
||||
}
|
||||
|
||||
impl UserData for ScriptableServer {
|
||||
|
||||
fn add_methods<'lua, M: UserDataMethods<'lua, Self>>(methods: &mut M) {
|
||||
methods.add_async_method("name", |_lua, obj, ()| async move {
|
||||
let name: Option<ServerDataResponse> = obj.addr.send(ServerDataMessage::Name).await.ok();
|
||||
let name: Option<ServerDataResponse> =
|
||||
obj.addr.send(ServerDataMessage::Name).await.ok();
|
||||
if let Some(Name(name)) = name {
|
||||
Ok(name)
|
||||
} else {
|
||||
Err(Error::RuntimeError("Name returned null or other value".to_string()))
|
||||
}
|
||||
});
|
||||
|
||||
methods.add_async_method("port", |_lua, obj, ()| async move {
|
||||
let port: Option<ServerDataResponse> = obj.addr.send(ServerDataMessage::Port).await.ok();
|
||||
if let Some(Port(name)) = port {
|
||||
Ok(name)
|
||||
} else {
|
||||
Err(Error::RuntimeError("Name returned null or other value".to_string()))
|
||||
Err(Error::RuntimeError(
|
||||
"Name returned null or other value".to_string(),
|
||||
))
|
||||
}
|
||||
});
|
||||
|
||||
methods.add_async_method("owner", |_lua, obj, ()| async move {
|
||||
let owner: Option<ServerDataResponse> = obj.addr.send(ServerDataMessage::Owner).await.ok();
|
||||
let owner: Option<ServerDataResponse> =
|
||||
obj.addr.send(ServerDataMessage::Owner).await.ok();
|
||||
if let Some(Owner(name)) = owner {
|
||||
Ok(name)
|
||||
} else {
|
||||
Err(Error::RuntimeError("Name returned null or other value".to_string()))
|
||||
Err(Error::RuntimeError(
|
||||
"Name returned null or other value".to_string(),
|
||||
))
|
||||
}
|
||||
});
|
||||
|
||||
methods.add_async_method("client_manager", |_lua, obj, ()| async move {
|
||||
let name: Option<ServerDataResponse> = obj.addr.send(ServerDataMessage::ClientManager).await.ok();
|
||||
if let Some(ClientManager(Some(cm))) = name {
|
||||
Ok(ScriptableClientManager::from(cm))
|
||||
} else {
|
||||
Err(Error::RuntimeError("Name returned null or other value".to_string()))
|
||||
}
|
||||
});
|
||||
methods.add_async_method(
|
||||
"client_manager",
|
||||
|_lua, obj, ()| async move {
|
||||
let name: Option<ServerDataResponse> =
|
||||
obj.addr.send(ServerDataMessage::ClientManager).await.ok();
|
||||
if let Some(ClientManager(Some(cm))) = name {
|
||||
Ok(ScriptableClientManager::from(cm))
|
||||
} else {
|
||||
Err(Error::RuntimeError(
|
||||
"Name returned null or other value".to_string(),
|
||||
))
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
methods.add_async_method("network_manager", |_lua, obj, ()| async move {
|
||||
let name: Option<ServerDataResponse> = obj.addr.send(ServerDataMessage::NetworkManager).await.ok();
|
||||
if let Some(NetworkManager(Some(nm))) = name {
|
||||
Ok(ScriptableNetworkManager::from(nm))
|
||||
} else {
|
||||
Err(Error::RuntimeError("Name returned null or other value".to_string()))
|
||||
}
|
||||
});
|
||||
methods.add_async_method(
|
||||
"network_manager",
|
||||
|_lua, obj, ()| async move {
|
||||
let name: Option<ServerDataResponse> =
|
||||
obj.addr.send(ServerDataMessage::NetworkManager).await.ok();
|
||||
if let Some(NetworkManager(Some(nm))) = name {
|
||||
Ok(ScriptableNetworkManager::from(nm))
|
||||
} else {
|
||||
Err(Error::RuntimeError(
|
||||
"Name returned null or other value".to_string(),
|
||||
))
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Addr<Server>> for ScriptableServer {
|
||||
fn from(addr: Addr<Server>) -> Self {
|
||||
Self {
|
||||
addr
|
||||
}
|
||||
Self { addr }
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,16 +0,0 @@
|
|||
/// Configuration for the server
|
||||
pub(super) struct ServerConfig {
|
||||
pub(super) port: u16,
|
||||
pub(super) name: String,
|
||||
pub(super) owner: String,
|
||||
}
|
||||
|
||||
impl Default for ServerConfig {
|
||||
fn default() -> Self {
|
||||
ServerConfig {
|
||||
owner: "john_smith@example.com".to_string(),
|
||||
name: "default server name".to_string(),
|
||||
port: 5600,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,13 +1,11 @@
|
|||
use actix::{Addr, Message, MessageResponse};
|
||||
use crate::client_management::ClientManager;
|
||||
use crate::network::NetworkManager;
|
||||
|
||||
use actix::{Addr, Message, MessageResponse};
|
||||
|
||||
#[derive(Message, Clone)]
|
||||
#[rtype(result = "ServerDataResponse")]
|
||||
pub enum ServerDataMessage {
|
||||
Name,
|
||||
Port,
|
||||
Owner,
|
||||
ClientManager,
|
||||
NetworkManager,
|
||||
|
|
@ -20,4 +18,4 @@ pub enum ServerDataResponse {
|
|||
Owner(String),
|
||||
ClientManager(Option<Addr<ClientManager>>),
|
||||
NetworkManager(Option<Addr<NetworkManager>>),
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,11 +4,10 @@
|
|||
//! and supervisor to the actor system.
|
||||
|
||||
mod server;
|
||||
mod config;
|
||||
|
||||
mod builder;
|
||||
mod messages;
|
||||
|
||||
use config::ServerConfig;
|
||||
pub use server::Server;
|
||||
pub use builder::ServerBuilder;
|
||||
pub use messages::*;
|
||||
pub use messages::*;
|
||||
pub use server::Server;
|
||||
|
|
|
|||
|
|
@ -4,34 +4,33 @@
|
|||
use crate::client_management::client::Client;
|
||||
use crate::client_management::ClientManagerMessage::AddClient;
|
||||
use crate::client_management::{ClientManager, ClientManagerOutput};
|
||||
use crate::config_manager::ConfigManager;
|
||||
use crate::config_manager::{
|
||||
ConfigManager, ConfigManagerDataMessage, ConfigManagerDataResponse,
|
||||
ConfigValue,
|
||||
};
|
||||
use crate::lua::LuaManager;
|
||||
use crate::network::ConnectionMessage::{CloseConnection, SendData};
|
||||
use crate::network::NetworkOutput::{InfoRequested, NewClient};
|
||||
use crate::network::{
|
||||
Connection, NetworkManager, NetworkMessage, NetworkOutput,
|
||||
};
|
||||
use crate::network::{Connection, NetworkManager, NetworkOutput};
|
||||
use crate::rhai::RhaiManager;
|
||||
use crate::server::config::ServerConfig;
|
||||
|
||||
use crate::server::{
|
||||
builder, ServerBuilder, ServerDataMessage, ServerDataResponse,
|
||||
};
|
||||
use actix::dev::MessageResponse;
|
||||
|
||||
use actix::fut::wrap_future;
|
||||
use actix::{
|
||||
Actor, ActorFutureExt, Addr, AsyncContext, Context, ContextFutureSpawner,
|
||||
Handler,
|
||||
};
|
||||
use actix::{Actor, ActorFutureExt, Addr, AsyncContext, Context, Handler};
|
||||
use foundation::messages::network::NetworkSockOut::GotInfo;
|
||||
use foundation::ClientDetails;
|
||||
use mlua::Lua;
|
||||
|
||||
/// This struct is the main actor of the server.
|
||||
/// all other actors are ran through here.
|
||||
pub struct Server {
|
||||
config: ServerConfig,
|
||||
name: Option<String>,
|
||||
owner: Option<String>,
|
||||
|
||||
network_manager: Option<Addr<NetworkManager>>,
|
||||
client_management: Option<Addr<ClientManager>>,
|
||||
client_manager: Option<Addr<ClientManager>>,
|
||||
rhai_manager: Option<Addr<RhaiManager>>,
|
||||
lua_manager: Option<Addr<LuaManager>>,
|
||||
}
|
||||
|
|
@ -49,7 +48,7 @@ impl Server {
|
|||
addr: Addr<Connection>,
|
||||
details: ClientDetails,
|
||||
) {
|
||||
if let Some(mgr) = self.client_management.as_ref() {
|
||||
if let Some(mgr) = self.client_manager.as_ref() {
|
||||
let client = Client::new(addr, details.clone());
|
||||
mgr.do_send(AddClient(details.uuid, client));
|
||||
}
|
||||
|
|
@ -63,8 +62,8 @@ impl Server {
|
|||
let fut = wrap_future(
|
||||
sender.send(SendData(
|
||||
serde_json::to_string(&GotInfo {
|
||||
server_name: self.config.name.clone(),
|
||||
server_owner: self.config.owner.clone(),
|
||||
server_name: self.name.as_ref().unwrap().clone(),
|
||||
server_owner: self.owner.as_ref().unwrap().clone(),
|
||||
})
|
||||
.expect("Failed to serialise"),
|
||||
)),
|
||||
|
|
@ -81,15 +80,16 @@ impl Actor for Server {
|
|||
type Context = Context<Self>;
|
||||
|
||||
fn started(&mut self, ctx: &mut Self::Context) {
|
||||
use ConfigManagerDataMessage::GetValue;
|
||||
use ConfigManagerDataResponse::GotValue;
|
||||
|
||||
let addr = ctx.address().downgrade();
|
||||
|
||||
let nm = NetworkManager::create(addr.clone().recipient())
|
||||
.port(self.config.port)
|
||||
.build();
|
||||
let nm = NetworkManager::create(addr.clone().recipient()).build();
|
||||
self.network_manager.replace(nm.clone());
|
||||
|
||||
let cm = ClientManager::new(addr.clone().recipient());
|
||||
self.client_management.replace(cm.clone());
|
||||
self.client_manager.replace(cm.clone());
|
||||
|
||||
let rm =
|
||||
RhaiManager::create(ctx.address(), nm.clone(), cm.clone()).build();
|
||||
|
|
@ -97,6 +97,37 @@ impl Actor for Server {
|
|||
|
||||
let lm = LuaManager::create(ctx.address(), nm, cm).build();
|
||||
self.lua_manager.replace(lm);
|
||||
|
||||
let name_fut = wrap_future(
|
||||
ConfigManager::shared().send(GetValue("Server.Name".to_owned())),
|
||||
)
|
||||
.map(|out, actor: &mut Server, _ctx| {
|
||||
let out =
|
||||
out.map(|inner| inner.ok()).ok().unwrap_or(None).unwrap_or(
|
||||
GotValue(ConfigValue::String("<UNKNOWN>".to_owned())),
|
||||
);
|
||||
|
||||
if let GotValue(ConfigValue::String(name)) = out {
|
||||
actor.name = Some(name);
|
||||
}
|
||||
});
|
||||
|
||||
let owner_fut = wrap_future(
|
||||
ConfigManager::shared().send(GetValue("Server.Owner".to_owned())),
|
||||
)
|
||||
.map(|out, actor: &mut Server, _ctx| {
|
||||
let out =
|
||||
out.map(|inner| inner.ok()).ok().unwrap_or(None).unwrap_or(
|
||||
GotValue(ConfigValue::String("<UNKNOWN>".to_owned())),
|
||||
);
|
||||
|
||||
if let GotValue(ConfigValue::String(owner)) = out {
|
||||
actor.owner = Some(owner);
|
||||
}
|
||||
});
|
||||
|
||||
ctx.spawn(name_fut);
|
||||
ctx.spawn(owner_fut);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -106,23 +137,18 @@ impl Handler<ServerDataMessage> for Server {
|
|||
fn handle(
|
||||
&mut self,
|
||||
msg: ServerDataMessage,
|
||||
ctx: &mut Self::Context,
|
||||
_ctx: &mut Self::Context,
|
||||
) -> Self::Result {
|
||||
println!("data message");
|
||||
match msg {
|
||||
ServerDataMessage::Name => {
|
||||
ServerDataResponse::Name(self.config.name.clone())
|
||||
}
|
||||
ServerDataMessage::Port => {
|
||||
ServerDataResponse::Port(self.config.port.clone())
|
||||
ServerDataResponse::Name(self.name.as_ref().unwrap().clone())
|
||||
}
|
||||
ServerDataMessage::Owner => {
|
||||
ServerDataResponse::Owner(self.config.owner.clone())
|
||||
ServerDataResponse::Owner(self.owner.as_ref().unwrap().clone())
|
||||
}
|
||||
ServerDataMessage::ClientManager => {
|
||||
ServerDataResponse::ClientManager(
|
||||
self.client_management.clone(),
|
||||
)
|
||||
ServerDataResponse::ClientManager(self.client_manager.clone())
|
||||
}
|
||||
ServerDataMessage::NetworkManager => {
|
||||
ServerDataResponse::NetworkManager(self.network_manager.clone())
|
||||
|
|
@ -155,8 +181,8 @@ impl Handler<ClientManagerOutput> for Server {
|
|||
|
||||
fn handle(
|
||||
&mut self,
|
||||
msg: ClientManagerOutput,
|
||||
ctx: &mut Self::Context,
|
||||
_msg: ClientManagerOutput,
|
||||
_ctx: &mut Self::Context,
|
||||
) -> Self::Result {
|
||||
todo!()
|
||||
}
|
||||
|
|
@ -165,17 +191,11 @@ impl Handler<ClientManagerOutput> for Server {
|
|||
impl From<ServerBuilder> for Server {
|
||||
fn from(builder: ServerBuilder) -> Self {
|
||||
Server {
|
||||
config: ServerConfig {
|
||||
port: builder.port.unwrap_or(5600),
|
||||
name: builder
|
||||
.name
|
||||
.unwrap_or_else(|| "Default Name".to_string()),
|
||||
owner: builder
|
||||
.owner
|
||||
.unwrap_or_else(|| "Default owner".to_string()),
|
||||
},
|
||||
name: None,
|
||||
owner: None,
|
||||
|
||||
network_manager: None,
|
||||
client_management: None,
|
||||
client_manager: None,
|
||||
rhai_manager: None,
|
||||
lua_manager: None,
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue