Share SessionMetadata between the server_router and the ipc_server thread
This commit is contained in:
parent
246899a1dd
commit
27393dfcf3
1 changed files with 73 additions and 57 deletions
|
|
@ -8,7 +8,7 @@ use std::{collections::HashMap, fs};
|
||||||
use std::{
|
use std::{
|
||||||
collections::HashSet,
|
collections::HashSet,
|
||||||
str::FromStr,
|
str::FromStr,
|
||||||
sync::{Arc, Mutex},
|
sync::{Arc, Mutex, RwLock},
|
||||||
};
|
};
|
||||||
use wasmer::{ChainableNamedResolver, Instance, Module, Store, Value};
|
use wasmer::{ChainableNamedResolver, Instance, Module, Store, Value};
|
||||||
use wasmer_wasi::{Pipe, WasiState};
|
use wasmer_wasi::{Pipe, WasiState};
|
||||||
|
|
@ -175,7 +175,7 @@ impl ServerInstruction {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct ClientMetaData {
|
struct SessionMetaData {
|
||||||
pub send_pty_instructions: SenderWithContext<PtyInstruction>,
|
pub send_pty_instructions: SenderWithContext<PtyInstruction>,
|
||||||
pub send_screen_instructions: SenderWithContext<ScreenInstruction>,
|
pub send_screen_instructions: SenderWithContext<ScreenInstruction>,
|
||||||
pub send_plugin_instructions: SenderWithContext<PluginInstruction>,
|
pub send_plugin_instructions: SenderWithContext<PluginInstruction>,
|
||||||
|
|
@ -184,7 +184,7 @@ struct ClientMetaData {
|
||||||
wasm_thread: Option<thread::JoinHandle<()>>,
|
wasm_thread: Option<thread::JoinHandle<()>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Drop for ClientMetaData {
|
impl Drop for SessionMetaData {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
let _ = self.send_pty_instructions.send(PtyInstruction::Exit);
|
let _ = self.send_pty_instructions.send(PtyInstruction::Exit);
|
||||||
let _ = self.send_screen_instructions.send(ScreenInstruction::Exit);
|
let _ = self.send_screen_instructions.send(ScreenInstruction::Exit);
|
||||||
|
|
@ -201,16 +201,67 @@ pub fn start_server(mut os_input: Box<dyn ServerOsApi>, opts: CliArgs) -> thread
|
||||||
> = channel();
|
> = channel();
|
||||||
let send_server_instructions =
|
let send_server_instructions =
|
||||||
SenderWithContext::new(SenderType::Sender(send_server_instructions));
|
SenderWithContext::new(SenderType::Sender(send_server_instructions));
|
||||||
|
|
||||||
|
let sessions: Arc<RwLock<HashMap<String, SessionMetaData>>> =
|
||||||
|
Arc::new(RwLock::new(HashMap::new()));
|
||||||
|
// We handle only single client for now
|
||||||
|
let session: Arc<RwLock<String>> = Arc::new(RwLock::new("session1".into()));
|
||||||
let router_thread = thread::Builder::new()
|
let router_thread = thread::Builder::new()
|
||||||
.name("server_router".to_string())
|
.name("server_router".to_string())
|
||||||
.spawn({
|
.spawn({
|
||||||
let os_input = os_input.clone();
|
let os_input = os_input.clone();
|
||||||
|
let session = session.clone();
|
||||||
|
let sessions = sessions.clone();
|
||||||
let send_server_instructions = send_server_instructions.clone();
|
let send_server_instructions = send_server_instructions.clone();
|
||||||
move || loop {
|
move || loop {
|
||||||
let (instruction, mut err_ctx) = os_input.server_recv();
|
let (instruction, mut err_ctx) = os_input.server_recv();
|
||||||
err_ctx.add_call(ContextType::IPCServer(ServerContext::from(&instruction)));
|
err_ctx.add_call(ContextType::IPCServer(ServerContext::from(&instruction)));
|
||||||
|
let rlocked_session = &*session.read().unwrap();
|
||||||
|
let rlocked_sessions = sessions.read().unwrap();
|
||||||
match instruction {
|
match instruction {
|
||||||
ServerInstruction::Exit => break,
|
ServerInstruction::Exit => break,
|
||||||
|
ServerInstruction::OpenFile(file_name) => {
|
||||||
|
rlocked_sessions[rlocked_session]
|
||||||
|
.send_pty_instructions
|
||||||
|
.send(PtyInstruction::SpawnTerminal(Some(file_name)))
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
ServerInstruction::SplitHorizontally => {
|
||||||
|
rlocked_sessions[rlocked_session]
|
||||||
|
.send_pty_instructions
|
||||||
|
.send(PtyInstruction::SpawnTerminalHorizontally(None))
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
ServerInstruction::SplitVertically => {
|
||||||
|
rlocked_sessions[rlocked_session]
|
||||||
|
.send_pty_instructions
|
||||||
|
.send(PtyInstruction::SpawnTerminalVertically(None))
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
ServerInstruction::MoveFocus => {
|
||||||
|
rlocked_sessions[rlocked_session]
|
||||||
|
.send_screen_instructions
|
||||||
|
.send(ScreenInstruction::FocusNextPane)
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
ServerInstruction::ToScreen(instruction) => {
|
||||||
|
rlocked_sessions[rlocked_session]
|
||||||
|
.send_screen_instructions
|
||||||
|
.send(instruction)
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
ServerInstruction::ToPty(instruction) => {
|
||||||
|
rlocked_sessions[rlocked_session]
|
||||||
|
.send_pty_instructions
|
||||||
|
.send(instruction)
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
ServerInstruction::PluginUpdate(pid, event) => {
|
||||||
|
rlocked_sessions[rlocked_session]
|
||||||
|
.send_plugin_instructions
|
||||||
|
.send(PluginInstruction::Update(pid, event))
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
_ => {
|
_ => {
|
||||||
send_server_instructions.send(instruction).unwrap();
|
send_server_instructions.send(instruction).unwrap();
|
||||||
}
|
}
|
||||||
|
|
@ -222,64 +273,29 @@ pub fn start_server(mut os_input: Box<dyn ServerOsApi>, opts: CliArgs) -> thread
|
||||||
thread::Builder::new()
|
thread::Builder::new()
|
||||||
.name("ipc_server".to_string())
|
.name("ipc_server".to_string())
|
||||||
.spawn({
|
.spawn({
|
||||||
let mut clients: HashMap<String, ClientMetaData> = HashMap::new();
|
|
||||||
// We handle only single client for now
|
|
||||||
let mut client: Option<String> = None;
|
|
||||||
move || loop {
|
move || loop {
|
||||||
let (instruction, mut err_ctx) = receive_server_instructions.recv().unwrap();
|
let (instruction, mut err_ctx) = receive_server_instructions.recv().unwrap();
|
||||||
err_ctx.add_call(ContextType::IPCServer(ServerContext::from(&instruction)));
|
err_ctx.add_call(ContextType::IPCServer(ServerContext::from(&instruction)));
|
||||||
match instruction {
|
match instruction {
|
||||||
ServerInstruction::OpenFile(file_name) => {
|
|
||||||
clients[client.as_ref().unwrap()]
|
|
||||||
.send_pty_instructions
|
|
||||||
.send(PtyInstruction::SpawnTerminal(Some(file_name)))
|
|
||||||
.unwrap();
|
|
||||||
}
|
|
||||||
ServerInstruction::SplitHorizontally => {
|
|
||||||
clients[client.as_ref().unwrap()]
|
|
||||||
.send_pty_instructions
|
|
||||||
.send(PtyInstruction::SpawnTerminalHorizontally(None))
|
|
||||||
.unwrap();
|
|
||||||
}
|
|
||||||
ServerInstruction::SplitVertically => {
|
|
||||||
clients[client.as_ref().unwrap()]
|
|
||||||
.send_pty_instructions
|
|
||||||
.send(PtyInstruction::SpawnTerminalVertically(None))
|
|
||||||
.unwrap();
|
|
||||||
}
|
|
||||||
ServerInstruction::MoveFocus => {
|
|
||||||
clients[client.as_ref().unwrap()]
|
|
||||||
.send_screen_instructions
|
|
||||||
.send(ScreenInstruction::FocusNextPane)
|
|
||||||
.unwrap();
|
|
||||||
}
|
|
||||||
ServerInstruction::NewClient(buffer_path, full_screen_ws) => {
|
ServerInstruction::NewClient(buffer_path, full_screen_ws) => {
|
||||||
client = Some(buffer_path.clone());
|
let session_data = init_session(
|
||||||
let client_data = init_client(
|
|
||||||
os_input.clone(),
|
os_input.clone(),
|
||||||
opts.clone(),
|
opts.clone(),
|
||||||
send_server_instructions.clone(),
|
send_server_instructions.clone(),
|
||||||
full_screen_ws,
|
full_screen_ws,
|
||||||
);
|
);
|
||||||
clients.insert(buffer_path.clone(), client_data);
|
drop(
|
||||||
clients[client.as_ref().unwrap()]
|
sessions
|
||||||
|
.write()
|
||||||
|
.unwrap()
|
||||||
|
.insert(session.read().unwrap().clone(), session_data),
|
||||||
|
);
|
||||||
|
sessions.read().unwrap()[&*session.read().unwrap()]
|
||||||
.send_pty_instructions
|
.send_pty_instructions
|
||||||
.send(PtyInstruction::NewTab)
|
.send(PtyInstruction::NewTab)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
os_input.add_client_sender(buffer_path);
|
os_input.add_client_sender(buffer_path);
|
||||||
}
|
}
|
||||||
ServerInstruction::ToScreen(instruction) => {
|
|
||||||
clients[client.as_ref().unwrap()]
|
|
||||||
.send_screen_instructions
|
|
||||||
.send(instruction)
|
|
||||||
.unwrap();
|
|
||||||
}
|
|
||||||
ServerInstruction::ToPty(instruction) => {
|
|
||||||
clients[client.as_ref().unwrap()]
|
|
||||||
.send_pty_instructions
|
|
||||||
.send(instruction)
|
|
||||||
.unwrap();
|
|
||||||
}
|
|
||||||
ServerInstruction::DoneClosingPane => {
|
ServerInstruction::DoneClosingPane => {
|
||||||
os_input.send_to_client(ClientInstruction::DoneClosingPane);
|
os_input.send_to_client(ClientInstruction::DoneClosingPane);
|
||||||
}
|
}
|
||||||
|
|
@ -292,14 +308,14 @@ pub fn start_server(mut os_input: Box<dyn ServerOsApi>, opts: CliArgs) -> thread
|
||||||
ServerInstruction::ClientShouldExit => {
|
ServerInstruction::ClientShouldExit => {
|
||||||
os_input.send_to_client(ClientInstruction::Exit);
|
os_input.send_to_client(ClientInstruction::Exit);
|
||||||
}
|
}
|
||||||
ServerInstruction::PluginUpdate(pid, event) => {
|
|
||||||
clients[client.as_ref().unwrap()]
|
|
||||||
.send_plugin_instructions
|
|
||||||
.send(PluginInstruction::Update(pid, event))
|
|
||||||
.unwrap();
|
|
||||||
}
|
|
||||||
ServerInstruction::ClientExit => {
|
ServerInstruction::ClientExit => {
|
||||||
clients.remove(client.as_ref().unwrap()).unwrap();
|
drop(
|
||||||
|
sessions
|
||||||
|
.write()
|
||||||
|
.unwrap()
|
||||||
|
.remove(&*session.read().unwrap())
|
||||||
|
.unwrap(),
|
||||||
|
);
|
||||||
os_input.server_exit();
|
os_input.server_exit();
|
||||||
let _ = router_thread.join();
|
let _ = router_thread.join();
|
||||||
let _ = os_input.send_to_client(ClientInstruction::Exit);
|
let _ = os_input.send_to_client(ClientInstruction::Exit);
|
||||||
|
|
@ -308,19 +324,19 @@ pub fn start_server(mut os_input: Box<dyn ServerOsApi>, opts: CliArgs) -> thread
|
||||||
ServerInstruction::Render(output) => {
|
ServerInstruction::Render(output) => {
|
||||||
os_input.send_to_client(ClientInstruction::Render(output))
|
os_input.send_to_client(ClientInstruction::Render(output))
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => panic!("Received unexpected instruction."),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.unwrap()
|
.unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn init_client(
|
fn init_session(
|
||||||
os_input: Box<dyn ServerOsApi>,
|
os_input: Box<dyn ServerOsApi>,
|
||||||
opts: CliArgs,
|
opts: CliArgs,
|
||||||
send_server_instructions: SenderWithContext<ServerInstruction>,
|
send_server_instructions: SenderWithContext<ServerInstruction>,
|
||||||
full_screen_ws: PositionAndSize,
|
full_screen_ws: PositionAndSize,
|
||||||
) -> ClientMetaData {
|
) -> SessionMetaData {
|
||||||
let (send_screen_instructions, receive_screen_instructions): ChannelWithContext<
|
let (send_screen_instructions, receive_screen_instructions): ChannelWithContext<
|
||||||
ScreenInstruction,
|
ScreenInstruction,
|
||||||
> = channel();
|
> = channel();
|
||||||
|
|
@ -736,7 +752,7 @@ fn init_client(
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.unwrap();
|
.unwrap();
|
||||||
ClientMetaData {
|
SessionMetaData {
|
||||||
send_plugin_instructions,
|
send_plugin_instructions,
|
||||||
send_screen_instructions,
|
send_screen_instructions,
|
||||||
send_pty_instructions,
|
send_pty_instructions,
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue