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://github.com/peter-knoppers">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 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      @Override
46      public final Object[] get(final Object[] address, final ReturnWrapper returnWrapper)
47              throws Sim0MQException, SerializationException
48      {
49          String bad = verifyMetaData(getAddressFields(), address);
50          if (bad != null)
51          {
52              returnWrapper.nack("Bad address; need id of a link");
53              return null;
54          }
55          Link link = this.network.getLink((String) address[0]);
56          if (null == link)
57          {
58              returnWrapper.nack("Network does not contain a link with id " + address[0]);
59              return null;
60          }
61          Set<Gtu> gtus = link.getGTUs();
62          Object[] result = new Object[gtus.size()];
63          int nextIndex = 0;
64          for (Gtu gtu : gtus)
65          {
66              result[nextIndex++] = gtu.getId();
67          }
68          return result;
69      }
70  
71      @Override
72      public String toString()
73      {
74          return "LinkGtuIdTransceiver [network=" + this.network + ", super=" + super.toString() + "]";
75      }
76  
77  }