123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- package de.tu_darmstadt.tk.SmartHomeNetworkSim.core.simpleImplementation;
- import java.util.Collection;
- import java.util.LinkedList;
- import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Connection;
- import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Link;
- import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Packet;
- import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.SmartDevice;
- import java.util.ArrayList;
- /**
- * Simple Implementation of {@link Link}, which allows connection of multiple
- * devices
- *
- * @author Andreas T. Meyer-Berg
- */
- public class SimpleLink implements Link {
- /**
- * Name of the connection
- */
- private String name;
- /**
- * Devices connected by this Link
- */
- private ArrayList<SmartDevice> devices;
- /**
- * Connections running via this Link
- */
- private ArrayList<Connection> connections;
- /**
- * List of packages to store packages sent during simulation intervals, or
- * after termination
- */
- private LinkedList<Packet> packets;
-
- /**
- * whether the status changed during the last simulation step
- */
- private boolean statusChanged;
-
- /**
- * Fixed Delay of the Link
- */
- private long fixedDelay = 2;
- /**
- * Initializes a simple Link with name and an empty devices list.
- *
- * @param name
- * Name the Link should have
- */
- public SimpleLink(String name) {
- this.name = name;
- this.devices = new ArrayList<SmartDevice>();
- this.packets = new LinkedList<Packet>();
- this.connections = new ArrayList<Connection>();
- }
-
- /**
- * Initializes a simple Link with default name and an empty devices list.
- */
- public SimpleLink() {
- this.name = "link name";
- this.devices = new ArrayList<SmartDevice>();
- this.packets = new LinkedList<Packet>();
- this.connections = new ArrayList<Connection>();
- }
- /**
- * Returns the name of this Link
- *
- * @return name of this Link
- */
- public String getName() {
- return name;
- }
- /**
- * Sets the name of this Link
- *
- * @param name
- * the name to set
- */
- public void setName(String name) {
- this.name = name;
- }
- /**
- * Returns the device that are part of this connection
- *
- * @return the devices of this Link
- */
- public Collection<SmartDevice> getDevices() {
- return devices;
- }
- /**
- * Adds the SmartDevice to this Link
- *
- * @param device
- * the devices to add
- */
- public void addDevice(SmartDevice device) {
- this.devices.add(device);
- }
- /**
- * Remove SmartDevice from this Link
- *
- * @param device
- * the device to remove
- */
- public void removeDevice(SmartDevice device) {
- this.devices.remove(device);
- }
- @Override
- public void simulateTimeInterval(long startTime, long duration) {
- packets.clear();
- statusChanged = false;
- for (Connection c : connections) {
- if (c.getLink() != this)
- continue;
- // Simulate just if source and link match
- if ((c.getStatus() == Connection.ACTIVE || c.getStatus() == Connection.HALTED))
- packets.addAll(c.simulateTimeInterval(startTime, duration));
- else if (c.getStatus() == Connection.FINISHED
- || c.getStatus() == Connection.TERMINATED)
- //Produce Termination packages
- packets.addAll(c.getTerminationPackages(startTime));
- statusChanged|=c.getStatusChanged();
- }
- /**
- * Unsorted Packets from multiple sorted Connections
- */
- }
- @Override
- public Collection<Packet> getPackets() {
- return packets;
- }
- @Override
- public Collection<Connection> getConnections() {
- return connections;
- }
- @Override
- public void addConnection(Connection connection) {
- connections.add(connection);
- }
- @Override
- public void removeConnection(Connection connection) {
- connections.remove(connection);
- }
- @Override
- public boolean getStatusChanged() {
- return statusChanged;
- }
- @Override
- public long getTransmissionDelayFrom(SmartDevice from, SmartDevice to) {
- return fixedDelay;
- }
-
- /**
- * Set the delay of this Link
- * @param delay fixed transmission delay
- */
- public void setFixedDelay(long delay){
- this.fixedDelay = delay;
- }
-
- /**
- * Returns the fixed transmission delay
- * @return transmission delay
- */
- public long getFixedDelay(){
- return this.fixedDelay;
- }
- }
|