1 package org.opentrafficsim.sim0mq.publisher;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import org.djunits.value.vdouble.scalar.Time;
7 import org.djutils.event.EventType;
8 import org.djutils.metadata.MetaData;
9 import org.djutils.metadata.ObjectDescriptor;
10
11 /**
12 * Transceiver for DJUNITS events.
13 * <p>
14 * Copyright (c) 2020-2023 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
15 * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
16 * </p>
17 * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
18 * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
19 * @author <a href="https://dittlab.tudelft.nl">Wouter Schakel</a>
20 */
21 public abstract class AbstractEventTransceiver extends AbstractTransceiver
22 {
23 /**
24 * Construct a new AbstractEventTransceiver.
25 * @param id String; name of the new AbstractEventTransceiver
26 * @param addressFields MetaData; address format accepted by the new AbstractEventTransceiver
27 * @param eventType EventType; type of the event that the AbstractEventTransceiver can subscribe to in the network
28 */
29 public AbstractEventTransceiver(final String id, final MetaData addressFields, final EventType eventType)
30 {
31 super(id, addressFields, constructResultFields(eventType));
32 }
33
34 /**
35 * Construct a Sim0MQ MetaData object that corresponds to the MetaData of DJUTILS EventType. Classes that do not have a
36 * corresponding Sim0MQ type will result in a ClassCastException.
37 * @param eventType EventType; the event type
38 * @return MetaData; a MetaData object that corresponds to the MetaData of the event type
39 * @throws ClassCastException when the <code>eventType</code> contains a class that cannot be carried over Sim0MQ
40 */
41 public static MetaData constructResultFields(final EventType eventType) throws ClassCastException
42 {
43 List<ObjectDescriptor> resultList = new ArrayList<>();
44 resultList.add(new ObjectDescriptor("TimeStamp", "Time", Time.class));
45 for (int index = 0; index < eventType.getMetaData().size(); index++)
46 {
47 ObjectDescriptor od = eventType.getMetaData().getObjectDescriptor(index);
48 switch (od.getObjectClass().getName())
49 {
50 case "java.lang.String":
51 case "java.lang.Double":
52 case "org.djunits.value.vdouble.scalar.Acceleration":
53 case "org.djunits.value.vdouble.scalar.Direction":
54 case "org.djunits.value.vdouble.scalar.Length":
55 case "org.djunits.value.vdouble.scalar.Speed":
56 case "org.djunits.value.vdouble.scalar.Time":
57 case "org.djunits.value.vdouble.vector.PositionVector":
58 resultList.add(od);
59 break;
60
61 default:
62 throw new ClassCastException("No conversion for class " + od.getObjectClass().getName());
63 }
64 }
65
66 return new MetaData(eventType.getMetaData().getName(), eventType.getMetaData().getDescription(),
67 resultList.toArray(new ObjectDescriptor[0]));
68 }
69
70 }