Skip to content

Commit

Permalink
rust: Add some default channels to library
Browse files Browse the repository at this point in the history
And move the ad-hoc implementation in examples/cp.rs and examples.pd.rs

Signed-off-by: Siddharth Chandrasekaran <[email protected]>
  • Loading branch information
sidcha committed Nov 2, 2023
1 parent 273ebf4 commit b15f950
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 97 deletions.
46 changes: 3 additions & 43 deletions rust/examples/cp.rs
Original file line number Diff line number Diff line change
@@ -1,55 +1,15 @@
use std::{
io::{Read,Write},
path::PathBuf,
os::unix::net::UnixStream,
time::Duration,
thread
};
use osdp::{
cp::ControlPanel,
common::{PdInfo, OsdpFlag, PdId},
channel::{OsdpChannel, Channel}
channel::{OsdpChannel, unix_channel::UnixChannel},
};

type Result<T> = anyhow::Result<T, anyhow::Error>;

struct UnixChannel {
stream: UnixStream,
}

impl UnixChannel {
pub fn new(path: PathBuf) -> Result<Self> {
let stream = UnixStream::connect(&path)?;
Ok(Self {
stream,
})
}
}

impl Read for UnixChannel {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
self.stream.read(buf)
}
}

impl Write for UnixChannel {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
self.stream.write(buf)
}

fn flush(&mut self) -> std::io::Result<()> {
self.stream.flush()
}
}

impl Channel for UnixChannel {
fn get_id(&self) -> i32 {
1
}
}

fn main() -> anyhow::Result<(), anyhow::Error> {
let stream = UnixChannel::new("/tmp/osdp-conn-1".into())?;
fn main() -> anyhow::Result<()> {
let stream = UnixChannel::connect("conn-1")?;
let channel: OsdpChannel = OsdpChannel::new::<UnixChannel>(Box::new(stream));
let pd0 = PdInfo::new(
"PD 101", 101,
Expand Down
53 changes: 4 additions & 49 deletions rust/examples/pd.rs
Original file line number Diff line number Diff line change
@@ -1,62 +1,17 @@
use std::{
os::unix::net::{UnixStream, UnixListener},
path::PathBuf,
io::{Read, Write},
time::Duration,
thread
};
use osdp::{
pd::PeripheralDevice,
common::{PdInfo, OsdpFlag, PdId, PdCapability, PdCapEntry},
channel::{OsdpChannel, Channel},
channel::{OsdpChannel, unix_channel::UnixChannel},
};

type Result<T> = anyhow::Result<T, anyhow::Error>;

struct UnixChannel {
stream: Option<UnixStream>,
}

impl UnixChannel {
pub fn new(path: PathBuf) -> Result<Self> {
let listener = UnixListener::bind(&path)?;
let (stream, _) = listener.accept()?;
Ok(Self {
stream: Some(stream),
})
}
}

impl Read for UnixChannel {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
let stream = self.stream.as_mut().unwrap();
stream.read(buf)
}
}

impl Write for UnixChannel {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
let stream = self.stream.as_mut().unwrap();
stream.write(buf)
}

fn flush(&mut self) -> std::io::Result<()> {
let stream = self.stream.as_mut().unwrap();
stream.flush()
}
}

impl Channel for UnixChannel {
fn get_id(&self) -> i32 {
1
}
}

fn main() -> anyhow::Result<(), anyhow::Error> {
let stream = UnixChannel::new("/tmp/osdp-conn-1".into())?;
fn main() -> anyhow::Result<()> {
let stream = UnixChannel::new("conn-1")?;
let channel: OsdpChannel = OsdpChannel::new::<UnixChannel>(Box::new(stream));
let mut pd_info = PdInfo::new(
"PD 101",
"PD 101",
101,
115200,
OsdpFlag::EnforceSecure,
Expand Down
15 changes: 14 additions & 1 deletion rust/src/channel.rs → rust/src/channel/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
pub mod thread_bus;
pub mod unix_channel;

use std::{
io::{Read, Write},
ffi::c_void,
sync::Mutex
sync::Mutex,
collections::hash_map::DefaultHasher,
hash::{Hash, Hasher},
};

pub trait Channel: Read + Write {
Expand Down Expand Up @@ -65,4 +70,12 @@ impl OsdpChannel {
flush: Some(raw_flush),
}
}
}

pub fn str_to_channel_id(key: &str) -> i32 {
let mut hasher = DefaultHasher::new();
key.hash(&mut hasher);
let mut id = hasher.finish();
id = (id >> 32) ^ id & 0xffffffff;
id as i32
}
7 changes: 4 additions & 3 deletions rust/util/thread_bus.rs → rust/src/channel/thread_bus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ use std::{
collections::HashMap, io::Error, io::ErrorKind
};
use anyhow::bail;
use lazy_static::lazy_static;
use multiqueue::{BroadcastSender, BroadcastReceiver};

use super::Channel;

type Result<T> = anyhow::Result<T, anyhow::Error>;

struct Bus {
Expand Down Expand Up @@ -68,7 +69,7 @@ impl BusDepot {
}
}

lazy_static! {
lazy_static::lazy_static! {
static ref ID: Mutex<BusDepot> = Mutex::new(BusDepot::new());
}

Expand Down Expand Up @@ -116,7 +117,7 @@ impl std::io::Write for OsdpThreadBus {
}
}

impl osdp::channel::Channel for OsdpThreadBus {
impl Channel for OsdpThreadBus {
fn get_id(&self) -> i32 {
self.id
}
Expand Down
64 changes: 64 additions & 0 deletions rust/src/channel/unix_channel.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use std::{
io::{Read,Write},
os::unix::net::{UnixStream, UnixListener},
path::PathBuf,
str::FromStr,
};
use super::Channel;

type Result<T> = anyhow::Result<T, anyhow::Error>;

pub struct UnixChannel {
id: i32,
stream: UnixStream,
}

impl UnixChannel {
pub fn connect(name: &str) -> Result<Self> {
let path = format!("/tmp/osdp-{name}");
let id = super::str_to_channel_id(&path);
let stream = UnixStream::connect(&path)?;
Ok(Self {
id,
stream,
})
}

pub fn new(name: &str) -> Result<Self> {
let path = format!("/tmp/osdp-{name}");
let id = super::str_to_channel_id(&path);
let path = PathBuf::from_str(&path)?;
if path.exists() {
std::fs::remove_file(&path)?;
}
let listener = UnixListener::bind(&path)?;
println!("Waiting for connection to unix::{}", path.display());
let (stream, _) = listener.accept()?;
Ok(Self {
id,
stream,
})
}
}

impl Read for UnixChannel {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
self.stream.read(buf)
}
}

impl Write for UnixChannel {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
self.stream.write(buf)
}

fn flush(&mut self) -> std::io::Result<()> {
self.stream.flush()
}
}

impl Channel for UnixChannel {
fn get_id(&self) -> i32 {
self.id
}
}
1 change: 0 additions & 1 deletion rust/util/lib.rs

This file was deleted.

0 comments on commit b15f950

Please sign in to comment.