1 package nl.tno.imb.mc;
2
3 import java.util.HashMap;
4 import java.util.Map;
5
6 import org.opentrafficsim.imb.SelfWrapper;
7
8 import nl.tno.imb.TByteBuffer;
9
10
11
12
13
14
15
16
17
18
19
20
21 public abstract class ModelEvent implements SelfWrapper
22 {
23
24
25
26
27
28 public abstract void prepare(TByteBuffer payload);
29
30
31
32
33
34 public abstract void qWrite(TByteBuffer payload);
35
36
37
38
39 class DeleteEvent extends ModelEvent
40 {
41
42 final int uid;
43
44
45
46
47
48 public DeleteEvent(TByteBuffer payload)
49 {
50 this.uid = payload.readInt32();
51 }
52
53
54 @Override
55 public void prepare(TByteBuffer payload)
56 {
57 payload.prepare(this.uid);
58 }
59
60
61 @Override
62 public void qWrite(TByteBuffer payload)
63 {
64 payload.qWrite(this.uid);
65 }
66
67 }
68
69
70
71
72 class ModelChangeEvent
73 {
74
75 final public int uid;
76
77
78 final ModelState state;
79
80
81 final String federation;
82
83
84
85
86
87 ModelChangeEvent(TByteBuffer payload)
88 {
89 this.uid = payload.readInt32();
90 int stateCode = payload.readInt32();
91 this.state = ModelState.byValue(stateCode);
92 this.federation = payload.readString();
93 }
94
95
96
97
98
99 public int getUid()
100 {
101 return this.uid;
102 }
103
104
105
106
107
108 public ModelState getState()
109 {
110 return this.state;
111 }
112
113
114
115
116
117 public String getFederation()
118 {
119 return this.federation;
120 }
121
122 }
123
124
125
126
127 enum ModelCommand
128 {
129
130
131 CLAIM(11),
132
133 UNCLAIM(12),
134
135
136 MODEL(21),
137
138 REQUEST_MODELS(22),
139
140
141 DEFAULT_PARAMETERS(31),
142
143 REQUEST_DEFAULT_PARAMETERS(32),
144
145
146 FOLDER_CONTENTS(41),
147
148 REQUEST_FOLDER_CONTENTS(42),
149
150
151 CONTROLLER(51),
152
153 REQUEST_CONTROLLERS(52),
154
155 CONTROLLER_CHANGE(53),
156
157
158 CONTROLLER_MODEL_SETUP(61),
159
160 REQUEST_CONTROLLER_MODEL_SETUPS(62),
161
162 CONTROLLER_MODEL_SETUP_NDW(63),
163
164 CONTROLLER_MODEL_SETUP_DELETE(64),
165
166 CONTROLLER_MODEL_SETUP_CHANGE(65),
167
168
169 INIT(71),
170
171 QUIT_APPLICATION(72),
172
173 PROGRESS(73);
174
175
176 private final int value;
177
178
179 protected static Map<Integer, ModelCommand> commandMap = new HashMap<>();
180
181 static
182 {
183 for (ModelCommand modelCommand : values())
184 {
185 commandMap.put(modelCommand.getValue(), modelCommand);
186 }
187 }
188
189
190
191
192
193 private ModelCommand(final int value)
194 {
195 this.value = value;
196 }
197
198
199
200
201
202 public final int getValue()
203 {
204 return this.value;
205 }
206
207
208
209
210
211
212
213 protected static ModelCommand byValue(final int value)
214 {
215 return commandMap.get(value);
216 }
217
218 }
219
220 }
221
222
223
224
225 class ChangeEvent extends ModelEvent
226 {
227
228 final int uid;
229
230
231 final ModelState state;
232
233
234 final String federation;
235
236
237
238
239
240 public ChangeEvent(TByteBuffer payload)
241 {
242 this.uid = payload.readInt32();
243 this.state = ModelState.byValue(payload.readInt32());
244 this.federation = payload.readString();
245 }
246
247
248
249
250
251
252
253 public ChangeEvent(final int uid, final ModelState state, final String federation)
254 {
255 this.uid = uid;
256 this.state = state;
257 this.federation = federation;
258 }
259
260
261 @Override
262 public void prepare(TByteBuffer payload)
263 {
264 payload.prepare(this.uid);
265 payload.prepare(this.state.getValue());
266 payload.prepare(this.federation);
267 }
268
269
270 @Override
271 public void qWrite(TByteBuffer payload)
272 {
273 payload.qWrite(this.uid);
274 payload.qWrite(this.state.getValue());
275 payload.qWrite(this.federation);
276 }
277
278 }
279
280
281
282
283 class InitEvent extends ModelEvent
284 {
285
286 final long linkId;
287
288
289 final int uid;
290
291
292 final String modelName;
293
294
295 final String modelPrivateEventName;
296
297
298
299
300
301 public InitEvent(final TByteBuffer payload)
302 {
303 this.linkId = payload.readInt64();
304 this.uid = payload.readInt32();
305 this.modelName = payload.readString();
306 this.modelPrivateEventName = payload.readString();
307 }
308
309
310
311
312
313
314
315
316 public InitEvent(final long linkId, final int uid, final String modelName, final String modelPrivateEventName)
317 {
318 this.linkId = linkId;
319 this.uid = uid;
320 this.modelName = modelName;
321 this.modelPrivateEventName = modelPrivateEventName;
322 }
323
324
325 @Override
326 public void prepare(final TByteBuffer payload)
327 {
328 payload.prepare(this.linkId);
329 payload.prepare(this.uid);
330 payload.prepare(this.modelName);
331 payload.prepare(this.modelPrivateEventName);
332 }
333
334
335 @Override
336 public void qWrite(final TByteBuffer payload)
337 {
338 payload.qWrite(this.linkId);
339 payload.qWrite(this.uid);
340 payload.qWrite(this.modelName);
341 payload.qWrite(this.modelPrivateEventName);
342 }
343
344 }
345
346
347
348
349 class NewEvent extends ModelEvent
350 {
351
352 final int uid;
353
354
355 final String modelName;
356
357
358 final String controller;
359
360
361 final int priority;
362
363
364 final ModelState state;
365
366
367 final String federation;
368
369
370 final String privateEventName;
371
372
373 final String controllerPrivateEventName;
374
375
376
377
378
379 public NewEvent(final TByteBuffer payload)
380 {
381 this.uid = payload.readInt32();
382 this.modelName = payload.readString();
383 this.controller = payload.readString();
384 this.priority = payload.readInt32();
385 this.state = ModelState.byValue(payload.readInt32());
386 this.federation = payload.readString();
387 this.privateEventName = payload.readString();
388 this.controllerPrivateEventName = payload.readString();
389 }
390
391
392
393
394
395
396
397
398
399
400
401
402 public NewEvent(final int uid, final String modelName, final String controller, final int priority, final ModelState state,
403 final String federation, final String privateEventName, final String controllerPrivateEventName)
404 {
405 this.uid = uid;
406 this.modelName = modelName;
407 this.controller = controller;
408 this.priority = priority;
409 this.state = state;
410 this.federation = federation;
411 this.privateEventName = privateEventName;
412 this.controllerPrivateEventName = controllerPrivateEventName;
413 }
414
415
416 @Override
417 public void prepare(TByteBuffer payload)
418 {
419 payload.prepare(this.uid);
420 payload.prepare(this.modelName);
421 payload.prepare(this.controller);
422 payload.prepare(this.priority);
423 payload.prepare(this.state.getValue());
424 payload.prepare(this.federation);
425 payload.prepare(this.privateEventName);
426 payload.prepare(this.controllerPrivateEventName);
427 }
428
429
430 @Override
431 public void qWrite(TByteBuffer payload)
432 {
433 payload.qWrite(this.uid);
434 payload.qWrite(this.modelName);
435 payload.qWrite(this.controller);
436 payload.qWrite(this.priority);
437 payload.qWrite(this.state.getValue());
438 payload.qWrite(this.federation);
439 payload.qWrite(this.privateEventName);
440 payload.qWrite(this.controllerPrivateEventName);
441 }
442
443 }