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.types;
20  
21  import java.io.IOException;
22  import java.io.ObjectInput;
23  import java.io.ObjectOutput;
24  
25  import jgroup.core.IID;
26  import jgroup.core.VMID;
27  
28  /**
29   *  Instances of this class uniquely identify invocations issued by a
30   *  client.  Each invocation identifier is composed of a virtual machine
31   *  identifier, identifying the client, and a progressive counter used
32   *  to distinguish among invocations performed by the same client.
33   *
34   *  @author Alberto Montresor
35   *  @since Jgroup 0.9
36   */
37  public final class IIDImpl
38    implements IID
39  {
40  
41    ////////////////////////////////////////////////////////////////////////////////////////////
42    // Fields
43    ////////////////////////////////////////////////////////////////////////////////////////////
44  
45    private static final long serialVersionUID = 3101263354396256254L;
46  
47    /* Identifier for the virtual machine that generated this invocation. */
48    private VMID vmid;
49  
50    /* Sequence number for this invocation. */
51    private int counter;
52  
53    /* Acknowledge counter for this invocation. */
54    private int ack;
55  
56  
57    ////////////////////////////////////////////////////////////////////////////////////////////
58    // Constructors
59    ////////////////////////////////////////////////////////////////////////////////////////////
60  
61    /**
62     *  Default public constructor for externalization.
63     */
64    public IIDImpl()
65    {
66    }
67  
68  
69    /**
70     *  Constructs a new invocation identifier.
71     */
72    public IIDImpl(VMID vmid, int counter, int ack)
73    {
74      this.ack = ack;
75      this.counter = counter;
76      this.vmid = vmid;
77    }
78  
79  
80    ////////////////////////////////////////////////////////////////////////////////////////////
81    // IID interface methods
82    ////////////////////////////////////////////////////////////////////////////////////////////
83  
84    /**
85     *  Returns the <code>VMID</code> of the (client) virtual machine that
86     *  generated this invocation.
87     */
88    public VMID getVmid()
89    {
90      return vmid;
91    }
92  
93  
94    /**
95     *  Returns the sequential counter identifying this particular
96     *  invocation.
97     */
98    public int getCounter()
99    {
100     return counter;
101   }
102 
103 
104   ////////////////////////////////////////////////////////////////////////////////////////////
105   // Package methods
106   ////////////////////////////////////////////////////////////////////////////////////////////
107 
108   /**
109    *  Returns ack information for this invocation identifier.
110    */
111   public int getAck()
112   {
113     return ack;
114   }
115 
116 
117   ////////////////////////////////////////////////////////////////////////////////////////////
118   // Object methods
119   ////////////////////////////////////////////////////////////////////////////////////////////
120 
121   /**
122    *  Returns a string representation of this object
123    */
124   public String toString()
125   {
126     StringBuilder buf = new StringBuilder();
127     buf.append("[IID: ");
128     buf.append(vmid.toString());
129     buf.append(", ");
130     buf.append(counter);
131     buf.append(", ");
132     buf.append(ack);
133     buf.append("]");
134     return buf.toString();
135   }
136 
137 
138   /**
139    *  Returns a hashcode for the <code>IID</code>.  Two
140    *  <code>IID</code>s will have the same hashcode if they are equal
141    *  with respect to their content.
142    */
143   public int hashCode()
144   {
145     return vmid.hashCode() + counter;
146   }
147 
148 
149   /**
150    *  Compares two objects for content equality.
151    *
152    *  @param obj 
153    *    the object to compare with
154    *  @return 
155    *    true if these objects are equal; false otherwise.
156    */
157   public boolean equals(Object obj)
158   {
159     if (!(obj instanceof IIDImpl))
160       return false;
161     IIDImpl iid = (IIDImpl) obj;
162     return (vmid.equals(iid.vmid) && counter == iid.counter);
163   }
164 
165 
166   ////////////////////////////////////////////////////////////////////////////////////////////
167   // Externalizable methods
168   ////////////////////////////////////////////////////////////////////////////////////////////
169 
170   /**
171    *  Write the content of this IID on stream <tt>out</tt>.  Note that
172    *  method writeExternal of vmid is called, to improve the efficiency
173    *  of this method.
174    * 
175    *  @param out the stream to be written
176    */
177   public void writeExternal(ObjectOutput out)
178     throws IOException
179   {
180     out.writeInt(ack);
181     out.writeInt(counter);
182     vmid.writeExternal(out);
183   }
184 
185 
186   /**
187    *  Restores the content of this object from the marshalled data contained
188    *  in the specified input stream.
189    * 
190    *  @param in the stream to be read
191    */
192   public void readExternal(ObjectInput in)
193     throws IOException, ClassNotFoundException
194   {
195     ack = in.readInt();
196     counter = in.readInt();
197     vmid = new VMIDImpl();
198     vmid.readExternal(in);
199   }
200 
201 } // END IIDImpl