MessengerRouter.java
Upload User: liulanlin
Upload Date: 2017-12-08
Package Size: 1274k
Code Size: 3k
Category:

VOIP program

Development Platform:

Java

  1. /*******************************************************************************
  2.  * Product of NIST/ITL Advanced Networking Technologies Division (ANTD).       *
  3.  ******************************************************************************/
  4. package gov.nist.applet.phone.ua.router;
  5. import java.util.Iterator;
  6. import java.util.LinkedList;
  7. import java.util.ListIterator;
  8. import javax.sip.ListeningPoint;
  9. import javax.sip.SipStack;
  10. import javax.sip.address.Hop;
  11. import javax.sip.address.Router;
  12. import javax.sip.message.Request;
  13. /** 
  14.  * This is the router of the application. When the implementation wants to forward
  15.  * a request and  had run out of othe options, then it calls this method
  16.  * to figure out where to send the request. The default router implements
  17.  * a simple "default routing algorithm" which just forwards to the configured
  18.  * proxy address. Use this for UAC/UAS implementations and use the GatewayRouter
  19.  * for proxies.
  20.  *
  21.  * @version  JAIN-SIP-1.1 $Revision: 1.6 $ $Date: 2007/10/15 18:52:11 $
  22.  *
  23.  * @author M. Ranganathan <mranga@nist.gov>  <br/>
  24.  *
  25.  * <a href="{@docRoot}/uncopyright.html">This code is in the public domain.</a>
  26.  *
  27.  */
  28. public class MessengerRouter implements Router {
  29. protected SipStack sipStack;
  30. protected MessengerHop defaultRoute;
  31. private   boolean checked;
  32. private   boolean localLoopDetected;
  33. /**
  34.  * Constructor.
  35.  */
  36. public MessengerRouter(SipStack sipStack, String defaultRoute) {
  37. this.sipStack =  sipStack;
  38. if (defaultRoute != null) {
  39. this.defaultRoute = new MessengerHop(defaultRoute);
  40. }
  41. }
  42. /**
  43.  *  Return true if I have a listener listening here.
  44.  */
  45. private boolean hopsBackToMe(String host, int port) {
  46. Iterator it =sipStack.getListeningPoints();
  47. while (it.hasNext()) {
  48. ListeningPoint lp = (ListeningPoint) it.next();
  49. if ( sipStack.getIPAddress().equalsIgnoreCase(host)
  50. && lp.getPort() == port)
  51. return true;
  52. }
  53. return false;
  54. }
  55. /**
  56.  * Return  addresses for default proxy to forward the request to.
  57.  * The list is organized in the following priority.
  58.  * If the requestURI refers directly to a host, the host and port
  59.  * information are extracted from it and made the next hop on the
  60.  * list.
  61.  * If the default route has been specified, then it is used
  62.  * to construct the next element of the list.
  63.  * Bug reported by Will Scullin -- maddr was being ignored when
  64.  * routing requests. Bug reported by Antonis Karydas - the RequestURI can
  65.  * be a non-sip URI.
  66.  *
  67.  * @param request is the sip request to route.
  68.  *
  69.  */
  70. public ListIterator getNextHops(Request request) {
  71. LinkedList ll = null;
  72. if (! checked) {
  73. checked = true;
  74. if ( defaultRoute != null) {
  75. localLoopDetected = hopsBackToMe(defaultRoute.getHost(), defaultRoute.getPort());
  76. }
  77. }
  78. if (defaultRoute != null   && !localLoopDetected ) {
  79. ll = new LinkedList();
  80. ll.add(defaultRoute);
  81. }
  82. return ll == null ? null : ll.listIterator();
  83. }
  84. /** 
  85.  * Get the default hop.
  86.  * 
  87.  * @return defaultRoute is the default route.
  88.  * public java.util.Iterator getDefaultRoute(Request request)
  89.  * { return this.getNextHops((SIPRequest)request); }
  90.  */
  91.         public Hop getNextHop(Request request)  {
  92. return this.defaultRoute;
  93. }
  94.         
  95. public javax.sip.address.Hop getOutboundProxy() {
  96. return this.defaultRoute;
  97. }
  98. /** 
  99.  * Get the default route (does the same thing as getOutboundProxy).
  100.  *
  101.  * @return the default route.
  102.  */
  103. public Hop getDefaultRoute() {
  104. return this.defaultRoute;
  105. }
  106. }