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://tudelft.nl/staff/p.knoppers-1">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 Network; the network
38       * @param nodeIdSource NodeIdTransceiver; 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      /** {@inheritDoc} */
55      @Override
56      public Object[] get(final Object[] address, final ReturnWrapper returnWrapper)
57              throws RemoteException, Sim0MQException, SerializationException
58      {
59          String bad = verifyMetaData(getAddressFields(), address);
60          if (bad != null)
61          {
62              returnWrapper.nack(bad);
63              return null;
64          }
65          Node node = this.network.getNode((String) address[0]);
66          if (null == node)
67          {
68              returnWrapper.nack("Network does not contain a node with id " + address[0]);
69              return null;
70          }
71          OrientedPoint2d nodeLocation = node.getLocation();
72          return new Object[] {node.getId(),
73                  new PositionVector(new double[] {nodeLocation.x, nodeLocation.y}, PositionUnit.METER),
74                  new Direction(nodeLocation.getDirZ(), DirectionUnit.EAST_DEGREE), node.getLinks().size()};
75      }
76  
77      /** {@inheritDoc} */
78      @Override
79      public TransceiverInterface getIdSource(final int addressLevel, final ReturnWrapper returnWrapper)
80              throws Sim0MQException, SerializationException
81      {
82          if (addressLevel != 0)
83          {
84              returnWrapper.nack("Only empty address is valid");
85              return null;
86          }
87          return this.nodeIdSource;
88      }
89  
90      /** {@inheritDoc} */
91      @Override
92      public boolean hasIdSource()
93      {
94          return true;
95      }
96  
97      /** {@inheritDoc} */
98      @Override
99      public String toString()
100     {
101         return "NodeTransceiver [network=" + this.network + ", super=" + super.toString() + "]";
102     }
103 
104 }