001: /*
002: * Portions Copyright 2000-2007 Sun Microsystems, Inc. All Rights
003: * Reserved. Use is subject to license terms.
004: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License version
008: * 2 only, as published by the Free Software Foundation.
009: *
010: * This program is distributed in the hope that it will be useful, but
011: * WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * General Public License version 2 for more details (a copy is
014: * included at /legal/license.txt).
015: *
016: * You should have received a copy of the GNU General Public License
017: * version 2 along with this work; if not, write to the Free Software
018: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
019: * 02110-1301 USA
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
022: * Clara, CA 95054 or visit www.sun.com if you need additional
023: * information or have any questions.
024: */
025: /*
026: * Hop.java
027: *
028: * Created on July 15, 2001, 2:28 PM
029: */
030:
031: package gov.nist.siplite.address;
032:
033: import gov.nist.core.*;
034:
035: import com.sun.midp.log.Logging;
036: import com.sun.midp.log.LogChannels;
037:
038: /**
039: * Routing algorithms return a list of hops to which the request is
040: * routed.
041: */
042: public class Hop extends Object {
043: /** Current host. */
044: protected String host;
045: /** Port number. */
046: protected int port;
047: /** Connection transport. */
048: protected String transport;
049: /** Explicit route from a ROUTE header. */
050: protected boolean explicitRoute;
051: /** Default route from the proxy addr. */
052: protected boolean defaultRoute;
053: /** URI route from the requestURI. */
054: protected boolean uriRoute;
055:
056: /**
057: * Encodes contents int a string.
058: * @return encoded string of object contents
059: */
060: public String toString() {
061: return host + ":" + port + "/" + transport;
062: }
063:
064: /**
065: * Compares fro equivalence.
066: * @param other the object to compare
067: * @return true if the object matches
068: */
069: public boolean equals(Object other) {
070: if (other.getClass().equals(this .getClass())) {
071: Hop otherhop = (Hop) other;
072: return (otherhop.host.equals(this .host) && otherhop.port == this .port);
073: } else
074: return false;
075: }
076:
077: /**
078: * Create new hop given host, port and transport.
079: * @param hostName hostname
080: * @param portNumber port
081: * @param trans transport
082: * @throws IllegalArgument exception if some parameter
083: * has invalid value.
084: */
085: public Hop(String hostName, int portNumber, String trans)
086: throws IllegalArgumentException {
087:
088: if (portNumber < 0) {
089: throw new IllegalArgumentException("Invalid port: "
090: + portNumber);
091: }
092:
093: host = hostName;
094: port = portNumber;
095:
096: if (trans == null)
097: transport = "UDP";
098: else if (trans == "")
099: transport = "UDP";
100: else
101: transport = trans;
102: }
103:
104: /**
105: * Creates new Hop
106: * @param hop is a hop string in the form of host:port/Transport
107: * @throws IllegalArgument exception if string is not properly formatted or
108: * null.
109: */
110: public Hop(String hop) throws IllegalArgumentException {
111: if (Logging.REPORT_LEVEL <= Logging.INFORMATION) {
112: Logging.report(Logging.INFORMATION, LogChannels.LC_JSR180,
113: "create hop for " + hop);
114: }
115:
116: if (hop == null)
117: throw new IllegalArgumentException("Null arg!");
118:
119: try {
120: StringTokenizer stringTokenizer = new StringTokenizer(hop
121: + "/");
122: String hostPort = stringTokenizer.getNextToken('/');
123: // Skip over the slash.
124: stringTokenizer.getNextChar();
125: // get the transport string.
126: transport = stringTokenizer.getNextToken('/').trim();
127: if (transport == null)
128: transport = "UDP";
129: else if (transport == "")
130: transport = "UDP";
131: if (Utils.compareToIgnoreCase(transport, "UDP") != 0
132: && Utils.compareToIgnoreCase(transport, "TCP") != 0) {
133: System.out.println("Bad transport string " + transport);
134: throw new IllegalArgumentException(hop);
135: }
136: stringTokenizer = new StringTokenizer(hostPort + ":");
137: host = stringTokenizer.getNextToken(':');
138: if (host == null || host.equals(""))
139: throw new IllegalArgumentException("no host!");
140: stringTokenizer.consume(1);
141: String portString = null;
142: portString = stringTokenizer.getNextToken(':');
143:
144: if (portString == null || portString.equals("")) {
145: throw new IllegalArgumentException("no port!");
146: // port = 5060;
147: } else {
148: try {
149: port = Integer.parseInt(portString);
150: } catch (NumberFormatException ex) {
151: throw new IllegalArgumentException("Bad port spec");
152: }
153: }
154: defaultRoute = true;
155: } catch (ParseException ex) {
156: throw new IllegalArgumentException("Bad hop");
157: }
158:
159: }
160:
161: /**
162: * Retruns the host string.
163: * @return host String
164: */
165: public String getHost() {
166: return host;
167: }
168:
169: /**
170: * Returns the port.
171: * @return port integer.
172: */
173: public int getPort() {
174: return port;
175: }
176:
177: /**
178: * Returns the transport string.
179: * @return the transport string
180: */
181: public String getTransport() {
182: return transport;
183: }
184:
185: /**
186: * Return true if this is an explicit route (extacted from a ROUTE
187: * Header).
188: * @return the explicit route
189: */
190: public boolean isExplicitRoute() {
191: return explicitRoute;
192: }
193:
194: /**
195: * Return true if this is a default route (next hop proxy address).
196: * @return true if this is the default route
197: */
198: public boolean isDefaultRoute() {
199: return defaultRoute;
200: }
201:
202: /**
203: * Return true if this is uriRoute.
204: * @return true if this is the URI route
205: */
206: public boolean isURIRoute() {
207: return uriRoute;
208: }
209:
210: /**
211: * Set the URIRoute flag.
212: */
213: public void setURIRouteFlag() {
214: uriRoute = true;
215: }
216:
217: /**
218: * Set the defaultRouteFlag.
219: */
220: public void setDefaultRouteFlag() {
221: defaultRoute = true;
222: }
223:
224: /**
225: * Set the explicitRoute flag.
226: */
227: public void setExplicitRouteFlag() {
228: explicitRoute = true;
229: }
230:
231: }
|