From 5289040c93094b3bc62fd7739230a4c63f839453 Mon Sep 17 00:00:00 2001 From: love_hangzhou Date: Mon, 25 Oct 2021 23:23:34 +0800 Subject: [PATCH] update plugin manger code --- src/process1/src/plugin/manager.rs | 134 ++++++-------- src/process1/src/plugin/mod.rs | 3 +- src/process1/src/plugin/unit.rs | 285 +++++++++++++++++++++++++++++ 3 files changed, 347 insertions(+), 75 deletions(-) create mode 100644 src/process1/src/plugin/unit.rs diff --git a/src/process1/src/plugin/manager.rs b/src/process1/src/plugin/manager.rs index 824ee2ff..d4c74b0f 100644 --- a/src/process1/src/plugin/manager.rs +++ b/src/process1/src/plugin/manager.rs @@ -1,74 +1,60 @@ -struct Manager { - id:String, - name:String, - desc:String, - configdir:String, - m_obj_list:Vec , //manage obj list -} - -trait MangerMethod { - fn init(self); - - fn load(&mut self); - - fn dispatch(&mut self) -> Option; - - fn relaod(&mut self) -> Option; - - fn destroy(&mut self); - - // reserved for sd event - fn event_dispatch(&mut self) -> Option; -} - -struct unit{ - -} -struct UnitManager { - unitManager: Manager -} - -impl MangerMethod for UnitManager { - fn init(self){ - let id = self.unitManager.id; - self.unitManager.configdir; - } - - fn load(&mut self){ - self.unitManager.m_obj_list.push(unit{}); - } - - fn dispatch(&mut self) -> Option { - None - } - - fn relaod(&mut self) -> Option{ - None - } - - fn destroy(&mut self){ - - } - - // reserved for sd event - fn event_dispatch(&mut self) -> Option{ - None - } - - -} - -pub fn load_Manager(){ - //**此处应该支持扩展多种Manager */ - let manager:Manager = Manager{ - id: todo!(), - name: todo!(), - desc: todo!(), - configdir: todo!(), - m_obj_list: todo!(), - }; - let um = UnitManager{unitManager:manager}; - um.init(); - um.load(); - um.dispatch(); -} + +use std::sync::Arc; +pub trait Mangerobj { + fn init(&self){ + + } + + fn load(&self); + + fn dispatch(&self) -> i32; + + fn reload(&self) -> Option; + + fn destroy(&self); + + // reserved for sd event + fn event_dispatch(&self) -> Option; +} + + + +pub struct MangerPlugins { + pub managers: Vec>, +} + + +impl MangerPlugins{ + pub fn new() -> Self{ + MangerPlugins{ + managers: Vec::new() + } + } + pub fn load_plugins(&mut self, d: Box) { + self.managers.push(d); + } + + pub fn run(&mut self) -> i32{ + let mut ret:i32 = 0; + for m in self.managers.iter(){ + m.init(); + m.load(); + ret =m.dispatch(); + } + ret + } + + pub fn destroy(&self) { + for m in self.managers.iter(){ + m.destroy(); + } + } + + pub fn reload(&self){ + for m in self.managers.iter(){ + m.reload(); + } + } +} + + diff --git a/src/process1/src/plugin/mod.rs b/src/process1/src/plugin/mod.rs index ff8de9eb..ddcec03e 100644 --- a/src/process1/src/plugin/mod.rs +++ b/src/process1/src/plugin/mod.rs @@ -1 +1,2 @@ -pub mod manager; +pub mod manager; +pub mod unit; \ No newline at end of file diff --git a/src/process1/src/plugin/unit.rs b/src/process1/src/plugin/unit.rs new file mode 100644 index 00000000..a226c520 --- /dev/null +++ b/src/process1/src/plugin/unit.rs @@ -0,0 +1,285 @@ +use crate::manager; +use std::collections::HashSet; +use std::cell::RefCell; +use std::sync::Arc; + +enum UnitType { + UNIT_SERVICE = 0, + UNIT_SOCKET, + UNIT_BUSNAME, + UNIT_TARGET, + UNIT_SNAPSHOT, + UNIT_DEVICE, +} + +enum UnitLoadState { + UNIT_STUB = 0, + UNIT_LOADED, + UNIT_NOT_FOUND, + UNIT_ERROR, + UNIT_MERGED, + UNIT_MASKED, + _UNIT_LOAD_STATE_MAX, + _UNIT_LOAD_STATE_INVALID = -1, +} + +enum UnitDependency { + UNIT_REQUIRES, + UNIT_REQUIRES_OVERRIDABLE, + UNIT_REQUISITE, + UNIT_REQUISITE_OVERRIDABLE, +} + +enum UnitNameFlags { + UNIT_NAME_PLAIN =1, + UNIT_NAME_INSTANCE = 2, + UNIT_NAME_TEMPLATE = 4, + UNIT_NAME_ANY = 1|2|4, +} + +struct Unit { + unit_type: UnitType, + load_state: UnitLoadState, + id: String, + instance: String, + name: String, + depencies: Vec, + desc: String, + documnetation: String, + fragment_path: String, + source_path: String, + fragment_mtine: u64, + source_mtime: u64, + dropin_mtime: u64, + + units_by_type: Vec, + has_requires_mounts_for: Vec, + load_queue: Vec, + dbus_queue: Vec, + cleanup_queue: Vec, + gc_queue: Vec, + cgroup_queue: Vec, + pids: HashSet, + sigchldgen: u64, + gc_marker: u64, + deseialize_job: i32, + load_error: i32, + stop_when_unneeded: bool, + refuse_manual_start: bool, + allow_isolate: bool, + ignore_on_isolate: bool, + ignore_on_snapshot: bool, + condition_result: bool, + assert_result: bool, + transient: bool, + in_load_queue: bool, + in_dubs_queue: bool, + in_cleanup_queue: bool, + in_gc_queue: bool, + manager: Option>, +} + +pub trait UnitObj { + fn init(&self){} + fn done(&self){} + fn load(&self){} + fn coldplug(&self){} + fn dump(&self){} + fn start(&self){} + fn stop(&self){} + fn reload(&self){} + fn kill(&self){} + fn check_gc(&self)->bool; + fn release_resources(&self){} + fn check_snapshot(&self){} + fn sigchld_events(&self, pid:u64,code:i32, status:i32){} + fn reset_failed(&self){} +} +struct MountUnit { + mount_unit:Unit, +} + +#[macro_export] +macro_rules! null_str{ + ($name:expr) => { + String::from($name) + } +} + +impl Unit { + pub fn new() -> Self { + Unit{ + unit_type: UnitType::UNIT_SERVICE, + load_state: UnitLoadState::UNIT_STUB, + id: String::from(""), + instance: String::from(""), + name: String::from(""), + depencies: Vec::::new(), + desc: String::from(""), + documnetation: null_str!(""), + fragment_path: null_str!(""), + source_path: null_str!(""), + fragment_mtine: 0, + source_mtime: 0, + dropin_mtime: 0, + units_by_type: Vec::::new(), + has_requires_mounts_for: Vec::::new(), + load_queue: Vec::::new(), + dbus_queue: Vec::::new(), + cleanup_queue: Vec::::new(), + gc_queue: Vec::::new(), + cgroup_queue: Vec::::new(), + pids: HashSet::::new(), + sigchldgen: 0, + gc_marker: 0, + deseialize_job: 0, + load_error: 0, + stop_when_unneeded: false, + refuse_manual_start: false, + allow_isolate: false, + ignore_on_isolate: false, + ignore_on_snapshot: false, + condition_result: false, + assert_result: false, + transient: false, + in_load_queue: false, + in_dubs_queue: false, + in_cleanup_queue: false, + in_gc_queue: false, + manager:None, + } + } + pub fn set_manager(&mut self,manger: Option>){ + self.manager = manger; + } +} +/* +impl <'l> Default for Unit<'l> { + fn default() -> Self { + Self{ + unitType: UnitType::UNIT_SERVICE, + load_state: UnitLoadState::UNIT_STUB, + id: String::from(""), + instance: String::from(""), + name: String::from(""), + depencies: Vec::::new(), + desc: String::from(""), + documnetation: null_str!(""), + fragment_path: null_str!(""), + source_path: null_str!(""), + fragment_mtine: 0, + source_mtime: 0, + dropin_mtime: 0, + units_by_type: Vec::::new(), + has_requires_mounts_for: Vec::::new(), + load_queue: Vec::::new(), + dbus_queue: Vec::::new(), + cleanup_queue: Vec::::new(), + gc_queue: Vec::::new(), + cgroup_queue: Vec::::new(), + pids: HashSet::::new(), + sigchldgen: 0, + gc_marker: 0, + deseialize_job: 0, + load_error: 0, + stop_when_unneeded: false, + refuse_manual_start: false, + allow_isolate: false, + ignore_on_isolate: false, + ignore_on_snapshot: false, + condition_result: false, + assert_result: false, + transient: false, + in_load_queue: false, + in_dubs_queue: false, + in_cleanup_queue: false, + in_gc_queue: false, + manager: None, + } + } +} +*/ +impl UnitObj for Unit{ + + fn init(&self){ + + } + fn done(&self){ + + } + fn check_gc(&self) -> bool { todo!() } +} + +impl UnitObj for MountUnit{ + +fn init(&self) { todo!() } +fn done(&self) { todo!() } +fn load(&self) { todo!() } +fn coldplug(&self) { todo!() } +fn start(&self) { todo!() } +fn dump(&self) { todo!() } +fn stop(&self) { todo!() } +fn reload(&self) { todo!() } +fn kill(&self) { todo!() } +fn check_gc(&self) -> bool { todo!() } +fn release_resources(&self) { todo!() } +fn check_snapshot(&self) { todo!() } +fn sigchld_events(&self, _: u64, _: i32, _: i32) { todo!() } +fn reset_failed(&self) { todo!() } +} + + +pub struct UnitManager { + units: RefCell>>>, +} + +impl UnitManager{ + pub fn new() -> Self{ + UnitManager {units: RefCell::new(Vec::new())} + } +} + +impl manager::Mangerobj for UnitManager { + + fn init(&self){ + } + + fn load(&self){ + let mut units_vec = self.units.borrow_mut(); + + let mut unit: Unit = Unit::new(); + + units_vec.push(RefCell::new(Box::new(unit))); + } + + fn dispatch(&self) -> i32 { + 0 + } + + fn reload(&self) -> Option{ + None + } + + fn destroy(&self){ + + } + + // reserved for sd event + fn event_dispatch(&self) -> Option{ + None + } +} + + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_mangerplugin(){ + let unit_manger = UnitManager::new(); + let mut mp = manager::MangerPlugins::new(); + mp.load_plugins(Box::new(unit_manger)); + assert_eq!(mp.run(),0); + } +} \ No newline at end of file -- Gitee