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