From 6b5f0c0f9ddfac9c052210c5dbf3224020646127 Mon Sep 17 00:00:00 2001 From: Dylan McKay Date: Sat, 22 Dec 2018 23:29:45 +1300 Subject: Support interactive stdin/stdout streams (#136) * Support interactive stdin/stdout streams This adds support for streaming stdin, stderr, and stdout independently to a running container. The underlying API is futures-based, meaning the code is implemented asynchronously. A synchronous API is also exposed, which is implemented by simply waiting on the asynchronous API futures. This also modifies the existing Tty logic so that the storage type of the data is a Vec rather than a String. This is also how the Rust standard library persists data from the standard streams. In my particular application, I'm using stdin/stdout as the communication method between a container a host application. In it, a byte-based protocol is used. Streaming works by performing a TCP upgrade; upgrading a higher-level HTTP connection to a lower-level TCP byte stream upon agreement with the server. Docker will automatically upgrade HTTP container log requests to TCP byte streams of a custom std{in,out,err} multiplexing protocol if the client requests it with the 'Connection: Upgrade' header. * Return an error rather than panic when Docker refuses to upgrade to TCP * Add interpret-as-string accessors to tty::Chunk Also updates the examples to use them. --- examples/containerexec.rs | 9 +++++---- examples/logs.rs | 9 +++++---- 2 files changed, 10 insertions(+), 8 deletions(-) (limited to 'examples') diff --git a/examples/containerexec.rs b/examples/containerexec.rs index 0b3575b..0bfd640 100644 --- a/examples/containerexec.rs +++ b/examples/containerexec.rs @@ -25,10 +25,11 @@ fn main() { .containers() .get(&id) .exec(&options) - .for_each(|line| { - match line.stream_type { - StreamType::StdOut => println!("Stdout: {}", line.data), - StreamType::StdErr => eprintln!("Stderr: {}", line.data), + .for_each(|chunk| { + match chunk.stream_type { + StreamType::StdOut => println!("Stdout: {}", chunk.as_string_lossy()), + StreamType::StdErr => eprintln!("Stderr: {}", chunk.as_string_lossy()), + StreamType::StdIn => unreachable!(), } Ok(()) }) diff --git a/examples/logs.rs b/examples/logs.rs index b35b0a3..6fad7d6 100644 --- a/examples/logs.rs +++ b/examples/logs.rs @@ -14,10 +14,11 @@ fn main() { .containers() .get(&id) .logs(&LogsOptions::builder().stdout(true).stderr(true).build()) - .for_each(|line| { - match line.stream_type { - StreamType::StdOut => println!("Stdout: {}", line.data), - StreamType::StdErr => eprintln!("Stderr: {}", line.data), + .for_each(|chunk| { + match chunk.stream_type { + StreamType::StdOut => println!("Stdout: {}", chunk.as_string_lossy()), + StreamType::StdErr => eprintln!("Stderr: {}", chunk.as_string_lossy()), + StreamType::StdIn => unreachable!(), } Ok(()) }) -- cgit v1.2.3