This commit is contained in:
pjht 2024-08-06 19:44:20 -05:00
parent 7ed40851e5
commit 618c32b9a5
Signed by: pjht
GPG Key ID: 7B5F6AFBEC7EE78E
4 changed files with 41 additions and 15 deletions

View File

@ -26,7 +26,7 @@ impl syslog_rpc::Server for Serv {
let binary_msg = Message {
from: message.from.clone(),
text: None,
binary: message.binary.clone()
binary: message.binary.clone(),
};
for subscriber in &*self.binary_subscribers.read() {
if (binary_msg.from != subscriber.1) || (!subscriber.2.contains(&binary.kind)) {
@ -43,7 +43,9 @@ impl syslog_rpc::Server for Serv {
}
fn subscribe_to_binary(&self, from: u64, message_from: String, kinds: Vec<u64>) {
self.binary_subscribers.write().push((from, message_from, kinds));
self.binary_subscribers
.write()
.push((from, message_from, kinds));
}
}

View File

@ -1,7 +1,7 @@
use std::{collections::VecDeque, os::mikros::ipc};
use parking_lot::Mutex;
pub use syslog_structs::{Message, BinaryMessage};
pub use syslog_structs::{BinaryMessage, Message};
const PROTO: u16 = 1;
static MSG_BUF: Mutex<VecDeque<syslog_structs::Message>> = Mutex::new(VecDeque::new());

View File

@ -3,7 +3,7 @@
use std::os::mikros::ipc::rpc::{self, IncomingCall};
use parking_lot::RwLock;
pub use syslog_structs::{Message, BinaryMessage};
pub use syslog_structs::{BinaryMessage, Message};
static SERVER: RwLock<Option<Box<dyn Server>>> = RwLock::new(None);
@ -23,15 +23,30 @@ impl Client {
Self(pid)
}
pub fn send_text_message(&self, from: impl Into<String>, text: impl Into<String>) -> Result<(), ()> {
pub fn send_text_message(
&self,
from: impl Into<String>,
text: impl Into<String>,
) -> Result<(), ()> {
self.send_raw_message(Message::new_text(from, text))
}
pub fn send_binary_message(&self, from: impl Into<String>, kind: u64, data: impl Into<Vec<u8>>) -> Result<(), ()> {
pub fn send_binary_message(
&self,
from: impl Into<String>,
kind: u64,
data: impl Into<Vec<u8>>,
) -> Result<(), ()> {
self.send_raw_message(Message::new_binary(from, kind, data))
}
pub fn send_text_binary_message(&self, from: impl Into<String>, text: impl Into<String>, kind: u64, data: impl Into<Vec<u8>>) -> Result<(), ()> {
pub fn send_text_binary_message(
&self,
from: impl Into<String>,
text: impl Into<String>,
kind: u64,
data: impl Into<Vec<u8>>,
) -> Result<(), ()> {
self.send_raw_message(Message::new_text_binary(from, text, kind, data))
}
@ -43,15 +58,18 @@ impl Client {
}
pub fn subscribe_to_text(&self) {
postcard::from_bytes(
&rpc::send_call(self.0, PROTO, 1, &[]).get_return(),
)
.unwrap()
postcard::from_bytes(&rpc::send_call(self.0, PROTO, 1, &[]).get_return()).unwrap()
}
pub fn subscribe_to_binary(&self, message_from: String, kinds: Vec<u64>) {
postcard::from_bytes(
&rpc::send_call(self.0, PROTO, 2, &postcard::to_stdvec(&(message_from, kinds)).unwrap()).get_return(),
&rpc::send_call(
self.0,
PROTO,
2,
&postcard::to_stdvec(&(message_from, kinds)).unwrap(),
)
.get_return(),
)
.unwrap()
}
@ -79,7 +97,8 @@ fn callback(call: IncomingCall) {
call.send_return(&ret);
} else if call.func == 2 {
let (message_from, kinds) = postcard::from_bytes(&call.args).unwrap();
let ret = postcard::to_stdvec(&server.subscribe_to_binary(call.from, message_from, kinds)).unwrap();
let ret = postcard::to_stdvec(&server.subscribe_to_binary(call.from, message_from, kinds))
.unwrap();
call.send_return(&ret);
}
}

View File

@ -27,7 +27,12 @@ impl Message {
}
}
pub fn new_text_binary(from: impl Into<String>, text: impl Into<String>, kind: u64, data: impl Into<Vec<u8>>) -> Self {
pub fn new_text_binary(
from: impl Into<String>,
text: impl Into<String>,
kind: u64,
data: impl Into<Vec<u8>>,
) -> Self {
Self {
from: from.into(),
text: Some(text.into()),
@ -42,5 +47,5 @@ impl Message {
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct BinaryMessage {
pub kind: u64,
pub data: Vec<u8>
pub data: Vec<u8>,
}