package org.opentrafficsim.imb.transceiver.urbanstrategy; import java.rmi.RemoteException; import org.opentrafficsim.core.dsol.OTSDEVSSimulatorInterface; import org.opentrafficsim.core.dsol.OTSSimTimeDouble; import org.opentrafficsim.core.network.Network; import org.opentrafficsim.core.network.Node; import org.opentrafficsim.core.network.OTSNetwork; import org.opentrafficsim.imb.IMBException; import org.opentrafficsim.imb.connector.Connector; import org.opentrafficsim.imb.connector.Connector.IMBEventType; import org.opentrafficsim.imb.transceiver.AbstractTransceiver; import nl.tudelft.simulation.event.EventInterface; import nl.tudelft.simulation.event.EventType; import nl.tudelft.simulation.event.TimedEvent; /** * OTS publishes events about the Nodes to IMB to be able to identify the nodes in the network.
* At the start of the OTS simulation, or when a Node is added later, a NEW message is sent to IMB for each node to identify the * node id. No CHANGE messages are posted. When a Node is removed from the network, a DELETE event is posted. The Node NEW * messages are posted after the Network NEW message is posted. *

* *

NEW

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
VariableTypeComments
timestampdoubletime of the event, in simulation time seconds
networkIdStringId of the Network where the Node resides
nodeIdStringid of the Node; unique within the Network
coordinate.xdoublex-coordinate of the Node
coordinate.ydoubley-coordinate of the Node
coordinate.zdoublez-coordinate of the Node
*

*

*

CHANGE

Not sent *

*

*

DELETE

* * * * * * * * * * * * * * * * * * * * * * * * *
VariableTypeComments
timestampdoubletime of the event, in simulation time seconds
networkIdStringId of the Network where the Node resides
nodeIdStringid of the Node that is removed from the Network
*

*

* Copyright (c) 2013-2017 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
* BSD-style license. See OpenTrafficSim License. *

* @version $Revision$, $LastChangedDate$, by $Author$, initial version Sep 13, 2016
* @author Alexander Verbraeck * @author Peter Knoppers * @author Wouter Schakel */ public class NodeTransceiver extends AbstractTransceiver { /** */ private static final long serialVersionUID = 20160918L; /** the OTS network on which Nodes are registered. */ private final OTSNetwork network; /** * Construct a new NodeTransceiver. * @param connector Connector; the IMB connector through which this transceiver communicates * @param simulator OTSDEVSSimulatorInterface; the simulator to schedule the incoming notifications on * @param network OTSNetwork; the OTS network on which Nodes are registered * @throws IMBException when the registration of one of the channels fails * @throws NullPointerException in case one of the arguments is null. */ public NodeTransceiver(final Connector connector, final OTSDEVSSimulatorInterface simulator, final OTSNetwork network) throws IMBException { super("Node", connector, simulator); this.network = network; // listen on network changes and register the listener to all the Links addListeners(); } /** * Ensure that we get notified about newly created and destroyed Nodes, and instrument all currently existing Nodes. * @throws IMBException in case notification of existing Nodes fails */ private void addListeners() throws IMBException { // Subscribe to all future link creation and removal events. this.network.addListener(this, Network.NODE_ADD_EVENT); this.network.addListener(this, Network.NODE_REMOVE_EVENT); // For already existing links, post ourselves a LINK_ADD_EVENT for (Node node : this.network.getNodeMap().values()) { try { this.notify(new TimedEvent(Network.NODE_ADD_EVENT, this.network, node.getId(), getSimulator().getSimulatorTime())); } catch (RemoteException exception) { throw new IMBException(exception); } } } /** {@inheritDoc} */ @Override public void notify(final EventInterface event) throws RemoteException { EventType type = event.getType(); if (type.equals(Network.NODE_ADD_EVENT)) { Node node = this.network.getNode((String) event.getContent()); try { getConnector().postIMBMessage("Node", IMBEventType.NEW, new Object[] { getSimulator().getSimulatorTime().getTime().si, this.network.getId(), node.getId(), node.getPoint().x, node.getPoint().y, node.getPoint().z }); } catch (IMBException exception) { exception.printStackTrace(); } } else if (type.equals(Network.NODE_REMOVE_EVENT)) { Node node = this.network.getNode((String) event.getContent()); try { getConnector().postIMBMessage("Node", IMBEventType.DELETE, new Object[] { getSimulator().getSimulatorTime().getTime().si, this.network.getId(), node.getId() }); } catch (IMBException exception) { exception.printStackTrace(); } } else { System.err.println("NodeTransceiver.notify: Unhandled event: " + event); } } }