From ce9f73fa7589c1d9e41db99379789b1d2bdc03a5 Mon Sep 17 00:00:00 2001 From: Keith Valin Date: Tue, 20 Dec 2022 00:17:44 -0500 Subject: [PATCH 1/5] Initial work towards plugin communcations with polychat-core --- Cargo.toml | 2 +- src/lib.rs | 1 + src/main/mod.rs | 37 +++++++++++++++++++++++++++++++++++++ src/plugin/mod.rs | 4 ++-- 4 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 src/main/mod.rs diff --git a/Cargo.toml b/Cargo.toml index 2868475..6267c48 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,6 @@ crate-type = ["rlib", "dylib"] [dependencies] libloading = "0.7.3" -polychat-plugin = "0.3.1" +polychat-plugin = {path="../polychat-plugin"} log = "0.4.14" walkdir = "2.3.2" diff --git a/src/lib.rs b/src/lib.rs index 2166845..1e721c4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,2 +1,3 @@ +pub mod main; pub mod plugin; pub mod plugin_manager; \ No newline at end of file diff --git a/src/main/mod.rs b/src/main/mod.rs new file mode 100644 index 0000000..b8ff3eb --- /dev/null +++ b/src/main/mod.rs @@ -0,0 +1,37 @@ +extern crate polychat_plugin; + +use log::info; + +use super::plugin_manager::PluginManager; + +use polychat_plugin::plugin::CoreInterface; + +pub struct Main { + plugin_manager: Option +} + +impl Main { + pub fn new() -> Main { + Main { + plugin_manager: None + } + } + + pub fn init<'a>(&'a mut self, path: &'a str) -> Result<(), &str>{ + let plugin_man = PluginManager::from(path)?; + self.plugin_manager = Some(plugin_man); + + Ok(()) + } + + pub fn is_initalized(&self) { + assert!(self.plugin_manager.is_some(), "Main was not initialized!"); + } +} + +impl CoreInterface for Main { + fn test(&self, test_msg: String) { + self.is_initalized(); + info!("Test function called with {}", test_msg); + } +} \ No newline at end of file diff --git a/src/plugin/mod.rs b/src/plugin/mod.rs index c6bb03a..15755c5 100644 --- a/src/plugin/mod.rs +++ b/src/plugin/mod.rs @@ -52,12 +52,12 @@ impl Plugin { (self.plugin_info.destroy_account)(account); } - pub fn post_message(&self, msg_body: String) -> SendStatus { + pub fn post_message(&self, account: Account, msg_body: String) -> SendStatus { let body_cstr = CString::new(msg_body).unwrap(); let msg = Message { body: body_cstr.as_ptr() }; - return (self.plugin_info.post_message)(&msg); + return (self.plugin_info.post_message)(account, &msg); } pub fn print(&self, account: Account) { From 8f83f2b46cecc889fd52afb1d95a2fa58d603580 Mon Sep 17 00:00:00 2001 From: Keith Valin Date: Tue, 20 Dec 2022 12:58:43 -0500 Subject: [PATCH 2/5] Updated test plugin to work with recent polychat-plugin updates Changed get_plugins to get_plugin_names Added get_protocol_name to Plugin Added get_plugin_by_name to PluginManager/Main --- src/main/mod.rs | 21 ++++++++++++++++++++- src/plugin/mod.rs | 4 ++++ src/plugin_manager/mod.rs | 6 +++++- tests/dummy_plugin/Cargo.toml | 2 +- tests/dummy_plugin/Makefile | 3 +++ tests/dummy_plugin/dummy_plugin.rs | 15 +++++++++++++-- 6 files changed, 46 insertions(+), 5 deletions(-) create mode 100644 tests/dummy_plugin/Makefile diff --git a/src/main/mod.rs b/src/main/mod.rs index b8ff3eb..91fda21 100644 --- a/src/main/mod.rs +++ b/src/main/mod.rs @@ -2,6 +2,8 @@ extern crate polychat_plugin; use log::info; +use crate::plugin::Plugin; + use super::plugin_manager::PluginManager; use polychat_plugin::plugin::CoreInterface; @@ -27,11 +29,28 @@ impl Main { pub fn is_initalized(&self) { assert!(self.plugin_manager.is_some(), "Main was not initialized!"); } + + pub fn get_plugin_names(&self) -> Vec { + self.is_initalized(); + let plugin_manager = self.plugin_manager.as_ref().expect("PluginManager is not initalized"); + + return plugin_manager.get_plugin_names(); + } + + pub fn get_plugin_by_name(&self, name: String) -> Result<&Plugin, &str> { + self.is_initalized(); + let plugin = self.plugin_manager.as_ref().unwrap().get_plugin_by_name(name); + + match plugin { + None => Err("Could not find plugin"), + Some(s) => Ok(s) + } + } } impl CoreInterface for Main { fn test(&self, test_msg: String) { self.is_initalized(); - info!("Test function called with {}", test_msg); + info!("[CoreInterface] test function called with {}", test_msg); } } \ No newline at end of file diff --git a/src/plugin/mod.rs b/src/plugin/mod.rs index 15755c5..515d3dc 100644 --- a/src/plugin/mod.rs +++ b/src/plugin/mod.rs @@ -67,6 +67,10 @@ impl Plugin { pub fn get_name(&self) -> &String { &self.plugin_info.name } + + pub fn get_protocol_name(&self) -> &String { + &self.plugin_info.protocol_name + } } fn new_from_loaded_lib(path: &str, lib: Library) -> Result{ diff --git a/src/plugin_manager/mod.rs b/src/plugin_manager/mod.rs index 0a7b58d..fe865ae 100644 --- a/src/plugin_manager/mod.rs +++ b/src/plugin_manager/mod.rs @@ -141,7 +141,7 @@ impl PluginManager { * Returns a vector of services that the PluginManager * currently supports. */ - pub fn get_services(&self) -> Vec { + pub fn get_plugin_names(&self) -> Vec { let mut output: Vec = Vec::::new(); for (key, _) in &self.plugin_map { @@ -150,6 +150,10 @@ impl PluginManager { return output; } + + pub fn get_plugin_by_name(&self, name: String) -> Option<&Plugin> { + return self.plugin_map.get::(name.as_ref()); + } } impl Drop for PluginManager { diff --git a/tests/dummy_plugin/Cargo.toml b/tests/dummy_plugin/Cargo.toml index 22723ce..36204fc 100644 --- a/tests/dummy_plugin/Cargo.toml +++ b/tests/dummy_plugin/Cargo.toml @@ -12,5 +12,5 @@ crate-type = ["cdylib"] [dependencies] -polychat-plugin = "0.3.1" +polychat-plugin = {path="../../../polychat-plugin"} libc = "0.2.113" diff --git a/tests/dummy_plugin/Makefile b/tests/dummy_plugin/Makefile new file mode 100644 index 0000000..e75f7a9 --- /dev/null +++ b/tests/dummy_plugin/Makefile @@ -0,0 +1,3 @@ +all: + cargo build + cp target/debug/libdummy_plugin.so ~/.polychat/plugins/dummy diff --git a/tests/dummy_plugin/dummy_plugin.rs b/tests/dummy_plugin/dummy_plugin.rs index a59c005..4cbb413 100644 --- a/tests/dummy_plugin/dummy_plugin.rs +++ b/tests/dummy_plugin/dummy_plugin.rs @@ -3,11 +3,13 @@ extern crate polychat_plugin; use std::boxed::Box; use std::ffi::CString; +use std::ffi::c_char; use polychat_plugin::types::Account; use polychat_plugin::plugin::PluginInfo; -use polychat_plugin::plugin::Message; +use polychat_plugin::plugin::{Message, Conversation}; use polychat_plugin::plugin::SendStatus; +use polychat_plugin::plugin::{AuthResult, AuthMethod}; extern "C" fn create_account() -> Account { Box::into_raw(Box::new(5)) as Account @@ -20,7 +22,7 @@ extern "C" fn print(acc: Account) { } } -extern "C" fn post_message(msg: * const Message) -> SendStatus { +extern "C" fn post_message(acc: Account, msg: * const Message) -> SendStatus { unsafe { println!("Instructed to post message with body {}", *(*msg).body); } @@ -33,11 +35,20 @@ extern "C" fn destroy_account(acc: Account) { } } +extern "C" fn login(acc: Account, method: * const AuthMethod, passwd: *const c_char) -> AuthResult { + return AuthResult::Success; +} + +extern "C" fn request_messages(acc: Account, conv: Conversation, ts: u64, limit: u32) {} + #[no_mangle] unsafe extern "C" fn initialize(info: *mut PluginInfo) { (*info).name = CString::new("dummy").expect("Could not create CString").into_raw(); + (*info).protocol_name = CString::new("Pseduo-Protocol").expect("Could not create CString").into_raw(); (*info).create_account = Some(create_account); (*info).destroy_account = Some(destroy_account); (*info).post_message = Some(post_message); (*info).print = Some(print); + (*info).request_messages = Some(request_messages); + (*info).login = Some(login); } From 5a316ff4b6f0ac064544b3cd849f1b4063e72782 Mon Sep 17 00:00:00 2001 From: Keith Valin Date: Tue, 20 Dec 2022 13:01:49 -0500 Subject: [PATCH 3/5] Fix test where I forgot to update a test --- tests/plugin_manager.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/plugin_manager.rs b/tests/plugin_manager.rs index d327e9d..7690499 100644 --- a/tests/plugin_manager.rs +++ b/tests/plugin_manager.rs @@ -73,7 +73,7 @@ fn double_free_never_happens() { fn get_services_returns_services() { let path = get_dummy_plugin(); let plugin_manager = PluginManager::from(path.as_str()).unwrap(); - let services = plugin_manager.get_services(); + let services = plugin_manager.get_plugin_names(); assert_eq!(services, ["dummy"]); } \ No newline at end of file From 24b7b6a28b71075715f6de6a260d080ce09f3064 Mon Sep 17 00:00:00 2001 From: jaredoconnell Date: Sat, 11 Feb 2023 21:55:21 -0500 Subject: [PATCH 4/5] Locked Polychat-plugin to specific version --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 6267c48..75cc050 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,6 @@ crate-type = ["rlib", "dylib"] [dependencies] libloading = "0.7.3" -polychat-plugin = {path="../polychat-plugin"} +polychat-plugin = "0.4.0" log = "0.4.14" walkdir = "2.3.2" From fa182c70ad2ad373febef1cf5845ce01bd729b81 Mon Sep 17 00:00:00 2001 From: jaredoconnell Date: Sun, 12 Feb 2023 11:18:08 -0500 Subject: [PATCH 5/5] Added IDE data to .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index a9d37c5..3f8a50c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ target Cargo.lock +.vs/