Search in sources :

Example 1 with PingThread

use of hudson.remoting.PingThread in project hudson-2.x by hudson.

the class FullDuplexHttpChannel method download.

/**
     * This is where we send the data to the client.
     *
     * <p>
     * If this connection is lost, we'll abort the channel.
     */
public synchronized void download(StaplerRequest req, StaplerResponse rsp) throws InterruptedException, IOException {
    rsp.setStatus(HttpServletResponse.SC_OK);
    // server->client channel.
    // this is created first, and this controls the lifespan of the channel
    rsp.addHeader("Transfer-Encoding", "chunked");
    OutputStream out = rsp.getOutputStream();
    if (DIY_CHUNKING)
        out = new ChunkedOutputStream(out);
    // send something out so that the client will see the HTTP headers
    out.write("Starting HTTP duplex channel".getBytes());
    out.flush();
    // wait until we have the other channel
    while (upload == null) wait();
    try {
        channel = new Channel("HTTP full-duplex channel " + uuid, Computer.threadPoolForRemoting, Mode.BINARY, upload, out, null, restricted);
        // so that we can detect dead clients, periodically send something
        PingThread ping = new PingThread(channel) {

            @Override
            protected void onDead() {
                LOGGER.info("Duplex-HTTP session " + uuid + " is terminated");
                // this will cause the channel to abort and subsequently clean up
                try {
                    upload.close();
                } catch (IOException e) {
                    // this can never happen
                    throw new AssertionError(e);
                }
            }
        };
        ping.start();
        main(channel);
        channel.join();
        ping.interrupt();
    } finally {
        // publish that we are done
        completed = true;
        notify();
    }
}
Also used : PingThread(hudson.remoting.PingThread) OutputStream(java.io.OutputStream) ChunkedOutputStream(hudson.util.ChunkedOutputStream) Channel(hudson.remoting.Channel) ChunkedOutputStream(hudson.util.ChunkedOutputStream) IOException(java.io.IOException)

Aggregations

Channel (hudson.remoting.Channel)1 PingThread (hudson.remoting.PingThread)1 ChunkedOutputStream (hudson.util.ChunkedOutputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1