two sub not-owning instance send update-request, on getting an update

This commit is contained in:
Sebastian Moser
2024-08-21 13:32:55 +02:00
parent f1e7d6ed68
commit fe9b6d8fca
3 changed files with 30 additions and 1 deletions

View File

@@ -229,9 +229,17 @@ impl Instance {
vec.push(sub); vec.push(sub);
}, },
None => { None => {
subs_inner.insert(id, vec![sub]); subs_inner.insert(id.clone(), vec![sub]);
} }
} }
// if we are not the owner of this item, send a sub msg to them
if !self.we_are_namespace()? {
let con = self.get_connection_by_ns(id.namespace())?;
let msg = MizeMessage::new_sub(id, con.id);
con.send(msg)?;
}
Ok(()) Ok(())
} }

View File

@@ -96,6 +96,13 @@ fn handle_msg(msg: &mut MizeMessage, instance: &Instance) -> MizeResult<()> {
instance.sub(id, sub)?; instance.sub(id, sub)?;
}, },
MessageCmd::Sub => {
let id = msg.id(instance)?;
let mut connection = instance.get_connection(msg.conn_id)?.clone();
let sub = Subscription::from_conn(connection.clone());
instance.sub(id, sub)?;
},
MessageCmd::Update => { MessageCmd::Update => {
let data = msg.data()?; let data = msg.data()?;
let id = msg.id(instance)?; let id = msg.id(instance)?;

View File

@@ -26,6 +26,7 @@ static CMD_CREATE: u16 = 4;
static CMD_CREATE_REPLY: u16 = 5; static CMD_CREATE_REPLY: u16 = 5;
static CMD_UPDATE_REQUEST: u16 = 6; static CMD_UPDATE_REQUEST: u16 = 6;
static CMD_GET_SUB: u16 = 7; static CMD_GET_SUB: u16 = 7;
static CMD_SUB: u16 = 8;
#[derive(Debug)] #[derive(Debug)]
pub enum MessageCmd { pub enum MessageCmd {
@@ -36,6 +37,7 @@ pub enum MessageCmd {
CreateReply, CreateReply,
UpdateRequest, UpdateRequest,
GetSub, GetSub,
Sub,
} }
@@ -66,6 +68,16 @@ impl MizeMessage {
MizeMessage::new(value, conn_id) MizeMessage::new(value, conn_id)
} }
pub fn new_sub(id: MizeId, conn_id: u64) -> MizeMessage {
let id_path = id.path().into_iter().map(|string| CborValue::Text(string.to_owned())).collect();
let cmd = (CborValue::Integer(MSG_CMD.into()), CborValue::Integer(CMD_SUB.into()));
let id = (CborValue::Integer(MSG_ID.into()), CborValue::Array(id_path));
let value = CborValue::Map(vec![cmd, id]);
MizeMessage::new(value, conn_id)
}
pub fn new_create(conn_id: u64) -> MizeMessage { pub fn new_create(conn_id: u64) -> MizeMessage {
let cmd = (CborValue::Integer(MSG_CMD.into()), CborValue::Integer(CMD_CREATE.into())); let cmd = (CborValue::Integer(MSG_CMD.into()), CborValue::Integer(CMD_CREATE.into()));
let value = CborValue::Map(vec![cmd]); let value = CborValue::Map(vec![cmd]);
@@ -157,6 +169,8 @@ impl MizeMessage {
4 => MessageCmd::Create, 4 => MessageCmd::Create,
5 => MessageCmd::CreateReply, 5 => MessageCmd::CreateReply,
6 => MessageCmd::UpdateRequest, 6 => MessageCmd::UpdateRequest,
7 => MessageCmd::GetSub,
8 => MessageCmd::Sub,
_ => { _ => {
return Err(MizeError::new().msg("error cmd of msg was not a valid command")); return Err(MizeError::new().msg("error cmd of msg was not a valid command"));
}, },