diff options
| author | Kunal Mohan <kunalmohan99@gmail.com> | 2021-03-24 15:34:53 +0530 |
|---|---|---|
| committer | Kunal Mohan <kunalmohan99@gmail.com> | 2021-05-04 20:47:52 +0530 |
| commit | e30ec5745ef1f5526a05be1f49fed7b406558805 (patch) | |
| tree | 5467fd236cc135d754352a0f49bcf69f38f8d1fb /src/server | |
| parent | Some documentation an ClientOsApi stuff (diff) | |
| download | sl0ppij-e30ec5745ef1f5526a05be1f49fed7b406558805.tar.gz sl0ppij-e30ec5745ef1f5526a05be1f49fed7b406558805.tar.xz sl0ppij-e30ec5745ef1f5526a05be1f49fed7b406558805.zip | |
Add router thread on server side as well
Diffstat (limited to 'src/server')
| -rw-r--r-- | src/server/mod.rs | 107 |
1 files changed, 84 insertions, 23 deletions
diff --git a/src/server/mod.rs b/src/server/mod.rs index 45c0db39..495cceb4 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -1,16 +1,33 @@ use crate::cli::CliArgs; -use crate::common::{ - ChannelWithContext, ClientInstruction, SenderType, SenderWithContext, ServerInstruction, -}; +use crate::common::{ChannelWithContext, ClientInstruction, SenderType, SenderWithContext}; use crate::errors::{ContextType, ErrorContext, OsContext, PtyContext, ServerContext}; use crate::os_input_output::{ServerOsApi, ServerOsApiInstruction}; use crate::panes::PaneId; use crate::pty_bus::{PtyBus, PtyInstruction}; use crate::screen::ScreenInstruction; +use serde::{Deserialize, Serialize}; use std::path::PathBuf; use std::sync::mpsc::channel; use std::thread; +/// Instructions related to server-side application including the +/// ones sent by client to server +#[derive(Serialize, Deserialize, Debug, Clone)] +pub enum ServerInstruction { + OpenFile(PathBuf), + SplitHorizontally, + SplitVertically, + MoveFocus, + NewClient(String), + ToPty(PtyInstruction), + ToScreen(ScreenInstruction), + OsApi(ServerOsApiInstruction), + DoneClosingPane, + ClosePluginPane(u32), + ClientExit, + Exit, +} + pub fn start_server(mut os_input: Box<dyn ServerOsApi>, opts: CliArgs) -> thread::JoinHandle<()> { let (send_pty_instructions, receive_pty_instructions): ChannelWithContext<PtyInstruction> = channel(); @@ -27,6 +44,14 @@ pub fn start_server(mut os_input: Box<dyn ServerOsApi>, opts: CliArgs) -> thread SenderType::Sender(send_os_instructions), ); + let (send_server_instructions, receive_server_instructions): ChannelWithContext< + ServerInstruction, + > = channel(); + let mut send_server_instructions = SenderWithContext::new( + ErrorContext::new(), + SenderType::Sender(send_server_instructions), + ); + // Don't use default layouts in tests, but do everywhere else #[cfg(not(test))] let default_layout = Some(PathBuf::from("default")); @@ -34,7 +59,12 @@ pub fn start_server(mut os_input: Box<dyn ServerOsApi>, opts: CliArgs) -> thread let default_layout = None; let maybe_layout = opts.layout.or(default_layout); - let mut pty_bus = PtyBus::new(receive_pty_instructions, os_input.clone(), opts.debug); + let mut pty_bus = PtyBus::new( + receive_pty_instructions, + os_input.clone(), + send_server_instructions.clone(), + opts.debug, + ); let pty_thread = thread::Builder::new() .name("pty".to_string()) @@ -47,43 +77,55 @@ pub fn start_server(mut os_input: Box<dyn ServerOsApi>, opts: CliArgs) -> thread match event { PtyInstruction::SpawnTerminal(file_to_open) => { let pid = pty_bus.spawn_terminal(file_to_open); - pty_bus.os_input.send_to_server(ServerInstruction::ToScreen( - ScreenInstruction::NewPane(PaneId::Terminal(pid)), - )); + pty_bus + .send_server_instructions + .send(ServerInstruction::ToScreen(ScreenInstruction::NewPane( + PaneId::Terminal(pid), + ))) + .unwrap(); } PtyInstruction::SpawnTerminalVertically(file_to_open) => { let pid = pty_bus.spawn_terminal(file_to_open); - pty_bus.os_input.send_to_server(ServerInstruction::ToScreen( - ScreenInstruction::VerticalSplit(PaneId::Terminal(pid)), - )); + pty_bus + .send_server_instructions + .send(ServerInstruction::ToScreen( + ScreenInstruction::VerticalSplit(PaneId::Terminal(pid)), + )) + .unwrap(); } PtyInstruction::SpawnTerminalHorizontally(file_to_open) => { let pid = pty_bus.spawn_terminal(file_to_open); - pty_bus.os_input.send_to_server(ServerInstruction::ToScreen( - ScreenInstruction::HorizontalSplit(PaneId::Terminal(pid)), - )); + pty_bus + .send_server_instructions + .send(ServerInstruction::ToScreen( + ScreenInstruction::HorizontalSplit(PaneId::Terminal(pid)), + )) + .unwrap(); } PtyInstruction::NewTab => { if let Some(layout) = maybe_layout.clone() { pty_bus.spawn_terminals_for_layout(layout); } else { let pid = pty_bus.spawn_terminal(None); - pty_bus.os_input.send_to_server(ServerInstruction::ToScreen( - ScreenInstruction::NewTab(pid), - )); + pty_bus + .send_server_instructions + .send(ServerInstruction::ToScreen(ScreenInstruction::NewTab(pid))) + .unwrap(); } } PtyInstruction::ClosePane(id) => { pty_bus.close_pane(id); pty_bus - .os_input - .send_to_server(ServerInstruction::DoneClosingPane); + .send_server_instructions + .send(ServerInstruction::DoneClosingPane) + .unwrap(); } PtyInstruction::CloseTab(ids) => { pty_bus.close_tab(ids); pty_bus - .os_input - .send_to_server(ServerInstruction::DoneClosingPane); + .send_server_instructions + .send(ServerInstruction::DoneClosingPane) + .unwrap(); } PtyInstruction::Exit => { break; @@ -118,16 +160,32 @@ pub fn start_server(mut os_input: Box<dyn ServerOsApi>, opts: CliArgs) -> thread }) .unwrap(); + let router_thread = thread::Builder::new() + .name("server_router".to_string()) + .spawn({ + let os_input = os_input.clone(); + move || loop { + let (instruction, err_ctx) = os_input.server_recv(); + send_server_instructions.update(err_ctx); + match instruction { + ServerInstruction::Exit => break, + _ => { + send_server_instructions.send(instruction).unwrap(); + } + } + } + }) + .unwrap(); + thread::Builder::new() .name("ipc_server".to_string()) .spawn({ move || loop { - let (instruction, mut err_ctx) = os_input.server_recv(); + let (instruction, mut err_ctx) = receive_server_instructions.recv().unwrap(); err_ctx.add_call(ContextType::IPCServer(ServerContext::from(&instruction))); send_pty_instructions.update(err_ctx); send_os_instructions.update(err_ctx); os_input.update_senders(err_ctx); - match instruction { ServerInstruction::OpenFile(file_name) => { let path = PathBuf::from(file_name); @@ -169,14 +227,17 @@ pub fn start_server(mut os_input: Box<dyn ServerOsApi>, opts: CliArgs) -> thread ServerInstruction::ClosePluginPane(pid) => { os_input.send_to_client(ClientInstruction::ClosePluginPane(pid)); } - ServerInstruction::Exit => { + ServerInstruction::ClientExit => { let _ = send_pty_instructions.send(PtyInstruction::Exit); let _ = send_os_instructions.send(ServerOsApiInstruction::Exit); + os_input.server_exit(); let _ = pty_thread.join(); let _ = os_thread.join(); + let _ = router_thread.join(); let _ = os_input.send_to_client(ClientInstruction::Exit); break; } + _ => {} } } }) |
