AbstractEventTransceiver.java

  1. package org.opentrafficsim.sim0mq.publisher;

  2. import java.util.ArrayList;
  3. import java.util.List;

  4. import org.djunits.value.vdouble.scalar.Time;
  5. import org.djutils.event.TimedEventType;
  6. import org.djutils.metadata.MetaData;
  7. import org.djutils.metadata.ObjectDescriptor;

  8. /**
  9.  * Transceiver for DJUNITS events.
  10.  * <p>
  11.  * Copyright (c) 2020-2022 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  12.  * BSD-style license. See <a href="http://opentrafficsim.org/docs/current/license.html">OpenTrafficSim License</a>.
  13.  * </p>
  14.  * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  15.  * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  16.  * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
  17.  */
  18. public abstract class AbstractEventTransceiver extends AbstractTransceiver
  19. {
  20.     /**
  21.      * Construct a new AbstractEventTransceiver.
  22.      * @param id String; name of the new AbstractEventTransceiver
  23.      * @param addressFields MetaData; address format accepted by the new AbstractEventTransceiver
  24.      * @param eventType TimedEventType; type of the event that the AbstractEventTransceiver can subscribe to in the network
  25.      */
  26.     public AbstractEventTransceiver(final String id, final MetaData addressFields, final TimedEventType eventType)
  27.     {
  28.         super(id, addressFields, constructResultFields(eventType));
  29.     }

  30.     /**
  31.      * Construct a Sim0MQ MetaData object that corresponds to the MetaData of DJUTILS EventType. Classes that do not have a
  32.      * corresponding Sim0MQ type will result in a ClassCastException.
  33.      * @param eventType TimedEventType; the event type
  34.      * @return MetaData; a MetaData object that corresponds to the MetaData of the event type
  35.      * @throws ClassCastException when the <code>eventType</code> contains a class that cannot be carried over Sim0MQ
  36.      */
  37.     public static MetaData constructResultFields(final TimedEventType eventType) throws ClassCastException
  38.     {
  39.         List<ObjectDescriptor> resultList = new ArrayList<>();
  40.         resultList.add(new ObjectDescriptor("TimeStamp", "Time", Time.class));
  41.         for (int index = 0; index < eventType.getMetaData().size(); index++)
  42.         {
  43.             ObjectDescriptor od = eventType.getMetaData().getObjectDescriptor(index);
  44.             switch (od.getObjectClass().getName())
  45.             {
  46.                 case "java.lang.String":
  47.                 case "java.lang.Double":
  48.                 case "org.djunits.value.vdouble.scalar.Acceleration":
  49.                 case "org.djunits.value.vdouble.scalar.Direction":
  50.                 case "org.djunits.value.vdouble.scalar.Length":
  51.                 case "org.djunits.value.vdouble.scalar.Speed":
  52.                 case "org.djunits.value.vdouble.scalar.Time":
  53.                 case "org.djunits.value.vdouble.vector.PositionVector":
  54.                     resultList.add(od);
  55.                     break;

  56.                 default:
  57.                     throw new ClassCastException("No conversion for class " + od.getObjectClass().getName());
  58.             }
  59.         }

  60.         return new MetaData(eventType.getMetaData().getName(), eventType.getMetaData().getDescription(),
  61.                 resultList.toArray(new ObjectDescriptor[0]));
  62.     }

  63. }