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 * The IMB Model events.
12 * <p>
13 * Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
14 * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
15 * <p>
16 * @version $Revision$, $LastChangedDate$, by $Author$, initial version Oct 14, 2016 <br>
17 * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
18 * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
19 * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
20 */
21 public abstract class ModelEvent implements SelfWrapper
22 {
23
24 /**
25 * Prepare this ModelEvent for transmission over IMB.
26 * @param payload TByteBuffer; the transmit buffer
27 */
28 public abstract void prepare(TByteBuffer payload);
29
30 /**
31 * Transmit this ModelEvent over IMB.
32 * @param payload TByteBuffer; the transmit buffer
33 */
34 public abstract void qWrite(TByteBuffer payload);
35
36 /**
37 * Delete event.
38 */
39 class DeleteEvent extends ModelEvent
40 {
41 /** UID. */
42 final int uid;
43
44 /**
45 * Construct a new DeleteEvent from data received over IMB.
46 * @param payload TByteBuffer; the received IMB data
47 */
48 public DeleteEvent(TByteBuffer payload)
49 {
50 this.uid = payload.readInt32();
51 }
52
53 /** {@inheritDoc} */
54 @Override
55 public void prepare(TByteBuffer payload)
56 {
57 payload.prepare(this.uid);
58 }
59
60 /** {@inheritDoc} */
61 @Override
62 public void qWrite(TByteBuffer payload)
63 {
64 payload.qWrite(this.uid);
65 }
66
67 }
68
69 /**
70 * Parse and store the content of an IMB model change event.
71 */
72 class ModelChangeEvent
73 {
74 /** Id. */
75 final public int uid;
76
77 /** Mode state. */
78 final ModelState state;
79
80 /** The IMB federation. */
81 final String federation;
82
83 /**
84 * Construct a new ModelChangeEvent from an IMB message.
85 * @param payload TByteBuffer; the IMB message
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 * Retrieve the Id.
97 * @return int
98 */
99 public int getUid()
100 {
101 return this.uid;
102 }
103
104 /**
105 * Retrieve the model state.
106 * @return ModelState; the model state
107 */
108 public ModelState getState()
109 {
110 return this.state;
111 }
112
113 /**
114 * Retrieve the federation.
115 * @return String; the federation
116 */
117 public String getFederation()
118 {
119 return this.federation;
120 }
121
122 }
123
124 /**
125 * Commands handled by the command interpreter.
126 */
127 enum ModelCommand
128 {
129
130 /** Claim the model. */
131 CLAIM(11),
132 /** Unclaim the model. */
133 UNCLAIM(12),
134
135 /** ???. */
136 MODEL(21),
137 /** Request models. */
138 REQUEST_MODELS(22),
139 /** Default parameters. */
140
141 DEFAULT_PARAMETERS(31),
142 /** Request default parameters. */
143 REQUEST_DEFAULT_PARAMETERS(32),
144
145 /** Folder contents. */
146 FOLDER_CONTENTS(41),
147 /** Request folder contents. */
148 REQUEST_FOLDER_CONTENTS(42),
149
150 /** Controller. */
151 CONTROLLER(51),
152 /** Request controllers. */
153 REQUEST_CONTROLLERS(52),
154 /** Controller change. */
155 CONTROLLER_CHANGE(53),
156
157 /** Controller model setup. */
158 CONTROLLER_MODEL_SETUP(61),
159 /** Request controller model setups. */
160 REQUEST_CONTROLLER_MODEL_SETUPS(62),
161 /** Controller model setup new. */
162 CONTROLLER_MODEL_SETUP_NDW(63),
163 /** Controller model setup delete. */
164 CONTROLLER_MODEL_SETUP_DELETE(64),
165 /** Controller model setup change. */
166 CONTROLLER_MODEL_SETUP_CHANGE(65),
167
168 /** Init. */
169 INIT(71),
170 /** Quit application. */
171 QUIT_APPLICATION(72),
172 /** Progress. */
173 PROGRESS(73);
174
175 /** Value that represent this model command in transit over IMB. */
176 private final int value;
177
178 /** Map to translate numeric value to enum. */
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 * Construct a new ModelCommand
191 * @param value
192 */
193 private ModelCommand(final int value)
194 {
195 this.value = value;
196 }
197
198 /**
199 * Retrieve the value that represents this model command during transmission over IMB.
200 * @return int; the value that represents this model command during transmission over IMB
201 */
202 public final int getValue()
203 {
204 return this.value;
205 }
206
207 /**
208 * Lookup the ModelCommand that corresponds to the value.
209 * @param value int; the value to look up
210 * @return ModelCommand; the ModelCommand that corresponds to the value, or null if no ModelCommand with the specified
211 * value is defined
212 */
213 protected static ModelCommand byValue(final int value)
214 {
215 return commandMap.get(value);
216 }
217
218 }
219
220 }
221
222 /**
223 * New event.
224 */
225 class ChangeEvent extends ModelEvent
226 {
227 /** UID. */
228 final int uid;
229
230 /** The state. */
231 final ModelState state;
232
233 /** The IMB federation. */
234 final String federation;
235
236 /**
237 * Construct a new ChangeEvent from data received over IMB.
238 * @param payload TByteBuffer; the received IMB data
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 * Construct a new ChangeEvent from uid, state and federation parameters.
249 * @param uid int; the uid of the new ChangeEvent
250 * @param state int; the state of the new ChangeEvent
251 * @param federation String; the federation of the new ChangeEvent
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 /** {@inheritDoc} */
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 /** {@inheritDoc} */
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 * Init event.
282 */
283 class InitEvent extends ModelEvent
284 {
285 /** Id of the link. */
286 final long linkId;
287
288 /** UID. */
289 final int uid;
290
291 /** Model name. */
292 final String modelName;
293
294 /** Private event name. */
295 final String modelPrivateEventName;
296
297 /**
298 * Construct an InitEvent from a TByteBuffer.
299 * @param payload TByteBuffer; the received IMB data
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 * Construct an InitEvent.
311 * @param linkId long; the link id
312 * @param uid int; the uid
313 * @param modelName String; the model name
314 * @param modelPrivateEventName String; the model private event name
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 /** {@inheritDoc} */
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 /** {@inheritDoc} */
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 * New event.
348 */
349 class NewEvent extends ModelEvent
350 {
351 /** UID. */
352 final int uid;
353
354 /** Name of the model. */
355 final String modelName;
356
357 /** The controller. */
358 final String controller;
359
360 /** The priority. */
361 final int priority;
362
363 /** The model state. */
364 final ModelState state;
365
366 /** The IMB federation. */
367 final String federation;
368
369 /** Private event name. */
370 final String privateEventName;
371
372 /** Controller private event name. */
373 final String controllerPrivateEventName;
374
375 /**
376 * Construct a new NewEvent from data received over IMB.
377 * @param payload TByteBuffer; the received IMB data
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 * Construct a new NewEvent.
393 * @param uid int; the uid
394 * @param modelName string; the name of the model
395 * @param controller String; the name of the controller (?)
396 * @param priority int; the priority
397 * @param state ModelState; the state of the model
398 * @param federation String; the IMB federation
399 * @param privateEventName String; ???
400 * @param controllerPrivateEventName String; ???
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 /** {@inheritDoc} */
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 /** {@inheritDoc} */
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 }