KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > net > TCSocketAddress


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.net;
5
6 import com.tc.exception.TCRuntimeException;
7 import com.tc.util.Assert;
8
9 import java.net.InetAddress JavaDoc;
10 import java.net.UnknownHostException JavaDoc;
11
12 /**
13  * An emulation (incomplete) of the java.net.InetSocketAddress class (which happens to only be available in 1.4
14  * runtimes) In the TC world, this class is used to describe a location (address + port) to connnect to, and/or listen
15  * on
16  *
17  * @author teck
18  */

19 public class TCSocketAddress {
20   /**
21    * Bytes for IPv4 wildcard address (ie. 0.0.0.0). This address is used to bind a listening socket to all available
22    * interfaces
23    */

24   private static final byte[] WILDCARD_BYTES = new byte[] { (byte) 0, (byte) 0, (byte) 0, (byte) 0 };
25
26   /**
27    * Bytes for the loopback adapator (ie. 127.0.0.1)
28    */

29   private static final byte[] LOOPBACK_BYTES = new byte[] { (byte) 127, (byte) 0, (byte) 0, (byte) 1 };
30
31   /**
32    * String form the loopback adaptor address (ie. 127.0.0.1)
33    */

34   public static final String JavaDoc LOOPBACK_IP = "127.0.0.1";
35
36   /**
37    * String form of the wildcard IP address (ie. 0.0.0.0)
38    */

39   public static final String JavaDoc WILDCARD_IP = "0.0.0.0";
40
41   /**
42    * java.net.InetAddress form of the wildcard IP address (ie. 0.0.0.0)
43    */

44   public static final InetAddress JavaDoc WILDCARD_ADDR;
45
46   /**
47    * java.net.InetAddress form of the wildcard IP address (ie. 127.0.0.1)
48    */

49   public static final InetAddress JavaDoc LOOPBACK_ADDR;
50
51   static {
52     try {
53       WILDCARD_ADDR = InetAddress.getByName(WILDCARD_IP);
54     } catch (UnknownHostException JavaDoc e) {
55       throw new TCRuntimeException("Cannot create InetAddress instance for " + WILDCARD_IP);
56     }
57
58     try {
59       LOOPBACK_ADDR = InetAddress.getByName(LOOPBACK_IP);
60     } catch (UnknownHostException JavaDoc e) {
61       throw new TCRuntimeException("Cannot create InetAddress instance for " + LOOPBACK_IP);
62     }
63   }
64
65   public static byte[] getLoopbackBytes() {
66     return (byte[]) LOOPBACK_BYTES.clone();
67   }
68
69   public static byte[] getWilcardBytes() {
70     return (byte[]) WILDCARD_BYTES.clone();
71   }
72
73   // TODO: add a constructor that takes the output of toStringForm() and
74
// reconsitutes a TCSocketAddress instance
75

76   /**
77    * Creates an address for localhost on the given port
78    *
79    * @param port the port number, can be zero
80    * @throws IllegalArgumentException if port is out of range (0 - 65535)
81    */

82   public TCSocketAddress(int port) {
83     this(LOOPBACK_ADDR, port);
84   }
85
86   /**
87    * Creates an address for localhost on the given port
88    *
89    * @param port the port number, can be zero
90    * @throws UnknownHostException
91    * @throws IllegalArgumentException if port is out of range (0 - 65535)
92    * @throws UnknownHostException if the host name provided can not be resolved
93    */

94   public TCSocketAddress(String JavaDoc host, int port) throws UnknownHostException JavaDoc {
95     this(InetAddress.getByName(host), port);
96   }
97
98   /**
99    * Create an TCSocketAdress instance for the gven address on the given port
100    *
101    * @param addr the address to connect to. If null, this constructor behaves exactly like
102    * <code>TCSocketAddress(int port)</code>
103    * @param port the port number, can be zero
104    * @throws IllegalArgumentException if port is out of range (0 - 65535)
105    */

106   public TCSocketAddress(InetAddress JavaDoc addr, int port) {
107     if (!isValidPort(port)) { throw new IllegalArgumentException JavaDoc("port (" + port + ") is out of range (0 - 0xFFFF)"); }
108
109     if (addr == null) {
110       try {
111         addr = InetAddress.getLocalHost();
112       } catch (UnknownHostException JavaDoc e) {
113         addr = LOOPBACK_ADDR;
114       }
115     }
116
117     this.addr = addr;
118     this.port = port;
119
120     Assert.eval(this.addr != null);
121   }
122
123   public InetAddress JavaDoc getAddress() {
124     return addr;
125   }
126
127   public int getPort() {
128     return port;
129   }
130
131   public byte[] getAddressBytes() {
132     return addr.getAddress();
133   }
134
135   public boolean equals(Object JavaDoc obj) {
136     if (obj instanceof TCSocketAddress) {
137       TCSocketAddress other = (TCSocketAddress) obj;
138       return ((this.port == other.port) && this.addr.equals(other.addr));
139     }
140     return false;
141   }
142
143   public int hashCode() {
144     if (addr == null) { return super.hashCode(); }
145
146     return addr.hashCode() + port;
147   }
148
149   public String JavaDoc toString() {
150     return getStringForm();
151   }
152
153   /**
154    * Returns a string description of this address instance in the following format: X.X.X.X:port where "X.X.X.X" is the
155    * IP address and "port" is the port number The only purpose of this method is to document the specific format as a
156    * contract. <code>toString()</code> is <b>not </b> required follow the same format
157    *
158    * @return string form of this address
159    */

160   public String JavaDoc getStringForm() {
161     StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
162     buf.append(addr.getHostAddress()).append(":").append(port);
163     return buf.toString();
164   }
165
166   public static boolean isValidPort(int port) {
167     return ((port >= 0) && (port <= 0xFFFF));
168   }
169
170   private final InetAddress JavaDoc addr;
171   private final int port;
172 }
Popular Tags