View Javadoc

1   /*
2    * Copyright (c) 1998-2004 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  package jgroup.relacs.gm;
19  
20  import java.io.Serializable;
21  
22  import jgroup.core.Layer;
23  import jgroup.core.View;
24  import jgroup.core.Layer.FinalizeLayer;
25  
26  /**
27   * Service interface for the timestamp layer.
28   * 
29   * @author Rohnny Moland
30   */
31  public interface TimestampService
32    extends Layer, FinalizeLayer
33  {
34    /**
35     * Returns a <code>Timestamp</code> object, and updates
36     * the timestamp counter.
37     */
38    public Timestamp getTimestamp(int id);
39  
40    /**
41     * Returns the timestamp counter value, and updates the
42     * timestamp counter.  This is useful for applications that
43     * do not need distinguish between groups and different views.
44     * That is, if the application is such that the counter value
45     * will only be used within the same group, and that view
46     * changes are handled otherwise.
47     */
48    public long getTimestampCounter();
49  
50  
51    /**
52     * The timestamp object is used to detect duplicate invocations
53     * from several members of the same group, and yet distinguish
54     * the invocations amongst multiple groups.<p>
55     */
56    public class Timestamp
57      implements Serializable
58    {
59      private static final long serialVersionUID = 1868153781418135225L;
60  
61      private long clientGroupViewId;
62      private long timestamp;
63      public int viewSize;
64      private int gid;
65      private int id;
66  
67      Timestamp(int gid, int id, int vsize)
68      {
69        this.gid = gid;
70        this.id = id;
71        this.viewSize = vsize;
72      }
73  
74      /**
75       * Invoked by the <code>TimestampLayer</code> of the client group
76       * in response to a view change in the client group.
77       */
78      void viewChange(View view)
79      {
80        clientGroupViewId = view.getVid();
81        viewSize = view.size();
82      }
83  
84      void updateTimestamp()
85      {
86        timestamp++;
87      }
88  
89      /**
90       * Returns the timestamp object that is the greatest among the given
91       * timestamp object and this timestamp object.
92       */
93      Timestamp greatest(Timestamp ts)
94      {
95        if (timestamp >= ts.timestamp)
96          return this;
97        else
98          return ts;
99      }
100 
101     public boolean equals(Object obj) 
102     {
103       if (obj instanceof Timestamp) {
104         Timestamp t = (Timestamp) obj;
105         if (timestamp == t.timestamp && gid == t.gid && id == t.id)
106           return true;
107         return false;
108       }
109       return false;
110     }
111 
112     public int hashCode() 
113     {
114       return gid ^ viewSize ^ id
115         ^ (int)(timestamp ^ (timestamp >>> 32))
116         ^ (int)(clientGroupViewId ^ (clientGroupViewId >>> 32));
117     }
118 
119     public String toString()
120     {
121       return "[ts=" + gid + ", " + timestamp + ", " + id + ", " + hashCode() + "]";
122     }
123   } // END Timestamp
124 
125 } // End TimestampService