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 /*
22 * FIXME: This data structure is not dynamic and uses the index
23 * of the host in the cluster; should change to a dynamic approach.
24 */
25 //FIXME clusterWindow is important only for members of the same
26 //cluster. HEIN: What exactly does this mean?
27
28 /**
29 * The <code>ClusterWindow</code> class maintains, for each member host
30 * in a cluster, the last message that it has delivered.
31 *
32 * @author Salvatore Cammarata
33 * @author Hein Meling
34 * @since Jgroup 1.2
35 */
36 final class ClusterWindow
37 implements MssConstants
38 {
39
40 ////////////////////////////////////////////////////////////////////////////////////////////
41 // Fields
42 ////////////////////////////////////////////////////////////////////////////////////////////
43
44 /** Last msg delivered for each member in the cluster */
45 private int[] lmd;
46
47
48 ////////////////////////////////////////////////////////////////////////////////////////////
49 // Constructors
50 ////////////////////////////////////////////////////////////////////////////////////////////
51
52 ClusterWindow(int members)
53 {
54 lmd = new int[members];
55 for (int i=0; i < members; i++)
56 lmd[i] = UNDEF;
57 }
58
59
60 ////////////////////////////////////////////////////////////////////////////////////////////
61 // Methods
62 ////////////////////////////////////////////////////////////////////////////////////////////
63
64 /**
65 * Returns the last message delivered in the cluster.
66 */
67 int getLastMsgDelivered()
68 {
69 int min = UNDEF;
70 int i=0;
71
72 while (i < lmd.length && lmd[i] == UNDEF)
73 i++;
74 if (i < lmd.length)
75 min = lmd[i++];
76 while (i < lmd.length) {
77 if (lmd[i] != UNDEF && lmd[i] < min)
78 min = lmd[i];
79 i++;
80 }
81 return min;
82 }
83
84
85 /**
86 * Sets the last message delivered for the host identified by the
87 * given index in this cluster.
88 *
89 * @param index
90 * The index of the host in this cluster
91 * @param lastMsgDelivered
92 * The identifier of the last message delivered by the given host
93 */
94 int set(int index, int lastMsgDelivered)
95 {
96 int ret = lmd[index];
97 lmd[index] = lastMsgDelivered;
98 return ret;
99 }
100
101
102 /**
103 * Clear the cluster window state.
104 */
105 void clear()
106 {
107 for (int i = 0; i < lmd.length; i++)
108 lmd[i] = UNDEF;
109 }
110
111
112 ////////////////////////////////////////////////////////////////////////////////////////////
113 // Methods
114 ////////////////////////////////////////////////////////////////////////////////////////////
115
116 /**
117 * Returns a string representation of this object
118 */
119 public String toString()
120 {
121 StringBuilder buf = new StringBuilder("[ClusterWindow: ");
122 for (int i = 0; i < lmd.length; i++) {
123 buf.append((i != 0) ? ", " : "");
124 buf.append("lastMsgDlvr[");
125 buf.append(i);
126 buf.append("]=");
127 buf.append(lmd[i]);
128 }
129 buf.append("]");
130 return buf.toString();
131 }
132
133 } // END ClusterWindow