|
@@ -7,52 +7,63 @@ 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.Port;
|
|
|
import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Protocol;
|
|
|
-import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.SmartDevice;
|
|
|
|
|
|
public class SimpleConnection implements Connection {
|
|
|
|
|
|
/**
|
|
|
- * SmartDevice which is the source of this connection
|
|
|
+ * SmartDevice(Port) which is the source of this connection
|
|
|
*/
|
|
|
- private SmartDevice source;
|
|
|
+ private Port source;
|
|
|
/**
|
|
|
- * SmartDevice which is the destination of this connection
|
|
|
+ * SmartDevice(Port) which is the destination of this connection
|
|
|
*/
|
|
|
- private SmartDevice destination;
|
|
|
+ private Port destination;
|
|
|
/**
|
|
|
- * Link which connects {@code source} and {@code destination}
|
|
|
+ * Link which connects {@code source} and {@code destination}
|
|
|
*/
|
|
|
private Link link;
|
|
|
/**
|
|
|
* Protocol, which specifies the packet generation
|
|
|
*/
|
|
|
private Protocol p;
|
|
|
-
|
|
|
- //for Termination
|
|
|
+
|
|
|
+ // for Termination
|
|
|
/**
|
|
|
- * SmartDevice which started the termination process
|
|
|
+ * SmartDevice(Port) which started the termination process
|
|
|
*/
|
|
|
- private SmartDevice srcOfTermination;
|
|
|
- /**
|
|
|
+ private Port srcOfTermination;
|
|
|
+ /* *
|
|
|
* SmartDevice which responds in the termination process
|
|
|
*/
|
|
|
- private SmartDevice other;
|
|
|
-
|
|
|
+ // private SmartDevice other;
|
|
|
+
|
|
|
/**
|
|
|
- * Creates a new connection between to SmartDevices on a given Link, which communicate with a protocol p
|
|
|
+ * Transmission status of the connection
|
|
|
+ */
|
|
|
+ private byte status = Connection.ACTIVE;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Creates a new connection between to SmartDevices on a given Link, which
|
|
|
+ * communicate with a protocol p
|
|
|
*
|
|
|
- * @param src SmartDevice which is the source of this connection
|
|
|
- * @param dest SmartDevice which is the destination of this connection
|
|
|
- * @param link Link which connects {@code src} and {@code dest}
|
|
|
- * @param p Protocol, which specifies the packet generation
|
|
|
+ * @param src
|
|
|
+ * SmartDevice which is the source of this connection
|
|
|
+ * @param dest
|
|
|
+ * SmartDevice which is the destination of this connection
|
|
|
+ * @param link
|
|
|
+ * Link which connects {@code src} and {@code dest}
|
|
|
+ * @param p
|
|
|
+ * Protocol, which specifies the packet generation
|
|
|
*/
|
|
|
- public SimpleConnection(SmartDevice src, SmartDevice dest, Link link, Protocol p) {
|
|
|
+ public SimpleConnection(Port src, Port dest, Link link, Protocol p) {
|
|
|
source = src;
|
|
|
destination = dest;
|
|
|
this.link = link;
|
|
|
this.p = p;
|
|
|
}
|
|
|
+
|
|
|
@Override
|
|
|
public Link getLink() {
|
|
|
return link;
|
|
@@ -61,31 +72,79 @@ public class SimpleConnection implements Connection {
|
|
|
@Override
|
|
|
public Collection<Packet> simulateTimeInterval(long startTime, long duration) {
|
|
|
LinkedList<Packet> list = new LinkedList<Packet>();
|
|
|
- for(long i = 0; i<duration; i+=100)
|
|
|
- list.add(p.generateNextPaket(startTime+i));
|
|
|
+ if(status != ACTIVE)return list;
|
|
|
+ //Generate packets by source
|
|
|
+ if(source.getLastTrigger()+source.getTriggerInterval()<startTime && source.getStatus()==Port.SENDING){
|
|
|
+ list.add(p.generateNextPaket(source, startTime));
|
|
|
+ }
|
|
|
+ while(source.getLastTrigger()+source.getTriggerInterval()<startTime+duration &&source.getStatus()==Port.SENDING)
|
|
|
+ list.add(p.generateNextPaket(source, source.getLastTrigger()+source.getTriggerInterval()));
|
|
|
+
|
|
|
+ if(destination.getLastTrigger()+destination.getTriggerInterval()<startTime && destination.getStatus()==Port.SENDING){
|
|
|
+ list.add(p.generateNextPaket(destination, startTime));
|
|
|
+ }
|
|
|
+ while(destination.getLastTrigger()+destination.getTriggerInterval()<startTime+duration &&destination.getStatus()==Port.SENDING)
|
|
|
+ list.add(p.generateNextPaket(destination, destination.getLastTrigger()+destination.getTriggerInterval()));
|
|
|
+
|
|
|
+ list.sort((x,y) -> Long.compare(x.getTimestamp(),y.getTimestamp()));
|
|
|
return list;
|
|
|
}
|
|
|
+
|
|
|
@Override
|
|
|
- public SmartDevice getSource() {
|
|
|
+ public Port getSource() {
|
|
|
return source;
|
|
|
}
|
|
|
+
|
|
|
@Override
|
|
|
- public Collection<SmartDevice> getParticipants() {
|
|
|
- return new LinkedList<SmartDevice>(Arrays.asList(source, destination));
|
|
|
+ public Collection<Port> getParticipants() {
|
|
|
+ return new LinkedList<Port>(Arrays.asList(source, destination));
|
|
|
}
|
|
|
+
|
|
|
@Override
|
|
|
- public void removeSmartDevice(SmartDevice sd) {
|
|
|
- //of source == null - connection was already terminated
|
|
|
- if(source == null)return;
|
|
|
- srcOfTermination = sd;
|
|
|
- other = sd == source? destination : source;
|
|
|
- source = null;
|
|
|
- destination = null;
|
|
|
+ public boolean removeSmartDevice(Port sd) {
|
|
|
+ if (sd == source || sd == destination) {
|
|
|
+ // of source == null - connection was already terminated
|
|
|
+ status = Connection.TERMINATED;
|
|
|
+ srcOfTermination = sd;
|
|
|
+ return true;
|
|
|
+ } else
|
|
|
+ return false;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
@Override
|
|
|
public Collection<Packet> getTerminationPackages(long startTime) {
|
|
|
- return new LinkedList<Packet>(Arrays.asList((Packet)new SimplePacket(startTime, srcOfTermination, other,"Terminated")));
|
|
|
+ status = DONE;
|
|
|
+ return new LinkedList<Packet>(Arrays.asList((Packet) new SimplePacket(
|
|
|
+ startTime, srcOfTermination.getOwner(),
|
|
|
+ srcOfTermination == source ? destination.getOwner() : source
|
|
|
+ .getOwner(), "Terminated")));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean addSmartDevice(Port sd) {
|
|
|
+ // Not possible to add Devices
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Protocol getProtocol() {
|
|
|
+ return p;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean setProtocol(Protocol protocol) {
|
|
|
+ return false;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public byte getStatus() {
|
|
|
+ return status;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void setStatus(byte status) {
|
|
|
+ this.status = status;
|
|
|
}
|
|
|
|
|
|
}
|