1 package org.opentrafficsim.sim0mq.publisher;
2
3 import java.io.Serializable;
4 import java.rmi.RemoteException;
5
6 import org.djutils.event.EventInterface;
7 import org.djutils.event.EventListenerInterface;
8 import org.djutils.event.EventProducer;
9 import org.djutils.event.EventProducerInterface;
10 import org.djutils.event.TimedEvent;
11 import org.djutils.event.TimedEventType;
12 import org.djutils.metadata.MetaData;
13 import org.djutils.metadata.ObjectDescriptor;
14 import org.djutils.serialization.SerializationException;
15 import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
16 import org.sim0mq.Sim0MQException;
17
18 import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
19
20
21
22
23
24
25
26
27
28
29 public class SimulatorStateTransceiver extends AbstractTransceiver
30 {
31
32 private final OTSSimulatorInterface simulator;
33
34
35 @SuppressWarnings("checkstyle:visibilitymodifier")
36 final EventProducerInterface eventMultiplexer;
37
38
39 public static final TimedEventType SIMULATOR_STATE_CHANGED =
40 new TimedEventType(new MetaData("SIMULATOR_STATE_CHANGED_EVENT", "simulator started or stopped",
41 new ObjectDescriptor[] {new ObjectDescriptor("New simulator state",
42 "New simulator state; true if running; false if stopped", Boolean.class)}));
43
44
45
46
47
48
49 public SimulatorStateTransceiver(final OTSSimulatorInterface simulator) throws RemoteException
50 {
51 super("Simulator state transceiver", MetaData.EMPTY,
52 new MetaData("SIMULATOR_STATE_CHANGED_EVENT", "simulator state changed"));
53 this.simulator = simulator;
54 this.eventMultiplexer = new EventMultiplexer(simulator);
55 }
56
57
58 @Override
59 public Object[] get(final Object[] address, final ReturnWrapper returnWrapper)
60 throws RemoteException, Sim0MQException, SerializationException
61 {
62 AbstractTransceiver.verifyMetaData(MetaData.EMPTY, address);
63 String result = null;
64 if (this.simulator.isInitialized())
65 {
66 if (this.simulator.isStartingOrRunning())
67 {
68 result = "Starting or running";
69 }
70 else
71 {
72 result = "Stopping or stopped";
73 }
74 }
75 else
76 {
77 result = "Not (yet) initialized";
78 }
79 return new Object[] {result};
80 }
81
82
83 private LookupEventProducerInterface lepi = new LookupEventProducerInterface()
84 {
85 @Override
86 public EventProducerInterface lookup(final Object[] address, final ReturnWrapper returnWrapper)
87 throws Sim0MQException, SerializationException
88 {
89 String bad = AbstractTransceiver.verifyMetaData(MetaData.EMPTY, address);
90 if (null != bad)
91 {
92 returnWrapper.nack(bad);
93 return null;
94 }
95 return SimulatorStateTransceiver.this.eventMultiplexer;
96 }
97
98 @Override
99 public MetaData getAddressMetaData()
100 {
101 return MetaData.EMPTY;
102 }
103 };
104
105
106
107
108
109 public LookupEventProducerInterface getLookupEventProducerInterface()
110 {
111 return this.lepi;
112 }
113
114 }
115
116
117
118
119
120 class EventMultiplexer extends EventProducer implements EventListenerInterface
121 {
122
123 private static final long serialVersionUID = 20200618L;
124
125
126
127
128
129 EventMultiplexer(final OTSSimulatorInterface simulator) throws RemoteException
130 {
131 simulator.addListener(this, SimulatorInterface.START_EVENT);
132 simulator.addListener(this, SimulatorInterface.STOP_EVENT);
133 }
134
135
136 @Override
137 public void notify(final EventInterface event) throws RemoteException
138 {
139 notifyTimedEvent(event);
140 }
141
142
143
144
145
146
147
148 private <C extends Serializable & Comparable<C>> void notifyTimedEvent(final EventInterface event)
149 {
150 @SuppressWarnings("unchecked")
151 TimedEvent<C> timedEvent = (TimedEvent<C>) event;
152 fireTimedEvent(SimulatorStateTransceiver.SIMULATOR_STATE_CHANGED,
153 event.getType().equals(SimulatorInterface.START_EVENT), timedEvent.getTimeStamp());
154 }
155
156
157 @Override
158 public Serializable getSourceId()
159 {
160
161 return "EventMultiplexer";
162 }
163
164 }