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.metadata.MetaData;
10  import org.djutils.metadata.ObjectDescriptor;
11  import org.djutils.serialization.SerializationException;
12  import org.opentrafficsim.core.geometry.OTSPoint3D;
13  import org.opentrafficsim.core.network.Node;
14  import org.opentrafficsim.core.network.OTSNetwork;
15  import org.sim0mq.Sim0MQException;
16  
17  /**
18   * Transceiver for Node data.
19   * <p>
20   * Copyright (c) 2020-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
21   * BSD-style license. See <a href="http://opentrafficsim.org/docs/current/license.html">OpenTrafficSim License</a>.
22   * </p>
23   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
24   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
25   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
26   */
27  public class NodeTransceiver extends AbstractTransceiver
28  {
29      /** The OTS network. */
30      private final OTSNetwork network;
31  
32      /** Transceiver for the Node ids. */
33      private final TransceiverInterface nodeIdSource;
34  
35      /**
36       * Construct a new NodeTransceiver.
37       * @param network OTSNetwork; the network
38       * @param nodeIdSource NodeIdTransceiver; the transceiver that can produce all Node ids in the Network
39       */
40      public NodeTransceiver(final OTSNetwork 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          return new Object[] { node.getId(), node.getPoint().doubleVector(PositionUnit.METER),
72                  OTSPoint3D.direction(node.getLocation(), DirectionUnit.EAST_RADIAN), node.getLinks().size() };
73      }
74  
75      /** {@inheritDoc} */
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      /** {@inheritDoc} */
89      @Override
90      public boolean hasIdSource()
91      {
92          return true;
93      }
94  
95      /** {@inheritDoc} */
96      @Override
97      public String toString()
98      {
99          return "NodeTransceiver [network=" + this.network + ", super=" + super.toString() + "]";
100     }
101 
102 }