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.draw.point.OrientedPoint2d;
10 import org.djutils.metadata.MetaData;
11 import org.djutils.metadata.ObjectDescriptor;
12 import org.djutils.serialization.SerializationException;
13 import org.opentrafficsim.core.network.Network;
14 import org.opentrafficsim.core.network.Node;
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 Network network;
31
32
33 private final TransceiverInterface nodeIdSource;
34
35
36
37
38
39
40 public NodeTransceiver(final Network 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 OrientedPoint2d nodeLocation = node.getLocation();
72 return new Object[] {node.getId(),
73 new PositionVector(new double[] {nodeLocation.x, nodeLocation.y}, PositionUnit.METER),
74 new Direction(nodeLocation.getDirZ(), DirectionUnit.EAST_DEGREE), node.getLinks().size()};
75 }
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.nodeIdSource;
88 }
89
90
91 @Override
92 public boolean hasIdSource()
93 {
94 return true;
95 }
96
97
98 @Override
99 public String toString()
100 {
101 return "NodeTransceiver [network=" + this.network + ", super=" + super.toString() + "]";
102 }
103
104 }