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
16
17
18
19
20
21
22
23
24 public class LinkGtuIdTransceiver extends AbstractTransceiver
25 {
26
27 private final Network network;
28
29
30
31
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
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
73 @Override
74 public String toString()
75 {
76 return "LinkGtuIdTransceiver [network=" + this.network + ", super=" + super.toString() + "]";
77 }
78
79 }