use interprocess::local_socket::LocalSocketListener;
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
use std::sync::mpsc::channel;
use std::thread;
use std::{collections::HashMap, fs};
use std::{
collections::HashSet,
str::FromStr,
sync::{Arc, Mutex, RwLock},
};
use wasmer::{ChainableNamedResolver, Instance, Module, Store, Value};
use wasmer_wasi::{Pipe, WasiState};
use zellij_tile::data::{Event, EventType, InputMode, ModeInfo};
use crate::cli::CliArgs;
use crate::client::ClientInstruction;
use crate::common::{
errors::{ContextType, PluginContext, PtyContext, ScreenContext, ServerContext},
input::actions::{Action, Direction},
input::handler::get_mode_info,
os_input_output::{set_permissions, ServerOsApi},
pty_bus::{PtyBus, PtyInstruction},
screen::{Screen, ScreenInstruction},
setup::{get_default_data_dir, install::populate_data_dir},
utils::consts::ZELLIJ_IPC_PIPE,
wasm_vm::{wasi_read_string, wasi_write_object, zellij_exports, PluginEnv, PluginInstruction},
ChannelWithContext, SenderType, SenderWithContext,
};
use crate::layout::Layout;
use crate::panes::PaneId;
use crate::panes::PositionAndSize;
/// Instructions related to server-side application including the
/// ones sent by client to server
#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum ServerInstruction {
TerminalResize(PositionAndSize),
NewClient(PositionAndSize, CliArgs),
Action(Action),
Render(Option<String>),
UnblockInputThread,
ClientExit,
}
struct SessionMetaData {
pub send_pty_instructions: SenderWithContext<PtyInstruction>,
pub send_screen_instructions: SenderWithContext<ScreenInstruction>,
pub send_plugin_instructions: SenderWithContext<PluginInstruction>,
screen_thread: Option<thread::JoinHandle<()>>,
pty_thread: Option<thread::JoinHandle<()>>,
wasm_thread: Option<thread::JoinHandle<()>>,
}
impl Drop for SessionMetaData {
fn drop(&mut self) {
let _ = self.send_pty_instructions.send(PtyInstruction::Exit);
let _ = self.send_screen_instructions.send(ScreenInstruction::Exit);
let _ = self.send_plugin_instructions.send(PluginInstruction::Exit);
let _ = self.screen_thread.take().unwrap().join();
let _ = self.pty_thread.take().unwrap().join();
let _ = self.wasm_thread.take().unwrap().join();
}
}
pub fn start_server(os_input: Box<dyn ServerOsApi>) -> thread::JoinHandle<()> {
let (send_server_instructions,