1 46 50 package org.mr.core.net; 51 52 import java.io.IOException ; 53 import java.net.InetAddress ; 54 import java.net.InetSocketAddress ; 55 import java.net.NetworkInterface ; 56 import java.net.SocketAddress ; 57 import java.util.Enumeration ; 58 59 import org.apache.commons.logging.Log; 60 import org.apache.commons.logging.LogFactory; 61 import org.mr.core.util.byteable.Byteable; 62 import org.mr.core.util.byteable.ByteableInputStream; 63 import org.mr.core.util.byteable.ByteableOutputStream; 64 import org.mr.core.util.byteable.ByteableRegistry; 65 66 73 public class TransportInfo implements Byteable { 74 75 InetSocketAddress addr; 77 TransportType transportType; 78 static Log log; 79 80 81 public TransportInfo() { 82 } 83 84 public TransportInfo(InetSocketAddress addr, TransportType type) { 85 this.addr = addr; 86 this.transportType = type; 87 } 88 89 public TransportInfo(InetAddress ip, int port, String transportInfoType) { 90 log=LogFactory.getLog("TransportInfo"); 91 try { 92 this.addr = new InetSocketAddress (ip, port); 93 } catch (IllegalArgumentException e) { 94 if(log.isErrorEnabled()){ 95 log.error("illegal IP or port for transport, ip = " + ip + "port = " + port+".", e); 96 } 97 this.addr = null; 98 } 99 this.transportType = TransportType.getTransportTypeFromString(transportInfoType); 100 } 101 102 public TransportInfo(String ipStr, int port, String transportInfoType) { 103 log=LogFactory.getLog("TransportInfo"); 104 try { 105 this.addr = new InetSocketAddress (ipStr, port); 106 } catch (IllegalArgumentException e) { 107 if(log.isErrorEnabled()){ 108 log.error("illegal IP or port for transport, ip = " + ipStr + "port = " + port+".", e); 109 } 110 this.addr = null; 111 } 112 113 if ((this.addr).isUnresolved()) { 114 if(log.isErrorEnabled()){ 115 log.error("Transport info created with invalid ip - ip="+ipStr); 116 } 117 } 118 this.transportType = TransportType.getTransportTypeFromString(transportInfoType); 121 } 122 123 124 125 126 129 public InetAddress getIp() { 130 return this.addr.getAddress(); 131 } 132 133 136 public int getPort() { 137 return this.addr.getPort(); 138 } 139 140 145 public SocketAddress getSocketAddress() { 146 return this.addr; 147 } 148 149 152 public TransportType getTransportInfoType() { 153 return transportType; 154 } 155 156 159 public void setTransportInfoType(String transportInfoType) { 160 this.transportType = TransportType.getTransportTypeFromString(transportInfoType); 161 } 162 163 public String toString(){ 164 return String.valueOf(addr)+"@"+String.valueOf(transportType); 165 } 166 167 public final boolean equals(Object obj) { 168 if(this == obj) 169 return true; 170 if (obj == null || !(obj instanceof TransportInfo)) 171 return false; 172 TransportInfo other = ((TransportInfo)obj); 173 return(other.addr.equals(this.addr)) && other.transportType.equals(this.transportType) ; 174 } 175 176 179 public int hashCode(){ 180 return this.addr.hashCode()+this.transportType.hashCode(); 181 } 182 183 public String getByteableName() { 184 return "TransportInfo"; 185 } 186 187 public void toBytes(ByteableOutputStream out) throws IOException { 188 out.write(this.addr.getAddress().getAddress()); 189 out.writeInt(this.addr.getPort()); 190 out.writeASCIIString(this.transportType.toString()); 191 } 192 193 public Byteable createInstance(ByteableInputStream in) throws IOException { 194 byte[] addr = new byte[4]; 195 int port; 196 String type; 197 TransportInfo result = new TransportInfo(); 198 199 in.read(addr); 200 port = in.readInt(); 201 type = in.readASCIIString(); 202 203 result.transportType = TransportType.getTransportTypeFromString(type); 204 result.addr = new InetSocketAddress (InetAddress.getByAddress(addr), 205 port); 206 207 return result; 208 } 209 210 public void registerToByteableRegistry() { 211 ByteableRegistry.registerByteableFactory(getByteableName(), this); 212 } 213 214 public static void register() { 215 TransportInfo info = new TransportInfo(); 216 217 info.registerToByteableRegistry(); 218 } 219 220 public static String getValidLocalAddress() { 221 try { 222 Enumeration ifs = NetworkInterface.getNetworkInterfaces(); 223 while (ifs.hasMoreElements()) { 224 NetworkInterface iface = (NetworkInterface ) 225 ifs.nextElement(); 226 Enumeration ips = iface.getInetAddresses(); 227 while (ips.hasMoreElements()) { 228 InetAddress ip = (InetAddress ) ips.nextElement(); 229 if (!ip.getHostAddress().equals("127.0.0.1")) { 230 return ip.getHostAddress(); 231 } 232 } 233 } 234 } catch (Throwable t) {} 235 236 return "127.0.0.1"; } 238 239 } 240 | Popular Tags |