we can recieve and send messages on a unix sock
This commit is contained in:
@@ -6,18 +6,20 @@ use crate::error::{MizeError, MizeResult, IntoMizeResult};
|
||||
|
||||
use super::Instance;
|
||||
|
||||
pub struct Peer {
|
||||
rx: Receiver<MizeMessage>,
|
||||
tx: Sender<MizeMessage>,
|
||||
#[derive(Clone)]
|
||||
pub struct Connection {
|
||||
pub rx: Receiver<MizeMessage>,
|
||||
pub tx: Sender<MizeMessage>,
|
||||
pub id: u64,
|
||||
}
|
||||
|
||||
pub trait PeerListener : Send + Sync {
|
||||
pub trait ConnListener : Send + Sync {
|
||||
fn listen(self, instance: Instance) -> MizeResult<()>;
|
||||
}
|
||||
|
||||
impl Peer {
|
||||
pub fn new(rx: Receiver<MizeMessage>, tx: Sender<MizeMessage>) -> Peer {
|
||||
Peer { rx , tx }
|
||||
impl Connection {
|
||||
pub fn send(&mut self, msg: MizeMessage) -> MizeResult<()> {
|
||||
Ok(self.tx.send(msg)?)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ use std::collections::HashMap;
|
||||
use std::fs::create_dir;
|
||||
use std::thread::JoinHandle;
|
||||
use std::{thread, vec};
|
||||
use crossbeam::channel;
|
||||
use crossbeam::channel::{self, Receiver, Sender};
|
||||
use std::sync::{Arc, Mutex};
|
||||
use tracing::{trace, debug, info, warn, error};
|
||||
use uuid::Uuid;
|
||||
@@ -16,23 +16,25 @@ use interner::shared::{VecStringPool, StringPool};
|
||||
use crate::error::{MizeError, MizeResult, IntoMizeResult, MizeResultTrait};
|
||||
use crate::instance::store::Store;
|
||||
use crate::instance::updater::updater_thread;
|
||||
use crate::instance::peer::Peer;
|
||||
use crate::id::{IntoMizeId, MizeId, Namespace};
|
||||
use crate::instance::subscription::Subscription;
|
||||
use crate::item::{Item, ItemData};
|
||||
use crate::memstore::MemStore;
|
||||
use crate::instance::updater::Operation;
|
||||
use crate::mize_err;
|
||||
use crate::proto::MizeMessage;
|
||||
|
||||
use self::peer::PeerListener;
|
||||
use self::connection::{ConnListener, Connection};
|
||||
|
||||
#[cfg(feature = "async")]
|
||||
use tokio::runtime::Runtime;
|
||||
use core::future::Future;
|
||||
|
||||
pub mod peer;
|
||||
pub mod connection;
|
||||
pub mod store;
|
||||
pub mod subscription;
|
||||
pub mod updater;
|
||||
pub mod msg_thread;
|
||||
|
||||
static MSG_CHANNEL_SIZE: usize = 200;
|
||||
|
||||
@@ -43,7 +45,8 @@ pub struct Instance {
|
||||
// would mean replication logic and such things is handeled by the instance
|
||||
// i think it would be better thogh to then implement a ReplicatedStore
|
||||
pub store: Box<dyn Store>,
|
||||
peers: Arc<Mutex<Vec<Peer>>>,
|
||||
connections: Arc<Mutex<Vec<Connection>>>,
|
||||
next_con_id: Arc<Mutex<u64>>,
|
||||
subs: Arc<Mutex<HashMap<MizeId, Subscription>>>,
|
||||
pub id_pool: Arc<Mutex<VecStringPool>>,
|
||||
pub namespace_pool: Arc<Mutex<StringPool>>,
|
||||
@@ -69,18 +72,19 @@ impl Instance {
|
||||
let store = MemStore::new();
|
||||
let id_pool = Arc::new(Mutex::new(VecStringPool::default()));
|
||||
let namespace_pool_raw = StringPool::default();
|
||||
let peers = Arc::new(Mutex::new(Vec::new()));
|
||||
let connections = Arc::new(Mutex::new(Vec::new()));
|
||||
let subs = Arc::new(Mutex::new(HashMap::new()));
|
||||
let namespace = Namespace ( namespace_pool_raw.get("mize.default.namespace") );
|
||||
let (op_tx, op_rx) = channel::unbounded();
|
||||
|
||||
let mut instance = Instance {
|
||||
store: Box::new(store),
|
||||
peers, subs, id_pool,
|
||||
connections, subs, id_pool,
|
||||
namespace, op_tx,
|
||||
namespace_pool: Arc::new(Mutex::new(namespace_pool_raw)),
|
||||
context: vec![],
|
||||
threads: vec![],
|
||||
next_con_id: Arc::new(Mutex::new(1)),
|
||||
|
||||
#[cfg(feature = "async")]
|
||||
runtime: Arc::new(Mutex::new(Runtime::new().mize_result_msg("Could not create async runtime")?)),
|
||||
@@ -90,6 +94,11 @@ impl Instance {
|
||||
let closure = move || updater_thread(op_rx, instance_clone);
|
||||
instance.spawn("updater_thread", closure)?;
|
||||
|
||||
// will probably move the msg stuff into it's own thread
|
||||
//let msg_instance_clone = instance.clone();
|
||||
//let msg_closure = move || msg_thread(op_rx, instance_clone);
|
||||
//instance.spawn("msg_thread", closure)?;
|
||||
|
||||
|
||||
return Ok(instance);
|
||||
}
|
||||
@@ -195,16 +204,32 @@ impl Instance {
|
||||
Ok(namespace)
|
||||
}
|
||||
|
||||
pub fn add_listener<T: PeerListener +'static>(&mut self, listener: T) -> MizeResult<()> {
|
||||
pub fn add_listener<T: ConnListener +'static>(&mut self, listener: T) -> MizeResult<()> {
|
||||
let mut instance_clone = self.clone();
|
||||
self.spawn("some_listener", move || listener.listen(instance_clone));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn add_peer(&mut self, peer: Peer) -> MizeResult<()> {
|
||||
let mut peer_inner = self.peers.lock()?;
|
||||
peer_inner.push(peer);
|
||||
Ok(())
|
||||
pub fn new_connection(&mut self, rx: Receiver<MizeMessage>, tx: Sender<MizeMessage>) -> MizeResult<u64> {
|
||||
let mut conn_inner = self.connections.lock()?;
|
||||
let mut next_con_id = self.next_con_id.lock()?;
|
||||
|
||||
let connection = Connection { id: next_con_id.to_owned(), rx, tx};
|
||||
conn_inner.push(connection);
|
||||
*next_con_id += 1;
|
||||
Ok(next_con_id.to_owned())
|
||||
}
|
||||
|
||||
pub fn get_connection(&mut self, conn_id: u64) -> MizeResult<Connection> {
|
||||
let mut conn_inner = self.connections.lock()?;
|
||||
|
||||
for connection in conn_inner.iter() {
|
||||
if connection.id == conn_id {
|
||||
return Ok(connection.clone());
|
||||
}
|
||||
}
|
||||
|
||||
return Err(mize_err!("Connection with id {} not known to instance", conn_id));
|
||||
}
|
||||
|
||||
pub fn spawn(&mut self, name: &str, func: impl FnOnce() -> MizeResult<()> + Send + 'static) -> MizeResult<()> {
|
||||
|
||||
2
src/core/instance/msg_thread.rs
Normal file
2
src/core/instance/msg_thread.rs
Normal file
@@ -0,0 +1,2 @@
|
||||
|
||||
|
||||
@@ -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