1 package org.opentrafficsim.sim0mq.publisher;
2
3 import java.rmi.RemoteException;
4
5 import org.djutils.metadata.MetaData;
6 import org.djutils.metadata.ObjectDescriptor;
7 import org.djutils.serialization.SerializationException;
8 import org.opentrafficsim.core.network.Link;
9 import org.opentrafficsim.core.network.Network;
10 import org.opentrafficsim.road.network.lane.CrossSectionLink;
11 import org.sim0mq.Sim0MQException;
12
13
14
15
16
17
18
19
20
21
22
23 public class LinkTransceiver extends AbstractTransceiver
24 {
25
26 private final Network network;
27
28
29 private final TransceiverInterface linkIdSource;
30
31
32
33
34
35
36 public LinkTransceiver(final Network network, final LinkIdTransceiver linkIdSource)
37 {
38 super("Link transceiver",
39 new MetaData("Link id", "Link id",
40 new ObjectDescriptor[] {new ObjectDescriptor("Link id", "Link id", String.class)}),
41 new MetaData("Link data",
42 "Link id, type, start node id, end node id, design line size, gtu count, cross section "
43 + "element count",
44 new ObjectDescriptor[] {new ObjectDescriptor("Link id", "link id", String.class),
45 new ObjectDescriptor("LinkType id", "Link type", String.class),
46 new ObjectDescriptor("Start node id", "Start node id", String.class),
47 new ObjectDescriptor("End node id", "End node id", String.class),
48 new ObjectDescriptor("Design line size", "Number of points in design line of link",
49 Integer.class),
50 new ObjectDescriptor("GTU count", "Total number of GTUs on the link", Integer.class),
51 new ObjectDescriptor("CrossSectionElement count",
52 "Number of cross section elements on the link", Integer.class)}));
53 this.network = network;
54 this.linkIdSource = linkIdSource;
55 }
56
57 @Override
58 public Object[] get(final Object[] address, final ReturnWrapper returnWrapper)
59 throws RemoteException, Sim0MQException, SerializationException
60 {
61 String bad = verifyMetaData(getAddressFields(), address);
62 if (bad != null)
63 {
64 returnWrapper.nack(bad);
65 return null;
66 }
67 Link link = this.network.getLink((String) address[0]).orElse(null);
68 if (null == link)
69 {
70 returnWrapper.nack("Network does not contain a link with id " + address[0]);
71 return null;
72 }
73 return new Object[] {link.getId(), link.getType().getId(), link.getStartNode().getId(), link.getEndNode().getId(),
74 link instanceof Link ? ((Link) link).getDesignLine().size() : 0, link.getGTUCount(),
75 link instanceof CrossSectionLink ? ((CrossSectionLink) link).getCrossSectionElementList().size() : 0};
76 }
77
78 @Override
79 public TransceiverInterface getIdSource(final int addressLevel, final ReturnWrapper returnWrapper)
80 throws Sim0MQException, SerializationException
81 {
82 if (addressLevel != 0)
83 {
84 returnWrapper.nack("Only empty address is valid");
85 return null;
86 }
87 return this.linkIdSource;
88 }
89
90 @Override
91 public boolean hasIdSource()
92 {
93 return true;
94 }
95
96 @Override
97 public String toString()
98 {
99 return "LinkTransceiver [network=" + this.network + ", super=" + super.toString() + "]";
100 }
101
102 }