final Condition negotiationComplete = _lock.newCondition();
closeOk = _lock.newCondition();
_lock.lock();
ClientDelegate connectionDelegate = new ClientDelegate()
{
private boolean receivedClose = false;
public SessionDelegate getSessionDelegate()
{
return new ClientSessionDelegate();
}
public void exception(Throwable t)
{
if (_closedListner != null)
{
_closedListner.onClosed(ErrorCode.CONNECTION_ERROR,ErrorCode.CONNECTION_ERROR.getDesc(),t);
}
else
{
throw new RuntimeException("connection closed",t);
}
}
public void closed()
{
if (_closedListner != null && !this.receivedClose)
{
_closedListner.onClosed(ErrorCode.CONNECTION_ERROR,ErrorCode.CONNECTION_ERROR.getDesc(),null);
}
}
@Override public void connectionCloseOk(Channel context, ConnectionCloseOk struct)
{
_lock.lock();
try
{
closed = true;
this.receivedClose = true;
closeOk.signalAll();
}
finally
{
_lock.unlock();
}
}
@Override public void connectionClose(Channel context, ConnectionClose connectionClose)
{
super.connectionClose(context, connectionClose);
ErrorCode errorCode = ErrorCode.get(connectionClose.getReplyCode().getValue());
if (_closedListner == null && errorCode != ErrorCode.NO_ERROR)
{
throw new RuntimeException
(new QpidException("Server closed the connection: Reason " +
connectionClose.getReplyText(),
errorCode,
null));
}
else
{
_closedListner.onClosed(errorCode, connectionClose.getReplyText(),null);
}
this.receivedClose = true;
}
@Override public void init(Channel ch, ProtocolHeader hdr)
{
// TODO: once the merge is done we'll need to update this code
// for handling 0.8 protocol version type i.e. major=8 and mino
if (hdr.getMajor() != 0 || hdr.getMinor() != 10)
{
Client.this.header = hdr;
_lock.lock();
negotiationComplete.signalAll();
_lock.unlock();
}
}
};
connectionDelegate.setCondition(_lock,negotiationComplete);
connectionDelegate.setUsername(username);
connectionDelegate.setPassword(password);
connectionDelegate.setVirtualHost(virtualHost);
String transport = System.getProperty("transport","io");
if (transport.equalsIgnoreCase("nio"))
{
_logger.info("using NIO Transport");