we can recieve and send messages on a unix sock
This commit is contained in:
@@ -1,14 +1,19 @@
|
||||
use crossbeam::channel::Receiver;
|
||||
use tracing::{trace, error};
|
||||
|
||||
use crate::mize_err;
|
||||
use crate::{instance::Instance, item::ItemData};
|
||||
use crate::id::MizeId;
|
||||
use crate:: error::{MizeResult, MizeError, MizeResultTrait};
|
||||
use crate::proto::{MessageCmd, MizeMessage};
|
||||
|
||||
use super::connection::{self, Connection};
|
||||
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Operation {
|
||||
Set(MizeId, ItemData),
|
||||
Msg(MizeMessage),
|
||||
}
|
||||
|
||||
pub fn updater_thread(operation_rx : Receiver<Operation>, mut instance: Instance) -> MizeResult<()> {
|
||||
@@ -20,7 +25,7 @@ pub fn updater_thread(operation_rx : Receiver<Operation>, mut instance: Instance
|
||||
error!("OPERATION FAILED: {:?}", operation);
|
||||
}
|
||||
}
|
||||
Ok(() )
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn handle_operation(operation: &mut Operation, instance: &mut Instance) -> MizeResult<()> {
|
||||
@@ -29,7 +34,35 @@ fn handle_operation(operation: &mut Operation, instance: &mut Instance) -> MizeR
|
||||
let item_data: ItemData = value.to_owned();
|
||||
let mut item = instance.get(id.clone())?;
|
||||
item.merge(item_data)?;
|
||||
},
|
||||
Operation::Msg(msg) => {
|
||||
handle_msg(msg, instance)?
|
||||
},
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn handle_msg(msg: &mut MizeMessage, instance: &mut Instance) -> MizeResult<()> {
|
||||
println!("got msg: {:?}", msg);
|
||||
match msg.cmd()? {
|
||||
MessageCmd::Get => {
|
||||
let id = msg.id(instance)?;
|
||||
let mut connection = instance.get_connection(msg.conn_id)?;
|
||||
let item = instance.get(id.clone())?;
|
||||
let msg = MizeMessage::new_give(id, item.as_data_full()?, msg.conn_id);
|
||||
connection.send(msg)?;
|
||||
},
|
||||
MessageCmd::Set => {
|
||||
let data = msg.data()?;
|
||||
let id = msg.id(instance)?;
|
||||
instance.set(id, data);
|
||||
},
|
||||
_ => {
|
||||
return Err(mize_err!("got a message, that is not handeled"));
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user