aboutsummaryrefslogtreecommitdiffstats
path: root/src/common
diff options
context:
space:
mode:
authorKunal Mohan <kunalmohan99@gmail.com>2021-03-22 02:08:39 +0530
committerKunal Mohan <kunalmohan99@gmail.com>2021-05-04 20:47:52 +0530
commitbc2345c413fb310628380ad06af9653eb9e12b9e (patch)
treeadba2a47157fbd886f5eea99ebffb3299becb73b /src/common
parentincrease snapshot time (diff)
downloadsl0ppij-bc2345c413fb310628380ad06af9653eb9e12b9e.tar.gz
sl0ppij-bc2345c413fb310628380ad06af9653eb9e12b9e.tar.xz
sl0ppij-bc2345c413fb310628380ad06af9653eb9e12b9e.zip
Hide server-side Ipc channels creation behind OsApis and some documentation fixes
Diffstat (limited to 'src/common')
-rw-r--r--src/common/errors.rs4
-rw-r--r--src/common/mod.rs10
-rw-r--r--src/common/os_input_output.rs30
3 files changed, 33 insertions, 11 deletions
diff --git a/src/common/errors.rs b/src/common/errors.rs
index 234745ad..83527448 100644
--- a/src/common/errors.rs
+++ b/src/common/errors.rs
@@ -146,6 +146,7 @@ pub enum ContextType {
Plugin(PluginContext),
/// An app-related call.
App(AppContext),
+ /// A server-related call.
IPCServer(ServerContext),
StdinHandler,
AsyncTask,
@@ -297,6 +298,7 @@ impl From<&PtyInstruction> for PtyContext {
}
}
+/// Stack call representations corresponding to the different types of [`ServerOsApiInstruction`]s.
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub enum OsContext {
SetTerminalSizeUsingFd,
@@ -372,7 +374,7 @@ impl From<&AppInstruction> for AppContext {
}
}
-/// Stack call representations corresponding to the different types of [`AppInstruction`]s.
+/// Stack call representations corresponding to the different types of [`ServerInstruction`]s.
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub enum ServerContext {
OpenFile,
diff --git a/src/common/mod.rs b/src/common/mod.rs
index 9554eb78..ec1f0959 100644
--- a/src/common/mod.rs
+++ b/src/common/mod.rs
@@ -42,6 +42,8 @@ use wasmer::{ChainableNamedResolver, Instance, Module, Store, Value};
use wasmer_wasi::{Pipe, WasiState};
use zellij_tile::data::{EventType, InputMode, ModeInfo};
+pub const IPC_BUFFER_SIZE: u32 = 8192;
+
#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum ServerInstruction {
OpenFile(PathBuf),
@@ -205,7 +207,7 @@ pub fn start(
opts: CliArgs,
server_os_input: Box<dyn ServerOsApi>,
) {
- let (ipc_thread, server_name) = start_server(server_os_input.clone(), opts.clone());
+ let ipc_thread = start_server(server_os_input.clone(), opts.clone());
let take_snapshot = "\u{1b}[?1049h";
os_input.unset_raw_mode(0);
@@ -235,9 +237,9 @@ pub fn start(
let mut send_app_instructions =
SenderWithContext::new(err_ctx, SenderType::SyncSender(send_app_instructions));
- let (client_buffer_path, client_buffer) = SharedRingBuffer::create_temp(8192).unwrap();
- let mut send_server_instructions =
- IpcSenderWithContext::new(SharedRingBuffer::open(&server_name).unwrap());
+ let (client_buffer_path, client_buffer) =
+ SharedRingBuffer::create_temp(IPC_BUFFER_SIZE).unwrap();
+ let mut send_server_instructions = os_input.get_server_sender().unwrap();
send_server_instructions
.send(ServerInstruction::NewClient(client_buffer_path))
.unwrap();
diff --git a/src/common/os_input_output.rs b/src/common/os_input_output.rs
index d0c13ccd..90bd0e49 100644
--- a/src/common/os_input_output.rs
+++ b/src/common/os_input_output.rs
@@ -1,5 +1,7 @@
+use crate::common::{IpcSenderWithContext, IPC_BUFFER_SIZE};
use crate::panes::PositionAndSize;
-use crate::utils::shared::default_palette;
+use crate::utils::consts::ZELLIJ_IPC_PIPE;
+use ipmpsc::{Receiver as IpcReceiver, Result as IpcResult, SharedRingBuffer};
use nix::fcntl::{fcntl, FcntlArg, OFlag};
use nix::pty::{forkpty, Winsize};
use nix::sys::signal::{kill, Signal};
@@ -8,6 +10,7 @@ use nix::sys::wait::waitpid;
use nix::unistd;
use nix::unistd::{ForkResult, Pid};
use serde::{Deserialize, Serialize};
+use std::env;
use std::io;
use std::io::prelude::*;
use std::os::unix::io::RawFd;
@@ -15,10 +18,6 @@ use std::path::PathBuf;
use std::process::{Child, Command};
use std::sync::{Arc, Mutex};
-use signal_hook::{consts::signal::*, iterator::Signals};
-
-use std::env;
-
fn into_raw_mode(pid: RawFd) {
let mut tio = termios::tcgetattr(pid).expect("could not get terminal attribute");
termios::cfmakeraw(&mut tio);
@@ -158,6 +157,7 @@ fn spawn_terminal(file_to_open: Option<PathBuf>, orig_termios: termios::Termios)
#[derive(Clone)]
pub struct ServerOsInputOutput {
orig_termios: Arc<Mutex<termios::Termios>>,
+ server_buffer: SharedRingBuffer,
}
/// The `ServerOsApi` trait represents an abstract interface to the features of an operating system that
@@ -180,6 +180,8 @@ pub trait ServerOsApi: Send + Sync {
fn kill(&mut self, pid: RawFd) -> Result<(), nix::Error>;
/// Returns a [`Box`] pointer to this [`OsApi`] struct.
fn box_clone(&self) -> Box<dyn ServerOsApi>;
+ fn get_server_receiver(&self) -> IpcReceiver;
+ fn get_server_sender(&self) -> IpcSenderWithContext;
}
impl ServerOsApi for ServerOsInputOutput {
@@ -207,6 +209,12 @@ impl ServerOsApi for ServerOsInputOutput {
waitpid(Pid::from_raw(pid), None).unwrap();
Ok(())
}
+ fn get_server_receiver(&self) -> IpcReceiver {
+ IpcReceiver::new(self.server_buffer.clone())
+ }
+ fn get_server_sender(&self) -> IpcSenderWithContext {
+ IpcSenderWithContext::new(self.server_buffer.clone())
+ }
}
impl Clone for Box<dyn ServerOsApi> {
@@ -218,9 +226,14 @@ impl Clone for Box<dyn ServerOsApi> {
pub fn get_server_os_input() -> ServerOsInputOutput {
let current_termios = termios::tcgetattr(0).unwrap();
let orig_termios = Arc::new(Mutex::new(current_termios));
- ServerOsInputOutput { orig_termios }
+ let server_buffer = SharedRingBuffer::create(ZELLIJ_IPC_PIPE, IPC_BUFFER_SIZE).unwrap();
+ ServerOsInputOutput {
+ orig_termios,
+ server_buffer,
+ }
}
+/// OS Instructions sent to the Server by clients
#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum ServerOsApiInstruction {
SetTerminalSizeUsingFd(RawFd, u16, u16),
@@ -251,6 +264,7 @@ pub trait ClientOsApi: Send + Sync {
fn read_from_stdin(&self) -> Vec<u8>;
/// Returns a [`Box`] pointer to this [`OsApi`] struct.
fn box_clone(&self) -> Box<dyn ClientOsApi>;
+ fn get_server_sender(&self) -> IpcResult<IpcSenderWithContext>;
}
impl ClientOsApi for ClientOsInputOutput {
@@ -280,6 +294,10 @@ impl ClientOsApi for ClientOsInputOutput {
let stdout = ::std::io::stdout();
Box::new(stdout)
}
+ fn get_server_sender(&self) -> IpcResult<IpcSenderWithContext> {
+ let buffer = SharedRingBuffer::open(ZELLIJ_IPC_PIPE)?;
+ Ok(IpcSenderWithContext::new(buffer))
+ }
}
impl Clone for Box<dyn ClientOsApi> {