diff --git a/src/core/instance/mod.rs b/src/core/instance/mod.rs index f68df8e..06fd2ef 100644 --- a/src/core/instance/mod.rs +++ b/src/core/instance/mod.rs @@ -229,9 +229,17 @@ impl Instance { vec.push(sub); }, 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(()) } diff --git a/src/core/instance/updater.rs b/src/core/instance/updater.rs index 8c51722..e6fe778 100644 --- a/src/core/instance/updater.rs +++ b/src/core/instance/updater.rs @@ -96,6 +96,13 @@ fn handle_msg(msg: &mut MizeMessage, instance: &Instance) -> MizeResult<()> { 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 => { let data = msg.data()?; let id = msg.id(instance)?; diff --git a/src/core/proto/mod.rs b/src/core/proto/mod.rs index 2e9b49c..d0e48c4 100644 --- a/src/core/proto/mod.rs +++ b/src/core/proto/mod.rs @@ -26,6 +26,7 @@ static CMD_CREATE: u16 = 4; static CMD_CREATE_REPLY: u16 = 5; static CMD_UPDATE_REQUEST: u16 = 6; static CMD_GET_SUB: u16 = 7; +static CMD_SUB: u16 = 8; #[derive(Debug)] pub enum MessageCmd { @@ -36,6 +37,7 @@ pub enum MessageCmd { CreateReply, UpdateRequest, GetSub, + Sub, } @@ -66,6 +68,16 @@ impl MizeMessage { 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 { let cmd = (CborValue::Integer(MSG_CMD.into()), CborValue::Integer(CMD_CREATE.into())); let value = CborValue::Map(vec![cmd]); @@ -157,6 +169,8 @@ impl MizeMessage { 4 => MessageCmd::Create, 5 => MessageCmd::CreateReply, 6 => MessageCmd::UpdateRequest, + 7 => MessageCmd::GetSub, + 8 => MessageCmd::Sub, _ => { return Err(MizeError::new().msg("error cmd of msg was not a valid command")); },