we connect and get the namespace of the peer

This commit is contained in:
Sebastian Moser
2024-08-14 13:14:54 +02:00
parent ae5bbd4028
commit 13f5598caa
6 changed files with 132 additions and 47 deletions

View File

@@ -12,7 +12,6 @@ use crate::item::{get_raw_from_cbor, Item};
// only created with Instance::new_connection()
#[derive(Clone)]
pub struct Connection {
pub rx: Receiver<MizeMessage>,
pub tx: Sender<MizeMessage>,
pub id: u64,
pub ns: Option<Namespace>,
@@ -46,13 +45,15 @@ pub fn value_raw_from_peer(item: &mut Item) -> MizeResult<Vec<u8>> {
let skipped = new_id_str.skip(4);
let new_id = item.instance.new_id(skipped.collect::<Vec<&String>>())?;
println!("value_raw_from_peer hereeeeeeeeeee");
let connection = item.instance.get_connection(conn_id)?;
println!("value_raw_from_peer afteeeeerrrrrrrrrrrrrrrr");
let msg = MizeMessage::new_get(new_id, connection.id);
let msg = MizeMessage::new_get(new_id.clone(), connection.id);
connection.tx.send(msg)?;
let data = item.instance.give_msg_wait(item.id())?;
let data = item.instance.give_msg_wait(new_id)?;
let raw_data = get_raw_from_cbor(data.cbor(), vec![])?;
return Ok(raw_data.to_owned());

View File

@@ -3,7 +3,7 @@ use std::collections::HashMap;
use std::fs::create_dir;
use std::thread::JoinHandle;
use std::{thread, vec};
use crossbeam::channel::{self, Receiver, Sender};
use crossbeam::channel::{self, bounded, Receiver, Sender};
use std::sync::{Arc, Mutex};
use tracing::{trace, debug, info, warn, error};
use uuid::Uuid;
@@ -23,6 +23,7 @@ use crate::memstore::MemStore;
use crate::instance::updater::Operation;
use crate::mize_err;
use crate::proto::MizeMessage;
use tokio::runtime::Handle;
use self::connection::{ConnListener, Connection};
@@ -56,7 +57,7 @@ pub struct Instance {
threads: Vec<String>,
#[cfg(feature = "async")]
pub runtime: Arc<Mutex<Runtime>>,
give_msg_wait: Arc<Mutex<HashMap<MizeId, (Sender<ItemData>, Receiver<ItemData>)>>>,
give_msg_wait: Arc<Mutex<HashMap<MizeId, Vec<Sender<ItemData>>>>>,
}
pub struct InstanceRef {
@@ -213,14 +214,15 @@ impl Instance {
Ok(())
}
pub fn new_connection(&mut self, rx: Receiver<MizeMessage>, tx: Sender<MizeMessage>) -> MizeResult<u64> {
pub fn new_connection(&mut self, tx: Sender<MizeMessage>) -> MizeResult<u64> {
let mut conn_inner = self.connections.lock()?;
let mut next_con_id = self.next_con_id.lock()?;
let old_next_con_id = *next_con_id;
let connection = Connection { id: next_con_id.to_owned(), rx, tx, ns: None};
let connection = Connection { id: next_con_id.to_owned(), tx, ns: None};
conn_inner.push(connection);
*next_con_id += 1;
Ok(next_con_id.to_owned())
Ok(old_next_con_id)
}
pub fn connection_set_namespace(&mut self, conn_id: u64, namespace: Namespace) -> MizeResult<()> {
@@ -230,6 +232,10 @@ impl Instance {
Ok(())
}
pub(crate) fn got_msg(&mut self, msg: MizeMessage) -> MizeResult<()> {
Ok(self.op_tx.send(Operation::Msg(msg))?)
}
fn set_connection(&mut self, conn_id: u64, new_connection: Connection) -> MizeResult<()> {
let mut conn_inner = self.connections.lock()?;
@@ -248,6 +254,7 @@ impl Instance {
let mut conn_inner = self.connections.lock()?;
for connection in conn_inner.iter() {
println!("get_connection iterrrrrrrrrrrrrr");
if connection.id == conn_id {
return Ok(connection.clone());
}
@@ -266,18 +273,20 @@ impl Instance {
let mut give_msg_wait_inner = self.give_msg_wait.lock()?;
let rx = match give_msg_wait_inner.get(&id) {
// if this id is already in the process of being requested, just use the rx already there
Some((tx, rx)) => rx.to_owned(),
let (tx, rx) = bounded::<ItemData>(1);
let vec = match give_msg_wait_inner.get_mut(&id) {
Some(vec) => vec,
None => {
let (tx, rx) = crossbeam::channel::bounded::<ItemData>(1);
give_msg_wait_inner.insert(id, (tx, rx.clone()));
rx
},
give_msg_wait_inner.insert(id.clone(), Vec::new());
give_msg_wait_inner.get_mut(&id).unwrap()
}
};
vec.push(tx);
// so that another thread can also give_msg_wait(), while we wait in the recv() of rx
drop(vec);
drop(give_msg_wait_inner);
let data = rx.recv()?;
@@ -291,15 +300,27 @@ impl Instance {
let runtime_inner = self.runtime.lock().unwrap();
runtime_inner.spawn(func);
}
#[cfg(feature = "async")]
pub fn async_get_handle(&mut self) -> Handle {
let runtime_inner = self.runtime.lock().unwrap();
let handle = runtime_inner.handle().to_owned();
handle
}
#[cfg(feature = "async")]
pub fn spawn_async_blocking<F: Future<Output = impl Send + Sync + 'static> + Send + Sync + 'static>(&mut self, name: &str, func: F) {
pub fn spawn_async_blocking<F: Future<Output = impl Send + Sync + 'static> + Send + Sync + 'static>(&mut self, name: &str, func: F) -> F::Output {
use std::process::Output;
self.threads.push(name.to_owned());
let runtime_inner = self.runtime.lock().unwrap();
runtime_inner.block_on(func);
let handle = runtime_inner.handle().to_owned();
drop(runtime_inner);
let result = handle.block_on(func);
return result;
}
pub fn wait(&self) {
info!("Instance main thread waiting");
loop {
thread::sleep_ms(10000000)
}

View File

@@ -1,5 +1,5 @@
use crossbeam::channel::Receiver;
use tracing::{trace, error};
use tracing::{error, trace, warn};
use crate::mize_err;
use crate::{instance::Instance, item::ItemData};
@@ -23,6 +23,7 @@ pub fn updater_thread(operation_rx : Receiver<Operation>, mut instance: Instance
if let Err(err) = result {
error!("OPERATION FAILED: {:?}", operation);
err.log();
}
}
Ok(())
@@ -57,6 +58,18 @@ fn handle_msg(msg: &mut MizeMessage, instance: &mut Instance) -> MizeResult<()>
let id = msg.id(instance)?;
instance.set(id, data);
},
MessageCmd::Give => {
let id = msg.id(instance)?;
let data = msg.data()?;
let give_msg_wait_inner = instance.give_msg_wait.lock()?;
if let Some(vec) = give_msg_wait_inner.get(&id) {
for tx in vec {
tx.send(data.clone());
}
} else {
warn!("got give msg for id '{}', that has no one waiting for it", id);
}
},
_ => {
return Err(mize_err!("got a message, that is not handeled"));
}

View File

@@ -81,6 +81,7 @@ impl MizeMessage {
let cmd = match val_of_c {
1 => MessageCmd::Get,
2 => MessageCmd::Set,
3 => MessageCmd::Give,
_ => {
return Err(MizeError::new().msg("error cmd of msg was not a valid command"));
},
@@ -104,7 +105,7 @@ impl MizeMessage {
for (key, val) in msg_as_map {
if let CborValue::Integer(key_int) = key {
// number 2 indicates the id field
let two: Integer = 1.into();
let two: Integer = 2.into();
if key_int == &two {
data = val;
}
@@ -170,6 +171,7 @@ impl MizeMessage {
pub enum MessageCmd {
Get,
Set,
Give,
}