From 2ef16646908fec98fdfde2251c0d73ed30a6f511 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Mon, 10 May 2021 01:52:58 +0100 Subject: [PATCH] mgmtchannel: Prepare for progress updates Signed-off-by: Ian Jackson --- src/bin/otter.rs | 4 ++-- src/commands.rs | 7 +++++++ src/mgmtchannel.rs | 20 ++++++++++++++------ 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/bin/otter.rs b/src/bin/otter.rs index f9c9457f..791dc873 100644 --- a/src/bin/otter.rs +++ b/src/bin/otter.rs @@ -1349,7 +1349,7 @@ mod upload_bundle { game: instance_name.clone(), hash: bundles::Hash(hash.into()), kind, }; - chan.cmd_withbulk(&cmd, &mut f, &mut io::sink())?; + chan.cmd_withbulk(&cmd, &mut f, &mut io::sink(), &mut |_|Ok((/*todo*/)))?; } inventory::submit!{Subcommand( @@ -1452,7 +1452,7 @@ mod download_bundle { game: instance_name.clone(), id, }; - chan.cmd_withbulk(&cmd, &mut io::empty(), &mut f) + chan.cmd_withbulk(&cmd, &mut io::empty(), &mut f, &mut |_| Ok(())) .context("download bundle")?; f.flush().context("flush bundle file")?; if let Some((path, tmp)) = path_tmp { diff --git a/src/commands.rs b/src/commands.rs index 0d221c4a..b17cafdc 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -84,6 +84,7 @@ pub struct AccountDetails { #[derive(Debug,Serialize,Deserialize)] pub enum MgmtResponse { Fine, + Progress(ProgressInfo), Error { error: MgmtError }, AlterGame { error: Option, responses: Vec }, AccountsList(Vec>), @@ -133,6 +134,12 @@ pub enum MgmtGameInstruction { // RemovePlayer { player: PlayerId }, todo, does a special setacl } +#[derive(Debug,Clone,Serialize,Deserialize)] +pub struct ProgressInfo { + count: usize, + of: usize, +} + #[derive(Debug,Serialize,Deserialize)] pub struct MgmtPlayerDetails { pub nick: Option, diff --git a/src/mgmtchannel.rs b/src/mgmtchannel.rs index 3869b8bc..016b83b5 100644 --- a/src/mgmtchannel.rs +++ b/src/mgmtchannel.rs @@ -69,17 +69,25 @@ impl MgmtChannel { } #[throws(AE)] - pub fn cmd_withbulk(&mut self, cmd: &MgmtCommand, - up: &mut U, down: &mut D) - -> MgmtResponse - where U: Read, D: Write + pub fn cmd_withbulk(&mut self, cmd: &MgmtCommand, + up: &mut U, down: &mut D, progress: &mut P) + -> MgmtResponse + where U: Read, D: Write, + P: FnMut(ProgressInfo) -> Result<(),AE>, { use MgmtResponse::*; let mut wbulk = self.write.write_withbulk(&cmd).context("send command")?; io::copy(up,&mut wbulk).context("copy bulk upload")?; wbulk.finish().context("finish sending command and data")?; - let (resp, mut rbulk)= self.read.read_withbulk().context("read response")?; + let (mut resp, mut rbulk) = + self.read.read_withbulk() + .context("read response")?; + while let MR::Progress(pi) = resp { + resp = (&mut rbulk).read_rmp()?; + progress(pi)?; + } match &resp { + Progress(_) => panic!(), Fine | AccountsList{..} | GamesList{..} | LibraryItems(_) | Bundles{..} => { }, AlterGame { error: None, .. } => { }, @@ -109,7 +117,7 @@ impl MgmtChannel { #[throws(AE)] pub fn cmd(&mut self, cmd: &MgmtCommand) -> MgmtResponse { - self.cmd_withbulk(cmd, &mut io::empty(), &mut io::sink())? + self.cmd_withbulk(cmd, &mut io::empty(), &mut io::sink(), &mut |_|Ok(()))? } #[throws(AE)] -- 2.30.2