View Javadoc
1   package org.opentrafficsim.sim0mq.publisher;
2   
3   import org.djunits.Throw;
4   import org.djutils.immutablecollections.ImmutableSet;
5   import org.djutils.metadata.MetaData;
6   import org.djutils.metadata.ObjectDescriptor;
7   import org.djutils.serialization.SerializationException;
8   import org.opentrafficsim.base.Identifiable;
9   import org.opentrafficsim.core.network.OTSNetwork;
10  import org.sim0mq.Sim0MQException;
11  
12  /**
13   * Common code for id transceivers that use an empty address.
14   * <p>
15   * Copyright (c) 2020-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
16   * BSD-style license. See <a href="http://opentrafficsim.org/docs/current/license.html">OpenTrafficSim License</a>.
17   * </p>
18   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
19   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
20   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
21   */
22  public abstract class AbstractIdTransceiver extends AbstractTransceiver
23  {
24      /** The network. */
25      private final OTSNetwork network;
26  
27      /**
28       * Construct a GTUIdTransceiver.
29       * @param network OTSNetwork; the OTS network
30       * @param id String; name of the IdTransceiver
31       */
32      public AbstractIdTransceiver(final OTSNetwork 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 Set&lt;?&gt;; the set of names of objects whose that can be subscribed to. Each object in this set should
44       *         implement <code>Identifiable</code>
45       */
46      abstract ImmutableSet<?> getSet();
47  
48      /** {@inheritDoc} */
49      @Override
50      public final Object[] get(final Object[] address, final ReturnWrapper returnWrapper)
51              throws Sim0MQException, SerializationException
52      {
53          String bad = verifyMetaData(getAddressFields(), address);
54          if (bad != null)
55          {
56              returnWrapper.nack(bad);
57              return null;
58          }
59          ImmutableSet<?> set = getSet();
60          Object[] result = new Object[set.size()];
61          int nextIndex = 0;
62          for (Object object : set)
63          {
64              result[nextIndex++] = ((Identifiable) object).getId();
65          }
66          return result;
67      }
68  
69      /**
70       * Retrieve the network.
71       * @return OTSNetwork; the network
72       */
73      final OTSNetwork getNetwork()
74      {
75          return this.network;
76      }
77  
78  }