throw new ConnectException("Connection refused to: " +
tt.getSocketAddress());
}
}
}
ConnectorHandler handler = null;
if (useCache) {
handler = streams.get(tt);
if ((handler != null) &&
(handler.getUnderlyingChannel() == null ||
!handler.getUnderlyingChannel().isOpen() ||
handler.getSelectorHandler() == null ||
handler.getSelectorHandler().getAsyncQueueWriter() ==
null)) {
// none of these warning should ever happen
/*
* The following checks are to ensure that we do not
* use stale connections after a listener has been
* disabled and then enabled.
*/
if (handler.getUnderlyingChannel() == null) {
if (logger.isLoggable(Level.FINEST)) {
logger.log(Level.FINEST,
"discarding a connector with a null channel " +
"for " + tt.toString());
}
}
if (handler.getSelectorHandler() == null) {
if (logger.isLoggable(Level.FINEST)) {
logger.log(Level.FINEST,
"discarding a connector with a null " +
"selector handler for " + tt.toString());
}
} else if (handler.getSelectorHandler().getAsyncQueueWriter() ==
null) {
if (logger.isLoggable(Level.FINEST)) {
logger.log(Level.FINEST,
"discarding a connector with a null async " +
"writer for " + tt.toString());
}
}
try {
handler.close();
} catch (Exception e) {
if (logger.isLoggable(Level.FINE)) {
logger.log(Level.FINE,
"sip.network.grizzly.handler.close.error",
new Object[]{handler});
}
}
connectionManager.remove(tt); //Cleanup a broken connection
handler = null;
}
}
if (handler == null) {
handler = createConnectorHandler(tt, sipServletMessage);
}
// The connection failed, returning null.
if (handler == null && useCache) {
if (sipServletMessage.getRetryPolicy() ==
SipServletMessageImpl.RetryPolicy.RESOLVE_AND_RETRY) {
if (sipServletMessage.getMessageType() ==
SipServletMessageImpl.SipMessageType.SipResponse) {
try {
resolve((SipServletResponseImpl) sipServletMessage);
//try again
tt = getBindTuple(sipServletMessage);
handler = createConnectorHandler(tt, sipServletMessage);
} catch (Exception e) {
logSendFailed((SipServletResponseImpl) sipServletMessage,
e);
}
}
} else {
//then we have already tried with the first createConnectorHandler(...) invocation
}
}
//after any potential retry
if (handler == null) {
throw new ConnectException("Connection refused to: " +
tt.getSocketAddress());
}
boolean cacheBuffer = false;
if (bb == null) {
sipServletMessage.toBufferInit();
bb = toBuffer(sipServletMessage);
cacheBuffer = true;
}
if (!bb.hasRemaining()) {
if (logger.isLoggable(Level.SEVERE)) {
logger.log(Level.SEVERE,
"sip.stack.network.invalid_bytebuffer");
}
return;
}
try {
if (sipServletMessage.toBufferHasRemaining()) {
synchronized (handler.getUnderlyingChannel()) {
writeMessage(bb, handler, tt);
while (sipServletMessage.toBufferHasRemaining()) {
//This is a big message
bb = toBuffer(sipServletMessage);
writeMessage(bb, handler, tt);
}
}
} else {
writeMessage(bb, handler, tt);
}
} catch (Exception ex) {
if (logger.isLoggable(Level.SEVERE)) {
logger.log(Level.SEVERE, "sip.network.grizzly.write.failed",
new Object[]{tt, ex.getMessage()});
}
try {
handler.close();
} catch (Exception e) {
if (logger.isLoggable(Level.FINEST)) {
logger.log(Level.FINEST,
"sip.network.grizzly.handler.close.error",
new Object[]{handler});