1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package jgroup.relacs.mss;
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.EndPoint;
27 import jgroup.relacs.types.GroupIndex;
28
29
30
31
32
33
34
35
36 public final class RoutingTable
37 implements MssConstants, Externalizable
38 {
39
40
41
42
43
44
45 TopologyEntry[] table;
46
47
48 private transient int marshalSize;
49
50
51 private transient int threshold;
52
53
54 private transient ClusterTable clustertable;
55
56
57
58
59
60
61
62
63
64 public RoutingTable()
65 {
66 }
67
68
69
70
71 public RoutingTable(ClusterTable ct, int threshold)
72 {
73 marshalSize = GroupIndex.SIZE;
74 this.clustertable = ct;
75 this.threshold = threshold;
76
77 table = new TopologyEntry[ct.size()];
78 for (int i=0; i<table.length; i++) {
79 Cluster cluster = ct.get(i);
80 table[i] = new TopologyEntry(cluster.getRoutingEntry(), cluster.getReachable(threshold));
81 marshalSize += table[i].getMarshalSize();
82 }
83 }
84
85
86
87
88
89
90
91
92
93
94 public int getMarshalSize()
95 {
96 return marshalSize;
97 }
98
99
100
101
102
103
104
105 public static int getMaxSize(int numOfHosts, int numOfClusters)
106 {
107 return GroupIndex.SIZE + (numOfClusters*TopologyEntry.getMaxSize(numOfHosts));
108 }
109
110
111
112
113
114
115
116
117
118
119 public boolean updateReachability()
120 {
121 marshalSize = GroupIndex.SIZE;
122 boolean changed = false;
123
124
125
126
127
128
129
130 if (table.length != clustertable.size()) {
131 table = new TopologyEntry[clustertable.size()];
132 for (int i = 0; i < table.length; i++) {
133 Cluster cluster = clustertable.get(i);
134 table[i] = new TopologyEntry(cluster.getRoutingEntry(), cluster.getReachable(threshold));
135 marshalSize += table[i].getMarshalSize();
136 }
137 changed = true;
138 } else {
139 for (int i = 0; i < table.length; i++) {
140 Cluster cluster = clustertable.get(i);
141
142
143
144
145 changed |= table[i].updateReachability(cluster.getReachable(threshold));
146 marshalSize += table[i].getMarshalSize();
147 }
148 }
149 return changed;
150 }
151
152
153
154
155
156 public void splitHorizonOn(EndPoint key)
157 {
158 for (int i=0; i < table.length; i++)
159 if (table[i].routing.route.equals(key))
160 table[i].routing.cost = -table[i].routing.cost;
161 }
162
163
164
165
166
167 public void splitHorizonOff()
168 {
169 for (int i=0; i < table.length; i++)
170 if (table[i].routing.cost < 0)
171 table[i].routing.cost = -table[i].routing.cost;
172 }
173
174
175
176
177
178
179
180
181
182
183
184
185 public void readExternal(ObjectInput in)
186 throws IOException, ClassNotFoundException
187 {
188
189 int entries = GroupIndex.unmarshal(in);
190 table = new TopologyEntry[entries];
191 for (int i=0; i < entries; i++) {
192 table[i] = new TopologyEntry();
193 table[i].readExternal(in);
194 }
195 }
196
197
198
199
200
201
202
203 public void writeExternal(ObjectOutput out)
204 throws IOException
205 {
206 GroupIndex.marshal(out, table.length);
207 for (int i=0; i<table.length; i++)
208 table[i].writeExternal(out);
209 }
210
211
212
213
214
215
216
217
218
219 public String toString()
220 {
221 StringBuilder buf = new StringBuilder("[RoutingTable: length=");
222 buf.append(table.length);
223 buf.append(", {");
224 for (int i = 0; i < table.length; i++) {
225 buf.append((i != 0) ? ", " : "");
226 buf.append(table[i].toString());
227 }
228 buf.append("}]");
229 return buf.toString();
230 }
231
232 }
233