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
This commit is contained in:
kohsuke committed 2009-04-28 17:36:50 +00:00
1 parent a540ed2249
commit e2500b07b9
3 files changed
+61 -104

No files matched your search

+1 -1
View File
@@ -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());
@@ -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;
}
@@ -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;
}