use of gov.nist.javax.sip.message.ResponseExt in project jain-sip.ha by RestComm.
the class MobicentsHASIPServerTransaction method sendMessage.
@Override
public void sendMessage(SIPMessage message) throws IOException {
final SIPResponse response = (SIPResponse) message;
if (response != null && Request.INVITE.equals(getMethod()) && response.getStatusCode() > 100 && response.getStatusCode() < 200) {
this.localDialogId = response.getDialogId(true);
if (logger.isLoggingEnabled(StackLogger.TRACE_DEBUG)) {
logger.logDebug(transactionId + " : local dialog Id " + localDialogId);
}
if (isReliable()) {
this.peerReliablePort = ((ResponseExt) response).getTopmostViaHeader().getPort();
if (logger.isLoggingEnabled(StackLogger.TRACE_DEBUG)) {
logger.logDebug(transactionId + " : peer Reliable Port " + peerReliablePort);
}
}
// store the tx when the response will be sent
try {
((ClusteredSipStack) sipStack).getSipCache().putServerTransaction(this);
} catch (SipCacheException e) {
logger.logError("problem storing server transaction " + transactionId + " into the distributed cache", e);
}
}
super.sendMessage(message);
}
use of gov.nist.javax.sip.message.ResponseExt in project load-balancer by RestComm.
the class SIPBalancerForwarder method processResponse.
/**
* @param originalRequest
* @param serverTransaction
* @throws ParseException
* @throws SipException
* @throws InvalidArgumentException
* @throws TransactionUnavailableException
*/
/*
* (non-Javadoc)
* @see javax.sip.SipListener#processResponse(javax.sip.ResponseEvent)
*/
public void processResponse(ResponseEvent responseEvent) {
BalancerAppContent content = (BalancerAppContent) responseEvent.getSource();
boolean isIpv6 = content.isIpv6();
SipProvider sipProvider = content.getProvider();
Response originalResponse = responseEvent.getResponse();
if (logger.isDebugEnabled()) {
logger.debug("got response :\n" + originalResponse);
}
updateStats(originalResponse);
final Response response = (Response) originalResponse;
Node senderNode = getSenderNode(response);
if (senderNode != null) {
if (logger.isDebugEnabled()) {
logger.debug("Updating Timestamp of sendernode: " + senderNode);
}
senderNode.updateTimerStamp();
}
// Topmost via headers is me. As it is response to external request
ViaHeader viaHeader = (ViaHeader) response.getHeader(ViaHeader.NAME);
String branch = viaHeader.getBranch();
int versionDelimiter = branch.lastIndexOf('_');
String version = branch.substring(versionDelimiter + 1);
InvocationContext ctx = balancerRunner.getInvocationContext(version);
if (viaHeader != null && !isHeaderExternal(viaHeader.getHost(), viaHeader.getPort(), viaHeader.getTransport(), isIpv6)) {
response.removeFirst(ViaHeader.NAME);
}
viaHeader = (ViaHeader) response.getHeader(ViaHeader.NAME);
String transport = viaHeader.getTransport();
if (viaHeader != null && !isHeaderExternal(viaHeader.getHost(), viaHeader.getPort(), viaHeader.getTransport(), isIpv6)) {
response.removeFirst(ViaHeader.NAME);
}
boolean fromServer = false;
if (balancerRunner.balancerContext.isTwoEntrypoints()) {
if (!isIpv6)
fromServer = sipProvider.equals(balancerRunner.balancerContext.internalSipProvider);
else
fromServer = sipProvider.equals(balancerRunner.balancerContext.internalIpv6SipProvider);
if (logger.isDebugEnabled()) {
if (!isIpv6)
logger.debug("fromServer : " + fromServer + ", sipProvider " + sipProvider + ", internalSipProvider " + balancerRunner.balancerContext.internalSipProvider);
else
logger.debug("fromServer : " + fromServer + ", sipProvider " + sipProvider + ", internalIpv6SipProvider " + balancerRunner.balancerContext.internalIpv6SipProvider);
}
} else {
fromServer = senderNode == null;
if (logger.isDebugEnabled()) {
logger.debug("fromServer : " + fromServer + ", senderNode " + senderNode);
}
}
// only for external responses
if (balancerRunner.balancerContext.isUseWithNexmo && !fromServer) {
viaHeader = (ViaHeader) response.getHeader(ViaHeader.NAME);
if (viaHeader != null) {
if (logger.isDebugEnabled())
logger.debug("We are going to remove rport and received parametres from :" + viaHeader + " from external response");
response.removeFirst(ViaHeader.NAME);
viaHeader.removeParameter("rport");
viaHeader.removeParameter("received");
try {
response.addFirst(viaHeader);
} catch (NullPointerException | SipException e) {
e.printStackTrace();
}
if (logger.isDebugEnabled())
logger.debug("After removing :" + response);
}
}
if (fromServer) {
if (senderNode != null && senderNode.getIp() != null) {
if (balancerRunner.balancerContext.maxRequestNumberWithoutResponse != null && balancerRunner.balancerContext.maxResponseTime != null) {
KeySip keySip = new KeySip(senderNode, isIpv6);
// adding null check for https://github.com/RestComm/load-balancer/issues/83
Node currNode = ctx.sipNodeMap(isIpv6).get(keySip);
if (currNode != null) {
if (logger.isDebugEnabled())
logger.debug("We are going to reset counters of health check" + currNode.getRequestNumberWithoutResponse() + " : " + currNode.getLastTimeResponse());
currNode.setLastTimeResponse(System.currentTimeMillis());
currNode.setRequestNumberWithoutResponse(0);
if (logger.isDebugEnabled())
logger.debug("Counter of request without responses is : " + currNode.getRequestNumberWithoutResponse() + " : " + currNode.getLastTimeResponse());
} else {
logger.warn("Node is null, we will not reset counters");
}
}
mediaFailureDetection(response, ctx, senderNode);
}
/*
if("true".equals(balancerRunner.balancerContext.properties.getProperty("removeNodesOn500Response")) && response.getStatusCode() == 500) {
// If the server is broken remove it from the list and try another one with the next retransmission
if(!(sourceNode instanceof ExtraServerNode)) {
if(balancerRunner.balancerContext.nodes.size()>1) {
balancerRunner.balancerContext.nodes.remove(sourceNode);
balancerRunner.balancerContext.balancerAlgorithm.nodeRemoved(sourceNode);
}
}
}
*/
String publicIp = null;
if (!isIpv6)
publicIp = balancerRunner.balancerContext.publicIP;
else
publicIp = balancerRunner.balancerContext.publicIPv6;
if (publicIp != null && publicIp.trim().length() > 0) {
if (logger.isDebugEnabled()) {
logger.debug("Will add Record-Route header to response with public IP Address: " + publicIp);
}
patchSipMessageForNAT(response, isIpv6);
}
// https://github.com/RestComm/load-balancer/issues/45 Adding sender node for the algorithm to be available
((ResponseExt) response).setApplicationData(senderNode);
ctx.balancerAlgorithm.processInternalResponse(response, isIpv6);
try {
if (logger.isDebugEnabled()) {
logger.debug("from server sending response externally " + response);
}
if (!isIpv6)
balancerRunner.balancerContext.externalSipProvider.sendResponse(response);
else
balancerRunner.balancerContext.externalIpv6SipProvider.sendResponse(response);
} catch (Exception ex) {
logger.error("Unexpected exception while forwarding the response \n" + response, ex);
}
} else {
try {
SIPMessage message = (SIPMessage) response;
String initialRemoteAddr = message.getPeerPacketSourceAddress().getHostAddress();
String initialRemotePort = String.valueOf(message.getPeerPacketSourcePort());
Header remoteAddrHeader = null;
Header remotePortHeader = null;
try {
HeaderFactory hf = SipFactory.getInstance().createHeaderFactory();
remoteAddrHeader = hf.createHeader("X-Sip-Balancer-InitialRemoteAddr", initialRemoteAddr);
remotePortHeader = hf.createHeader("X-Sip-Balancer-InitialRemotePort", initialRemotePort);
} catch (PeerUnavailableException e) {
logger.error("Unexpected exception while creating custom headers for REGISTER message ", e);
} catch (ParseException e) {
logger.error("Unexpected exception while creating custom headers for REGISTER message ", e);
}
if (remoteAddrHeader != null)
response.addHeader(remoteAddrHeader);
if (remotePortHeader != null)
response.addHeader(remotePortHeader);
if (balancerRunner.balancerContext.isTwoEntrypoints()) {
ctx.balancerAlgorithm.processExternalResponse(response, isIpv6);
if (logger.isDebugEnabled()) {
logger.debug("two entry points: from external sending response " + response);
}
if (!isIpv6)
balancerRunner.balancerContext.internalSipProvider.sendResponse(response);
else
balancerRunner.balancerContext.internalIpv6SipProvider.sendResponse(response);
} else {
if (!comesFromInternalNode(response, ctx, initialRemoteAddr, message.getPeerPacketSourcePort(), transport, isIpv6))
ctx.balancerAlgorithm.processExternalResponse(response, isIpv6);
else
ctx.balancerAlgorithm.processInternalResponse(response, isIpv6);
if (logger.isDebugEnabled()) {
logger.debug("one entry point: from external sending response " + response);
}
if (!isIpv6)
balancerRunner.balancerContext.externalSipProvider.sendResponse(response);
else
balancerRunner.balancerContext.externalIpv6SipProvider.sendResponse(response);
}
} catch (Exception ex) {
logger.error("Unexpected exception while forwarding the response \n" + response, ex);
}
}
}
Aggregations