KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > View


1 // $Id: View.java,v 1.7 2005/04/20 10:32:36 belaban Exp $
2

3 package org.jgroups;
4
5
6 import org.jgroups.util.Streamable;
7 import org.jgroups.util.Util;
8
9 import java.io.*;
10 import java.util.Vector JavaDoc;
11 import java.util.Iterator JavaDoc;
12
13
14 /**
15  * A view is a local representation of the current membership of a group
16  * Only one view is installed in a channel at a time
17  * Views contain the address of its creator, an ID and a list of member addresses
18  * These adresses are ordered, and the first address is always the coordinator of the view
19  * This way, each member of the group knows who the new coordinator will be if the current one
20  * crashes or leaves the group.
21  * The views are sent between members using the VIEW_CHANGE event.
22  */

23 public class View implements Externalizable, Cloneable JavaDoc, Streamable {
24     /* A view is uniquely identified by its ViewID
25      * The view id contains the creator address and a Lamport time.
26      * The Lamport time is the highest timestamp seen or sent from a view.
27      * if a view change comes in with a lower Lamport time, the event is discarded.
28      */

29     protected ViewId vid=null;
30
31     /**
32      * A list containing all the members of the view
33      * This list is always ordered, with the coordinator being the first member.
34      * the second member will be the new coordinator if the current one disappears
35      * or leaves the group.
36      */

37     protected Vector JavaDoc members=null;
38
39
40     /**
41      * creates an empty view, should not be used
42      */

43     public View() {
44     }
45
46
47     /**
48      * Creates a new view
49      *
50      * @param vid The view id of this view (can not be null)
51      * @param members Contains a list of all the members in the view, can be empty but not null.
52      */

53     public View(ViewId vid, Vector JavaDoc members) {
54         this.vid=vid;
55         this.members=members;
56     }
57
58
59     /**
60      * Creates a new view
61      *
62      * @param creator The creator of this view (can not be null)
63      * @param id The lamport timestamp of this view
64      * @param members Contains a list of all the members in the view, can be empty but not null.
65      */

66     public View(Address creator, long id, Vector JavaDoc members) {
67         this(new ViewId(creator, id), members);
68     }
69
70
71     /**
72      * returns the view ID of this view
73      * if this view was created with the empty constructur, null will be returned
74      *
75      * @return the view ID of this view
76      */

77     public ViewId getVid() {
78         return vid;
79     }
80
81     /**
82      * returns the creator of this view
83      * if this view was created with the empty constructur, null will be returned
84      *
85      * @return the creator of this view in form of an Address object
86      */

87     public Address getCreator() {
88         return vid != null ? vid.getCoordAddress() : null;
89     }
90
91     /**
92      * Returns a reference to the List of members (ordered)
93      * Do NOT change this list, hence your will invalidate the view
94      * Make a copy if you have to modify it.
95      *
96      * @return a reference to the ordered list of members in this view
97      */

98     public Vector JavaDoc getMembers() {
99         return members;
100     }
101
102     /**
103      * returns true, if this view contains a certain member
104      *
105      * @param mbr - the address of the member,
106      * @return true if this view contains the member, false if it doesn't
107      * if the argument mbr is null, this operation returns false
108      */

109     public boolean containsMember(Address mbr) {
110         if(mbr == null || members == null) {
111             return false;
112         }
113         return members.contains(mbr);
114     }
115
116
117     public boolean equals(Object JavaDoc obj) {
118         if(obj == null)
119             return false;
120         if(vid != null) {
121             int rc=vid.compareTo(((View)obj).vid);
122             if(rc != 0)
123                 return false;
124             if(members != null && ((View)obj).members != null) {
125                 return members.equals(((View)obj).members);
126             }
127         }
128         return false;
129     }
130
131     /**
132      * returns the number of members in this view
133      *
134      * @return the number of members in this view 0..n
135      */

136     public int size() {
137         return members == null ? 0 : members.size();
138     }
139
140
141     /**
142      * creates a copy of this view
143      *
144      * @return a copy of this view
145      */

146     public Object JavaDoc clone() {
147         ViewId vid2=vid != null ? (ViewId)vid.clone() : null;
148         Vector JavaDoc members2=members != null ? (Vector JavaDoc)members.clone() : null;
149         return new View(vid2, members2);
150     }
151
152
153     /**
154      * debug only
155      */

156     public String JavaDoc printDetails() {
157         StringBuffer JavaDoc ret=new StringBuffer JavaDoc();
158         ret.append(vid).append("\n\t");
159         if(members != null) {
160             for(int i=0; i < members.size(); i++) {
161                 ret.append(members.elementAt(i)).append("\n\t");
162             }
163             ret.append('\n');
164         }
165         return ret.toString();
166     }
167
168
169     public String JavaDoc toString() {
170         StringBuffer JavaDoc ret=new StringBuffer JavaDoc(64);
171         ret.append(vid + " " + members);
172         return ret.toString();
173     }
174
175
176     public void writeExternal(ObjectOutput out) throws IOException {
177         out.writeObject(vid);
178         out.writeObject(members);
179     }
180
181
182     public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException JavaDoc {
183         vid=(ViewId)in.readObject();
184         members=(Vector JavaDoc)in.readObject();
185     }
186
187
188     public void writeTo(DataOutputStream out) throws IOException {
189         // vid
190
if(vid != null) {
191             out.write(1);
192             vid.writeTo(out);
193         }
194         else
195             out.write(0);
196
197         // members:
198
if(members != null) {
199             out.write(1);
200             out.writeInt(members.size());
201             for(Iterator JavaDoc it=members.iterator(); it.hasNext();) {
202                 Address addr=(Address)it.next();
203                 Util.writeAddress(addr, out);
204             }
205         }
206         else
207             out.write(0);
208     }
209
210
211     public void readFrom(DataInputStream in) throws IOException, IllegalAccessException JavaDoc, InstantiationException JavaDoc {
212         int b;
213         // vid:
214
b=in.read();
215         if(b == 1) {
216             vid=new ViewId();
217             vid.readFrom(in);
218         }
219
220         // members:
221
b=in.read();
222         if(b == 1) {
223             b=in.readInt();
224             members=new Vector JavaDoc(b);
225             Address addr;
226             for(int i=0; i < b; i++) {
227                 addr=Util.readAddress(in);
228                 members.add(addr);
229             }
230         }
231     }
232
233
234 }
235
Popular Tags