mirror of
https://github.com/Astatin3/unshell.git
synced 2026-06-08 22:38:01 -06:00
Make unshell-lib, and module system with components.
This commit is contained in:
@@ -28,21 +28,6 @@ version = "1.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801"
|
||||
|
||||
[[package]]
|
||||
name = "crossbeam-channel"
|
||||
version = "0.5.15"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "82b8f8f868b36967f9606790d1903570de9ceaf870a7bf9fbbd3016d636a2cb2"
|
||||
dependencies = [
|
||||
"crossbeam-utils",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crossbeam-utils"
|
||||
version = "0.8.21"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28"
|
||||
|
||||
[[package]]
|
||||
name = "libloading"
|
||||
version = "0.8.9"
|
||||
@@ -124,21 +109,19 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9312f7c4f6ff9069b165498234ce8be658059c6728633667c526e27dc2cf1df5"
|
||||
|
||||
[[package]]
|
||||
name = "unshell-modules"
|
||||
name = "unshell-breakout-module"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"libloading",
|
||||
"log",
|
||||
"unshell-lib",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unshell-test-module"
|
||||
name = "unshell-lib"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"bincode",
|
||||
"crossbeam-channel",
|
||||
"libloading",
|
||||
"log",
|
||||
"unshell-modules",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -1,19 +1,21 @@
|
||||
cargo-features = ["trim-paths"]
|
||||
|
||||
[package]
|
||||
name = "unshell-test-module"
|
||||
name = "unshell-breakout-module"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
# unshell-modules = {path = "../unshell-modules"}
|
||||
unshell-lib = {path = "../unshell-lib"}
|
||||
|
||||
[lib]
|
||||
crate-type = ["cdylib"]
|
||||
|
||||
[features]
|
||||
default = ["client-component", "server-component"]
|
||||
client-component = []
|
||||
server-component = []
|
||||
# server = []
|
||||
default = ["client", "server"]
|
||||
client = []
|
||||
server = []
|
||||
|
||||
[profile.release]
|
||||
strip = true # Strip symbols from the binary
|
||||
@@ -26,10 +28,3 @@ debug = false
|
||||
trim-paths = "all"
|
||||
|
||||
# crate-type = ["cdylib"]
|
||||
|
||||
|
||||
[dependencies]
|
||||
bincode = "2.0.1"
|
||||
crossbeam-channel = "0.5.15"
|
||||
log = "0.4.28"
|
||||
unshell-modules = {path = "../unshell-modules"}
|
||||
@@ -0,0 +1,28 @@
|
||||
#![no_main]
|
||||
|
||||
use std::collections::HashMap;
|
||||
use unshell_lib::Component;
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
pub fn get_components() -> HashMap<&'static str, Box<dyn Component>> {
|
||||
let mut components: HashMap<&'static str, Box<dyn Component>> = HashMap::new();
|
||||
|
||||
#[cfg(feature = "client")]
|
||||
components.insert(
|
||||
unshell_lib::client::MODULE_NAME,
|
||||
Box::new(unshell_lib::client::ClientComponent::new()),
|
||||
);
|
||||
|
||||
components
|
||||
|
||||
// vec![
|
||||
// Feature::Client,
|
||||
// #[cfg(feature = "server")]
|
||||
// Feature::Server,
|
||||
// ]
|
||||
}
|
||||
|
||||
#[cfg(feature = "client")]
|
||||
pub use unshell_lib::client::*;
|
||||
#[cfg(feature = "server")]
|
||||
pub use unshell_lib::server::*;
|
||||
Generated
+3
-27
@@ -28,21 +28,6 @@ version = "1.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801"
|
||||
|
||||
[[package]]
|
||||
name = "crossbeam-channel"
|
||||
version = "0.5.15"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "82b8f8f868b36967f9606790d1903570de9ceaf870a7bf9fbbd3016d636a2cb2"
|
||||
dependencies = [
|
||||
"crossbeam-utils",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crossbeam-utils"
|
||||
version = "0.8.21"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28"
|
||||
|
||||
[[package]]
|
||||
name = "libloading"
|
||||
version = "0.8.9"
|
||||
@@ -127,25 +112,16 @@ checksum = "9312f7c4f6ff9069b165498234ce8be658059c6728633667c526e27dc2cf1df5"
|
||||
name = "unshell-cli"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"unshell-test-module",
|
||||
"unshell-lib",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unshell-modules"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"libloading",
|
||||
"log",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unshell-test-module"
|
||||
name = "unshell-lib"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"bincode",
|
||||
"crossbeam-channel",
|
||||
"libloading",
|
||||
"log",
|
||||
"unshell-modules",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
||||
@@ -4,4 +4,4 @@ version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
unshell-test-module = {path = "../unshell-test-module"}#, default-features = false, features = ["server-component"]}
|
||||
unshell-lib = {path = "../unshell-lib"}#, default-features = false, features = ["server-component"]}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use std::io::{Write, stdin, stdout};
|
||||
|
||||
use unshell_test_module::Announcement;
|
||||
use unshell_lib::Announcement;
|
||||
|
||||
// use unshell_test_module::ListenerRuntime;
|
||||
|
||||
@@ -9,7 +9,7 @@ use unshell_test_module::Announcement;
|
||||
// }
|
||||
|
||||
fn main() {
|
||||
let mut serverruntime = unshell_test_module::ListenerRuntime::new();
|
||||
let mut serverruntime = unshell_lib::server::ListenerRuntime::new();
|
||||
|
||||
loop {
|
||||
print!("> ");
|
||||
|
||||
Generated
+136
@@ -0,0 +1,136 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "bincode"
|
||||
version = "2.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "36eaf5d7b090263e8150820482d5d93cd964a81e4019913c972f4edcc6edb740"
|
||||
dependencies = [
|
||||
"bincode_derive",
|
||||
"serde",
|
||||
"unty",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "bincode_derive"
|
||||
version = "2.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bf95709a440f45e986983918d0e8a1f30a9b1df04918fc828670606804ac3c09"
|
||||
dependencies = [
|
||||
"virtue",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801"
|
||||
|
||||
[[package]]
|
||||
name = "libloading"
|
||||
version = "0.8.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d7c4b02199fee7c5d21a5ae7d8cfa79a6ef5bb2fc834d6e9058e89c825efdc55"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"windows-link",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "log"
|
||||
version = "0.4.28"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "34080505efa8e45a4b816c349525ebe327ceaa8559756f0356cba97ef3bf7432"
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.103"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5ee95bc4ef87b8d5ba32e8b7714ccc834865276eab0aed5c9958d00ec45f49e8"
|
||||
dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.42"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a338cc41d27e6cc6dce6cefc13a0729dfbb81c262b1f519331575dd80ef3067f"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde"
|
||||
version = "1.0.228"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e"
|
||||
dependencies = [
|
||||
"serde_core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_core"
|
||||
version = "1.0.228"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad"
|
||||
dependencies = [
|
||||
"serde_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_derive"
|
||||
version = "1.0.228"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "2.0.109"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2f17c7e013e88258aa9543dcbe81aca68a667a9ac37cd69c9fbc07858bfe0e2f"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-ident"
|
||||
version = "1.0.22"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9312f7c4f6ff9069b165498234ce8be658059c6728633667c526e27dc2cf1df5"
|
||||
|
||||
[[package]]
|
||||
name = "unshell-lib"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"bincode",
|
||||
"libloading",
|
||||
"log",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unty"
|
||||
version = "0.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6d49784317cd0d1ee7ec5c716dd598ec5b4483ea832a2dced265471cc0f690ae"
|
||||
|
||||
[[package]]
|
||||
name = "virtue"
|
||||
version = "0.0.18"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "051eb1abcf10076295e815102942cc58f9d5e3b4560e46e53c21e8ff6f3af7b1"
|
||||
|
||||
[[package]]
|
||||
name = "windows-link"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5"
|
||||
@@ -0,0 +1,10 @@
|
||||
[package]
|
||||
name = "unshell-lib"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
libloading = {version = "0.8.9"}
|
||||
bincode = "2.0.1"
|
||||
log = "0.4.28"
|
||||
# unshell-modules = {path = "../unshell-modules"}
|
||||
@@ -1,19 +1,5 @@
|
||||
#![no_main]
|
||||
#[macro_use]
|
||||
extern crate log;
|
||||
|
||||
use bincode::{Decode, Encode};
|
||||
|
||||
#[cfg(feature = "client-component")]
|
||||
mod client;
|
||||
#[cfg(feature = "client-component")]
|
||||
pub use client::*;
|
||||
|
||||
#[cfg(feature = "server-component")]
|
||||
mod server;
|
||||
#[cfg(feature = "server-component")]
|
||||
pub use server::ListenerRuntime;
|
||||
|
||||
#[derive(Debug, Encode, Decode)]
|
||||
pub enum Announcement {
|
||||
TestAnnouncement(String),
|
||||
+2
-2
@@ -8,9 +8,9 @@ use std::{
|
||||
thread::{self, JoinHandle},
|
||||
};
|
||||
|
||||
use unshell_modules::{Manager, ModuleRuntime};
|
||||
// use unshell_modules::{Manager, ModuleRuntime};
|
||||
|
||||
use crate::Announcement;
|
||||
use crate::{Announcement, ModuleRuntime, module::Manager};
|
||||
|
||||
pub struct RuntimeTest {
|
||||
thread_handle: JoinHandle<()>,
|
||||
@@ -0,0 +1,74 @@
|
||||
mod client_runtime;
|
||||
|
||||
// use crate::module::Interface;
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
pub const MODULE_NAME: &'static str = "client";
|
||||
|
||||
// pub use unshell_modules::setup_logger;
|
||||
|
||||
// use unshell_modules::{Manager, ModuleRuntime, module_interface};
|
||||
|
||||
use crate::{
|
||||
Component,
|
||||
ModuleRuntime,
|
||||
client::client_runtime::RuntimeTest,
|
||||
module::{Interface, Manager},
|
||||
module_interface,
|
||||
// module_interface,
|
||||
};
|
||||
|
||||
pub extern "C" fn test1() {
|
||||
warn!("Test1 called xxxxxxxxxxx");
|
||||
}
|
||||
pub extern "C" fn test2() {
|
||||
warn!("Test2 called");
|
||||
}
|
||||
pub extern "C" fn test3() {
|
||||
warn!("Test3 called");
|
||||
}
|
||||
|
||||
module_interface! {
|
||||
ClientInterface {
|
||||
fn test1();
|
||||
fn test2();
|
||||
fn test3();
|
||||
}
|
||||
}
|
||||
|
||||
// #[unsafe(no_mangle)]
|
||||
// pub fn interface() -> Interface {
|
||||
// Interface::from_raw(test1, test2, test3)
|
||||
// }
|
||||
|
||||
// #[unsafe(no_mangle)]
|
||||
// pub fn init(manager: Arc<Mutex<Manager>>) -> Box<dyn ModuleRuntime> {
|
||||
// info!("Initializing client module");
|
||||
// }
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct ClientComponent;
|
||||
|
||||
impl ClientComponent {
|
||||
pub fn new() -> Self {
|
||||
ClientComponent
|
||||
}
|
||||
}
|
||||
|
||||
impl Component for ClientComponent {
|
||||
fn name(&self) -> &'static str {
|
||||
MODULE_NAME
|
||||
}
|
||||
|
||||
fn start_runtime(&self, manager: Arc<Mutex<Manager>>) -> Option<Box<dyn ModuleRuntime>> {
|
||||
Some(Box::new(RuntimeTest::new(manager)))
|
||||
}
|
||||
|
||||
fn clone_box(&self) -> Box<dyn Component> {
|
||||
Box::new(self.clone())
|
||||
}
|
||||
|
||||
fn get_interface(&self) -> Box<dyn Interface> {
|
||||
Box::new(ClientInterface::from_raw(test1, test2, test3))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
#![no_main]
|
||||
#[macro_use]
|
||||
extern crate log;
|
||||
|
||||
pub mod client;
|
||||
pub mod module;
|
||||
pub mod server;
|
||||
|
||||
mod announcement;
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
pub use announcement::Announcement;
|
||||
|
||||
use crate::module::{Interface, Manager};
|
||||
|
||||
///Generic error type for module-related operations.
|
||||
#[derive(Debug)]
|
||||
pub enum ModuleError {
|
||||
LibLoadingError(libloading::Error),
|
||||
LogError(log::SetLoggerError),
|
||||
LinkError(String),
|
||||
Error(String),
|
||||
}
|
||||
|
||||
/// Trait for defining modules that have a runtime.
|
||||
pub trait ModuleRuntime: Send {
|
||||
/// Returns true if the module is running.
|
||||
/// After returning false, the module will be dropped.
|
||||
fn is_running(&self) -> bool;
|
||||
/// Consumes the module, implementation should kill whatever is running.
|
||||
fn kill(self: Box<Self>);
|
||||
}
|
||||
|
||||
pub trait Component {
|
||||
fn name(&self) -> &'static str;
|
||||
fn start_runtime(&self, manager: Arc<Mutex<Manager>>) -> Option<Box<dyn ModuleRuntime>>;
|
||||
fn get_interface(&self) -> Box<dyn Interface>;
|
||||
fn clone_box(&self) -> Box<dyn Component>;
|
||||
}
|
||||
|
||||
impl Clone for Box<dyn Component> {
|
||||
fn clone(&self) -> Box<dyn Component> {
|
||||
self.clone_box()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
sync::{Arc, Mutex},
|
||||
thread,
|
||||
time::Duration,
|
||||
};
|
||||
|
||||
use crate::{Component, ModuleRuntime, module::Module};
|
||||
|
||||
pub struct Manager {
|
||||
modules: Vec<Module>,
|
||||
components: HashMap<&'static str, Box<dyn Component>>,
|
||||
}
|
||||
|
||||
// static mut MANAGER_RUNTIME: Option<Arc<Mutex<Manager>>> = None;
|
||||
|
||||
impl Manager {
|
||||
/// Create Manager, and run initilization for each Module
|
||||
#[allow(static_mut_refs)]
|
||||
pub fn run<'a>(modules: Vec<Module>) {
|
||||
let this: Self = Self::load_modules(modules);
|
||||
let components = this.components.clone();
|
||||
|
||||
let this = Arc::new(Mutex::new(this));
|
||||
|
||||
let mut runtimes: Vec<Box<dyn ModuleRuntime>> = Vec::new();
|
||||
|
||||
for (name, component) in components {
|
||||
let module_runtime = component.start_runtime(this.clone());
|
||||
if let Some(module_runtime) = module_runtime {
|
||||
info!("Initialized {}", name);
|
||||
runtimes.push(module_runtime);
|
||||
}
|
||||
}
|
||||
|
||||
Self::join(&mut runtimes);
|
||||
}
|
||||
|
||||
pub fn load_modules<'a>(modules: Vec<Module>) -> Self {
|
||||
let module_count = modules.len();
|
||||
let mut this = Self {
|
||||
modules,
|
||||
components: HashMap::new(),
|
||||
};
|
||||
|
||||
// let mut runtimes = Vec::new();
|
||||
|
||||
for i in 0..module_count {
|
||||
info!("Importing module {}", i);
|
||||
// let this_lock = .unwrap();
|
||||
let component_func = if let Ok(component_func) = this.modules[i]
|
||||
.get_symbol::<fn() -> HashMap<&'static str, Box<dyn Component>>>(b"get_components")
|
||||
{
|
||||
component_func
|
||||
} else {
|
||||
warn!("get_components function not found");
|
||||
continue;
|
||||
};
|
||||
|
||||
let components = component_func();
|
||||
|
||||
info!("[{i}] Loaded {} components", components.len());
|
||||
|
||||
this.components.extend(components);
|
||||
}
|
||||
|
||||
this
|
||||
}
|
||||
|
||||
/// Iterateratively loop through all runtimes, until all are finished executing
|
||||
pub fn join(runtimes: &mut Vec<Box<dyn ModuleRuntime>>) {
|
||||
// let mut len = runtimes.len().clone();
|
||||
while runtimes.len() > 0 {
|
||||
runtimes.retain(|runtime| runtime.is_running());
|
||||
|
||||
thread::sleep(Duration::from_micros(100));
|
||||
}
|
||||
}
|
||||
|
||||
// pub extern "C" fn test1234(&self, float: f32) {
|
||||
// info!("Manager Test Sucsessfull! {}", float.powf(2.));
|
||||
// }
|
||||
|
||||
// #[allow(static_mut_refs, improper_ctypes_definitions)]
|
||||
// pub extern "C" fn get_manager() -> Arc<Mutex<Manager>> {
|
||||
// unsafe { MANAGER_RUNTIME.clone().unwrap() }
|
||||
// }
|
||||
}
|
||||
@@ -1,30 +1,15 @@
|
||||
#[macro_use]
|
||||
extern crate log;
|
||||
|
||||
mod logger;
|
||||
mod manager;
|
||||
mod module;
|
||||
|
||||
// use std::any::Any;
|
||||
|
||||
pub use logger::setup_logger;
|
||||
pub use manager::Manager;
|
||||
pub use module::Module;
|
||||
|
||||
///Generic error type for module-related operations.
|
||||
#[derive(Debug)]
|
||||
pub enum ModuleError {
|
||||
LibLoadingError(libloading::Error),
|
||||
LogError(log::SetLoggerError),
|
||||
LinkError(String),
|
||||
Error(String),
|
||||
}
|
||||
|
||||
/// Trait for defining modules that have a runtime.
|
||||
pub trait ModuleRuntime: Send {
|
||||
/// Returns true if the module is running.
|
||||
/// After returning false, the module will be dropped.
|
||||
fn is_running(&self) -> bool;
|
||||
/// Consumes the module, implementation should kill whatever is running.
|
||||
fn kill(self: Box<Self>);
|
||||
pub trait Interface {
|
||||
fn as_any(self: Box<Self>) -> Box<dyn std::any::Any>;
|
||||
}
|
||||
|
||||
/// "Module Interface" helper macro that creates a struct with function pointers
|
||||
@@ -73,5 +58,11 @@ macro_rules! module_interface {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl crate::module::Interface for $interface_name {
|
||||
fn as_any(self: Box<Self>) -> Box<dyn std::any::Any> {
|
||||
self
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -1,8 +1,7 @@
|
||||
use libloading::{Library, Symbol};
|
||||
|
||||
use crate::{ModuleError, logger::SetupLogger};
|
||||
use crate::{ModuleError, module::logger::SetupLogger};
|
||||
|
||||
// #[derive(Clone, Copy)]
|
||||
pub struct Module {
|
||||
// name: String,
|
||||
lib: Library,
|
||||
@@ -31,15 +30,15 @@ impl Module {
|
||||
|
||||
Ok(symbol)
|
||||
}
|
||||
pub fn get_interface<T>(&self) -> Result<T, ModuleError> {
|
||||
if let Ok(interface_function) = self.get_symbol::<fn() -> T>(b"interface") {
|
||||
Ok(interface_function())
|
||||
} else {
|
||||
Err(ModuleError::LinkError(format!(
|
||||
"Interface function not found!"
|
||||
)))
|
||||
}
|
||||
}
|
||||
// pub fn get_interface<T>(&self) -> Result<T, ModuleError> {
|
||||
// if let Ok(interface_function) = self.get_symbol::<fn() -> T>(b"interface") {
|
||||
// Ok(interface_function())
|
||||
// } else {
|
||||
// Err(ModuleError::LinkError(format!(
|
||||
// "Interface function not found!"
|
||||
// )))
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
// extern "C" fn test1234() {
|
||||
+1
-29
@@ -5,7 +5,7 @@ use std::{
|
||||
thread::{self, JoinHandle},
|
||||
};
|
||||
|
||||
use unshell_modules::ModuleRuntime;
|
||||
use crate::ModuleRuntime;
|
||||
|
||||
use crate::Announcement;
|
||||
|
||||
@@ -31,39 +31,11 @@ impl ListenerRuntime {
|
||||
let stream = stream.unwrap();
|
||||
println!("New connection from {}", stream.peer_addr().unwrap());
|
||||
streams.lock().unwrap().push(stream);
|
||||
|
||||
// thread::spawn(move || {
|
||||
// let _ = handle_connection(&mut stream);
|
||||
// info!("Connection from {} closed", stream.peer_addr().unwrap());
|
||||
// });
|
||||
}
|
||||
});
|
||||
|
||||
// let reader = BufReader::new(stream.try_clone().unwrap());
|
||||
// let writer = BufWriter::new(stream.try_clone().unwrap());
|
||||
|
||||
Self {
|
||||
thread_handle,
|
||||
streams,
|
||||
// stream,
|
||||
// reader,
|
||||
// writer,
|
||||
// thread_handle: thread::spawn(move || {
|
||||
|
||||
// for stream in listener.incoming() {
|
||||
// let mut stream = stream.unwrap();
|
||||
|
||||
// thread::spawn(move || {
|
||||
// let _ = handle_connection(&mut stream);
|
||||
// info!("Connection from {} closed", stream.peer_addr().unwrap());
|
||||
// });
|
||||
// }
|
||||
// // thread::sleep(Duration::from_secs(2));
|
||||
|
||||
// // let manager_lock = manager.lock().unwrap();
|
||||
// // manager_lock.test1234(111.1111);
|
||||
// // drop(manager_lock);
|
||||
// }),
|
||||
}
|
||||
}
|
||||
|
||||
Generated
-39
@@ -1,39 +0,0 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801"
|
||||
|
||||
[[package]]
|
||||
name = "libloading"
|
||||
version = "0.8.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d7c4b02199fee7c5d21a5ae7d8cfa79a6ef5bb2fc834d6e9058e89c825efdc55"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"windows-link",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "log"
|
||||
version = "0.4.28"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "34080505efa8e45a4b816c349525ebe327ceaa8559756f0356cba97ef3bf7432"
|
||||
|
||||
[[package]]
|
||||
name = "unshell-modules"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"libloading",
|
||||
"log",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-link"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5"
|
||||
@@ -1,12 +0,0 @@
|
||||
[package]
|
||||
name = "unshell-modules"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[features]
|
||||
default = ["exec"]
|
||||
"exec" = ["libloading"]
|
||||
|
||||
[dependencies]
|
||||
libloading = {version = "0.8.9", optional = true}
|
||||
log = "0.4.28"
|
||||
@@ -1,71 +0,0 @@
|
||||
use std::{
|
||||
sync::{Arc, Mutex},
|
||||
thread,
|
||||
time::Duration,
|
||||
};
|
||||
|
||||
use crate::{Module, ModuleRuntime};
|
||||
|
||||
pub struct Manager {
|
||||
modules: Vec<Module>,
|
||||
}
|
||||
|
||||
static mut MANAGER_RUNTIME: Option<Arc<Mutex<Manager>>> = None;
|
||||
|
||||
impl Manager {
|
||||
/// Create Manager, and run initilization for each Module
|
||||
#[allow(static_mut_refs)]
|
||||
pub fn run<'a>(modules: Vec<Module>) {
|
||||
let module_count = modules.len();
|
||||
let this = Self { modules };
|
||||
|
||||
let this = Arc::new(Mutex::new(this));
|
||||
|
||||
let mut runtimes = Vec::new();
|
||||
|
||||
for i in 0..module_count {
|
||||
info!("Initializing {}", i);
|
||||
let this_lock = this.lock().unwrap();
|
||||
let init = if let Ok(init) =
|
||||
this_lock.modules[i]
|
||||
.get_symbol::<fn(Arc<Mutex<Manager>>) -> Box<dyn ModuleRuntime>>(b"init")
|
||||
{
|
||||
init.to_owned()
|
||||
} else {
|
||||
warn!("init not found");
|
||||
continue;
|
||||
};
|
||||
|
||||
let runtime = init(this.clone());
|
||||
|
||||
info!("Initialized {}", i);
|
||||
|
||||
runtimes.push(runtime);
|
||||
}
|
||||
|
||||
Self::join(&mut runtimes);
|
||||
}
|
||||
|
||||
/// Iterateratively loop through all runtimes, until all are finished executing
|
||||
pub fn join(runtimes: &mut Vec<Box<dyn ModuleRuntime>>) {
|
||||
// let mut len = runtimes.len().clone();
|
||||
while runtimes.len() > 0 {
|
||||
runtimes.retain(|runtime| runtime.is_running());
|
||||
|
||||
thread::sleep(Duration::from_micros(100));
|
||||
}
|
||||
}
|
||||
|
||||
// pub extern "C" fn test123() {
|
||||
// info!("Manager Test Sucsessfull!");
|
||||
// }
|
||||
|
||||
pub extern "C" fn test1234(&self, float: f32) {
|
||||
info!("Manager Test Sucsessfull! {}", float.powf(2.));
|
||||
}
|
||||
|
||||
#[allow(static_mut_refs, improper_ctypes_definitions)]
|
||||
pub extern "C" fn get_manager() -> Arc<Mutex<Manager>> {
|
||||
unsafe { MANAGER_RUNTIME.clone().unwrap() }
|
||||
}
|
||||
}
|
||||
Generated
+35
-2
@@ -61,6 +61,26 @@ dependencies = [
|
||||
"windows-sys 0.60.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "bincode"
|
||||
version = "2.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "36eaf5d7b090263e8150820482d5d93cd964a81e4019913c972f4edcc6edb740"
|
||||
dependencies = [
|
||||
"bincode_derive",
|
||||
"serde",
|
||||
"unty",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "bincode_derive"
|
||||
version = "2.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bf95709a440f45e986983918d0e8a1f30a9b1df04918fc828670606804ac3c09"
|
||||
dependencies = [
|
||||
"virtue",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.4"
|
||||
@@ -324,9 +344,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9312f7c4f6ff9069b165498234ce8be658059c6728633667c526e27dc2cf1df5"
|
||||
|
||||
[[package]]
|
||||
name = "unshell-modules"
|
||||
name = "unshell-lib"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"bincode",
|
||||
"libloading",
|
||||
"log",
|
||||
]
|
||||
@@ -339,15 +360,27 @@ dependencies = [
|
||||
"libloading",
|
||||
"log",
|
||||
"pretty_env_logger",
|
||||
"unshell-modules",
|
||||
"unshell-lib",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unty"
|
||||
version = "0.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6d49784317cd0d1ee7ec5c716dd598ec5b4483ea832a2dced265471cc0f690ae"
|
||||
|
||||
[[package]]
|
||||
name = "utf8parse"
|
||||
version = "0.2.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
|
||||
|
||||
[[package]]
|
||||
name = "virtue"
|
||||
version = "0.0.18"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "051eb1abcf10076295e815102942cc58f9d5e3b4560e46e53c21e8ff6f3af7b1"
|
||||
|
||||
[[package]]
|
||||
name = "winapi-util"
|
||||
version = "0.1.11"
|
||||
|
||||
@@ -11,7 +11,7 @@ libloading = "0.8.9"
|
||||
log = "0.4.28"
|
||||
pretty_env_logger = "0.5.0"
|
||||
# unshell-logger = {path = "../unshell-logger"}
|
||||
unshell-modules = {path = "../unshell-modules"}
|
||||
unshell-lib = {path = "../unshell-lib"}
|
||||
|
||||
[profile.release]
|
||||
strip = true # Strip symbols from the binary
|
||||
|
||||
@@ -1,16 +1,11 @@
|
||||
use unshell_modules::{Manager, Module, ModuleError, module_interface};
|
||||
use unshell_lib::{
|
||||
ModuleError,
|
||||
module::{Manager, Module},
|
||||
};
|
||||
|
||||
#[macro_use]
|
||||
extern crate log;
|
||||
|
||||
module_interface! {
|
||||
Interface {
|
||||
fn test1();
|
||||
fn test2();
|
||||
fn test3();
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// Init the logger (This uses like 600MB of storage)
|
||||
pretty_env_logger::init();
|
||||
@@ -26,7 +21,6 @@ fn main() {
|
||||
modules.push(Module::new(&arg)?)
|
||||
}
|
||||
Manager::run(modules);
|
||||
// manager.join();
|
||||
|
||||
Ok(())
|
||||
}() {
|
||||
|
||||
@@ -1,38 +0,0 @@
|
||||
mod client_runtime;
|
||||
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
pub use unshell_modules::setup_logger;
|
||||
|
||||
use unshell_modules::{Manager, ModuleRuntime, module_interface};
|
||||
|
||||
use crate::client::client_runtime::RuntimeTest;
|
||||
|
||||
pub extern "C" fn test1() {
|
||||
warn!("Test1 called");
|
||||
}
|
||||
pub extern "C" fn test2() {
|
||||
warn!("Test2 called");
|
||||
}
|
||||
pub extern "C" fn test3() {
|
||||
warn!("Test3 called");
|
||||
}
|
||||
|
||||
module_interface! {
|
||||
Interface {
|
||||
fn test1();
|
||||
fn test2();
|
||||
fn test3();
|
||||
}
|
||||
}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
pub fn interface() -> Interface {
|
||||
Interface::from_raw(test1, test2, test3)
|
||||
}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
pub fn init(manager: Arc<Mutex<Manager>>) -> Box<dyn ModuleRuntime> {
|
||||
info!("Initializing client module");
|
||||
Box::new(RuntimeTest::new(manager))
|
||||
}
|
||||
Reference in New Issue
Block a user