1 3 package org.jgroups.stack; 4 5 import org.jgroups.Address; 6 import org.jgroups.util.Util; 7 8 import java.io.*; 9 import java.net.InetAddress ; 10 import java.net.SocketAddress ; 11 import java.util.ArrayList ; 12 import java.util.Date ; 13 import java.util.List ; 14 15 16 17 32 public class LogicalAddress1_4 implements Address { 33 protected static int count=1; 34 protected String host=null; 35 protected long timestamp=0; 36 protected int id=0; 37 protected boolean multicast_addr=false; 38 39 44 transient SocketAddress primary_physical_addr=null; 45 46 47 protected ArrayList physical_addrs=null; 48 49 50 byte[] additional_data=null; 51 52 53 54 public LogicalAddress1_4() { 56 } 57 58 59 60 public LogicalAddress1_4(String host_name, List physical_addrs) { 61 init(host_name, physical_addrs); 62 } 63 64 65 66 protected void init(String host_name, List physical_addrs) { 67 if(host_name != null) { 68 this.host=host_name; 69 } 70 else { 71 try { 72 host=InetAddress.getLocalHost().getHostName(); 73 } 74 catch(Exception e) { 75 host="localhost"; 76 } 77 } 78 79 timestamp=System.currentTimeMillis(); 80 81 synchronized(LogicalAddress1_4.class) { 82 id=count++; 83 } 84 85 if(physical_addrs != null) { 86 this.physical_addrs=new ArrayList (physical_addrs); 87 } 88 } 89 90 91 public String getHost() { 92 return host; 93 } 94 95 public long getTimestamp() { 96 return timestamp; 97 } 98 99 public long getId() { 100 return id; 101 } 102 103 public SocketAddress getPrimaryPhysicalAddress() { 104 return primary_physical_addr; 105 } 106 107 public void setPrimaryPhysicalAddress(SocketAddress primary_physical_addr) { 108 this.primary_physical_addr=primary_physical_addr; 109 } 110 111 116 public ArrayList getPhysicalAddresses() { 117 return physical_addrs != null? (ArrayList )physical_addrs.clone() : null; 118 } 119 120 124 public void addPhysicalAddress(SocketAddress addr) { 125 if(addr != null) { 126 if(physical_addrs == null) 127 physical_addrs=new ArrayList (); 128 if(!physical_addrs.contains(addr)) 129 physical_addrs.add(addr); 130 } 131 } 132 133 137 public void removePhysicalAddress(SocketAddress addr) { 138 if(addr != null && physical_addrs != null) 139 physical_addrs.remove(addr); 140 } 141 142 145 public void removeAllPhysicalAddresses() { 146 if(physical_addrs != null) 147 physical_addrs.clear(); 148 } 149 150 public boolean isMulticastAddress() { 151 return false; } 153 154 public int size() { 155 return 22; 156 } 157 158 162 public byte[] getAdditionalData() { 163 return additional_data; 164 } 165 166 170 public void setAdditionalData(byte[] additional_data) { 171 this.additional_data = additional_data; 172 } 173 174 175 180 public int compare(LogicalAddress1_4 other) { 181 return compareTo(other); 182 } 183 184 185 194 public int compareTo(Object o) { 195 int rc; 196 197 if ((o == null) || !(o instanceof LogicalAddress1_4)) 198 throw new ClassCastException ("LogicalAddress1_4.compareTo(): comparison between different classes"); 199 LogicalAddress1_4 other = (LogicalAddress1_4) o; 200 201 rc=this.host.compareTo(other.host); 202 if(rc != 0) return rc; 203 if(this.timestamp != other.timestamp) 204 return this.timestamp < other.timestamp? -1 : 1; 205 if(this.id != other.id) 206 return this.id < other.id? -1 : 1; 207 return 0; 208 } 209 210 211 212 public boolean equals(Object obj) { 213 if(obj == null) return false; 214 return compareTo(obj) == 0 ? true : false; 215 } 216 217 218 219 220 public int hashCode() { 221 int retval=(int)(host.hashCode() + timestamp + id); 222 return retval; 223 } 224 225 226 227 228 public String toString() { 229 return toString(false); 230 } 231 232 233 public String toString(boolean print_details) { 234 StringBuffer sb=new StringBuffer (); 235 236 sb.append(host); 237 sb.append(':').append(id); 238 if(print_details) { 239 sb.append(" (created ").append(new Date (timestamp)).append(')'); 240 if(physical_addrs != null) 241 sb.append("\nphysical addrs: ").append(physical_addrs); 242 } 243 if(additional_data != null) 244 sb.append(" (additional data: ").append(additional_data.length).append(" bytes)"); 245 return sb.toString(); 246 } 247 248 249 250 public void writeExternal(ObjectOutput out) throws IOException { 251 out.writeObject(host); 252 out.writeLong(timestamp); 253 out.writeInt(id); 254 255 if(physical_addrs != null) { 256 out.writeInt(physical_addrs.size()); 257 out.writeObject(physical_addrs); 258 } 259 else 260 out.writeInt(0); 261 262 if(additional_data != null) { 263 out.writeInt(additional_data.length); 264 out.write(additional_data, 0, additional_data.length); 265 } 266 else 267 out.writeInt(0); 268 } 269 270 271 272 273 public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { 274 int len; 275 276 host=(String )in.readObject(); 277 timestamp=in.readLong(); 278 id=in.readInt(); 279 280 len=in.readInt(); 281 if(len > 0) { 282 physical_addrs=(ArrayList )in.readObject(); 283 } 284 285 len=in.readInt(); 286 if(len > 0) { 287 additional_data=new byte[len]; 288 in.readFully(additional_data, 0, additional_data.length); 289 } 290 } 291 292 293 294 public void writeTo(DataOutputStream out) throws IOException { 295 Util.writeString(host, out); 296 out.writeLong(timestamp); 297 out.writeInt(id); 298 out.writeBoolean(multicast_addr); 299 ObjectOutputStream oos=new ObjectOutputStream(out); 300 oos.writeObject(physical_addrs); 301 oos.close(); 302 Util.writeByteBuffer(additional_data, out); 303 } 304 305 public void readFrom(DataInputStream in) throws IOException, IllegalAccessException , InstantiationException { 306 host=Util.readString(in); 307 timestamp=in.readLong(); 308 id=in.readInt(); 309 multicast_addr=in.readBoolean(); 310 ObjectInputStream ois=new ObjectInputStream(in); 311 try { 312 physical_addrs=(ArrayList )ois.readObject(); 313 } 314 catch(ClassNotFoundException e) { 315 } 316 additional_data=Util.readByteBuffer(in); 317 } 318 319 public Object clone() throws CloneNotSupportedException { 320 LogicalAddress1_4 ret=new LogicalAddress1_4(); 321 ret.host=host; 322 ret.timestamp=timestamp; 323 ret.id=id; 324 ret.multicast_addr=multicast_addr; 325 ret.additional_data=additional_data; 326 ret.primary_physical_addr=primary_physical_addr; 327 if(physical_addrs != null) 328 ret.physical_addrs=(ArrayList )physical_addrs.clone(); 329 return ret; 330 } 331 332 public LogicalAddress1_4 copy() { 333 try { 334 return (LogicalAddress1_4)clone(); 335 } 336 catch(CloneNotSupportedException e) { 337 return null; 338 } 339 } 340 341 342 } 343 | Popular Tags |