LinkGtuIdTransceiver.java

  1. package org.opentrafficsim.sim0mq.publisher;

  2. import java.util.Set;

  3. import org.djutils.exceptions.Throw;
  4. import org.djutils.metadata.MetaData;
  5. import org.djutils.metadata.ObjectDescriptor;
  6. import org.djutils.serialization.SerializationException;
  7. import org.opentrafficsim.core.gtu.Gtu;
  8. import org.opentrafficsim.core.network.Link;
  9. import org.opentrafficsim.core.network.Network;
  10. import org.sim0mq.Sim0MQException;

  11. /**
  12.  * Transceiver for the ids of the GTUs on a link.
  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://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
  19.  * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
  20.  */
  21. public class LinkGtuIdTransceiver extends AbstractTransceiver
  22. {
  23.     /** The network. */
  24.     private final Network network;

  25.     /**
  26.      * Construct a GtuIdTransceiver.
  27.      * @param network Network; the OTS network
  28.      */
  29.     public LinkGtuIdTransceiver(final Network network)
  30.     {
  31.         super("Link GTU id transceiver",
  32.                 new MetaData("Link id", "Link id",
  33.                         new ObjectDescriptor[] {new ObjectDescriptor("Link id", "Link id", String.class)}),
  34.                 new MetaData("String array with all Link ids", "String array with all Link ids",
  35.                         new ObjectDescriptor[] {new ObjectDescriptor("String array",
  36.                                 "String array filled with all currently valid Link ids", String[].class)}));
  37.         Throw.whenNull(network, "Network may not be null");
  38.         this.network = network;
  39.     }

  40.     /** {@inheritDoc} */
  41.     @Override
  42.     public final Object[] get(final Object[] address, final ReturnWrapper returnWrapper)
  43.             throws Sim0MQException, SerializationException
  44.     {
  45.         String bad = verifyMetaData(getAddressFields(), address);
  46.         if (bad != null)
  47.         {
  48.             returnWrapper.nack("Bad address; need id of a link");
  49.             return null;
  50.         }
  51.         Link link = this.network.getLink((String) address[0]);
  52.         if (null == link)
  53.         {
  54.             returnWrapper.nack("Network does not contain a link with id " + address[0]);
  55.             return null;
  56.         }
  57.         Set<Gtu> gtus = link.getGTUs();
  58.         Object[] result = new Object[gtus.size()];
  59.         int nextIndex = 0;
  60.         for (Gtu gtu : gtus)
  61.         {
  62.             result[nextIndex++] = gtu.getId();
  63.         }
  64.         return result;
  65.     }

  66.     /** {@inheritDoc} */
  67.     @Override
  68.     public String toString()
  69.     {
  70.         return "LinkGtuIdTransceiver [network=" + this.network + ", super=" + super.toString() + "]";
  71.     }

  72. }