plugin_framework, plugin manager

Signed-off-by: fufesou <shuanglongchen@yeah.net>
This commit is contained in:
fufesou
2023-04-22 22:21:02 +08:00
parent b1b867dd9d
commit ae789ff5f1
7 changed files with 421 additions and 70 deletions

View File

@@ -1414,7 +1414,7 @@ pub fn plugin_get_session_option(
#[cfg(feature = "plugin_framework")]
#[cfg(not(any(target_os = "android", target_os = "ios")))]
{
return SyncReturn(crate::plugin::PeerConfig::get(&_id, &_peer, &_key));
SyncReturn(crate::plugin::PeerConfig::get(&_id, &_peer, &_key))
}
#[cfg(any(
not(feature = "plugin_framework"),
@@ -1422,7 +1422,7 @@ pub fn plugin_get_session_option(
target_os = "ios"
))]
{
return SyncReturn(None);
SyncReturn(None)
}
}
@@ -1440,7 +1440,7 @@ pub fn plugin_get_local_option(_id: String, _key: String) -> SyncReturn<Option<S
#[cfg(feature = "plugin_framework")]
#[cfg(not(any(target_os = "android", target_os = "ios")))]
{
return SyncReturn(crate::plugin::LocalConfig::get(&_id, &_key));
SyncReturn(crate::plugin::LocalConfig::get(&_id, &_key))
}
#[cfg(any(
not(feature = "plugin_framework"),
@@ -1448,7 +1448,7 @@ pub fn plugin_get_local_option(_id: String, _key: String) -> SyncReturn<Option<S
target_os = "ios"
))]
{
return SyncReturn(None);
SyncReturn(None)
}
}
@@ -1461,6 +1461,94 @@ pub fn plugin_set_local_option(_id: String, _key: String, _value: String) {
}
}
#[inline]
pub fn plugin_reload(_id: String) {
#[cfg(feature = "plugin_framework")]
#[cfg(not(any(target_os = "android", target_os = "ios")))]
{
// to-do:
// reload plugin
}
}
#[inline]
pub fn plugin_id_enable(_id: String, v: bool) {
#[cfg(feature = "plugin_framework")]
#[cfg(not(any(target_os = "android", target_os = "ios")))]
{
if v {
allow_err!(crate::plugin::ManagerConfig::set_plugin_enabled(&_id, true));
allow_err!(crate::plugin::reload_plugin(&_id));
} else {
allow_err!(crate::plugin::ManagerConfig::set_plugin_enabled(&_id, false));
crate::plugin::unload_plugin(&_id);
}
}
}
pub fn plugin_id_is_enabled(_id: String) -> SyncReturn<bool> {
#[cfg(feature = "plugin_framework")]
#[cfg(not(any(target_os = "android", target_os = "ios")))]
{
SyncReturn(
crate::plugin::ManagerConfig::get_plugin_status(&_id, |s| s.enabled).unwrap_or(false),
)
}
#[cfg(any(
not(feature = "plugin_framework"),
target_os = "android",
target_os = "ios"
))]
{
SyncReturn(false)
}
}
pub fn plugin_enable(v: bool) {
#[cfg(feature = "plugin_framework")]
#[cfg(not(any(target_os = "android", target_os = "ios")))]
{
allow_err!(crate::plugin::ManagerConfig::set_enabled(v));
if v {
allow_err!(crate::plugin::load_plugins());
} else {
crate::plugin::unload_plugins();
}
}
}
pub fn plugin_is_enabled() -> SyncReturn<bool> {
#[cfg(feature = "plugin_framework")]
#[cfg(not(any(target_os = "android", target_os = "ios")))]
{
SyncReturn(crate::plugin::ManagerConfig::is_enabled())
}
#[cfg(any(
not(feature = "plugin_framework"),
target_os = "android",
target_os = "ios"
))]
{
SyncReturn(false)
}
}
pub fn plugin_feature_is_enabled() -> SyncReturn<bool> {
#[cfg(feature = "plugin_framework")]
#[cfg(not(any(target_os = "android", target_os = "ios")))]
{
SyncReturn(true)
}
#[cfg(any(
not(feature = "plugin_framework"),
target_os = "android",
target_os = "ios"
))]
{
SyncReturn(false)
}
}
#[cfg(target_os = "android")]
pub mod server_side {
use hbb_common::{config, log};

View File

@@ -1,3 +1,5 @@
use crate::plugins::Plugin;
use super::desc::ConfigItem;
use hbb_common::{bail, config::Config as HbbConfig, lazy_static, ResultType};
use serde_derive::{Deserialize, Serialize};
@@ -12,6 +14,10 @@ lazy_static::lazy_static! {
static ref CONFIG_LOCAL_ITEMS: Arc<Mutex<HashMap<String, Vec<ConfigItem>>>> = Default::default();
static ref CONFIG_PEERS: Arc<Mutex<HashMap<String, PeersConfig>>> = Default::default();
static ref CONFIG_PEER_ITEMS: Arc<Mutex<HashMap<String, Vec<ConfigItem>>>> = Default::default();
static ref CONFIG_MANAGER: Arc<Mutex<ManagerConfig>> = {
let conf = hbb_common::config::load_path::<ManagerConfig>(ManagerConfig::path());
Arc::new(Mutex::new(conf))
};
}
pub(super) const CONFIG_TYPE_LOCAL: &str = "local";
@@ -178,3 +184,78 @@ pub(super) fn set_peer_items(id: &str, items: &Vec<ConfigItem>) {
.unwrap()
.insert(id.to_owned(), items.clone());
}
#[derive(Debug, Serialize, Deserialize)]
pub struct PluginStatus {
pub enabled: bool,
}
const MANAGER_VERSION: &str = "0.1.0";
#[derive(Debug, Serialize, Deserialize)]
pub struct ManagerConfig {
pub version: String,
pub enabled: bool,
pub plugins: HashMap<String, PluginStatus>,
}
impl Default for ManagerConfig {
fn default() -> Self {
Self {
version: "0.1.0".to_owned(),
enabled: true,
plugins: HashMap::new(),
}
}
}
// Do not care about the `store_path` error, no need to store the old value and restore if failed.
impl ManagerConfig {
#[inline]
fn path() -> PathBuf {
HbbConfig::path("plugins").join("manager.toml")
}
#[inline]
pub fn is_enabled() -> bool {
CONFIG_MANAGER.lock().unwrap().enabled
}
#[inline]
pub fn set_enabled(enabled: bool) -> ResultType<()> {
let mut lock = CONFIG_MANAGER.lock().unwrap();
lock.enabled = enabled;
hbb_common::config::store_path(Self::path(), &*lock)
}
#[inline]
pub fn get_plugin_status<T>(id: &str, f: fn(&PluginStatus) -> T) -> Option<T> {
let lock = CONFIG_MANAGER.lock().unwrap();
lock.plugins.get(id).map(f)
}
pub fn set_plugin_enabled(id: &str, enabled: bool) -> ResultType<()> {
let mut lock = CONFIG_MANAGER.lock().unwrap();
if let Some(status) = lock.plugins.get_mut(id) {
status.enabled = enabled;
hbb_common::config::store_path(Self::path(), &*lock)
} else {
bail!("No such plugin {}", id)
}
}
#[inline]
pub fn add_plugin(id: &str) -> ResultType<()> {
let mut lock = CONFIG_MANAGER.lock().unwrap();
lock.plugins
.insert(id.to_owned(), PluginStatus { enabled: true });
hbb_common::config::store_path(Self::path(), &*lock)
}
#[inline]
pub fn remove_plugin(id: &str) -> ResultType<()> {
let mut lock = CONFIG_MANAGER.lock().unwrap();
lock.plugins.remove(id);
hbb_common::config::store_path(Self::path(), &*lock)
}
}

View File

@@ -9,7 +9,7 @@ mod plugins;
pub use plugins::{
handle_client_event, handle_server_event, handle_ui_event, load_plugin, load_plugins,
reload_plugin, unload_plugin,
reload_plugin, unload_plugin, unload_plugins,
};
const MSG_TO_UI_TYPE_PLUGIN_DESC: &str = "plugin_desc";
@@ -17,7 +17,7 @@ const MSG_TO_UI_TYPE_PLUGIN_EVENT: &str = "plugin_event";
const MSG_TO_UI_TYPE_PLUGIN_RELOAD: &str = "plugin_reload";
const MSG_TO_UI_TYPE_PLUGIN_OPTION: &str = "plugin_option";
pub use config::{LocalConfig, PeerConfig};
pub use config::{LocalConfig, ManagerConfig, PeerConfig};
#[inline]
fn cstr_to_string(cstr: *const c_char) -> ResultType<String> {

View File

@@ -1,7 +1,7 @@
use std::{
collections::HashMap,
ffi::{c_char, c_void},
path::Path,
path::PathBuf,
sync::{Arc, RwLock},
};
@@ -19,10 +19,16 @@ const METHOD_HANDLE_UI: &[u8; 10] = b"handle_ui\0";
const METHOD_HANDLE_PEER: &[u8; 12] = b"handle_peer\0";
lazy_static::lazy_static! {
static ref PLUGIN_INFO: Arc<RwLock<HashMap<String, PluginInfo>>> = Default::default();
pub static ref PLUGINS: Arc<RwLock<HashMap<String, Plugin>>> = Default::default();
pub static ref LOCAL_PEER_ID: Arc<RwLock<String>> = Default::default();
}
struct PluginInfo {
path: String,
desc: Desc,
}
/// Initialize the plugins.
///
/// Return null ptr if success.
@@ -84,8 +90,6 @@ macro_rules! make_plugin {
($($field:ident : $tp:ty),+) => {
pub struct Plugin {
_lib: Library,
path: String,
desc_v: Option<Desc>,
$($field: $tp),+
}
@@ -111,8 +115,6 @@ macro_rules! make_plugin {
Ok(Self {
_lib: lib,
path: path.to_string(),
desc_v: None,
$( $field ),+
})
}
@@ -130,26 +132,43 @@ make_plugin!(
set_cb_get_id: PluginFuncGetIdCallback
);
pub fn load_plugins<P: AsRef<Path>>(dir: P) -> ResultType<()> {
for entry in std::fs::read_dir(dir)? {
match entry {
Ok(entry) => {
let path = entry.path();
if path.is_file() {
let path = path.to_str().unwrap_or("");
if path.ends_with(".so") {
if let Err(e) = load_plugin(path) {
log::error!("{e}");
pub fn load_plugins() -> ResultType<()> {
let exe = std::env::current_exe()?.to_string_lossy().to_string();
match PathBuf::from(&exe).parent() {
Some(dir) => {
for entry in std::fs::read_dir(dir)? {
match entry {
Ok(entry) => {
let path = entry.path();
if path.is_file() {
let path = path.to_str().unwrap_or("");
if path.ends_with(".so") {
if let Err(e) = load_plugin(Some(path), None) {
log::error!("{e}");
}
}
}
}
Err(e) => {
log::error!("Failed to read dir entry, {}", e);
}
}
}
Err(e) => {
log::error!("Failed to read dir entry, {}", e);
}
Ok(())
}
None => {
bail!("Failed to get parent dir of {}", exe);
}
}
Ok(())
}
pub fn unload_plugins() {
let mut plugins = PLUGINS.write().unwrap();
for (id, plugin) in plugins.iter() {
let _ret = (plugin.clear)();
log::info!("Plugin {} unloaded", id);
}
plugins.clear();
}
pub fn unload_plugin(id: &str) {
@@ -159,12 +178,12 @@ pub fn unload_plugin(id: &str) {
}
pub fn reload_plugin(id: &str) -> ResultType<()> {
let path = match PLUGINS.read().unwrap().get(id) {
let path = match PLUGIN_INFO.read().unwrap().get(id) {
Some(plugin) => plugin.path.clone(),
None => bail!("Plugin {} not found", id),
};
unload_plugin(id);
load_plugin(&path)
load_plugin(Some(&path), Some(id))
}
#[no_mangle]
@@ -182,8 +201,8 @@ fn get_local_peer_id() -> *const c_char {
id.as_ptr() as _
}
pub fn load_plugin(path: &str) -> ResultType<()> {
let mut plugin = Plugin::new(path)?;
fn load_plugin_path(path: &str) -> ResultType<()> {
let plugin = Plugin::new(path)?;
let desc = (plugin.desc)();
let desc_res = Desc::from_cstr(desc);
unsafe {
@@ -198,11 +217,28 @@ pub fn load_plugin(path: &str) -> ResultType<()> {
update_ui_plugin_desc(&desc);
update_config(&desc);
reload_ui(&desc);
plugin.desc_v = Some(desc);
let plugin_info = PluginInfo {
path: path.to_string(),
desc,
};
PLUGIN_INFO.write().unwrap().insert(id.clone(), plugin_info);
PLUGINS.write().unwrap().insert(id, plugin);
Ok(())
}
pub fn load_plugin(path: Option<&str>, id: Option<&str>) -> ResultType<()> {
match (path, id) {
(Some(path), _) => load_plugin_path(path),
(None, Some(id)) => match PLUGIN_INFO.read().unwrap().get(id) {
Some(plugin) => load_plugin_path(&plugin.path),
None => bail!("Plugin {} not found", id),
},
(None, None) => {
bail!("path and id are both None");
}
}
}
fn handle_event(method: &[u8], id: &str, peer: &str, event: &[u8]) -> ResultType<()> {
let mut peer: String = peer.to_owned();
peer.push('\0');
@@ -264,19 +300,23 @@ pub fn handle_client_event(id: &str, peer: &str, event: &[u8]) -> Option<Message
libc::free(ret as _);
}
if code > ERR_RUSTDESK_HANDLE_BASE && code < ERR_PLUGIN_HANDLE_BASE {
let name = plugin.desc_v.as_ref().unwrap().name();
let name = match PLUGIN_INFO.read().unwrap().get(id) {
Some(plugin) => plugin.desc.name(),
None => "???",
}
.to_owned();
match code {
ERR_CALL_NOT_SUPPORTED_METHOD => Some(make_plugin_response(
id,
name,
&name,
"plugin method is not supported",
)),
ERR_CALL_INVALID_ARGS => Some(make_plugin_response(
id,
name,
&name,
"plugin arguments is invalid",
)),
_ => Some(make_plugin_response(id, name, &msg)),
_ => Some(make_plugin_response(id, &name, &msg)),
}
} else {
log::error!(