From e2500b07b9717aac6e7159d94dea35058c0806a4 Mon Sep 17 00:00:00 2001 From: kohsuke Date: Tue, 28 Apr 2009 17:36:50 +0000 Subject: [PATCH] throwing away much of the early experiment git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@17576 71c3de6d-444a-0410-be80-ed276b4c234a --- cli/src/main/java/hudson/cli/CLI.java | 2 +- .../hudson/cli/ChunkedHttpStreamPair.java | 103 ------------------ .../java/hudson/cli/FullDuplexHttpStream.java | 60 ++++++++++ 3 files changed, 61 insertions(+), 104 deletions(-) delete mode 100644 cli/src/main/java/hudson/cli/ChunkedHttpStreamPair.java create mode 100644 cli/src/main/java/hudson/cli/FullDuplexHttpStream.java diff --git a/cli/src/main/java/hudson/cli/CLI.java b/cli/src/main/java/hudson/cli/CLI.java index 4ea678be36..6f0cb710b8 100644 --- a/cli/src/main/java/hudson/cli/CLI.java +++ b/cli/src/main/java/hudson/cli/CLI.java @@ -14,7 +14,7 @@ import java.lang.reflect.Method; public class CLI { public static void main(final String[] args) throws Exception { URL target = new URL("http://localhost:8080/duplexChannel"); - ChunkedHttpStreamPair con = new ChunkedHttpStreamPair(target); + FullDuplexHttpStream con = new FullDuplexHttpStream(target); ExecutorService pool = Executors.newCachedThreadPool(); Channel channel = new Channel("Chunked connection to "+target, pool,con.getInputStream(),con.getOutputStream()); diff --git a/cli/src/main/java/hudson/cli/ChunkedHttpStreamPair.java b/cli/src/main/java/hudson/cli/ChunkedHttpStreamPair.java deleted file mode 100644 index 37f18a4ce4..0000000000 --- a/cli/src/main/java/hudson/cli/ChunkedHttpStreamPair.java +++ /dev/null @@ -1,103 +0,0 @@ -package hudson.cli; - -import hudson.cli.SequenceOutputStream.Block; - -import java.io.FilterOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.net.HttpURLConnection; -import java.net.URL; -import java.util.UUID; - -/** - * Creates a capacity-unlimited bi-directional {@link InputStream}/{@link OutputStream} pair over - * HTTP, which is a request/response protocol. - * - * @author Kohsuke Kawaguchi - */ -public class ChunkedHttpStreamPair { - private final URL target; - /** - * Uniquely identifies this connection, so that the server can bundle separate HTTP requests together. - */ - private final UUID uuid = UUID.randomUUID(); - - private final OutputStream output; - private final InputStream input; - - public InputStream getInputStream() { - return input; - } - - public OutputStream getOutputStream() { - return output; - } - - public ChunkedHttpStreamPair(URL target) throws IOException { - this.target = target; - - // server->client side is very simple - HttpURLConnection con = (HttpURLConnection) target.openConnection(); - con.setDoOutput(true); // request POST - con.setRequestMethod("POST"); - con.addRequestProperty("Session",uuid.toString()); - con.addRequestProperty("Side","download"); - con.getOutputStream().close(); - input = con.getInputStream(); - - // client->server needs to be chopped up into blocks since URLConnection - // doesn't allow POST without Content-Length -// output = new SequenceOutputStream(new HttpBlock(makeConnection())) { -// protected Block next(Block current) throws IOException { -// // wait for the server to finish the response -// // before initiating the next connection. This guarantees -// // that the uploaded data is handled in-order by the server -// ((HttpBlock)current).close(); -// -// return new HttpBlock(makeConnection()); -// } -// }; - - // chunked encoding - con = (HttpURLConnection) target.openConnection(); - con.setDoOutput(true); // request POST - con.setRequestMethod("POST"); - con.setChunkedStreamingMode(0); - con.addRequestProperty("Session",uuid.toString()); - con.addRequestProperty("Side","upload"); - output = con.getOutputStream(); - } - - private class HttpBlock extends Block { - private final HttpURLConnection con; - - private HttpBlock(HttpURLConnection con) throws IOException { - super(new FilterOutputStream(con.getOutputStream()) { - public void write(byte[] b, int off, int len) throws IOException { - out.write(b, off, len); - } - }, BLOCK_SIZE); - this.con = con; - } - - void close() throws IOException { - con.getInputStream().read(); - } - } - - private HttpURLConnection makeConnection() throws IOException { - HttpURLConnection con = (HttpURLConnection) target.openConnection(); - con.setDoOutput(true); // request POST - con.setRequestMethod("POST"); - con.addRequestProperty("User-Agent","Hudson"); - con.addRequestProperty("Session",uuid.toString()); - con.addRequestProperty("Side","upload"); - con.setFixedLengthStreamingMode(BLOCK_SIZE); - con.connect(); - - return con; - } - - static final int BLOCK_SIZE = 1024; -} diff --git a/cli/src/main/java/hudson/cli/FullDuplexHttpStream.java b/cli/src/main/java/hudson/cli/FullDuplexHttpStream.java new file mode 100644 index 0000000000..3632766c1b --- /dev/null +++ b/cli/src/main/java/hudson/cli/FullDuplexHttpStream.java @@ -0,0 +1,60 @@ +package hudson.cli; + +import hudson.cli.SequenceOutputStream.Block; + +import java.io.FilterOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.UUID; + +/** + * Creates a capacity-unlimited bi-directional {@link InputStream}/{@link OutputStream} pair over + * HTTP, which is a request/response protocol. + * + * @author Kohsuke Kawaguchi + */ +public class FullDuplexHttpStream { + private final URL target; + /** + * Uniquely identifies this connection, so that the server can bundle separate HTTP requests together. + */ + private final UUID uuid = UUID.randomUUID(); + + private final OutputStream output; + private final InputStream input; + + public InputStream getInputStream() { + return input; + } + + public OutputStream getOutputStream() { + return output; + } + + public FullDuplexHttpStream(URL target) throws IOException { + this.target = target; + + // server->client + HttpURLConnection con = (HttpURLConnection) target.openConnection(); + con.setDoOutput(true); // request POST to avoid caching + con.setRequestMethod("POST"); + con.addRequestProperty("Session",uuid.toString()); + con.addRequestProperty("Side","download"); + con.getOutputStream().close(); + input = con.getInputStream(); + + // client->server uses chunked encoded POST for unlimited capacity. + con = (HttpURLConnection) target.openConnection(); + con.setDoOutput(true); // request POST + con.setRequestMethod("POST"); + con.setChunkedStreamingMode(0); + con.addRequestProperty("Session",uuid.toString()); + con.addRequestProperty("Side","upload"); + output = con.getOutputStream(); + } + + static final int BLOCK_SIZE = 1024; +}