diff options
Diffstat (limited to 'jetty-spdy/spdy-client/src/main/java/org/eclipse/jetty/spdy/client/NegotiatingClientConnection.java')
-rw-r--r-- | jetty-spdy/spdy-client/src/main/java/org/eclipse/jetty/spdy/client/NegotiatingClientConnection.java | 133 |
1 files changed, 133 insertions, 0 deletions
diff --git a/jetty-spdy/spdy-client/src/main/java/org/eclipse/jetty/spdy/client/NegotiatingClientConnection.java b/jetty-spdy/spdy-client/src/main/java/org/eclipse/jetty/spdy/client/NegotiatingClientConnection.java new file mode 100644 index 0000000000..a2fc8511f4 --- /dev/null +++ b/jetty-spdy/spdy-client/src/main/java/org/eclipse/jetty/spdy/client/NegotiatingClientConnection.java @@ -0,0 +1,133 @@ +// +// ======================================================================== +// Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd. +// ------------------------------------------------------------------------ +// All rights reserved. This program and the accompanying materials +// are made available under the terms of the Eclipse Public License v1.0 +// and Apache License v2.0 which accompanies this distribution. +// +// The Eclipse Public License is available at +// http://www.eclipse.org/legal/epl-v10.html +// +// The Apache License v2.0 is available at +// http://www.opensource.org/licenses/apache2.0.php +// +// You may elect to redistribute this code under either of these licenses. +// ======================================================================== +// + +package org.eclipse.jetty.spdy.client; + +import java.io.IOException; +import java.util.Map; +import java.util.concurrent.Executor; +import javax.net.ssl.SSLEngine; + +import org.eclipse.jetty.io.AbstractConnection; +import org.eclipse.jetty.io.ClientConnectionFactory; +import org.eclipse.jetty.io.Connection; +import org.eclipse.jetty.io.EndPoint; +import org.eclipse.jetty.io.RuntimeIOException; +import org.eclipse.jetty.util.BufferUtil; +import org.eclipse.jetty.util.log.Log; +import org.eclipse.jetty.util.log.Logger; + +public abstract class NegotiatingClientConnection extends AbstractConnection +{ + private static final Logger LOG = Log.getLogger(NegotiatingClientConnection.class); + + private final SSLEngine engine; + private final ClientConnectionFactory connectionFactory; + private final Map<String, Object> context; + private volatile boolean completed; + + protected NegotiatingClientConnection(EndPoint endp, Executor executor, SSLEngine sslEngine, ClientConnectionFactory connectionFactory, Map<String, Object> context) + { + super(endp, executor); + this.engine = sslEngine; + this.connectionFactory = connectionFactory; + this.context = context; + } + + protected SSLEngine getSSLEngine() + { + return engine; + } + + protected void completed() + { + completed = true; + } + + @Override + public void onOpen() + { + super.onOpen(); + try + { + getEndPoint().flush(BufferUtil.EMPTY_BUFFER); + if (completed) + replaceConnection(); + else + fillInterested(); + } + catch (IOException x) + { + close(); + throw new RuntimeIOException(x); + } + } + + @Override + public void onFillable() + { + while (true) + { + int filled = fill(); + if (filled == 0 && !completed) + fillInterested(); + if (filled <= 0 || completed) + break; + } + if (completed) + replaceConnection(); + } + + private int fill() + { + try + { + return getEndPoint().fill(BufferUtil.EMPTY_BUFFER); + } + catch (IOException x) + { + LOG.debug(x); + close(); + return -1; + } + } + + private void replaceConnection() + { + EndPoint endPoint = getEndPoint(); + try + { + Connection oldConnection = endPoint.getConnection(); + Connection newConnection = connectionFactory.newConnection(endPoint, context); + ClientConnectionFactory.Helper.replaceConnection(oldConnection, newConnection); + } + catch (Throwable x) + { + LOG.debug(x); + close(); + } + } + + @Override + public void close() + { + // Gentler close for SSL. + getEndPoint().shutdownOutput(); + super.close(); + } +} |