1 package org.opentrafficsim.sim0mq.publisher;
2
3 import java.rmi.RemoteException;
4
5 import org.djunits.unit.DirectionUnit;
6 import org.djunits.unit.PositionUnit;
7 import org.djunits.value.vdouble.scalar.Direction;
8 import org.djunits.value.vdouble.vector.PositionVector;
9 import org.djutils.metadata.MetaData;
10 import org.djutils.metadata.ObjectDescriptor;
11 import org.djutils.serialization.SerializationException;
12 import org.opentrafficsim.core.geometry.OTSPoint3D;
13 import org.opentrafficsim.core.network.Node;
14 import org.opentrafficsim.core.network.OTSNetwork;
15 import org.sim0mq.Sim0MQException;
16
17
18
19
20
21
22
23
24
25
26
27 public class NodeTransceiver extends AbstractTransceiver
28 {
29
30 private final OTSNetwork network;
31
32
33 private final TransceiverInterface nodeIdSource;
34
35
36
37
38
39
40 public NodeTransceiver(final OTSNetwork network, final NodeIdTransceiver nodeIdSource)
41 {
42 super("Node transceiver",
43 new MetaData("Node id", "Node id",
44 new ObjectDescriptor[] { new ObjectDescriptor("Node id", "Node id", String.class) }),
45 new MetaData("Node data", "Node id, position, direction, number of Links",
46 new ObjectDescriptor[] { new ObjectDescriptor("Node id", "Node id", String.class),
47 new ObjectDescriptor("Position", "Position", PositionVector.class),
48 new ObjectDescriptor("Direction", "Direction", Direction.class),
49 new ObjectDescriptor("Number of links", "Number of links", Integer.class) }));
50 this.network = network;
51 this.nodeIdSource = nodeIdSource;
52 }
53
54
55 @Override
56 public Object[] get(final Object[] address, final ReturnWrapper returnWrapper)
57 throws RemoteException, Sim0MQException, SerializationException
58 {
59 String bad = verifyMetaData(getAddressFields(), address);
60 if (bad != null)
61 {
62 returnWrapper.nack(bad);
63 return null;
64 }
65 Node node = this.network.getNode((String) address[0]);
66 if (null == node)
67 {
68 returnWrapper.nack("Network does not contain a node with id " + address[0]);
69 return null;
70 }
71 return new Object[] { node.getId(), node.getPoint().doubleVector(PositionUnit.METER),
72 OTSPoint3D.direction(node.getLocation(), DirectionUnit.EAST_RADIAN), node.getLinks().size() };
73 }
74
75
76 @Override
77 public TransceiverInterface getIdSource(final int addressLevel, final ReturnWrapper returnWrapper)
78 throws Sim0MQException, SerializationException
79 {
80 if (addressLevel != 0)
81 {
82 returnWrapper.nack("Only empty address is valid");
83 return null;
84 }
85 return this.nodeIdSource;
86 }
87
88
89 @Override
90 public boolean hasIdSource()
91 {
92 return true;
93 }
94
95
96 @Override
97 public String toString()
98 {
99 return "NodeTransceiver [network=" + this.network + ", super=" + super.toString() + "]";
100 }
101
102 }