View Javadoc

1   /*
2    * Copyright (c) 1998-2002 The Jgroup Team.
3    *
4    * This program is free software; you can redistribute it and/or modify
5    * it under the terms of the GNU Lesser General Public License version 2 as
6    * published by the Free Software Foundation.
7    *
8    * This program is distributed in the hope that it will be useful,
9    * but WITHOUT ANY WARRANTY; without even the implied warranty of
10   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11   * GNU Lesser General Public License for more details.
12   *
13   * You should have received a copy of the GNU Lesser General Public License
14   * along with this program; if not, write to the Free Software
15   * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16   *
17   */
18  
19  package jgroup.relacs.mss;
20  
21  import jgroup.core.EndPoint;
22  import jgroup.util.BooleanSet;
23  
24  
25  /**
26   *  The <code>DSView</code> class describe the various member sets.  It
27   *  includes the complete reachable set, the new reachable set, the new
28   *  unreachable set, and the set of new incarnations associated with
29   *  this view.
30   *
31   *  @author Salvatore Cammarata
32   *  @since Jgroup 1.2
33   */
34  final class DSView
35  {
36  
37    ////////////////////////////////////////////////////////////////////////////////////////////
38    // Fields
39    ////////////////////////////////////////////////////////////////////////////////////////////
40  
41    /* Hosttable */
42    private HostTable hosttable;
43    
44    /** Boolean set of the reachable members */
45    private BooleanSet reachableSet;    
46    
47    /** Boolean set of the new unreachable members */
48    private BooleanSet newUnreachableSet;
49    
50    /** Boolean set of the new reachable members */
51    private BooleanSet newReachableSet; 
52    
53    /** Boolean set of the member with new incarnation */
54    private BooleanSet newIncarnationSet; 
55  
56  
57    ////////////////////////////////////////////////////////////////////////////////////////////
58    // Constructor
59    ////////////////////////////////////////////////////////////////////////////////////////////
60    
61    DSView(HostTable hosttable, MssHost localhost)
62    {
63      /* Copy references */
64      this.hosttable = hosttable;
65      
66      /* Instantiates bit sets */
67      reachableSet      = new BooleanSet(hosttable.size());
68      reachableSet.set(localhost.getIndex());
69      newUnreachableSet = new BooleanSet(hosttable.size());
70      newReachableSet   = new BooleanSet(hosttable.size());   
71      newIncarnationSet = new BooleanSet(hosttable.size()); 
72    }
73  
74  
75    ////////////////////////////////////////////////////////////////////////////////////////////
76    // Methods
77    ////////////////////////////////////////////////////////////////////////////////////////////
78  
79    /**
80     *  Set host member as unreachable 
81     */
82    void setAsUnreachable(MssHost member)
83    {
84      reachableSet.unset(member.getIndex());
85      newReachableSet.unset(member.getIndex());
86      newUnreachableSet.set(member.getIndex());
87      newIncarnationSet.unset(member.getIndex());
88    }
89    
90    /**
91     *  Set host member as reachable 
92     */
93    void setAsReachable(MssHost member)
94    {
95      reachableSet.set(member.getIndex());
96      newReachableSet.set(member.getIndex());
97      newUnreachableSet.unset(member.getIndex());
98    }
99  
100   /**
101    *  Set host member as having a new incarnation
102    */
103   void setNewIncarnation(MssHost member)
104   {
105     newIncarnationSet.set(member.getIndex());
106   }
107 
108   /**
109    *  Clear the set of new reachable, new unreachable and new
110    *  incarnation hosts.
111    */
112   void clear()
113   {
114     newReachableSet.clear();
115     newUnreachableSet.clear();
116     newIncarnationSet.clear();
117   }
118 
119   /**
120    *  Returns true if the current view contains new reachable hosts
121    *  or new unreachable hosts.
122    */
123   boolean hasChanged()
124   {
125     return (newReachableSet.size() > 0) || (newUnreachableSet.size() > 0);
126   }
127  
128   /**
129    *  Return the array of reachable hosts 
130    */
131   MssHost[] getReachableHosts()
132   {
133     return bitToHosts(reachableSet);
134   }
135   
136   /**
137    *  Return the array of reachable hosts 
138    */
139   MssHost[] getNewReachableHosts()
140   {
141     return bitToHosts(newReachableSet);
142   }
143   
144   /**
145    *  Return the array of reachable hosts 
146    */
147   MssHost[] getNewUnreachableHosts()
148   {
149     return bitToHosts(newUnreachableSet);
150   }
151   
152   /**
153    *  Return the array of reachable hosts 
154    */
155   MssHost[] getNewIncarnationHosts()
156   {
157     return bitToHosts(newIncarnationSet);
158   }
159   
160   /**
161    *  Return the array of reachable hosts 
162    */
163   EndPoint[] getReachableEndPoints()
164   {
165     return bitToEndPoints(reachableSet);
166   }
167   
168   /**
169    *  Return the array of reachable hosts 
170    */
171   EndPoint[] getNewReachableEndPoints()
172   {
173     return bitToEndPoints(newReachableSet);
174   }
175   
176   /**
177    *  Return the array of reachable hosts 
178    */
179   EndPoint[] getNewUnreachableEndPoints()
180   {
181     return bitToEndPoints(newUnreachableSet);
182   }
183   
184   /**
185    *  Return the array of reachable hosts 
186    */
187   EndPoint[] getNewIncarnationEndPoints()
188   {
189     return bitToEndPoints(newIncarnationSet);
190   }
191 
192   
193   ////////////////////////////////////////////////////////////////////////////////////////////
194   // Private methods
195   ////////////////////////////////////////////////////////////////////////////////////////////
196 
197   private EndPoint[] bitToEndPoints(BooleanSet bitSet) 
198   { 
199     EndPoint[] members = new EndPoint[bitSet.size()];
200     for (int i=0, j=0; i < bitSet.lastIndex(); i++) {
201       if (bitSet.lookup(i)) 
202         members[j++] = hosttable.get(i).getEndPoint();
203     }
204     return members;
205   }
206   
207   private MssHost[] bitToHosts(BooleanSet bitSet) 
208   { 
209     MssHost[] members = new MssHost[bitSet.size()];
210     for (int i=0, j=0; i < bitSet.lastIndex(); i++) {
211       if (bitSet.lookup(i)) 
212         members[j++] = hosttable.get(i);
213     }
214     return members;
215   }
216 
217 
218   ////////////////////////////////////////////////////////////////////////////////////////////
219   // Object methods
220   ////////////////////////////////////////////////////////////////////////////////////////////
221 
222   public String toString()
223   {
224     StringBuilder buf = new StringBuilder("[ReachableSet: ");
225     boolean first = true;
226     for (int i = 0; i < reachableSet.lastIndex(); i++) {
227       if (reachableSet.lookup(i)) {
228         if (first) {
229           first = false;
230         } else {
231           buf.append(", ");
232         }
233         buf.append(hosttable.get(i));
234       }
235     }
236     buf.append("]");
237     return buf.toString();
238   }
239 
240 } // END DSView