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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
|
/*
* Copyright (c) 2008 Paul C. Buetow, vs-sim@dev.buetow.org
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* All icons of the icons/ folder are under a Creative Commons
* Attribution-Noncommercial-Share Alike License a CC-by-nc-sa.
*
* The icon's homepage is http://code.google.com/p/ultimate-gnome/
*/
package events;
import java.util.*;
import prefs.*;
import core.*;
import utils.*;
/**
* The class VSRegisteredEvents. This class is responsible to manage all
* events. It manages the event classnames, the event shortnames and the event
* names. It also checks if a protocol (which is an event as well) has
* variables which are editable through the GUI of the simulator.
*
* @author Paul C. Buetow
*/
public final class VSRegisteredEvents {
/** The serial version uid */
private static final long serialVersionUID = 1L;
/** 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;
/**
* Registers available events.
*
* @param prefs_ the prefs_
*/
public static void init(VSPrefs prefs_) {
prefs = prefs_;
/*
registerEvent("events.implementations.VSProcessCrashEvent",
"Prozessabsturz");
registerEvent("events.implementations.VSProcessRecoverEvent",
"Prozesswiederbelebung");
registerEvent("protocols.implementations.VSBasicMulticastProtocol",
"Basic Multicast", "Basic Multicast");
registerEvent("protocols.implementations.VSBerkelyTimeProtocol",
"Berkeley Algorithmus zur internen Sync.", "Berkeley");
registerEvent("protocols.implementations.VSBroadcastProtocol",
"Broadcast");
registerEvent("protocols.implementations.VSDummyProtocol",
"Beispiel/Dummy");
registerEvent("protocols.implementations.VSExternalTimeSyncProtocol",
"Christians Methode zur externen Sync.", "Christians");
registerEvent("protocols.implementations.VSInternalTimeSyncProtocol",
"Interne Synchronisation", "Interne Sync.");
registerEvent("protocols.implementations.VSOnePhaseCommitProtocol",
"Ein-Phasen Commit", "1-Phasen Commit");
registerEvent("protocols.implementations.VSPingPongProtocol",
"Ping Pong");
registerEvent("protocols.implementations.VSReliableMulticastProtocol",
"Reliable Multicast", "Reliable Multicast");
registerEvent("protocols.implementations.VSTwoPhaseCommitProtocol",
"Zwei-Phasen Commit", "2-Phasen Commit");
*/
/* Using the NEW way. The old way above (specifying protocol names
in this file still works as well */
registerEvent("events.implementations.VSProcessCrashEvent");
registerEvent("events.implementations.VSProcessRecoverEvent");
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");
/* 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,
new Boolean(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.en." + eventClassname);
String eventShortname =
prefs.getString("lang.en." + eventClassname + ".short");
registerEvent(eventClassname, eventName, eventShortname);
}
/**
* Registers an event.
*
* @param eventClassname the event classname
* @param eventName the event name
*/
private static void registerEvent(String eventClassname, String eventName) {
registerEvent(eventClassname, eventName, null);
}
/**
* 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);
}
}
|