}
private void send(SipServletMessageImpl sipServletMessage,
boolean useCache, ByteBuffer bb) throws IOException {
TargetTuple tt = getBindTuple(sipServletMessage);
//added this check to prevent FT failures
if(staleConnectionsTimeout > 0 ) {
if ((tt.getProtocol() == SipTransports.UDP_PROT)
&& (staleUDPConnections.get(tt) != null)) {
Long time = staleUDPConnections.get(tt);
if ((System.currentTimeMillis() - time)
>= staleConnectionsTimeout) {
staleUDPConnections.remove(tt);
} else {
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();