View Javadoc
1   package org.opentrafficsim.sim0mq.publisher;
2   
3   import org.djutils.base.Identifiable;
4   import org.djutils.exceptions.Throw;
5   import org.djutils.immutablecollections.ImmutableSet;
6   import org.djutils.metadata.MetaData;
7   import org.djutils.metadata.ObjectDescriptor;
8   import org.djutils.serialization.SerializationException;
9   import org.opentrafficsim.core.network.Network;
10  import org.sim0mq.Sim0MQException;
11  
12  /**
13   * Common code for id transceivers that use an empty address.
14   * <p>
15   * Copyright (c) 2020-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
16   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
17   * </p>
18   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
19   * @author <a href="https://github.com/peter-knoppers">Peter Knoppers</a>
20   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
21   */
22  public abstract class AbstractIdTransceiver extends AbstractTransceiver
23  {
24      /** The network. */
25      private final Network network;
26  
27      /**
28       * Construct a GtuIdTransceiver.
29       * @param network the OTS network
30       * @param id name of the IdTransceiver
31       */
32      public AbstractIdTransceiver(final Network network, final String id)
33      {
34          super(id, new MetaData("No address", "empty address", new ObjectDescriptor[0]),
35                  new MetaData("No address", "empty address", new ObjectDescriptor[] {new ObjectDescriptor("String array",
36                          "String array filled with all currently valid GTU ids", String[].class)}));
37          Throw.whenNull(network, "Network may not be null");
38          this.network = network;
39      }
40  
41      /**
42       * Retrieve the set of names of objects that can be individually subscribed to.
43       * @return the set of names of objects whose that can be subscribed to. Each object in this set should implement
44       *         <code>Identifiable</code>
45       */
46      abstract ImmutableSet<?> getSet();
47  
48      @Override
49      public final Object[] get(final Object[] address, final ReturnWrapper returnWrapper)
50              throws Sim0MQException, SerializationException
51      {
52          String bad = verifyMetaData(getAddressFields(), address);
53          if (bad != null)
54          {
55              returnWrapper.nack(bad);
56              return null;
57          }
58          ImmutableSet<?> set = getSet();
59          Object[] result = new Object[set.size()];
60          int nextIndex = 0;
61          for (Object object : set)
62          {
63              result[nextIndex++] = ((Identifiable) object).getId();
64          }
65          return result;
66      }
67  
68      /**
69       * Retrieve the network.
70       * @return the network
71       */
72      final Network getNetwork()
73      {
74          return this.network;
75      }
76  
77  }