View Javadoc
1   package org.opentrafficsim.sim0mq.publisher;
2   
3   import java.rmi.RemoteException;
4   
5   import org.djutils.metadata.MetaData;
6   import org.djutils.metadata.ObjectDescriptor;
7   import org.djutils.serialization.SerializationException;
8   import org.opentrafficsim.core.network.Link;
9   import org.opentrafficsim.core.network.Link;
10  import org.opentrafficsim.core.network.Network;
11  import org.opentrafficsim.road.network.lane.CrossSectionLink;
12  import org.sim0mq.Sim0MQException;
13  
14  /**
15   * Transceiver for Link 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://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
22   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
23   */
24  public class LinkTransceiver extends AbstractTransceiver
25  {
26      /** The OTS network. */
27      private final Network network;
28  
29      /** Transceiver for the GTU ids. */
30      private final TransceiverInterface linkIdSource;
31  
32      /**
33       * Construct a new LinkTransceiver.
34       * @param network Network; the network
35       * @param linkIdSource LinkIdTransceiver; the transceiver that can produce all Link ids in the Network
36       */
37      public LinkTransceiver(final Network network, final LinkIdTransceiver linkIdSource)
38      {
39          super("Link transceiver",
40                  new MetaData("Link id", "Link id",
41                          new ObjectDescriptor[] {new ObjectDescriptor("Link id", "Link id", String.class)}),
42                  new MetaData("Link data",
43                          "Link id, type, start node id, end node id, design line size, gtu count, cross section "
44                                  + "element count",
45                          new ObjectDescriptor[] {new ObjectDescriptor("Link id", "link id", String.class),
46                                  new ObjectDescriptor("LinkType id", "Link type", String.class),
47                                  new ObjectDescriptor("Start node id", "Start node id", String.class),
48                                  new ObjectDescriptor("End node id", "End node id", String.class),
49                                  new ObjectDescriptor("Design line size", "Number of points in design line of link",
50                                          Integer.class),
51                                  new ObjectDescriptor("GTU count", "Total number of GTUs on the link", Integer.class),
52                                  new ObjectDescriptor("CrossSectionElement count",
53                                          "Number of cross section elements on the link", Integer.class)}));
54          this.network = network;
55          this.linkIdSource = linkIdSource;
56      }
57  
58      /** {@inheritDoc} */
59      @Override
60      public Object[] get(final Object[] address, final ReturnWrapper returnWrapper)
61              throws RemoteException, Sim0MQException, SerializationException
62      {
63          String bad = verifyMetaData(getAddressFields(), address);
64          if (bad != null)
65          {
66              returnWrapper.nack(bad);
67              return null;
68          }
69          Link link = this.network.getLink((String) address[0]);
70          if (null == link)
71          {
72              returnWrapper.nack("Network does not contain a link with id " + address[0]);
73              return null;
74          }
75          return new Object[] {link.getId(), link.getType().getId(), link.getStartNode().getId(), link.getEndNode().getId(),
76                  link instanceof Link ? ((Link) link).getDesignLine().size() : 0, link.getGTUCount(),
77                  link instanceof CrossSectionLink ? ((CrossSectionLink) link).getCrossSectionElementList().size() : 0};
78      }
79  
80      /** {@inheritDoc} */
81      @Override
82      public TransceiverInterface getIdSource(final int addressLevel, final ReturnWrapper returnWrapper)
83              throws Sim0MQException, SerializationException
84      {
85          if (addressLevel != 0)
86          {
87              returnWrapper.nack("Only empty address is valid");
88              return null;
89          }
90          return this.linkIdSource;
91      }
92  
93      /** {@inheritDoc} */
94      @Override
95      public boolean hasIdSource()
96      {
97          return true;
98      }
99  
100     /** {@inheritDoc} */
101     @Override
102     public String toString()
103     {
104         return "LinkTransceiver [network=" + this.network + ", super=" + super.toString() + "]";
105     }
106 
107 }