diff options
author | Aram Drevekenin <aram@poor.dev> | 2023-05-08 16:26:29 +0200 |
---|---|---|
committer | Aram Drevekenin <aram@poor.dev> | 2023-05-08 16:26:29 +0200 |
commit | a0ec6e0a83f6921018206ff0978591298be67372 (patch) | |
tree | 59a3e76c5e4ca362d0f986a1af743816b2bdac94 | |
parent | 677f1bff1b15fc7276836b9cb30980d5f88334a1 (diff) |
various refactoringz
-rw-r--r-- | zellij-server/src/logging_pipe.rs | 8 | ||||
-rw-r--r-- | zellij-server/src/plugins/mod.rs | 10 | ||||
-rw-r--r-- | zellij-server/src/plugins/plugin_loader.rs | 156 | ||||
-rw-r--r-- | zellij-server/src/plugins/plugin_map.rs | 2 | ||||
-rw-r--r-- | zellij-server/src/plugins/wasm_bridge.rs | 23 |
5 files changed, 81 insertions, 118 deletions
diff --git a/zellij-server/src/logging_pipe.rs b/zellij-server/src/logging_pipe.rs index 7d6844a22..d2155282e 100644 --- a/zellij-server/src/logging_pipe.rs +++ b/zellij-server/src/logging_pipe.rs @@ -6,23 +6,23 @@ use std::{ use log::{debug, error}; use wasmer_wasi::{WasiFile, WasiFsError}; use zellij_utils::{errors::prelude::*, serde}; +use crate::plugins::PluginId; use chrono::prelude::*; use serde::{Deserialize, Serialize}; // 16kB log buffer -// const ZELLIJ_MAX_PIPE_BUFFER_SIZE: usize = 16_384; -const ZELLIJ_MAX_PIPE_BUFFER_SIZE: usize = 16_384_000; // TODO: sort this out +const ZELLIJ_MAX_PIPE_BUFFER_SIZE: usize = 16_384; #[derive(Debug, Serialize, Deserialize)] #[serde(crate = "self::serde")] pub struct LoggingPipe { buffer: VecDeque<u8>, plugin_name: String, - plugin_id: u32, + plugin_id: PluginId, } impl LoggingPipe { - pub fn new(plugin_name: &str, plugin_id: u32) -> LoggingPipe { + pub fn new(plugin_name: &str, plugin_id: PluginId) -> LoggingPipe { LoggingPipe { buffer: VecDeque::new(), plugin_name: String::from(plugin_name), diff --git a/zellij-server/src/plugins/mod.rs b/zellij-server/src/plugins/mod.rs index 99a181368..f4bed9be4 100644 --- a/zellij-server/src/plugins/mod.rs +++ b/zellij-server/src/plugins/mod.rs @@ -34,8 +34,8 @@ pub enum PluginInstruction { ClientId, Size, ), - Update(Vec<(Option<u32>, Option<ClientId>, Event)>), // Focused plugin / broadcast, client_id, event data - Unload(u32), // plugin_id + Update(Vec<(Option<PluginId>, Option<ClientId>, Event)>), // Focused plugin / broadcast, client_id, event data + Unload(PluginId), // plugin_id Reload( Option<bool>, // should float Option<String>, // pane title @@ -43,7 +43,7 @@ pub enum PluginInstruction { usize, // tab index Size, ), - Resize(u32, usize, usize), // plugin_id, columns, rows + Resize(PluginId, usize, usize), // plugin_id, columns, rows AddClient(ClientId), RemoveClient(ClientId), NewTab( @@ -54,7 +54,7 @@ pub enum PluginInstruction { usize, // tab_index ClientId, ), - ApplyCachedEvents(Vec<u32>), // a list of plugin id + ApplyCachedEvents(Vec<PluginId>), PostMessageToPluginWorker( PluginId, ClientId, @@ -180,7 +180,7 @@ pub(crate) fn plugin_thread_main( tab_index, client_id, ) => { - let mut plugin_ids: HashMap<RunPluginLocation, Vec<u32>> = HashMap::new(); + let mut plugin_ids: HashMap<RunPluginLocation, Vec<PluginId>> = HashMap::new(); let mut extracted_run_instructions = tab_layout .clone() .unwrap_or_else(|| layout.new_tab().0) diff --git a/zellij-server/src/plugins/plugin_loader.rs b/zellij-server/src/plugins/plugin_loader.rs index 857a8a814..2afffa81f 100644 --- a/zellij-server/src/plugins/plugin_loader.rs +++ b/zellij-server/src/plugins/plugin_loader.rs @@ -1,3 +1,4 @@ +use crate::plugins::PluginId; use crate::plugins::plugin_map::{PluginEnv, PluginMap, RunningPlugin, RunningWorker, Subscriptions}; use crate::plugins::zellij_exports::{wasi_read_string, zellij_exports}; use highway::{HighwayHash, PortableHash}; @@ -11,7 +12,7 @@ use std::{ }; use url::Url; use wasmer::{ChainableNamedResolver, Instance, Module, Store}; -use wasmer_wasi::{Pipe, WasiState, WasiEnv}; +use wasmer_wasi::{Pipe, WasiState}; use crate::{ logging_pipe::LoggingPipe, screen::ScreenInstruction, thread_bus::ThreadSenders, @@ -152,7 +153,7 @@ pub struct PluginLoader<'a> { plugin_path: PathBuf, loading_indication: &'a mut LoadingIndication, senders: ThreadSenders, - plugin_id: u32, + plugin_id: PluginId, client_id: ClientId, store: Store, plugin: PluginConfig, @@ -165,7 +166,7 @@ pub struct PluginLoader<'a> { impl<'a> PluginLoader<'a> { pub fn reload_plugin_from_memory( - plugin_id: u32, + plugin_id: PluginId, plugin_dir: PathBuf, plugin_cache: Arc<Mutex<HashMap<PathBuf, Module>>>, senders: ThreadSenders, @@ -195,7 +196,7 @@ impl<'a> PluginLoader<'a> { plugin_loader .load_module_from_memory() .and_then(|module| { - plugin_loader.create_plugin_instance_environment_and_subscriptions(module) + plugin_loader.create_plugin_environment(module) }) .and_then(|(instance, plugin_env, subscriptions)| { log::info!("load_plugin_instance reload_plugin_from_memory"); @@ -215,7 +216,7 @@ impl<'a> PluginLoader<'a> { } pub fn start_plugin( - plugin_id: u32, + plugin_id: PluginId, client_id: ClientId, plugin: &PluginConfig, tab_index: usize, @@ -246,7 +247,7 @@ impl<'a> PluginLoader<'a> { .or_else(|_e| plugin_loader.load_module_from_hd_cache()) .or_else(|_e| plugin_loader.compile_module()) .and_then(|module| { - plugin_loader.create_plugin_instance_environment_and_subscriptions(module) + plugin_loader.create_plugin_environment(module) }) .and_then(|(instance, plugin_env, subscriptions)| { log::info!("load_plugin_instance start plugin"); @@ -295,7 +296,7 @@ impl<'a> PluginLoader<'a> { plugin_loader .load_module_from_memory() .and_then(|module| { - plugin_loader.create_plugin_instance_environment_and_subscriptions(module) + plugin_loader.create_plugin_environment(module) }) .and_then(|(instance, plugin_env, subscriptions)| { log::info!("load_plugin_instance add_client"); @@ -312,7 +313,7 @@ impl<'a> PluginLoader<'a> { } pub fn reload_plugin( - plugin_id: u32, + plugin_id: PluginId, plugin_dir: PathBuf, plugin_cache: Arc<Mutex<HashMap<PathBuf, Module>>>, senders: ThreadSenders, @@ -343,7 +344,7 @@ impl<'a> PluginLoader<'a> { plugin_loader .compile_module() .and_then(|module| { - plugin_loader.create_plugin_instance_environment_and_subscriptions(module) + plugin_loader.create_plugin_environment(module) }) .and_then(|(instance, plugin_env, subscriptions)| { log::info!("load_plugin_instance reload plugin"); @@ -365,7 +366,7 @@ impl<'a> PluginLoader<'a> { plugin_cache: &Arc<Mutex<HashMap<PathBuf, Module>>>, loading_indication: &'a mut LoadingIndication, senders: &ThreadSenders, - plugin_id: u32, + plugin_id: PluginId, client_id: ClientId, store: &Store, plugin: PluginConfig, @@ -397,7 +398,7 @@ impl<'a> PluginLoader<'a> { plugin_map: &Arc<Mutex<PluginMap>>, loading_indication: &'a mut LoadingIndication, senders: &ThreadSenders, - plugin_id: u32, + plugin_id: PluginId, client_id: ClientId, store: &Store, plugin_dir: &'a PathBuf, @@ -435,7 +436,7 @@ impl<'a> PluginLoader<'a> { plugin_map: &Arc<Mutex<PluginMap>>, loading_indication: &'a mut LoadingIndication, senders: &ThreadSenders, - plugin_id: u32, + plugin_id: PluginId, client_id: ClientId, store: &Store, plugin_dir: &'a PathBuf, @@ -557,49 +558,12 @@ impl<'a> PluginLoader<'a> { .with_context(err_context)?; Ok(module) } - pub fn create_plugin_instance_environment_and_subscriptions( + pub fn create_plugin_environment( &mut self, module: Module, ) -> Result<(Instance, PluginEnv, Arc<Mutex<Subscriptions>>)> { - let err_context = || { - format!( - "Failed to create instance and plugin env for plugin {}", - self.plugin_id - ) - }; - let mut wasi_env = WasiState::new("Zellij") - .env("CLICOLOR_FORCE", "1") - .map_dir("/host", ".") - .and_then(|wasi| wasi.map_dir("/data", &self.plugin_own_data_dir)) - .and_then(|wasi| wasi.map_dir("/tmp", ZELLIJ_TMP_DIR.as_path())) - .and_then(|wasi| { - wasi.stdin(Box::new(Pipe::new())) - .stdout(Box::new(Pipe::new())) - .stderr(Box::new(LoggingPipe::new( - &self.plugin.location.to_string(), - self.plugin_id, - ))) - .finalize() - }) - .with_context(err_context)?; - let wasi = wasi_env.import_object(&module).with_context(err_context)?; - - let mut mut_plugin = self.plugin.clone(); - mut_plugin.set_tab_index(self.tab_index); - let plugin_env = PluginEnv { - plugin_id: self.plugin_id, - client_id: self.client_id, - plugin: mut_plugin, - senders: self.senders.clone(), - wasi_env, - plugin_own_data_dir: self.plugin_own_data_dir.clone(), - tab_index: self.tab_index, - }; - - let subscriptions = Arc::new(Mutex::new(HashSet::new())); - let zellij = zellij_exports(&self.store, &plugin_env, &subscriptions); - let instance = - Instance::new(&module, &zellij.chain_back(wasi)).with_context(err_context)?; + let err_context = || format!("Failed to create environment for plugin"); + let (instance, plugin_env, subscriptions) = self.create_plugin_instance_env_and_subscriptions(&module)?; assert_plugin_version(&instance, &plugin_env).with_context(err_context)?; // Only do an insert when everything went well! let cloned_plugin = self.plugin.clone(); @@ -610,7 +574,6 @@ impl<'a> PluginLoader<'a> { Ok((instance, plugin_env, subscriptions)) } pub fn create_plugin_instance_and_wasi_env_for_worker(&mut self) -> Result<(Instance, PluginEnv)> { - // TODO: combine with above function let err_context = || { format!( "Failed to create instance and plugin env for worker {}", @@ -623,48 +586,7 @@ impl<'a> PluginLoader<'a> { .get(&self.plugin.path) .with_context(err_context)? .clone(); - let mut wasi_env = WasiState::new("Zellij") - .env("CLICOLOR_FORCE", "1") - .map_dir("/host", ".") - .and_then(|wasi| wasi.map_dir("/data", &self.plugin_own_data_dir)) - .and_then(|wasi| wasi.map_dir("/tmp", ZELLIJ_TMP_DIR.as_path())) - .and_then(|wasi| { - wasi.stdin(Box::new(Pipe::new())) - .stdout(Box::new(Pipe::new())) - .stderr(Box::new(LoggingPipe::new( - &self.plugin.location.to_string(), - self.plugin_id, - ))) - .finalize() - }) - .with_context(err_context)?; - let wasi = wasi_env.import_object(&module).with_context(err_context)?; - - let mut mut_plugin = self.plugin.clone(); - mut_plugin.set_tab_index(self.tab_index); - let plugin_env = PluginEnv { - plugin_id: self.plugin_id, - client_id: self.client_id, - plugin: mut_plugin, - senders: self.senders.clone(), - wasi_env, - plugin_own_data_dir: self.plugin_own_data_dir.clone(), - tab_index: self.tab_index, - }; - - let subscriptions = Arc::new(Mutex::new(HashSet::new())); // TODO: maybe include the - // original subscriptions to - // avoid confusion? - let zellij = zellij_exports(&self.store, &plugin_env, &subscriptions); - let instance = - Instance::new(&module, &zellij.chain_back(wasi)).with_context(err_context)?; - // assert_plugin_version(&instance, &plugin_env).with_context(err_context)?; - // Only do an insert when everything went well! -// let cloned_plugin = self.plugin.clone(); -// self.plugin_cache -// .lock() -// .unwrap() -// .insert(cloned_plugin.path, module); + let (instance, plugin_env, _subscriptions) = self.create_plugin_instance_env_and_subscriptions(&module)?; Ok((instance, plugin_env)) } pub fn load_plugin_instance( @@ -768,7 +690,7 @@ impl<'a> PluginLoader<'a> { .load_module_from_memory() .and_then(|module| { plugin_loader_for_client - .create_plugin_instance_environment_and_subscriptions(module) + .create_plugin_environment(module) }) .and_then(|(instance, plugin_env, subscriptions)| { log::info!("load_plugin_instance for other clients..."); @@ -812,6 +734,48 @@ impl<'a> PluginLoader<'a> { }, } } + fn create_plugin_instance_env_and_subscriptions(&self, module: &Module) -> Result<(Instance, PluginEnv, Arc<Mutex<Subscriptions>>)> { + let err_context = || { + format!( + "Failed to create instance, plugin env and subscriptions for plugin {}", + self.plugin_id + ) + }; + let mut wasi_env = WasiState::new("Zellij") + .env("CLICOLOR_FORCE", "1") + .map_dir("/host", ".") + .and_then(|wasi| wasi.map_dir("/data", &self.plugin_own_data_dir)) + .and_then(|wasi| wasi.map_dir("/tmp", ZELLIJ_TMP_DIR.as_path())) + .and_then(|wasi| { + wasi.stdin(Box::new(Pipe::new())) + .stdout(Box::new(Pipe::new())) + .stderr(Box::new(LoggingPipe::new( + &self.plugin.location.to_string(), + self.plugin_id, + ))) + .finalize() + }) + .with_context(err_context)?; + let wasi = wasi_env.import_object(&module).with_context(err_context)?; + + let mut mut_plugin = self.plugin.clone(); + mut_plugin.set_tab_index(self.tab_index); + let plugin_env = PluginEnv { + plugin_id: self.plugin_id, + client_id: self.client_id, + plugin: mut_plugin, + senders: self.senders.clone(), + wasi_env, + plugin_own_data_dir: self.plugin_own_data_dir.clone(), + tab_index: self.tab_index, + }; + + let subscriptions = Arc::new(Mutex::new(HashSet::new())); + let zellij = zellij_exports(&self.store, &plugin_env, &subscriptions); + let instance = + Instance::new(&module, &zellij.chain_back(wasi)).with_context(err_context)?; + Ok((instance, plugin_env, subscriptions)) + } } fn create_plugin_fs_entries(plugin_own_data_dir: &PathBuf) -> Result<()> { diff --git a/zellij-server/src/plugins/plugin_map.rs b/zellij-server/src/plugins/plugin_map.rs index 53e7281b4..8bbf0bca1 100644 --- a/zellij-server/src/plugins/plugin_map.rs +++ b/zellij-server/src/plugins/plugin_map.rs @@ -28,7 +28,7 @@ pub type Subscriptions = HashSet<EventType>; #[derive(Clone)] pub struct PluginEnv { - pub plugin_id: u32, + pub plugin_id: PluginId, pub plugin: PluginConfig, pub senders: ThreadSenders, pub wasi_env: WasiEnv, diff --git a/zellij-server/src/plugins/wasm_bridge.rs b/zellij-server/src/plugins/wasm_bridge.rs index 5fe933754..30a8026d1 100644 --- a/zellij-server/src/plugins/wasm_bridge.rs +++ b/zellij-server/src/plugins/wasm_bridge.rs @@ -38,14 +38,14 @@ pub struct WasmBridge { plugin_dir: PathBuf, plugin_cache: Arc<Mutex<HashMap<PathBuf, Module>>>, plugin_map: Arc<Mutex<PluginMap>>, - next_plugin_id: u32, - cached_events_for_pending_plugins: HashMap<u32, Vec<Event>>, // u32 is the plugin id - cached_resizes_for_pending_plugins: HashMap<u32, (usize, usize)>, // (rows, columns) + next_plugin_id: PluginId, + cached_events_for_pending_plugins: HashMap<PluginId, Vec<Event>>, + cached_resizes_for_pending_plugins: HashMap<PluginId, (usize, usize)>, // (rows, columns) cached_worker_messages: HashMap<PluginId, Vec<(ClientId, String, String, String)>>, // Vec<clientid, // worker_name, // message, // payload> - loading_plugins: HashMap<(u32, RunPlugin), JoinHandle<()>>, // plugin_id to join-handle + loading_plugins: HashMap<(PluginId, RunPlugin), JoinHandle<()>>, // plugin_id to join-handle pending_plugin_reloads: HashSet<RunPlugin>, } @@ -82,8 +82,7 @@ impl WasmBridge { tab_index: usize, size: Size, client_id: Option<ClientId>, - ) -> Result<u32> { - log::info!("load_plugin, connected_clients: {:?}", self.connected_clients); + ) -> Result<PluginId> { // returns the plugin id let err_context = move || format!("failed to load plugin"); @@ -156,11 +155,11 @@ impl WasmBridge { self.next_plugin_id += 1; Ok(plugin_id) } - pub fn unload_plugin(&mut self, pid: u32) -> Result<()> { + pub fn unload_plugin(&mut self, pid: PluginId) -> Result<()> { info!("Bye from plugin {}", &pid); // TODO: remove plugin's own data directory let mut plugin_map = self.plugin_map.lock().unwrap(); - let ids_in_plugin_map: Vec<(u32, ClientId)> = plugin_map.keys().copied().collect(); + let ids_in_plugin_map: Vec<(PluginId, ClientId)> = plugin_map.keys().copied().collect(); for (plugin_id, client_id) in ids_in_plugin_map { if pid == plugin_id { drop(plugin_map.remove(&(plugin_id, client_id))); @@ -272,7 +271,7 @@ impl WasmBridge { Err(e) => Err(e), } } - pub fn resize_plugin(&mut self, pid: u32, new_columns: usize, new_rows: usize) -> Result<()> { + pub fn resize_plugin(&mut self, pid: PluginId, new_columns: usize, new_rows: usize) -> Result<()> { let err_context = move || format!("failed to resize plugin {pid}"); for ((plugin_id, client_id), (running_plugin, _subscriptions, workers)) in self.plugin_map.lock().unwrap().iter_mut() @@ -405,7 +404,7 @@ impl WasmBridge { } Ok(()) } - pub fn apply_cached_events(&mut self, plugin_ids: Vec<u32>) -> Result<()> { + pub fn apply_cached_events(&mut self, plugin_ids: Vec<PluginId>) -> Result<()> { let mut applied_plugin_paths = HashSet::new(); for plugin_id in plugin_ids { self.apply_cached_events_and_resizes_for_plugin(plugin_id)?; @@ -629,14 +628,14 @@ fn handle_plugin_loading_failure( } pub fn apply_event_to_plugin( - plugin_id: u32, + plugin_id: PluginId, client_id: ClientId, instance: &Instance, plugin_env: &PluginEnv, event: &Event, rows: usize, columns: usize, - plugin_bytes: &mut Vec<(u32, ClientId, Vec<u8>)>, + plugin_bytes: &mut Vec<(PluginId, ClientId, Vec<u8>)>, ) -> Result<()> { let err_context = || format!("Failed to apply event to plugin {plugin_id}"); let update = instance |