package de.tu_darmstadt.tk.SmartHomeNetworkSim.core; import java.util.Collection; import java.util.Comparator; import java.util.Iterator; import java.util.LinkedList; import java.util.TreeSet; import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.util.PacketComparator; /** * Implementation of the Connection Interface, with focus on precision * * @author Andreas T. Meyer-Berg */ public class ConnectionPrecision extends ConnectionPerformance { /** * Packets which should be returned in the next time step */ private LinkedList outOfBoundsPackets = new LinkedList(); /** * Initializes the connection, adds participants of the protocol * * @param l Link which this connection uses * @param p Protocol of the connection */ public ConnectionPrecision(Link l, Protocol p) { super(l,p); } /** * Initializes the connection */ public ConnectionPrecision() { super(); } @Override public Collection simulateTimeInterval(long startTime, long duration) { /** * Time step the simulation interval ends */ long endTime = startTime+duration; /** * All packets which should be returned in this time step */ LinkedList returnPackets = new LinkedList(); returnPackets.addAll(getTerminationPackages(startTime)); startTime += returnPackets.size(); /** * Add packets of the last time step */ returnPackets.addAll(outOfBoundsPackets); /** * Sorted Tree (Sorted by next trigger time) */ TreeSet portTree = new TreeSet(new PortComparator()); portTree.addAll(participants); /** * Iterator to move through the Tree */ Iterator it = portTree.iterator(); /** * Traverse the tree ascending */ while(it.hasNext()){ Port p = it.next(); if(p.getStatus()==Port.SENDING && p.getLastTrigger()+p.getTriggerInterval() add back to the tree */ if(p.getLastTrigger()+p.getTriggerInterval()endTime) { outOfBoundsPackets.addFirst(returnPackets.removeLast()); if(returnPackets.isEmpty()) break; last = returnPackets.getLast(); } if(label!=0) returnPackets.forEach(p->p.setLabel(label)); return returnPackets; } /** * Comparator for comparing the next trigger time of two ports, the lower one should trigger first * * @author Andreas T. Meyer-Berg */ private class PortComparator implements Comparator{ @Override public int compare(Port o1, Port o2) { return Long.compare(o1.getLastTrigger()+o1.getTriggerInterval(),o2.getLastTrigger()+o2.getTriggerInterval()); } } }