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 java.util.HashMap;
22 import java.util.Iterator;
23
24 import jgroup.core.ConfigurationException;
25 import jgroup.core.EndPoint;
26
27 /**
28 * The <code> HostTable</code> class
29 *
30 * @author Jgroup Team
31 * @since Jgroup 0.5
32 */
33 public final class HostTable
34 {
35
36 ////////////////////////////////////////////////////////////////////////////////////////////
37 // Constants
38 ////////////////////////////////////////////////////////////////////////////////////////////
39
40 /** Initial capacity of the table */
41 private final static int CAPACITY = 32;
42
43 ////////////////////////////////////////////////////////////////////////////////////////////
44 // Fields
45 ////////////////////////////////////////////////////////////////////////////////////////////
46
47 /** Hash map containing the hosts */
48 private HashMap map;
49
50 /** Table of MssHost */
51 private MssHost[] table;
52
53 /** Actual size of the table */
54 private int size;
55
56 /** This host */
57 private static MssHost me;
58
59
60 ////////////////////////////////////////////////////////////////////////////////////////////
61 // Constructors
62 ////////////////////////////////////////////////////////////////////////////////////////////
63
64 /**
65 * Creates an empty table.
66 */
67 public HostTable()
68 {
69 map = new HashMap();
70 table = new MssHost[CAPACITY];
71 }
72
73
74 ////////////////////////////////////////////////////////////////////////////////////////////
75 // Public methods
76 ////////////////////////////////////////////////////////////////////////////////////////////
77
78 /**
79 * Inserts host <code>host</code> in the table, unless an host with the same key
80 * is already in the table.
81 *
82 * @param host the host to be inserted.
83 * @exception ConfigurationException if the element is already in the table
84 */
85 public void insert(MssHost host)
86 throws ConfigurationException
87 {
88 if (host == null)
89 throw new NullPointerException();
90 if (map.containsKey(host.getEndPoint()))
91 throw new ConfigurationException(host + " already present in hosttable.");
92 if (host.isLocal()) {
93 if (me == null) {
94 me = host;
95 }
96 }
97 if (size >= table.length) {
98 doubleCapacity();
99 }
100 table[size++] = host;
101 map.put(host.getEndPoint(), host);
102 host.setIndex(size-1);
103 }
104
105
106 /**
107 * Returns the host reference to which this address is mapped.
108 *
109 * @param endpoint host endpoint
110 * @return the host reference to which this address is mapped.
111 */
112 public MssHost lookup(EndPoint endpoint)
113 {
114 return (MssHost) map.get(endpoint);
115 }
116
117
118 /**
119 * Returns the host reference having the specified index
120 *
121 * @param index host index
122 * @return the host reference having the specified index.
123 */
124 public MssHost get(int index)
125 {
126 if (index >= size)
127 throw new IndexOutOfBoundsException();
128 return table[index];
129 }
130
131
132 /**
133 * Returns the <code>MssHost</code> descriptor of the local host.
134 *
135 * @throws IllegalStateException
136 * Raised if the local host has not yet been initialized.
137 */
138 public static MssHost getLocalHost()
139 {
140 if (me == null)
141 throw new IllegalStateException("Local host not yet initialized in the HostTable.");
142 return me;
143 }
144
145
146 /**
147 * Return the number of hosts contained in the table.
148 */
149 public int size()
150 {
151 return size;
152 }
153
154
155 /**
156 * Return an iterator over the values in this host table.
157 */
158 public Iterator iterator()
159 {
160 return map.values().iterator();
161 }
162
163
164 /**
165 * Reset the message flow of all hosts in this
166 * <code>HostTable</code>.
167 */
168 void resetMsgFlow()
169 {
170 for (Iterator iter = map.values().iterator(); iter.hasNext(); ) {
171 MssHost host = (MssHost) iter.next();
172 host.resetMsgFlow();
173 }
174 }
175
176
177 ////////////////////////////////////////////////////////////////////////////////////////////
178 // Methods from Object
179 ////////////////////////////////////////////////////////////////////////////////////////////
180
181 /**
182 * Returns a string representation of this object
183 */
184 public String toString()
185 {
186 StringBuilder buffer = new StringBuilder();
187 buffer.append("[HostTable: {");
188 for (int i=0; i < size; i++) {
189 if (i!=0)
190 buffer.append(",");
191 buffer.append(table[i]);
192 }
193 buffer.append("}]");
194 return buffer.toString();
195 }
196
197 ////////////////////////////////////////////////////////////////////////////////////////////
198 // Private methods
199 ////////////////////////////////////////////////////////////////////////////////////////////
200
201 /**
202 * Double the capacity of the table
203 *
204 */
205 private void doubleCapacity()
206 {
207 MssHost[] newtable = new MssHost[table.length*2];
208 System.arraycopy(table, 0, newtable, 0, table.length);
209 table = newtable;
210 }
211
212
213 } // END HostTable