chiark / gitweb /
mgmtchannels: Genericise again
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Mon, 31 May 2021 20:24:54 +0000 (21:24 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Mon, 31 May 2021 20:24:54 +0000 (21:24 +0100)
Client wants to be Box<dyn Read> etc. so it can be ssh pipes.

Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
apitest/apitest.rs
daemon/cmdlistener.rs
src/bin/otter.rs
src/mgmtchannel.rs
src/prelude.rs
src/timedfd.rs

index 2a34fa695b2f67f7daaee22230e99efca967af33..d1db9bad7fda3e2595a4544e164ed900199f9b3a 100644 (file)
@@ -21,6 +21,8 @@ pub use serde_json::json;
 pub use structopt::StructOpt;
 pub use reqwest;
 
+pub type MgmtChannel = ClientMgmtChannel;
+
 pub type JsV = serde_json::Value;
 pub type MC = MgmtCommand;
 
index 31c606eedead4b9296ed5b82e398f348d61611eb..744dfafaf3bbc2ba39a879e37a97dbbca88b4530 100644 (file)
@@ -39,7 +39,7 @@ pub struct CommandListener {
 }
 
 struct CommandStream<'d> {
-  chan: MgmtChannel,
+  chan: MgmtChannel<TimedFdReader, TimedFdWriter>,
   d: CommandStreamData<'d>,
 }
 
@@ -1535,7 +1535,7 @@ impl CommandListener {
         })().unwrap_or_else(|e| format!("<error: {}>", e));
         write!(&mut desc, " user={}", user_desc)?;
 
-        let chan = MgmtChannel::new(conn)?;
+        let chan = MgmtChannel::new_timed(conn)?;
 
         let d = CommandStreamData {
           account: None, desc: &desc,
index c3f2e94d6af95d91ad882278556df7307c33fb12..4368dccd56823d22043ce77da888adcd3dc0fe88 100644 (file)
@@ -4,6 +4,8 @@
 
 #![allow(unused_imports)]
 
+pub type MgmtChannel = ClientMgmtChannel;
+
 // xxx ssh keys: need a force option to set key for non ssh: account
 
 use otter::imports::*;
@@ -515,7 +517,7 @@ fn main() {
 }
 
 struct Conn {
-  chan: MgmtChannel,
+  chan: ClientMgmtChannel,
 }
 
 deref_to_field_mut!{Conn, MgmtChannel, chan}
index 473fdde1e6baadb8d4ac850ccbfcaf0b49b731eb..0d93704487d757f7d771f9120981de3499774011 100644 (file)
@@ -34,32 +34,32 @@ impl From<rmp_serde::encode::Error> for MgmtChannelWriteError {
   }
 }
 
-pub struct MgmtChannel {
-  pub read:  FrameReader<TimedFdReader>,
-  pub write: FrameWriter<TimedFdWriter>,
+pub struct MgmtChannel<R:Read, W:Write> {
+  pub read:  FrameReader<R>,
+  pub write: FrameWriter<W>,
 }
 
-impl Debug for MgmtChannel{ 
+impl<R,W> Debug for MgmtChannel<R,W> where R: Read, W: Write {
   #[throws(fmt::Error)]
   fn fmt(&self, f: &mut fmt::Formatter) {
     f.write_str("MgmtChannel{...}")?
   }
 }
 
-impl MgmtChannel {
-  pub const PROGRESS: ProgressUpdateMode = PUM::Duplex;
+pub type ClientMgmtChannel = MgmtChannel<TimedFdReader,TimedFdWriter>;
 
+impl MgmtChannel<TimedFdReader,TimedFdWriter> {
   #[throws(AE)]
-  pub fn connect(socket_path: &str) -> MgmtChannel {
+  pub fn connect(socket_path: &str) -> Self {
     let unix = UnixStream::connect(socket_path)
       .with_context(||socket_path.to_owned())
       .context("connect to server")?; 
-    let chan = MgmtChannel::new(unix)?;
+    let chan = MgmtChannel::new_timed(unix)?;
     chan
   }
 
   #[throws(AE)]
-  pub fn new<U>(conn: U) -> MgmtChannel
+  pub fn new_timed<U>(conn: U) -> Self
   where U: IoTryClone + Read + Write + IntoRawFd + Send + 'static,
   {
     let read = conn.try_clone().context("dup the command stream")?;
@@ -69,6 +69,16 @@ impl MgmtChannel {
     let write = FrameWriter::new(write);
     MgmtChannel { read, write }
   }
+}
+
+impl<R,W> MgmtChannel<R,W> where R: Read, W: Write + Send {
+  pub fn read_inner_mut(&mut self) -> &mut R {
+    self.read.inner_mut()
+  }
+}
+
+impl ClientMgmtChannel {
+  pub const PROGRESS: ProgressUpdateMode = PUM::Duplex;
 
   #[throws(AE)]
   pub fn cmd_withbulk<U,D>(&mut self, cmd: &MgmtCommand,
@@ -146,10 +156,6 @@ impl MgmtChannel {
                       &mut termprogress::Null)?
   }
 
-  pub fn read_inner_mut(&mut self) -> &mut TimedFdReader {
-    self.read.inner_mut()
-  }
-
   pub fn for_game(self, game: InstanceName, how: MgmtGameUpdateMode)
                   -> MgmtChannelForGame {
     MgmtChannelForGame {
@@ -170,11 +176,11 @@ impl IoTryClone for UnixStream {
 
 #[derive(Debug)]
 pub struct MgmtChannelForGame {
-  pub chan: MgmtChannel,
+  pub chan: ClientMgmtChannel,
   pub game: InstanceName,
   pub how: MgmtGameUpdateMode,
 }
-deref_to_field_mut!{MgmtChannelForGame, MgmtChannel, chan}
+deref_to_field_mut!{MgmtChannelForGame, ClientMgmtChannel, chan}
 
 impl MgmtChannelForGame {
   #[throws(AE)]
index 3a572af3b6d6c1804f634bd4e9ce317f4b6df6cb..ef16284279100e090be3013811328d9a282ba39f 100644 (file)
@@ -144,8 +144,8 @@ pub use crate::gamestate::*;
 pub use crate::global::*;
 pub use crate::hidden::*;
 pub use crate::keydata::*;
-pub use crate::mgmtchannel::*;
 pub use crate::nwtemplates;
+pub use crate::mgmtchannel::*;
 pub use crate::occultilks::*;
 pub use crate::organise;
 pub use crate::packetframe::{FrameReader, FrameWriter, ReadFrame, WriteFrame};
index b592d72284e348928607869dac384aa02b547325..641b1fbad41940cce2b035829213679771e67e4a 100644 (file)
@@ -165,3 +165,4 @@ impl Drop for Fd {
     if fd >= 2 { let _ = nix::unistd::close(fd); }
   }
 }
+