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.registry;
20  
21  import java.io.Externalizable;
22  import java.io.IOException;
23  import java.io.ObjectInput;
24  import java.io.ObjectOutput;
25  
26  import jgroup.core.VMID;
27  import jgroup.relacs.types.VMIDImpl;
28  import jgroup.util.IntList;
29  
30  /**
31   *  This class is a compact representation of the set of bindings 
32   *  (active and removed) currently maintained in a registry replica.
33   *  Objects of this class are generated by the registry during merging
34   *  operations, and contains all currently active bindings, together
35   *  with the identifiers of the bindings that have been removed from
36   *  the registry.
37   *
38   *  @author Alberto Montresor
39   *  @since Jgroup 0.4
40   */
41  public final class MergingData
42    implements Externalizable
43  {
44  
45    ////////////////////////////////////////////////////////////////////////////////////////////
46    // Fields
47    ////////////////////////////////////////////////////////////////////////////////////////////
48  
49    private static final long serialVersionUID = -3216043980643626103L;
50  
51    /** 
52     *  Array of virtual machine identifiers for servers that are registered
53     *  in the registry that generated this object.
54     */
55    VMID[] vmids;
56  
57    /**
58     *  For each of the virtual machines in vmids, the numeric identifiers
59     *  of each of the bindings that have been removed from the registry.
60     */
61    IntList[] removed;
62  
63    /**
64     *  For each of the virtual machines in vmids, the array of bindings
65     *  still active.
66     */
67    BindingList[][] bindings;
68  
69  
70    ////////////////////////////////////////////////////////////////////////////////////////////
71    // Object methods
72    ////////////////////////////////////////////////////////////////////////////////////////////
73  
74    /**
75     *  Returns a string representation of this object
76     */
77    public String toString()
78    {
79      StringBuilder buf = new StringBuilder("[MergingData: ");
80      int initlen = buf.length();
81      if (bindings.length > 0) {
82        buf.append("; bindings: ");
83        for (int i = 0; i < bindings.length; i++) {
84          for (int j = 0; j < bindings[i].length; j++) {
85            buf.append(bindings[i][j]);
86            buf.append(", ");
87          }
88        }
89      }
90      if (removed.length > 0) {
91        buf.append("; removed: ");
92        for (int i = 0; i < removed.length; i++) {
93          buf.append(removed[i]);
94          buf.append(", ");
95        }
96      }
97      if (vmids.length > 0) {
98        buf.append("VMIDS: ");
99        for (int i = 0; i < vmids.length; i++) {
100         buf.append(vmids[i]);
101         buf.append(", ");
102       }
103     }
104     if (buf.length() == initlen)
105       buf.append("<empty> ]");
106     else
107       buf.append("]");
108     return buf.toString();
109   }
110 
111 
112   ////////////////////////////////////////////////////////////////////////////////////////////
113   // Externalization
114   ////////////////////////////////////////////////////////////////////////////////////////////
115 
116   /**
117    *  Marshals the content of this object to the specified output stream.
118    * 
119    *  @param out the stream to be written
120    */
121   public void writeExternal(ObjectOutput out)
122     throws IOException
123   {
124     // Write vmids
125     int size = vmids.length;
126     out.writeInt(size);
127     for (int i = 0; i < size; i++)
128       vmids[i].writeExternal(out);
129 
130     // Write removed
131     size = removed.length;
132     out.writeInt(size);
133     for (int i = 0; i < size; i++)
134       removed[i].writeExternal(out);
135 
136     // Write bindings
137     size = bindings.length;
138     out.writeInt(size);
139     for (int i = 0; i < size; i++) {
140       int size2 = bindings[i].length;
141       out.writeInt(size2);
142       for (int j = 0; j < size2; j++)
143         bindings[i][j].writeExternal(out);
144     }
145   }
146 
147 
148   /**
149    *  Restores the content of this object from the marshalled data contained
150    *  in the specified input stream.
151    * 
152    *  @param in the stream to be read
153    */
154   public void readExternal(ObjectInput in)
155     throws IOException, ClassNotFoundException
156   {
157     // Read vmids
158     int size = in.readInt();
159     vmids = new VMID[size];
160     for (int i = 0; i < size; i++) {
161       vmids[i] = new VMIDImpl();
162       vmids[i].readExternal(in);
163     }
164 
165     // Read removed
166     size = in.readInt();
167     removed = new IntList[size];
168     for (int i = 0; i < size; i++) {
169       removed[i] = new IntList();
170       removed[i].readExternal(in);
171     }
172 
173     // Read bindings
174     size = in.readInt();
175     bindings = new BindingList[size][];
176     for (int i = 0; i < size; i++) {
177       int size2 = in.readInt();
178       bindings[i] = new BindingList[size2];
179       for (int j = 0; j < size2; j++) {
180         bindings[i][j] = new BindingList();
181         bindings[i][j].readExternal(in);
182       }
183     }
184   }
185 
186 } // END MergingData