Search in sources :

Example 1 with ResponseExt

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);
}
Also used : SIPResponse(gov.nist.javax.sip.message.SIPResponse) ResponseExt(gov.nist.javax.sip.message.ResponseExt) SipCacheException(org.mobicents.ha.javax.sip.cache.SipCacheException)

Example 2 with ResponseExt

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);
        }
    }
}
Also used : PeerUnavailableException(javax.sip.PeerUnavailableException) Node(org.mobicents.tools.heartbeat.api.Node) ResponseExt(gov.nist.javax.sip.message.ResponseExt) HeaderFactory(javax.sip.header.HeaderFactory) ListeningPoint(javax.sip.ListeningPoint) InvalidArgumentException(javax.sip.InvalidArgumentException) ParseException(java.text.ParseException) TransportNotSupportedException(javax.sip.TransportNotSupportedException) TransportAlreadySupportedException(javax.sip.TransportAlreadySupportedException) ObjectInUseException(javax.sip.ObjectInUseException) PeerUnavailableException(javax.sip.PeerUnavailableException) TooManyListenersException(java.util.TooManyListenersException) SipException(javax.sip.SipException) UnknownHostException(java.net.UnknownHostException) TransactionUnavailableException(javax.sip.TransactionUnavailableException) Response(javax.sip.message.Response) SIPResponse(gov.nist.javax.sip.message.SIPResponse) ReasonHeader(javax.sip.header.ReasonHeader) ContactHeader(javax.sip.header.ContactHeader) ViaHeader(javax.sip.header.ViaHeader) CallIdHeader(javax.sip.header.CallIdHeader) Header(javax.sip.header.Header) MaxForwardsHeader(javax.sip.header.MaxForwardsHeader) ToHeader(javax.sip.header.ToHeader) RouteHeader(javax.sip.header.RouteHeader) FromHeader(javax.sip.header.FromHeader) RecordRouteHeader(javax.sip.header.RecordRouteHeader) ViaHeader(javax.sip.header.ViaHeader) SIPMessage(gov.nist.javax.sip.message.SIPMessage) ParseException(java.text.ParseException) SipException(javax.sip.SipException) SipProvider(javax.sip.SipProvider)

Aggregations

ResponseExt (gov.nist.javax.sip.message.ResponseExt)2 SIPResponse (gov.nist.javax.sip.message.SIPResponse)2 SIPMessage (gov.nist.javax.sip.message.SIPMessage)1 UnknownHostException (java.net.UnknownHostException)1 ParseException (java.text.ParseException)1 TooManyListenersException (java.util.TooManyListenersException)1 InvalidArgumentException (javax.sip.InvalidArgumentException)1 ListeningPoint (javax.sip.ListeningPoint)1 ObjectInUseException (javax.sip.ObjectInUseException)1 PeerUnavailableException (javax.sip.PeerUnavailableException)1 SipException (javax.sip.SipException)1 SipProvider (javax.sip.SipProvider)1 TransactionUnavailableException (javax.sip.TransactionUnavailableException)1 TransportAlreadySupportedException (javax.sip.TransportAlreadySupportedException)1 TransportNotSupportedException (javax.sip.TransportNotSupportedException)1 CallIdHeader (javax.sip.header.CallIdHeader)1 ContactHeader (javax.sip.header.ContactHeader)1 FromHeader (javax.sip.header.FromHeader)1 Header (javax.sip.header.Header)1 HeaderFactory (javax.sip.header.HeaderFactory)1