KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > stack > LogicalAddress1_4


1 // $Id: LogicalAddress1_4.java,v 1.6 2005/04/15 13:17:03 belaban Exp $
2

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 JavaDoc;
10 import java.net.SocketAddress JavaDoc;
11 import java.util.ArrayList JavaDoc;
12 import java.util.Date JavaDoc;
13 import java.util.List JavaDoc;
14
15
16
17 /**
18  * Logical address that spans the lifetime of a member. Assigned at member (JVM) startup, and
19  * retained until member is shutdown. Note that the address does <em>not</em> change on
20  * disconnect-connect sequences. For example, when a member is shunned and subsequently
21  * readmitted to the group, the member's address (LogicalAddress1_4) remains the same.<br/>
22  * An instance of LogicalAddress1_4 is generated by the transport protocol. Currently, only
23  * UDP1_4 generates LogicalAddresses.<br/>
24  * Note that host, timestamp and id are supposed to make LogicalAddress1_4 as unique as possible.
25  * However, there is a remote chance that 2 instances started on the same machine create their
26  * address at exactly the same time, resulting in identical addresses (leading to problems).
27  * In the future, I will try to make this totally unique, by for example using the PID of the current
28  * process (once available though the JDK, or by locking on a common resource (e.g. /dev/random)
29  * to serialize creation. However, as for now, chances are you will never experience this problem.
30  * @author Bela Ban, Dec 23 2003
31  */

32 public class LogicalAddress1_4 implements Address {
33     protected static int count=1;
34     protected String JavaDoc host=null;
35     protected long timestamp=0;
36     protected int id=0;
37     protected boolean multicast_addr=false;
38
39     /** Address of the primary physical address. This is set to the sender when a message is received.
40      * If this field is set, we will send unicast messages only to this address, not to all addresses listed
41      * in physical_addrs; this reduces the number of msgs we have to send.<br/>
42      * Note that this field is not shipped across the wire.
43      */

44     transient SocketAddress JavaDoc primary_physical_addr=null;
45
46     /** List<SocketAddress> of physical addresses */
47     protected ArrayList JavaDoc physical_addrs=null;
48
49     /** To tack on some additional data */
50     byte[] additional_data=null;
51
52
53
54     // Used only by Externalization
55
public LogicalAddress1_4() {
56     }
57
58
59     /** Use this constructor to create an instance, not the null-constructor */
60     public LogicalAddress1_4(String JavaDoc host_name, List JavaDoc physical_addrs) {
61         init(host_name, physical_addrs);
62     }
63
64
65
66     protected void init(String JavaDoc host_name, List JavaDoc 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 JavaDoc 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 JavaDoc(physical_addrs);
87         }
88     }
89
90
91     public String JavaDoc 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 JavaDoc getPrimaryPhysicalAddress() {
104         return primary_physical_addr;
105     }
106
107     public void setPrimaryPhysicalAddress(SocketAddress JavaDoc primary_physical_addr) {
108         this.primary_physical_addr=primary_physical_addr;
109     }
110
111     /**
112      * Returns a <em>copy</em> of the list of physical addresses. Reason for the copy is that the list is not supposed
113      * to be modified (should be immutable).
114      * @return List of physical addresses (return value maybe null)
115      */

116     public ArrayList JavaDoc getPhysicalAddresses() {
117         return physical_addrs != null? (ArrayList JavaDoc)physical_addrs.clone() : null;
118     }
119
120     /**
121      * For internal use only ! Don't use this method !
122      * @param addr
123      */

124     public void addPhysicalAddress(SocketAddress JavaDoc addr) {
125         if(addr != null) {
126             if(physical_addrs == null)
127                 physical_addrs=new ArrayList JavaDoc();
128             if(!physical_addrs.contains(addr))
129                 physical_addrs.add(addr);
130         }
131     }
132
133     /**
134      * For internal use only ! Don't use this method !
135      * @param addr
136      */

137     public void removePhysicalAddress(SocketAddress JavaDoc addr) {
138         if(addr != null && physical_addrs != null)
139             physical_addrs.remove(addr);
140     }
141
142     /**
143      * For internal use only ! Don't use this method !
144      */

145     public void removeAllPhysicalAddresses() {
146         if(physical_addrs != null)
147             physical_addrs.clear();
148     }
149
150     public boolean isMulticastAddress() {
151         return false; // LogicalAddresses can never be multicast
152
}
153
154     public int size() {
155         return 22;
156     }
157
158     /**
159      * Returns the additional_data.
160      * @return byte[]
161      */

162     public byte[] getAdditionalData() {
163         return additional_data;
164     }
165
166     /**
167      * Sets the additional_data.
168      * @param additional_data The additional_data to set
169      */

170     public void setAdditionalData(byte[] additional_data) {
171         this.additional_data = additional_data;
172     }
173
174
175     /**
176      * Establishes an order between 2 addresses. Assumes other contains non-null IpAddress.
177      * Excludes channel_name from comparison.
178      * @return 0 for equality, value less than 0 if smaller, greater than 0 if greater.
179      */

180     public int compare(LogicalAddress1_4 other) {
181         return compareTo(other);
182     }
183
184
185     /**
186      * implements the java.lang.Comparable interface
187      * @see Comparable
188      * @param o - the Object to be compared
189      * @return a negative integer, zero, or a positive integer as this object is less than,
190      * equal to, or greater than the specified object.
191      * @exception ClassCastException - if the specified object's type prevents it
192      * from being compared to this Object.
193      */

194     public int compareTo(Object JavaDoc o) {
195         int rc;
196
197         if ((o == null) || !(o instanceof LogicalAddress1_4))
198             throw new ClassCastException JavaDoc("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 JavaDoc 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 JavaDoc toString() {
229         return toString(false);
230     }
231
232
233     public String JavaDoc toString(boolean print_details) {
234         StringBuffer JavaDoc sb=new StringBuffer JavaDoc();
235
236         sb.append(host);
237         sb.append(':').append(id);
238         if(print_details) {
239             sb.append(" (created ").append(new Date JavaDoc(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 JavaDoc {
274         int len;
275
276         host=(String JavaDoc)in.readObject();
277         timestamp=in.readLong();
278         id=in.readInt();
279
280         len=in.readInt();
281         if(len > 0) {
282             physical_addrs=(ArrayList JavaDoc)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 JavaDoc, InstantiationException JavaDoc {
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 JavaDoc)ois.readObject();
313         }
314         catch(ClassNotFoundException JavaDoc e) {
315         }
316         additional_data=Util.readByteBuffer(in);
317     }
318
319     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
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 JavaDoc)physical_addrs.clone();
329         return ret;
330     }
331
332     public LogicalAddress1_4 copy() {
333         try {
334             return (LogicalAddress1_4)clone();
335         }
336         catch(CloneNotSupportedException JavaDoc e) {
337             return null;
338         }
339     }
340
341
342 }
343
Popular Tags