View Javadoc
1   package org.opentrafficsim.sim0mq.publisher;
2   
3   import java.util.Set;
4   
5   import org.djutils.exceptions.Throw;
6   import org.djutils.metadata.MetaData;
7   import org.djutils.metadata.ObjectDescriptor;
8   import org.djutils.serialization.SerializationException;
9   import org.opentrafficsim.core.gtu.Gtu;
10  import org.opentrafficsim.core.network.Link;
11  import org.opentrafficsim.core.network.Network;
12  import org.sim0mq.Sim0MQException;
13  
14  /**
15   * Transceiver for the ids of the GTUs on a link.
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 LinkGtuIdTransceiver extends AbstractTransceiver
25  {
26      /** The network. */
27      private final Network network;
28  
29      /**
30       * Construct a GtuIdTransceiver.
31       * @param network Network; the OTS network
32       */
33      public LinkGtuIdTransceiver(final Network network)
34      {
35          super("Link GTU id transceiver",
36                  new MetaData("Link id", "Link id",
37                          new ObjectDescriptor[] {new ObjectDescriptor("Link id", "Link id", String.class)}),
38                  new MetaData("String array with all Link ids", "String array with all Link ids",
39                          new ObjectDescriptor[] {new ObjectDescriptor("String array",
40                                  "String array filled with all currently valid Link ids", String[].class)}));
41          Throw.whenNull(network, "Network may not be null");
42          this.network = network;
43      }
44  
45      /** {@inheritDoc} */
46      @Override
47      public final Object[] get(final Object[] address, final ReturnWrapper returnWrapper)
48              throws Sim0MQException, SerializationException
49      {
50          String bad = verifyMetaData(getAddressFields(), address);
51          if (bad != null)
52          {
53              returnWrapper.nack("Bad address; need id of a link");
54              return null;
55          }
56          Link link = this.network.getLink((String) address[0]);
57          if (null == link)
58          {
59              returnWrapper.nack("Network does not contain a link with id " + address[0]);
60              return null;
61          }
62          Set<Gtu> gtus = link.getGTUs();
63          Object[] result = new Object[gtus.size()];
64          int nextIndex = 0;
65          for (Gtu gtu : gtus)
66          {
67              result[nextIndex++] = gtu.getId();
68          }
69          return result;
70      }
71  
72      /** {@inheritDoc} */
73      @Override
74      public String toString()
75      {
76          return "LinkGtuIdTransceiver [network=" + this.network + ", super=" + super.toString() + "]";
77      }
78  
79  }