Revision: 15234
Author: vladimir.ralev
Date: Tue Nov 16 00:54:41 2010
Log: Update Issue 1852

Backport all revisions to JBCP 5
http://code.google.com/p/mobicents/source/detail?r=15234

Modified:
/branches/jbcp/5.x-mead/sip-servlets/sip-servlets-impl/src/main/java/org/mobicents/servlet/sip/core/SipApplicationDispatcherImpl.java /branches/jbcp/5.x-mead/sip-servlets/sip-servlets-impl/src/main/java/org/mobicents/servlet/sip/proxy/ProxyBranchImpl.java /branches/jbcp/5.x-mead/sip-servlets/sip-servlets-impl/src/main/java/org/mobicents/servlet/sip/proxy/ProxyImpl.java

=======================================
--- /branches/jbcp/5.x-mead/sip-servlets/sip-servlets-impl/src/main/java/org/mobicents/servlet/sip/core/SipApplicationDispatcherImpl.java Mon Nov 15 09:43:41 2010 +++ /branches/jbcp/5.x-mead/sip-servlets/sip-servlets-impl/src/main/java/org/mobicents/servlet/sip/core/SipApplicationDispatcherImpl.java Tue Nov 16 00:54:41 2010
@@ -1190,8 +1190,12 @@
                                                if(logger.isDebugEnabled()) {
logger.debug("no sip session were returned for this key " + sipServletMessageImpl.getSipSessionKey() + " and message " + sipServletMessageImpl);
                                                }
-                                       }
-
+                                       }
+
+                                       if(tad.getProxyBranch() != null) {
+                                               
tad.getProxyBranch().removeTransaction(transaction.getBranchId());
+                                       }
+
// Issue 1333 : B2buaHelper.getPendingMessages(linkedSession, UAMode.UAC) returns empty list // don't remove the transaction on terminated state for INVITE Tx because it won't be possible
                                        // to create the ACK on second leg for 
B2BUA apps
=======================================
--- /branches/jbcp/5.x-mead/sip-servlets/sip-servlets-impl/src/main/java/org/mobicents/servlet/sip/proxy/ProxyBranchImpl.java Sun Nov 14 18:12:53 2010 +++ /branches/jbcp/5.x-mead/sip-servlets/sip-servlets-impl/src/main/java/org/mobicents/servlet/sip/proxy/ProxyBranchImpl.java Tue Nov 16 00:54:41 2010
@@ -17,19 +17,19 @@
 package org.mobicents.servlet.sip.proxy;

 import gov.nist.javax.sip.TransactionExt;
+import gov.nist.javax.sip.header.Via;
 import gov.nist.javax.sip.message.SIPMessage;
 import gov.nist.javax.sip.stack.SIPClientTransaction;
-import gov.nist.javax.sip.stack.SIPTransaction;

 import java.io.Externalizable;
 import java.io.IOException;
 import java.io.ObjectInput;
 import java.io.ObjectOutput;
-import java.lang.reflect.Method;
 import java.net.InetAddress;
 import java.net.InetSocketAddress;
 import java.text.ParseException;
 import java.util.ArrayList;
+import java.util.LinkedList;
 import java.util.List;
 import java.util.Timer;

@@ -45,6 +45,7 @@
 import javax.sip.ClientTransaction;
 import javax.sip.SipException;
 import javax.sip.SipProvider;
+import javax.sip.Transaction;
 import javax.sip.header.RouteHeader;
 import javax.sip.header.ViaHeader;
 import javax.sip.message.Request;
@@ -101,8 +102,24 @@
        private boolean waitingForPrack;
        public transient ViaHeader viaHeader;

-       private static transient Timer timer = new Timer();
-
+       /*
+ * It is best to use a linked list here because we expect more than one tx only very rarely. + * Hashmaps, sets, etc allocate some buffers, making them have much bigger mem footprint. + * This list is kept because we need to support concurrent INVITE and other transactions happening + * in both directions in proxy. http://code.google.com/p/mobicents/issues/detail?id=1852
+        *
+        */
+ public transient LinkedList<TransactionRequest> ongoingTransactions = new LinkedList<TransactionRequest>();
+
+       public static class TransactionRequest {
+               public TransactionRequest(String branch, SipServletRequestImpl 
request) {
+                       this.branchId = branch;
+                       this.request = request;
+               }
+               public String branchId;
+               public SipServletRequestImpl request;
+       }
+       private static transient Timer timer = new Timer();
        // empty constructor used only for Externalizable interface
        public ProxyBranchImpl() {}

@@ -320,6 +337,10 @@
                        }
                        recordRoute = recordRouteURI;
                }
+ if(!originalRequest.getMethod().equalsIgnoreCase("ACK") && !originalRequest.getMethod().equalsIgnoreCase("PRACK")) { + String branch = ((Via)originalRequest.getMessage().getHeader(Via.NAME)).getBranch(); + this.ongoingTransactions.add(new TransactionRequest(branch, originalRequest));
+               }

                Request cloned = ProxyUtils.createProxiedRequest(
                                outgoingRequest,
@@ -439,7 +460,7 @@
                                this.setWaitingForPrack(true); // this branch 
is expecting a PRACK now
                        }

-                       final SipServletResponse proxiedResponse =
+                       final SipServletResponseImpl proxiedResponse =
                                ProxyUtils.createProxiedResponse(response, 
this);

                        if(proxiedResponse == null) {
@@ -449,10 +470,21 @@
                        }

                        try {
+ String branch = ((Via)proxiedResponse.getMessage().getHeader(Via.NAME)).getBranch();
+                               synchronized(this.ongoingTransactions) {
+                                       for(TransactionRequest tr : 
this.ongoingTransactions) {
+                                               if(tr.branchId.equals(branch)) {
+ ((SipServletResponseImpl)proxiedResponse).setTransaction(tr.request.getTransaction()); + ((SipServletResponseImpl)proxiedResponse).setOriginalRequest(tr.request);
+                                                       break;
+                                               }
+                                       }
+                               }
+
                                proxiedResponse.send();
                                if(logger.isDebugEnabled())
                                        logger.debug("Proxy response sent out 
sucessfully");
-                       } catch (IOException e) {
+                       } catch (Exception e) {
                                logger.error("A problem occured while proxying a 
response", e);
                        }

@@ -520,6 +552,28 @@
        public boolean isTimedOut() {
                return timedOut;
        }
+
+       public void removeTransaction(String branch) {
+               synchronized(this.ongoingTransactions) {
+                       TransactionRequest remove = null;
+                       for(TransactionRequest tr : this.ongoingTransactions) {
+                               if(tr.branchId.equals(branch)) {
+                                       remove = tr;
+                                       break;
+                               }
+                       }
+                       if(remove != null) {
+                               boolean removed = 
this.ongoingTransactions.remove(remove);
+                               if(logger.isDebugEnabled()) {
+ logger.debug("Removed transaction from proxy " + branch + " success: " + removed);
+                               }
+                       } else {
+                               if(logger.isDebugEnabled()) {
+ logger.debug("Removing transaction from proxy " + branch + " FAILED. It was null");
+                               }
+                       }
+               }
+       }

        /**
* Call this method when a subsequent request must be proxied through the branch.
@@ -527,12 +581,15 @@
         * @param request
         */
        public void proxySubsequentRequest(SipServletRequestImpl request) {
-
+ if(!request.getMethod().equalsIgnoreCase("ACK") && !request.getMethod().equalsIgnoreCase("PRACK")) { + String branch = ((Via)request.getMessage().getHeader(Via.NAME)).getBranch();
+                       this.ongoingTransactions.add(new 
TransactionRequest(branch, request));
+               }
// A re-INVITE needs special handling without goind through the dialog-stateful methods
                if(request.getMethod().equalsIgnoreCase("INVITE")) {
                        if(logger.isDebugEnabled()) {
                                logger.debug("Proxying reinvite request " + 
request);
-                       }
+                       }
                        proxyDialogStateless(request);
                        return;
                }
@@ -615,8 +672,11 @@
                // Update the last proxied request
                request.setRoutingState(RoutingState.PROXIED);
                this.prackOriginalRequest = request;
-
-               URI targetURI = this.targetURI;
+
+               URI targetURI = null;
+ if(request.getMethod().equals(Request.PRACK) || request.getMethod().equals(Request.ACK)) {
+                       targetURI = this.targetURI;
+               }

// Determine the direction of the request. Either it's from the dialog initiator (the caller)
                // or from the callee
@@ -625,7 +685,6 @@
                        targetURI = proxy.getPreviousNode();
                }

-//             ProxyParams params = new ProxyParams(targetURI, null, null, 
null);
                Request clonedRequest =
ProxyUtils.createProxiedRequest(request, this, targetURI, null, null, null);

=======================================
--- /branches/jbcp/5.x-mead/sip-servlets/sip-servlets-impl/src/main/java/org/mobicents/servlet/sip/proxy/ProxyImpl.java Sun Nov 14 18:12:53 2010 +++ /branches/jbcp/5.x-mead/sip-servlets/sip-servlets-impl/src/main/java/org/mobicents/servlet/sip/proxy/ProxyImpl.java Tue Nov 16 00:54:41 2010
@@ -24,7 +24,6 @@
 import java.io.ObjectOutput;
 import java.net.InetAddress;
 import java.net.InetSocketAddress;
-import java.text.ParseException;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.LinkedHashMap;
@@ -52,13 +51,14 @@
 import org.apache.log4j.Logger;
 import org.mobicents.javax.servlet.sip.ProxyExt;
 import org.mobicents.servlet.sip.JainSipUtils;
-import org.mobicents.servlet.sip.SipFactories;
 import org.mobicents.servlet.sip.address.SipURIImpl;
 import org.mobicents.servlet.sip.address.TelURLImpl;
+import org.mobicents.servlet.sip.core.session.MobicentsSipApplicationSession;
 import org.mobicents.servlet.sip.core.session.MobicentsSipSession;
 import org.mobicents.servlet.sip.message.SipFactoryImpl;
 import org.mobicents.servlet.sip.message.SipServletRequestImpl;
 import org.mobicents.servlet.sip.message.SipServletResponseImpl;
+import org.mobicents.servlet.sip.proxy.ProxyBranchImpl.TransactionRequest;

 /**
  * @author root
@@ -171,6 +171,8 @@
         * @see javax.servlet.sip.Proxy#cancel()
         */
        public void cancel() {
+               if(ackReceived)
+ throw new IllegalStateException("There has been an ACK received. Can not cancel more brnaches, the INVITE tx has finished.");
                cancelAllExcept(null, null, null, null, true);
        }

@@ -179,13 +181,16 @@
* @see javax.servlet.sip.Proxy#cancel(java.lang.String[], int[], java.lang.String[])
         */
public void cancel(String[] protocol, int[] reasonCode, String[] reasonText) {
+               if(ackReceived)
+ throw new IllegalStateException("There has been an ACK received. Can not cancel more brnaches, the INVITE tx has finished.");
                cancelAllExcept(null, protocol, reasonCode, reasonText, true);
        }

public void cancelAllExcept(ProxyBranch except, String[] protocol, int[] reasonCode, String[] reasonText, boolean throwExceptionIfCannotCancel) { - if(ackReceived) throw new IllegalStateException("There has been an ACK received on this branch. Can not cancel.");
                for(ProxyBranch proxyBranch : proxyBranches.values()) {
                        if(!proxyBranch.equals(except)) {
+ // Do not make this check in the beginning of the method, because in case of reINVITE etc, we already have a single brnch nd this method + // would have no actual effect, no need to fail it just because we've already seen ACK. Only throw exception if there are other branches.
                                try {
                                        proxyBranch.cancel(protocol, 
reasonCode, reasonText);
                                } catch (IllegalStateException e) {
@@ -658,12 +663,12 @@
                }

                if(logger.isDebugEnabled())
-                       logger.debug("Proxy branch has NOT timed out");
+                                       logger.debug("Proxy branch has NOT timed 
out");

                //Otherwise proceed with proxying the response
                final SipServletResponseImpl proxiedResponse =
                        ProxyUtils.createProxiedResponse(response, proxyBranch);
-
+

                if(proxiedResponse == null || proxiedResponse.getMessage() == 
null) {
                        if(logger.isDebugEnabled())
@@ -674,7 +679,7 @@
                try {

                        if(originalRequest != null && 
proxiedResponse.getRequest() != null) {
-
+
                                // non retransmission case
                                try {
String branch = ((Via)proxiedResponse.getMessage().getHeader(Via.NAME)).getBranch();

Reply via email to