View Javadoc
1   package org.opentrafficsim.sim0mq.publisher;
2   
3   import java.rmi.RemoteException;
4   
5   import org.djunits.unit.DirectionUnit;
6   import org.djunits.unit.PositionUnit;
7   import org.djunits.value.vdouble.scalar.Direction;
8   import org.djunits.value.vdouble.vector.PositionVector;
9   import org.djutils.draw.point.OrientedPoint2d;
10  import org.djutils.metadata.MetaData;
11  import org.djutils.metadata.ObjectDescriptor;
12  import org.djutils.serialization.SerializationException;
13  import org.opentrafficsim.core.network.Network;
14  import org.opentrafficsim.core.network.Node;
15  import org.sim0mq.Sim0MQException;
16  
17  /**
18   * Transceiver for Node data.
19   * <p>
20   * Copyright (c) 2020-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
21   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
22   * </p>
23   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
24   * @author <a href="https://github.com/peter-knoppers">Peter Knoppers</a>
25   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
26   */
27  public class NodeTransceiver extends AbstractTransceiver
28  {
29      /** The OTS network. */
30      private final Network network;
31  
32      /** Transceiver for the Node ids. */
33      private final TransceiverInterface nodeIdSource;
34  
35      /**
36       * Construct a new NodeTransceiver.
37       * @param network the network
38       * @param nodeIdSource the transceiver that can produce all Node ids in the Network
39       */
40      public NodeTransceiver(final Network network, final NodeIdTransceiver nodeIdSource)
41      {
42          super("Node transceiver",
43                  new MetaData("Node id", "Node id",
44                          new ObjectDescriptor[] {new ObjectDescriptor("Node id", "Node id", String.class)}),
45                  new MetaData("Node data", "Node id, position, direction, number of Links",
46                          new ObjectDescriptor[] {new ObjectDescriptor("Node id", "Node id", String.class),
47                                  new ObjectDescriptor("Position", "Position", PositionVector.class),
48                                  new ObjectDescriptor("Direction", "Direction", Direction.class),
49                                  new ObjectDescriptor("Number of links", "Number of links", Integer.class)}));
50          this.network = network;
51          this.nodeIdSource = nodeIdSource;
52      }
53  
54      @Override
55      public Object[] get(final Object[] address, final ReturnWrapper returnWrapper)
56              throws RemoteException, Sim0MQException, SerializationException
57      {
58          String bad = verifyMetaData(getAddressFields(), address);
59          if (bad != null)
60          {
61              returnWrapper.nack(bad);
62              return null;
63          }
64          Node node = this.network.getNode((String) address[0]);
65          if (null == node)
66          {
67              returnWrapper.nack("Network does not contain a node with id " + address[0]);
68              return null;
69          }
70          OrientedPoint2d nodeLocation = node.getLocation();
71          return new Object[] {node.getId(),
72                  new PositionVector(new double[] {nodeLocation.x, nodeLocation.y}, PositionUnit.METER),
73                  new Direction(nodeLocation.getDirZ(), DirectionUnit.EAST_DEGREE), node.getLinks().size()};
74      }
75  
76      @Override
77      public TransceiverInterface getIdSource(final int addressLevel, final ReturnWrapper returnWrapper)
78              throws Sim0MQException, SerializationException
79      {
80          if (addressLevel != 0)
81          {
82              returnWrapper.nack("Only empty address is valid");
83              return null;
84          }
85          return this.nodeIdSource;
86      }
87  
88      @Override
89      public boolean hasIdSource()
90      {
91          return true;
92      }
93  
94      @Override
95      public String toString()
96      {
97          return "NodeTransceiver [network=" + this.network + ", super=" + super.toString() + "]";
98      }
99  
100 }