PageRenderTime 25ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/src/edu/thu/thss/yxy/sip/ProxyHop.java

http://gophone.googlecode.com/
Java | 157 lines | 115 code | 24 blank | 18 comment | 29 complexity | 5110fd2864e8dfc78e9ba0f2ceba519f MD5 | raw file
  1. package edu.thu.thss.yxy.sip;
  2. import java.util.StringTokenizer;
  3. import javax.sip.address.Hop;
  4. /**
  5. * Routing algorithms return a list of hops to which the request is
  6. * routed.
  7. *
  8. * @author Andy Gao
  9. *
  10. */
  11. public class ProxyHop extends Object
  12. implements Hop{
  13. protected String host;
  14. protected int port;
  15. protected String transport;
  16. protected boolean explicitRoute; // this is generated from a ROUTE header.
  17. protected boolean defaultRoute; // This is generated from the proxy addr
  18. protected boolean uriRoute; // This is extracted from the requestURI.
  19. public ProxyHop(String host, int port, String transport) {
  20. super();
  21. this.host = host;
  22. this.port = port;
  23. this.transport = transport;
  24. }
  25. /**
  26. * Creates new Hop
  27. *@param hop is a hop string in the form of host:port/Transport
  28. *@throws IllegalArgument exception if string is not properly formatted or
  29. * null.
  30. */
  31. public ProxyHop(String hop) {
  32. if (hop == null) {
  33. throw new IllegalArgumentException("Null arg!");
  34. }
  35. StringTokenizer stringTokenizer = new StringTokenizer(hop + "/");
  36. String hostPort = stringTokenizer.nextToken("/").trim();
  37. transport = stringTokenizer.nextToken().trim();
  38. if (transport == null || "".equals(transport)) {
  39. transport = "UDP";
  40. }
  41. if (transport.compareToIgnoreCase("UDP") != 0
  42. && transport.compareToIgnoreCase("TCP") != 0) {
  43. System.out.println("Bad transport string " + transport);
  44. throw new IllegalArgumentException(hop);
  45. }
  46. String portString = null;
  47. // IPv6 hostport
  48. if (hostPort.charAt(0) == '[') {
  49. int rightSqBrackIndex = hostPort.indexOf(']');
  50. if (rightSqBrackIndex == -1) {
  51. throw new IllegalArgumentException("Bad IPv6 reference spec");
  52. }
  53. host = hostPort.substring(0, rightSqBrackIndex + 1);
  54. int portColon = hostPort.indexOf(':', rightSqBrackIndex);
  55. if (portColon != -1) {
  56. try {
  57. portString = hostPort.substring(portColon + 1).trim();
  58. } catch (IndexOutOfBoundsException exc) {
  59. // Do nothing - handled later
  60. }
  61. }
  62. }
  63. // IPv6 address and no port
  64. else if (hostPort.indexOf(':') != hostPort.lastIndexOf(":")) {
  65. host = '[' + hostPort + ']';
  66. } else { // no square brackets and a single or zero colons => IPv4
  67. // hostPort
  68. int portColon = hostPort.indexOf(':');
  69. if (portColon == -1) {
  70. host = hostPort;
  71. } else {
  72. host = hostPort.substring(0, portColon).trim();
  73. try {
  74. portString = hostPort.substring(portColon + 1).trim();
  75. } catch (IndexOutOfBoundsException exc) {
  76. // Do nothing - handled later
  77. }
  78. }
  79. }
  80. if (host == null || host.equals("")) {
  81. throw new IllegalArgumentException("no host!");
  82. }
  83. if (portString == null || portString.equals("")) {
  84. port = 5060;
  85. } else {
  86. try {
  87. port = Integer.parseInt(portString);
  88. } catch (NumberFormatException ex) {
  89. throw new IllegalArgumentException("Bad port spec");
  90. }
  91. }
  92. }
  93. public String getHost() {
  94. return host;
  95. }
  96. public void setHost(String host) {
  97. this.host = host;
  98. }
  99. public int getPort() {
  100. return port;
  101. }
  102. public void setPort(int port) {
  103. this.port = port;
  104. }
  105. public String getTransport() {
  106. return transport;
  107. }
  108. public void setTransport(String transport) {
  109. this.transport = transport;
  110. }
  111. public boolean isExplicitRoute() {
  112. return explicitRoute;
  113. }
  114. public void setExplicitRoute(boolean explicitRoute) {
  115. this.explicitRoute = explicitRoute;
  116. }
  117. public boolean isDefaultRoute() {
  118. return defaultRoute;
  119. }
  120. public void setDefaultRoute(boolean defaultRoute) {
  121. this.defaultRoute = defaultRoute;
  122. }
  123. public boolean isUriRoute() {
  124. return uriRoute;
  125. }
  126. public void setUriRoute(boolean uriRoute) {
  127. this.uriRoute = uriRoute;
  128. }
  129. public String toString()
  130. {
  131. return host + ":" + port + "/" + transport;
  132. }
  133. }