LinkTransceiver.java

  1. package org.opentrafficsim.sim0mq.publisher;

  2. import java.rmi.RemoteException;

  3. import org.djutils.metadata.MetaData;
  4. import org.djutils.metadata.ObjectDescriptor;
  5. import org.djutils.serialization.SerializationException;
  6. import org.opentrafficsim.core.network.Link;
  7. import org.opentrafficsim.core.network.Link;
  8. import org.opentrafficsim.core.network.Network;
  9. import org.opentrafficsim.road.network.lane.CrossSectionLink;
  10. import org.sim0mq.Sim0MQException;

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

  25.     /** Transceiver for the GTU ids. */
  26.     private final TransceiverInterface linkIdSource;

  27.     /**
  28.      * Construct a new LinkTransceiver.
  29.      * @param network the network
  30.      * @param linkIdSource the transceiver that can produce all Link ids in the Network
  31.      */
  32.     public LinkTransceiver(final Network network, final LinkIdTransceiver linkIdSource)
  33.     {
  34.         super("Link transceiver",
  35.                 new MetaData("Link id", "Link id",
  36.                         new ObjectDescriptor[] {new ObjectDescriptor("Link id", "Link id", String.class)}),
  37.                 new MetaData("Link data",
  38.                         "Link id, type, start node id, end node id, design line size, gtu count, cross section "
  39.                                 + "element count",
  40.                         new ObjectDescriptor[] {new ObjectDescriptor("Link id", "link id", String.class),
  41.                                 new ObjectDescriptor("LinkType id", "Link type", String.class),
  42.                                 new ObjectDescriptor("Start node id", "Start node id", String.class),
  43.                                 new ObjectDescriptor("End node id", "End node id", String.class),
  44.                                 new ObjectDescriptor("Design line size", "Number of points in design line of link",
  45.                                         Integer.class),
  46.                                 new ObjectDescriptor("GTU count", "Total number of GTUs on the link", Integer.class),
  47.                                 new ObjectDescriptor("CrossSectionElement count",
  48.                                         "Number of cross section elements on the link", Integer.class)}));
  49.         this.network = network;
  50.         this.linkIdSource = linkIdSource;
  51.     }

  52.     @Override
  53.     public Object[] get(final Object[] address, final ReturnWrapper returnWrapper)
  54.             throws RemoteException, Sim0MQException, SerializationException
  55.     {
  56.         String bad = verifyMetaData(getAddressFields(), address);
  57.         if (bad != null)
  58.         {
  59.             returnWrapper.nack(bad);
  60.             return null;
  61.         }
  62.         Link link = this.network.getLink((String) address[0]);
  63.         if (null == link)
  64.         {
  65.             returnWrapper.nack("Network does not contain a link with id " + address[0]);
  66.             return null;
  67.         }
  68.         return new Object[] {link.getId(), link.getType().getId(), link.getStartNode().getId(), link.getEndNode().getId(),
  69.                 link instanceof Link ? ((Link) link).getDesignLine().size() : 0, link.getGTUCount(),
  70.                 link instanceof CrossSectionLink ? ((CrossSectionLink) link).getCrossSectionElementList().size() : 0};
  71.     }

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

  83.     @Override
  84.     public boolean hasIdSource()
  85.     {
  86.         return true;
  87.     }

  88.     @Override
  89.     public String toString()
  90.     {
  91.         return "LinkTransceiver [network=" + this.network + ", super=" + super.toString() + "]";
  92.     }

  93. }