NodeTransceiver.java

  1. package org.opentrafficsim.sim0mq.publisher;

  2. import java.rmi.RemoteException;

  3. import org.djunits.unit.DirectionUnit;
  4. import org.djunits.unit.PositionUnit;
  5. import org.djunits.value.vdouble.scalar.Direction;
  6. import org.djunits.value.vdouble.vector.PositionVector;
  7. import org.djutils.draw.point.OrientedPoint2d;
  8. import org.djutils.metadata.MetaData;
  9. import org.djutils.metadata.ObjectDescriptor;
  10. import org.djutils.serialization.SerializationException;
  11. import org.opentrafficsim.core.network.Network;
  12. import org.opentrafficsim.core.network.Node;
  13. import org.sim0mq.Sim0MQException;

  14. /**
  15.  * Transceiver for Node data.
  16.  * <p>
  17.  * Copyright (c) 2020-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  18.  * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  19.  * </p>
  20.  * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
  21.  * @author <a href="https://github.com/peter-knoppers">Peter Knoppers</a>
  22.  * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
  23.  */
  24. public class NodeTransceiver extends AbstractTransceiver
  25. {
  26.     /** The OTS network. */
  27.     private final Network network;

  28.     /** Transceiver for the Node ids. */
  29.     private final TransceiverInterface nodeIdSource;

  30.     /**
  31.      * Construct a new NodeTransceiver.
  32.      * @param network the network
  33.      * @param nodeIdSource the transceiver that can produce all Node ids in the Network
  34.      */
  35.     public NodeTransceiver(final Network network, final NodeIdTransceiver nodeIdSource)
  36.     {
  37.         super("Node transceiver",
  38.                 new MetaData("Node id", "Node id",
  39.                         new ObjectDescriptor[] {new ObjectDescriptor("Node id", "Node id", String.class)}),
  40.                 new MetaData("Node data", "Node id, position, direction, number of Links",
  41.                         new ObjectDescriptor[] {new ObjectDescriptor("Node id", "Node id", String.class),
  42.                                 new ObjectDescriptor("Position", "Position", PositionVector.class),
  43.                                 new ObjectDescriptor("Direction", "Direction", Direction.class),
  44.                                 new ObjectDescriptor("Number of links", "Number of links", Integer.class)}));
  45.         this.network = network;
  46.         this.nodeIdSource = nodeIdSource;
  47.     }

  48.     @Override
  49.     public Object[] get(final Object[] address, final ReturnWrapper returnWrapper)
  50.             throws RemoteException, Sim0MQException, SerializationException
  51.     {
  52.         String bad = verifyMetaData(getAddressFields(), address);
  53.         if (bad != null)
  54.         {
  55.             returnWrapper.nack(bad);
  56.             return null;
  57.         }
  58.         Node node = this.network.getNode((String) address[0]);
  59.         if (null == node)
  60.         {
  61.             returnWrapper.nack("Network does not contain a node with id " + address[0]);
  62.             return null;
  63.         }
  64.         OrientedPoint2d nodeLocation = node.getLocation();
  65.         return new Object[] {node.getId(),
  66.                 new PositionVector(new double[] {nodeLocation.x, nodeLocation.y}, PositionUnit.METER),
  67.                 new Direction(nodeLocation.getDirZ(), DirectionUnit.EAST_DEGREE), node.getLinks().size()};
  68.     }

  69.     @Override
  70.     public TransceiverInterface getIdSource(final int addressLevel, final ReturnWrapper returnWrapper)
  71.             throws Sim0MQException, SerializationException
  72.     {
  73.         if (addressLevel != 0)
  74.         {
  75.             returnWrapper.nack("Only empty address is valid");
  76.             return null;
  77.         }
  78.         return this.nodeIdSource;
  79.     }

  80.     @Override
  81.     public boolean hasIdSource()
  82.     {
  83.         return true;
  84.     }

  85.     @Override
  86.     public String toString()
  87.     {
  88.         return "NodeTransceiver [network=" + this.network + ", super=" + super.toString() + "]";
  89.     }

  90. }