View Javadoc
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.TimedEventType;
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-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
15   * BSD-style license. See <a href="http://opentrafficsim.org/docs/current/license.html">OpenTrafficSim License</a>.
16   * </p>
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 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 TimedEventType; type of the event that the AbstractEventTransceiver can subscribe to in the network
28       */
29      public AbstractEventTransceiver(final String id, final MetaData addressFields, final TimedEventType 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 TimedEventType; 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 TimedEventType 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 "org.djunits.value.vdouble.scalar.Acceleration":
52                  case "org.djunits.value.vdouble.scalar.Direction":
53                  case "org.djunits.value.vdouble.scalar.Length":
54                  case "org.djunits.value.vdouble.scalar.Speed":
55                  case "org.djunits.value.vdouble.vector.PositionVector":
56                      resultList.add(od);
57                      break;
58  
59                  default:
60                      throw new ClassCastException("No conversion for class " + od.getObjectClass().getName());
61              }
62          }
63  
64          return new MetaData(eventType.getMetaData().getName(), eventType.getMetaData().getDescription(),
65                  resultList.toArray(new ObjectDescriptor[0]));
66      }
67  
68  }