1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
|
package events;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Set;
import java.util.Vector;
import core.VSInternalProcess;
import prefs.VSPrefs;
import utils.VSClassLoader;
/**
* Registry and manager for all available events and protocols in the simulator.
* This class provides a centralized location for:
* <ul>
* <li>Registering event and protocol implementations</li>
* <li>Mapping between class names, short names, and display names</li>
* <li>Identifying which protocols have editable parameters</li>
* <li>Managing client/server variable metadata for protocols</li>
* </ul>
*
* <p>All events and protocols must be registered in {@link #init(VSPrefs)}
* to be available in the simulator. The registry uses reflection to discover
* protocol properties and determine which ones expose editable parameters.</p>
*
* <p>This is a static utility class and cannot be instantiated.</p>
*
* @see VSAbstractEvent
* @see protocols.VSAbstractProtocol
* @author Paul C. Buetow
*/
public final class VSRegisteredEvents {
/** The event classnames by eventnames. */
private static HashMap<String,String> eventClassnamesByNames =
new HashMap<String,String>();
/** The event shortnames by classnames. */
private static HashMap<String,String> eventShortnamesByClassnames =
new HashMap<String,String>();
/** The event names by classnames. */
private static HashMap<String,String> eventNamesByClassnames =
new HashMap<String,String>();
/** The event classnames by shortnames. */
private static HashMap<String,String> eventClassnamesByShortnames =
new HashMap<String,String>();
/** The editable protocols classnames. */
private static ArrayList<String> editableProtocolsClassnames =
new ArrayList<String>();
private static HashMap<String,ArrayList<String>> clientVariables =
new HashMap<String,ArrayList<String>>();
private static HashMap<String,ArrayList<String>> serverVariables =
new HashMap<String,ArrayList<String>>();
private static HashMap<String,Boolean> isOnServerStartProtocol =
new HashMap<String,Boolean>();
/** The prefs. */
private static VSPrefs prefs;
/**
* Initializes the event registry with all available events and protocols.
* This method must be called before any events or protocols can be used.
*
* <p>The initialization process:</p>
* <ol>
* <li>Registers all built-in events (crashes, recoveries, timestamps)</li>
* <li>Registers all protocol implementations</li>
* <li>Uses reflection to discover editable protocol parameters</li>
* <li>Builds metadata for protocol client/server variables</li>
* </ol>
*
* <p>To add a new event or protocol, add a registerEvent() call here
* with the fully qualified class name.</p>
*
* @param prefs_ the preferences object for the simulator
*/
public static void init(VSPrefs prefs_) {
prefs = prefs_;
registerEvent("events.implementations.VSProcessCrashEvent");
registerEvent("events.implementations.VSProcessRecoverEvent");
registerEvent("events.implementations.VSLamportTimestampEvent");
registerEvent("events.implementations.VSVectorTimestampEvent");
registerEvent("events.implementations.VSTimestampMonitorEvent");
registerEvent("events.implementations.VSTimestampTriggeredEvent");
registerEvent("protocols.implementations.VSBasicMulticastProtocol");
registerEvent("protocols.implementations.VSBerkelyTimeProtocol");
registerEvent("protocols.implementations.VSBroadcastProtocol");
registerEvent("protocols.implementations.VSDummyProtocol");
registerEvent("protocols.implementations.VSExternalTimeSyncProtocol");
registerEvent("protocols.implementations.VSInternalTimeSyncProtocol");
registerEvent("protocols.implementations.VSOnePhaseCommitProtocol");
registerEvent("protocols.implementations.VSPingPongProtocol");
registerEvent("protocols.implementations.VSReliableMulticastProtocol");
registerEvent("protocols.implementations.VSTwoPhaseCommitProtocol");
registerEvent("protocols.implementations.VSTimestampDemoProtocol");
/* Make dummy objects of each protocol, to see if they contain VSPrefs
values to edit */
Vector<String> protocolClassnames = getProtocolClassnames();
VSClassLoader classLoader = new VSClassLoader();
for (String protocolClassname : protocolClassnames) {
Object serverObject = classLoader.newInstance(protocolClassname);
Object clientObject = classLoader.newInstance(protocolClassname);
if (clientObject instanceof protocols.VSAbstractProtocol &&
serverObject instanceof protocols.VSAbstractProtocol) {
protocols.VSAbstractProtocol serverProtocol =
(protocols.VSAbstractProtocol) serverObject;
protocols.VSAbstractProtocol clientProtocol =
(protocols.VSAbstractProtocol) clientObject;
serverProtocol.onServerInit();
clientProtocol.onClientInit();
if (!serverProtocol.isEmpty() || !clientProtocol.isEmpty())
editableProtocolsClassnames.add(protocolClassname);
if (!serverProtocol.isEmpty()) {
ArrayList<String> variables = new ArrayList<String>();
variables.addAll(serverProtocol.getAllFullKeys());
serverVariables.put(protocolClassname, variables);
}
if (!clientProtocol.isEmpty()) {
ArrayList<String> variables = new ArrayList<String>();
variables.addAll(clientProtocol.getAllFullKeys());
clientVariables.put(protocolClassname, variables);
}
if (serverProtocol.hasOnServerStart())
isOnServerStartProtocol.put(protocolClassname,
Boolean.valueOf(true));
}
}
}
/**
* Gets the editable protocols classnames.
*
* @return the editable protocols classnames
*/
public static ArrayList<String> getEditableProtocolsClassnames() {
return editableProtocolsClassnames;
}
/**
* Gets the protocols server variable names.
*
* @return The variable names
*/
public static ArrayList<String> getProtocolServerVariables(
String protocolClassname) {
return serverVariables.get(protocolClassname);
}
/**
* Gets the protocols server variable names.
*
* @return The variable names
*/
public static ArrayList<String> getProtocolClientVariables(
String protocolClassname) {
return clientVariables.get(protocolClassname);
}
/**
* Gets the protocol names.
*
* @return the protocol names
*/
public static Vector<String> getProtocolNames() {
Set<String> set = eventClassnamesByNames.keySet();
Vector<String> vector = new Vector<String>();
for (String eventName : set)
if (getClassnameByEventname(eventName).startsWith(
"protocols.implementations"))
vector.add(eventName);
Collections.sort(vector);
return vector;
}
/**
* Gets the protocol classnames.
*
* @return the protocol classnames
*/
public static Vector<String> getProtocolClassnames() {
ArrayList<String> shortnames = new ArrayList<String>();
shortnames.addAll(eventClassnamesByShortnames.keySet());
Collections.sort(shortnames);
Vector<String> vector = new Vector<String>();
for (String eventShortname : shortnames) {
String eventClassname = getClassnameByShortname(eventShortname);
if (eventClassname.startsWith("protocols.implementations"))
vector.add(eventClassname);
}
return vector;
}
/**
* Gets the non protocol names.
*
* @return the non protocol names
*/
public static Vector<String> getNonProtocolNames() {
Set<String> set = eventClassnamesByNames.keySet();
Vector<String> vector = new Vector<String>();
for (String eventName : set)
if (getClassnameByEventname(eventName).startsWith(
"events.implementations"))
vector.add(eventName);
Collections.sort(vector);
return vector;
}
/**
* Gets the non protocol classnames.
*
* @return the non protocol classnames
*/
public static Vector<String> getNonProtocolClassnames() {
Set<String> set = eventNamesByClassnames.keySet();
Vector<String> vector = new Vector<String>();
for (String eventClassname : set)
if (eventClassname.startsWith("events.implementations"))
vector.add(eventClassname);
Collections.sort(vector);
return vector;
}
/**
* Gets the classname.
*
* @param eventName the event name
*
* @return the classname
*/
public static String getClassnameByEventname(String eventName) {
return eventClassnamesByNames.get(eventName);
}
/**
* Gets the name.
*
* @param eventClassname the event classname
*
* @return the name
*/
public static String getNameByClassname(String eventClassname) {
return eventNamesByClassnames.get(eventClassname);
}
/**
* Gets the shortname.
*
* @param eventClassname the event classname
*
* @return the shortname
*/
public static String getShortnameByClassname(String eventClassname) {
return eventShortnamesByClassnames.get(eventClassname);
}
/**
* Gets the classname.
*
* @param eventShortname the event shortname
*
* @return the shortname
*/
public static String getClassnameByShortname(String eventShortname) {
return eventClassnamesByShortnames.get(eventShortname);
}
/**
* Checks if the protocol uses onServerStart or onClientStart
*
* @param protocolClassname the protocol's classname
*
* @return true if onServerStart, false if onClientStart
*/
public static boolean isOnServerStartProtocol(String protocolClassname) {
if (isOnServerStartProtocol.containsKey(protocolClassname)) {
Boolean bool = isOnServerStartProtocol.get(protocolClassname);
return bool.booleanValue();
}
return false;
}
/**
* Creates the event instance by classname.
*
* @param eventClassname the event classname
* @param process the process
*
* @return An instance of the event classname, if exists. Else null.
*/
public static VSAbstractEvent createEventInstanceByClassname(
String eventClassname, VSInternalProcess process) {
Object protocolObj = new VSClassLoader().newInstance(eventClassname);
if (protocolObj instanceof VSAbstractEvent) {
VSAbstractEvent event = (VSAbstractEvent) protocolObj;
event.init(process);
return event;
}
return null;
}
/**
* Creates the event instance by name.
*
* @param eventName the event name
* @param process the process
*
* @return An instance of the event, if exists. Else null.
*/
public static VSAbstractEvent createEventInstanceByName(String eventName,
VSInternalProcess process) {
return createEventInstanceByClassname(
eventClassnamesByNames.get(eventName), process);
}
/**
* Registers an event. Use the language settings of VSPrefs.
*
* @param eventClassname the event classname
*/
private static void registerEvent(String eventClassname) {
String eventName =
prefs.getString("lang." + eventClassname);
String eventShortname =
prefs.getString("lang." + eventClassname + ".short");
registerEvent(eventClassname, eventName, eventShortname);
}
/**
* Registers an event.
*
* @param eventClassname the event classname
* @param eventName the event name
* @param eventShortname the event shortname
*/
private static void registerEvent(String eventClassname, String eventName,
String eventShortname) {
if (eventShortname == null)
eventShortname = eventName;
eventNamesByClassnames.put(eventClassname, eventName);
eventShortnamesByClassnames.put(eventClassname, eventShortname);
eventClassnamesByNames.put(eventName, eventClassname);
eventClassnamesByShortnames.put(eventShortname, eventClassname);
}
}
|