Improved plugin loader
This commit is contained in:
@@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize};
|
|||||||
use std::{
|
use std::{
|
||||||
collections::HashMap,
|
collections::HashMap,
|
||||||
fs,
|
fs,
|
||||||
io::Read,
|
io::{BufRead, BufReader, Read},
|
||||||
net::{TcpListener, TcpStream},
|
net::{TcpListener, TcpStream},
|
||||||
path::Path,
|
path::Path,
|
||||||
process::Command,
|
process::Command,
|
||||||
@@ -27,14 +27,12 @@ pub struct PluginHandshake {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub struct PluginLoader {
|
pub struct PluginLoader {
|
||||||
plugins: Vec<Plugin>,
|
|
||||||
plugin_clients: Arc<Mutex<HashMap<String, TcpStream>>>,
|
plugin_clients: Arc<Mutex<HashMap<String, TcpStream>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PluginLoader {
|
impl PluginLoader {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
plugins: Vec::new(),
|
|
||||||
plugin_clients: Arc::new(Mutex::new(HashMap::new())),
|
plugin_clients: Arc::new(Mutex::new(HashMap::new())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -58,15 +56,15 @@ impl PluginLoader {
|
|||||||
.is_none()
|
.is_none()
|
||||||
{}
|
{}
|
||||||
LOGGER.info(format!("Loaded {}", plugin_json.id));
|
LOGGER.info(format!("Loaded {}", plugin_json.id));
|
||||||
Plugin(
|
let a = self
|
||||||
self.plugin_clients
|
.plugin_clients
|
||||||
.lock()
|
.lock()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.get(&plugin_json.id)
|
.get(&plugin_json.id)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.try_clone()
|
.try_clone()
|
||||||
.unwrap(),
|
.unwrap();
|
||||||
)
|
Plugin(a.try_clone().unwrap(), BufReader::new(a))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn start_server(&self) {
|
pub fn start_server(&self) {
|
||||||
@@ -75,9 +73,10 @@ impl PluginLoader {
|
|||||||
|
|
||||||
std::thread::spawn(move || {
|
std::thread::spawn(move || {
|
||||||
for stream in listener.incoming() {
|
for stream in listener.incoming() {
|
||||||
let mut stream = stream.unwrap();
|
let stream = stream.unwrap();
|
||||||
|
let mut reader = BufReader::new(&stream);
|
||||||
let mut s = String::new();
|
let mut s = String::new();
|
||||||
stream.read_to_string(&mut s).unwrap();
|
reader.read_line(&mut s).unwrap();
|
||||||
let plugin_handshake: PluginHandshake = serde_json::from_str(&s).unwrap();
|
let plugin_handshake: PluginHandshake = serde_json::from_str(&s).unwrap();
|
||||||
plugin_clients
|
plugin_clients
|
||||||
.lock()
|
.lock()
|
||||||
|
|||||||
+22
-4
@@ -1,7 +1,25 @@
|
|||||||
use std::net::TcpStream;
|
|
||||||
|
|
||||||
pub mod loader;
|
pub mod loader;
|
||||||
|
pub mod types;
|
||||||
|
|
||||||
pub struct Plugin(TcpStream);
|
use std::{
|
||||||
|
io::{BufRead, BufReader, Read, Write},
|
||||||
|
net::TcpStream,
|
||||||
|
};
|
||||||
|
|
||||||
impl Plugin {}
|
use crate::plugin::types::{LoaderMessage, PluginMessage};
|
||||||
|
|
||||||
|
pub struct Plugin(TcpStream, BufReader<TcpStream>);
|
||||||
|
|
||||||
|
impl Plugin {
|
||||||
|
pub fn send(&mut self, m: LoaderMessage) {
|
||||||
|
self.0
|
||||||
|
.write(serde_json::to_string(&m).unwrap().as_bytes())
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn read(&mut self) -> PluginMessage {
|
||||||
|
let mut buf = String::new();
|
||||||
|
self.1.read_line(&mut buf).unwrap();
|
||||||
|
serde_json::from_str(&buf).unwrap()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
#[serde(tag = "type", content = "params", rename_all = "snake_case")]
|
||||||
|
pub enum LoaderMessage {}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
#[serde(tag = "type", content = "params", rename_all = "snake_case")]
|
||||||
|
pub enum PluginMessage {}
|
||||||
Reference in New Issue
Block a user