1 package org.opentrafficsim.imb.transceiver;
2
3 import org.opentrafficsim.core.dsol.OTSDEVSSimulatorInterface;
4 import org.opentrafficsim.imb.IMBException;
5
6 import nl.tno.imb.TByteBuffer;
7 import nl.tudelft.simulation.dsol.SimRuntimeException;
8 import nl.tudelft.simulation.event.Event;
9 import nl.tudelft.simulation.event.EventInterface;
10 import nl.tudelft.simulation.event.EventType;
11
12 /**
13 * The PubSubIMBMessageHandler handles the IMB message by transforming it into a DSOL Event and sending it to an EventListener
14 * at the current simulation time through a simulation event. The EventListener is identified by the OTSToIMBTransformer and
15 * stored in an IMBTransformResult after parsing the IMB message.
16 * <p>
17 * Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
18 * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
19 * </p>
20 * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
21 * initial version Sep 11, 2016 <br>
22 * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
23 * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
24 * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
25 */
26 public class PubSubIMBMessageHandler implements IMBMessageHandler
27 {
28 /** The simulator to schedule the incoming notifications on. */
29 private final OTSDEVSSimulatorInterface simulator;
30
31 /** The IMB event name (String). */
32 private final String imbEventName;
33
34 /** The corresponding EventType for OTS. */
35 private final EventType eventType;
36
37 /** The Transformer to use for the given IMB message type. */
38 private final IMBToOTSTransformer imbToOTSTransformer;
39
40 /**
41 * Construct a new PubSubIMBMessageHandler. The PubSubIMBMessageHandler handles the IMB message by transforming it into a
42 * DSOL Event and sending it to the EventListener at the current simulation time through a simulation event.
43 * @param imbEventName String; the name of the IMB event
44 * @param eventType EventType; the event type that the listener subscribes to
45 * @param imbToOTSTransformer IMBToOTSTransformer; the transformer that creates the event content and identifies the exact
46 * listener on the basis of the IBM event payload, e.g., on the basis of an id within the payload
47 * @param simulator OTSDEVSSimulatorInterface; The simulator to schedule the incoming notifications on
48 * @throws IMBException in case the construction fails
49 */
50 public PubSubIMBMessageHandler(final String imbEventName, final EventType eventType,
51 final IMBToOTSTransformer imbToOTSTransformer, final OTSDEVSSimulatorInterface simulator) throws IMBException
52 {
53 this.imbEventName = imbEventName;
54 this.eventType = eventType;
55 this.imbToOTSTransformer = imbToOTSTransformer;
56 this.simulator = simulator;
57 }
58
59 /**
60 * Handle the event by notifying the EventListener that was identified by the imbPayload through the imbToOTSTransformer.
61 * The notification is scheduled on the simulator to make sure that the separate thread that receives events from IMB does
62 * not interfere with the execution of the OTS simulation.
63 * @param imbPayload TByteBuffer; the IMB payload
64 * @throws IMBException in case the message cannot be handled
65 */
66 @Override
67 public void handle(final TByteBuffer imbPayload) throws IMBException
68 {
69 IMBTransformResult imbTransformResult = this.imbToOTSTransformer.transform(imbPayload);
70 EventInterface event = new Event(this.eventType, this, imbTransformResult.getEventContent());
71 try
72 {
73 this.simulator.scheduleEventNow(this, imbTransformResult.getEventListener(), "notify", new Object[] { event });
74 }
75 catch (SimRuntimeException exception)
76 {
77 throw new IMBException(exception);
78 }
79 }
80
81 /** {@inheritDoc} */
82 @Override
83 public final String getIMBEventName()
84 {
85 return this.imbEventName;
86 }
87
88 }