package org.eclipse.jetty.io;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.Executor;
import javax.net.ssl.SSLEngine;
import org.eclipse.jetty.util.BufferUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public abstract class NegotiatingClientConnection extends AbstractConnection
{
private static final Logger LOG = LoggerFactory.getLogger(NegotiatingClientConnection.class);
private final SSLEngine engine;
private final ClientConnectionFactory connectionFactory;
private final Map<String, Object> context;
private String protocol;
private volatile boolean completed;
protected NegotiatingClientConnection(EndPoint endPoint, Executor executor, SSLEngine sslEngine, ClientConnectionFactory connectionFactory, Map<String, Object> context)
{
super(endPoint, executor);
this.engine = sslEngine;
this.connectionFactory = connectionFactory;
this.context = context;
}
public SSLEngine getSSLEngine()
{
return engine;
}
public String getProtocol()
{
return protocol;
}
protected void completed(String protocol)
{
this.protocol = protocol;
completed = true;
}
@Override
public void onOpen()
{
super.onOpen();
try
{
getEndPoint().flush(BufferUtil.EMPTY_BUFFER);
if (completed)
replaceConnection();
else
fillInterested();
}
catch (Throwable x)
{
close();
throw new RuntimeIOException(x);
}
}
@Override
public void onFillable()
{
while (true)
{
int filled = fill();
if (completed || filled < 0)
{
replaceConnection();
break;
}
if (filled == 0)
{
fillInterested();
break;
}
}
}
private int fill()
{
try
{
return getEndPoint().fill(BufferUtil.EMPTY_BUFFER);
}
catch (IOException x)
{
LOG.debug("Unable to fill from endpoint", x);
close();
return -1;
}
}
private void replaceConnection()
{
EndPoint endPoint = getEndPoint();
try
{
endPoint.upgrade(connectionFactory.newConnection(endPoint, context));
}
catch (Throwable x)
{
LOG.debug("Unable to replace connection", x);
close();
}
}
@Override
public void close()
{
getEndPoint().shutdownOutput();
super.close();
}
}