MessengerHop.java
Upload User: liulanlin
Upload Date: 2017-12-08
Package Size: 1274k
Code Size: 5k
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.StringTokenizer;
  6. /** 
  7.  * Routing algorithms return a list of hops to which the request is
  8.  * routed.
  9.  *
  10.  * @version  JAIN-SIP-1.1 $Revision: 1.2 $ $Date: 2004/04/22 22:50:32 $
  11.  *
  12.  * @author M. Ranganathan <mranga@nist.gov>  <br/>
  13.  *
  14.  * <a href="{@docRoot}/uncopyright.html">This code is in the public domain.</a>
  15.  *
  16.  * IPv6 Support added by Emil Ivov (emil_ivov@yahoo.com)<br/>
  17.  * Network Research Team (http://www-r2.u-strasbg.fr))<br/>
  18.  * Louis Pasteur University - Strasbourg - France<br/>
  19.  *
  20.  */
  21. public class MessengerHop extends Object implements javax.sip.address.Hop {
  22. protected String host;
  23. protected int port;
  24. protected String transport;
  25. protected boolean explicitRoute; // this is generated from a ROUTE header.
  26. protected boolean defaultRoute; // This is generated from the proxy addr
  27. protected boolean uriRoute; // This is extracted from the requestURI.
  28. /**
  29.  * Debugging println.
  30.  */
  31. public String toString() {
  32. return host + ":" + port + "/" + transport;
  33. }
  34. /**
  35.  * Create new hop given host, port and transport.
  36.  * @param hostName hostname
  37.  * @param portNumber port
  38.  * @param trans transport
  39.  */
  40. public MessengerHop(String hostName, int portNumber, String trans) {
  41. host = hostName;
  42. port = portNumber;
  43. transport = trans;
  44. }
  45. /**
  46.  * Creates new Hop
  47.  * @param hop is a hop string in the form of host:port/Transport
  48.  * @throws IllegalArgument exception if string is not properly formatted or null.
  49.  */
  50. public MessengerHop(String hop) throws IllegalArgumentException {
  51. if (hop == null)
  52. throw new IllegalArgumentException("Null arg!");
  53. // System.out.println("hop = " + hop);
  54. StringTokenizer stringTokenizer = new StringTokenizer(hop + "/");
  55. String hostPort = stringTokenizer.nextToken("/").trim();
  56. transport = stringTokenizer.nextToken().trim();
  57. // System.out.println("Hop: transport = " + transport);
  58. if (transport == null)
  59. transport = "UDP";
  60. else if (transport == "")
  61. transport = "UDP";
  62. if (transport.compareToIgnoreCase("UDP") != 0
  63. && transport.compareToIgnoreCase("TCP") != 0) {
  64. System.out.println("Bad transport string " + transport);
  65. throw new IllegalArgumentException(hop);
  66. }
  67. String portString = null;
  68. //IPv6 hostport
  69. if (hostPort.charAt(0) == '[') {
  70. int rightSqBrackIndex = hostPort.indexOf(']');
  71. if (rightSqBrackIndex == -1)
  72. throw new IllegalArgumentException("Bad IPv6 reference spec");
  73. host = hostPort.substring(0, rightSqBrackIndex + 1);
  74. int portColon = hostPort.indexOf(':', rightSqBrackIndex);
  75. if (portColon != -1)
  76. try {
  77. portString = hostPort.substring(portColon + 1).trim();
  78. } catch (IndexOutOfBoundsException exc) {
  79. //Do nothing - handled later
  80. }
  81. }
  82. //IPv6 address and no port
  83. else if (hostPort.indexOf(':') != hostPort.lastIndexOf(":")) {
  84. host = '[' + hostPort + ']';
  85. } else //no square brackets and a single or zero colons => IPv4 hostPort
  86. {
  87. int portColon = hostPort.indexOf(':');
  88. if (portColon == -1)
  89. host = hostPort;
  90. else {
  91. host = hostPort.substring(0, portColon).trim();
  92. try {
  93. portString = hostPort.substring(portColon + 1).trim();
  94. } catch (IndexOutOfBoundsException exc) {
  95. //Do nothing - handled later
  96. }
  97. }
  98. }
  99. if (host == null || host.equals(""))
  100. throw new IllegalArgumentException("no host!");
  101. if (portString == null || portString.equals("")) {
  102. port = 5060;
  103. } else {
  104. try {
  105. port = Integer.parseInt(portString);
  106. } catch (NumberFormatException ex) {
  107. throw new IllegalArgumentException("Bad port spec");
  108. }
  109. }
  110. }
  111. /**
  112.  * Retruns the host string.
  113.  * @return host String
  114.  */
  115. public String getHost() {
  116. return host;
  117. }
  118. /**
  119.  * Returns the port.
  120.  * @return port integer.
  121.  */
  122. public int getPort() {
  123. return port;
  124. }
  125. /** returns the transport string.
  126.  */
  127. public String getTransport() {
  128. return transport;
  129. }
  130. /** Return true if this is an explicit route (ie. extrcted from a ROUTE
  131.  * Header)
  132.  */
  133. public boolean isExplicitRoute() {
  134. return explicitRoute;
  135. }
  136. /** Return true if this is a default route (ie. next hop proxy address)
  137.  */
  138. public boolean isDefaultRoute() {
  139. return defaultRoute;
  140. }
  141. /** Return true if this is uriRoute
  142.  */
  143. public boolean isURIRoute() {
  144. return uriRoute;
  145. }
  146. /** Set the URIRoute flag.
  147.  */
  148. public void setURIRouteFlag() {
  149. uriRoute = true;
  150. }
  151. /** Set the defaultRouteFlag.
  152.  */
  153. public void setDefaultRouteFlag() {
  154. defaultRoute = true;
  155. }
  156. /** Set the explicitRoute flag.
  157.  */
  158. public void setExplicitRouteFlag() {
  159. explicitRoute = true;
  160. }
  161. /**
  162.  * Set the host of the router
  163.  * @param host - host of the router
  164.  */
  165. public void setHost(String host){
  166. if(host!=null)    
  167. this.host=host;
  168. }
  169. /**
  170.  * Set the port of the router
  171.  * @param port - port of the router
  172.  */
  173. public void setPort(int port){
  174. if(port>0)    
  175. this.port=port;
  176. }
  177. /**
  178.  * Set the transport of the router
  179.  * @param host - transport of the router
  180.  */
  181. public void setTransport(String transport){
  182. if(transport!=null)    
  183. this.transport=transport;
  184. }
  185. }