From 3f4c9fdb5af46dcce0c9ce007852d684f15d3501 Mon Sep 17 00:00:00 2001 From: able Date: Thu, 28 Jul 2022 20:15:02 -0500 Subject: [PATCH] implementing socket related IPC --- Cargo.lock | 14 +++++++++-- ableos/Cargo.toml | 4 +-- ableos/src/handle.rs | 20 +++++++-------- ableos/src/ipc/mod.rs | 48 +++++++++++++++++++++++++++++------- ableos/src/ipc/socket.rs | 13 ++++++++-- ableos/src/kmain.rs | 13 +++++++++- ableos/src/rhai_shell/mod.rs | 1 + ableos/src/scratchpad.rs | 1 + 8 files changed, 88 insertions(+), 26 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e3138769..11d2321a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -38,6 +38,7 @@ dependencies = [ "genfs", "hashbrown 0.7.2", "kernel", + "lazy_static", "libwasm", "linked_list_allocator", "lliw", @@ -54,7 +55,7 @@ dependencies = [ "rkyv", "serde", "shadeable", - "spin", + "spin 0.9.2", "toml", "uart_16550", "unicode-width", @@ -310,7 +311,7 @@ dependencies = [ "bitflags", "genfs", "rlibc", - "spin", + "spin 0.9.2", ] [[package]] @@ -439,6 +440,9 @@ name = "lazy_static" version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" +dependencies = [ + "spin 0.5.2", +] [[package]] name = "libc" @@ -896,6 +900,12 @@ dependencies = [ "version_check", ] +[[package]] +name = "spin" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d" + [[package]] name = "spin" version = "0.9.2" diff --git a/ableos/Cargo.toml b/ableos/Cargo.toml index cffe6fb9..7c020def 100644 --- a/ableos/Cargo.toml +++ b/ableos/Cargo.toml @@ -42,6 +42,8 @@ test-args = [ ] [dependencies] +lazy_static = { version = "*", features = ["spin_no_std"] } + linked_list_allocator = "0.9.0" lliw = "0.2.0" spin = "0.9" @@ -57,8 +59,6 @@ axel = { git = "https://git.ablecorp.us/able/aos_userland" } versioning = { git = "https://git.ablecorp.us/able/aos_userland" } embedded-graphics = "*" pc-keyboard = "0.5" - - [dependencies.logos] version = "0.12" default-features = false diff --git a/ableos/src/handle.rs b/ableos/src/handle.rs index 0873d153..b6fd420e 100644 --- a/ableos/src/handle.rs +++ b/ableos/src/handle.rs @@ -11,15 +11,14 @@ pub struct BinaryData { data: Vec, } -#[derive(Debug)] +#[derive(Debug, Eq, Hash, PartialEq, Clone, Copy)] pub enum HandleResource { - Channel(Channel), - // Device - /// Used for things like files or images or the like - BinaryData(BinaryData), + Channel, + Socket, + BinaryData, } -#[derive(Debug)] +#[derive(Debug, Eq, Hash, PartialEq, Clone, Copy)] pub struct Handle { inner: u128, res: HandleResource, @@ -29,8 +28,9 @@ impl Display for Handle { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { write!(f, "Handle-{:032x}", self.inner)?; match &self.res { - HandleResource::Channel(_) => write!(f, "-Channel")?, - HandleResource::BinaryData(data) => write!(f, "-Binary-{}", &data.name)?, + HandleResource::Channel => write!(f, "-Channel")?, + HandleResource::BinaryData => write!(f, "-Binary")?, + Socket => write!(f, "-Socket")?, } Ok(()) @@ -39,10 +39,10 @@ impl Display for Handle { use crate::handle::HandleResource::*; impl Handle { - pub fn from_channel(channel: Channel) -> Self { + pub fn new(htype: HandleResource) -> Self { Self { inner: generate_process_pass(), - res: Channel(channel), + res: htype, } } } diff --git a/ableos/src/ipc/mod.rs b/ableos/src/ipc/mod.rs index 519f906f..d648cc3e 100644 --- a/ableos/src/ipc/mod.rs +++ b/ableos/src/ipc/mod.rs @@ -1,16 +1,46 @@ -use self::{channel::Channel, socket::Socket}; +use hashbrown::HashMap; + +use crate::handle::Handle; + +use self::{ + channel::Channel, + socket::{Socket, SocketError}, +}; pub mod channel; pub mod socket; -pub static IPC: spin::Mutex = spin::Mutex::new(IPCService { - sockets: vec![], - channels: vec![], -}); - +lazy_static::lazy_static! { + pub static ref IPC: spin::Mutex = spin::Mutex::new(IPCService { + sockets: HashMap::new(), + channels: HashMap::new(), + }); +} pub struct IPCService { - pub sockets: Vec, - pub channels: Vec, + pub sockets: HashMap, + pub channels: HashMap, // TODO: Add a public board of each down below which use some method of pointing to the above } -impl IPCService {} + +// Socket Related Impl +impl IPCService { + pub fn create_socket(&mut self) -> Handle { + let handle = Handle::new(crate::handle::HandleResource::Socket); + + let sock = Socket::new(); + self.sockets.insert(handle.clone(), sock); + + handle + } + + pub fn send_socket(&mut self, handle: Handle, data: String) -> Result<(), SocketError> { + let sock = self.sockets.get_mut(&handle); + + match sock { + Some(socket) => { + return socket.write(data); + } + None => return Err(SocketError::NonexistantSocket), + } + } +} diff --git a/ableos/src/ipc/socket.rs b/ableos/src/ipc/socket.rs index 5cd7d4aa..1546c397 100644 --- a/ableos/src/ipc/socket.rs +++ b/ableos/src/ipc/socket.rs @@ -1,16 +1,21 @@ // SEEALSO: https://git.ablecorp.us/able/ableos/src/branch/master/ableos/src/relib/network/socket.rs pub enum SocketError { + NonexistantSocket, EmptySocket, } - +#[derive(Debug)] pub struct Socket { + name: Option, stream: Vec, } impl Socket { pub fn new() -> Self { - Self { stream: vec![] } + Self { + name: None, + stream: vec![], + } } pub fn write(&mut self, data: String) -> Result<(), SocketError> { for c in data.chars() { @@ -28,4 +33,8 @@ impl Socket { } return Err(SocketError::EmptySocket); } + + pub fn name(&mut self, name: String) { + self.name = Some(name); + } } diff --git a/ableos/src/kmain.rs b/ableos/src/kmain.rs index 47d86e06..986a54fc 100644 --- a/ableos/src/kmain.rs +++ b/ableos/src/kmain.rs @@ -1,14 +1,18 @@ #![allow(clippy::empty_loop)] +use core::sync::atomic::Ordering; + use crate::arch::drivers::sysinfo::master; +use crate::ipc::{self, IPC}; use crate::scheduler::SCHEDULER; +use crate::time::fetch_time; use crate::{ arch::{init, sloop}, relib::network::socket::{SimpleSock, Socket}, scratchpad, }; use crate::{boot_conf::KernelConfig, systeminfo::RELEASE_TYPE}; -use kernel::KERNEL_VERSION; +use kernel::{KERNEL_VERSION, TICK}; use spin::Lazy; // TODO: Change this structure to allow for multiple cores loaded @@ -25,6 +29,13 @@ pub fn kernel_main() -> ! { log::set_max_level(log::LevelFilter::Off); } + let mut ipc_service = IPC.lock(); + // create some channels or whatever here then drop it + let handle = ipc_service.create_socket(); + ipc_service.send_socket(handle, "nerd".to_string()); + + drop(ipc_service); + x86_64::instructions::interrupts::without_interrupts(|| { SCHEDULER.lock().enqueue_spawn(scratchpad); }); diff --git a/ableos/src/rhai_shell/mod.rs b/ableos/src/rhai_shell/mod.rs index 7886826a..8c7c768b 100644 --- a/ableos/src/rhai_shell/mod.rs +++ b/ableos/src/rhai_shell/mod.rs @@ -217,6 +217,7 @@ fn engine_construction() -> Engine { engine.register_fn("poke", poke_memory); engine.register_fn("sloop", sloop); engine.register_fn("log_dump", log_dump); + // engine.register_fn("ls", ls); engine } diff --git a/ableos/src/scratchpad.rs b/ableos/src/scratchpad.rs index 1a33a3ec..1b0a7021 100644 --- a/ableos/src/scratchpad.rs +++ b/ableos/src/scratchpad.rs @@ -7,6 +7,7 @@ use crate::channels::{Channel, ChannelPermission}; use crate::devices::pci; use crate::filesystem::FILE_SYSTEM; use crate::handle::Handle; +use crate::ipc::IPC; use crate::rhai_shell::shell; use crate::rhai_shell::KEYBUFF; use crate::wasm_jumploader::run_program;