package de.tu_darmstadt.tk.SmartHomeNetworkSim.core; import java.util.Collection; import java.util.LinkedList; /** * Implementation of the Connection Interface, with main focus on performance * * @author Andreas T. Meyer-Berg */ public class ConnectionPerformance implements Connection { /** Link on which this connection runs */ protected Link link; /** Link on which this connection runs */ protected LinkedList participants; /** Link on which this connection runs */ protected LinkedList removedParticipants; /** Link on which this connection runs */ protected Protocol protocol; /** Link on which this connection runs */ protected double packetLossRate; /** Link on which this connection runs */ protected byte status; /** Link on which this connection runs */ protected boolean changed = false; /** Link on which this connection runs */ protected String name; /** default label */ protected short label = 0; /** * Initializes the connection, adds participants of the protocol * * @param l Link which this connection uses * @param p Protocol of the connection */ public ConnectionPerformance(Link l, Protocol p) { link = l; participants=new LinkedList(); removedParticipants = new LinkedList(); this.protocol = p; status =Connection.ACTIVE; if(p==null) name = "unspecified-connection"; else name = p.getName()+"-connection"; } public ConnectionPerformance() { link = null; this.protocol = null; participants=new LinkedList(); removedParticipants = new LinkedList(); status =Connection.ACTIVE; name = "unspecified-connection"; } @Override public Link getLink() { return link; } @Override public void setLink(Link link){ this.link = link; } @Override public Collection getParticipants() { LinkedList out = new LinkedList(); out.addAll(participants); return out; } @Override public Collection getParticipantsAndRemoved() { LinkedList out = new LinkedList(); out.addAll(participants); out.addAll(removedParticipants); return out; } @Override public boolean removeSmartDevice(Port sd) { boolean removed = participants.remove(sd); if(removed) removedParticipants.add(sd); if(participants.size()<2)setStatus(Connection.TERMINATED); return removed; } @Override public boolean addSmartDevice(Port sd) { return participants.add(sd); } @Override public Collection simulateTimeInterval(long startTime, long duration) { LinkedList returnPackets=new LinkedList(); returnPackets.addAll(getTerminationPackages(startTime)); for(Port p:participants){ if(p.getLastTrigger()+p.getTriggerInterval()(Long.compare(a.getTimestamp(),b.getTimestamp()))); if(label!=0) returnPackets.forEach(p->p.setLabel(label)); return returnPackets; } @Override public Collection encapsulatePackages(Collection packets) { packets.forEach(p->{ p.setLabel((short) Math.max(Math.max(label, p.getLabel()), p.getSource().getOwner().getLabel())); }); return packets; } @Override public Collection getTerminationPackages(long startTime) { changed=!removedParticipants.isEmpty(); removedParticipants.clear(); if(status==TERMINATED)status = DONE; return protocol.generateNextPackets(null, startTime, false); } @Override public Protocol getProtocol() { return protocol; } @Override public boolean setProtocol(Protocol protocol) { this.protocol = protocol; return true; } @Override public byte getStatus() { return status; } @Override public void setStatus(byte status) { this.status = status; } @Override public void setPacketLossProbability(double lossPercentage) { packetLossRate=lossPercentage; } @Override public double getPacketLossProbability() { return packetLossRate; } @Override public boolean getStatusChanged() { return changed; } @Override public String getName() { return name; } @Override public void setName(String name) { this.name = name; } @Override public short getLabel() { return label; } @Override public void setLabel(short label) { this.label = label; } }