package de.tu_darmstadt.tk.SmartHomeNetworkSim.core;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.util.Pair;
/**
* Protocol according to which the packages are created. Can contain different
* Roles and multiple participating SmartDevice(Port)s.
*
* @author Andreas T. Meyer-Berg
*/
public interface Protocol {
/*
* Topology options
*/
/**
* Every Port/Device is connected to every other Port/Device
*/
public static final byte FULLY_CONNECTED = 0;
/**
* Every Port/Device is connected to the Device of Role 0. (Has to be exactly one device of Role 0)
*/
public static final byte STAR = 1;
/**
* Custom topology, getTopology has to be overwritten.
*/
public static final byte CUSTOM = 127;
/**
* Generates the next packets
* If {@code port==null} the terminating packets shall be sent.
* This method should update port.setLastTrigger.
*
* @param port SmartDevice(Port) which sends the package, null if terminating packets shall be sent.
* @param timestep
* Time the package should be sent, in System.currentTimeMillis
* @param packetLost True if the packet was lost
* @return next Packet, which was sent
*/
public Collection generateNextPackets(Port port, long timestep, boolean packetLost);
/**
* Returns the number of different roles the participating SmartDevice(Port)s
* could have
*
* @return number of different roles
*/
public default int getNumberOfRoles(){
return getRoles() == null ? 0 : getRoles().length;
}
/**
* Returns the textual representation of the different Roles. The Array
* should contain {@code NumberOfRoles} Strings. {@code Array[role]} should be
* a human-readable String representation of the Role at its position
* {@code role}.
*
* @return String representations of the roles
*/
public String[] getRoles();
/**
* Returns all SmartDevice(Port)s of the given Role. Returns {@code null}, if the role
* number was invalid.
*
* @param role
* Position of the role in {@code getNumberOfRoles}
* @return SmartDevices of Role with index {@code role}
*/
public Collection getDevicesWithRole(int role);
/**
* Returns the role of the given device, returns -1, if the Device is not part of the protocol
*
* @param device device which roles should be calculated
* @return role of the device, -1 if it has no role
*/
public default int getRoleOfDevice(Port device){
if(device == null)
return -1;
for(int i = 0; i getDevices(){
LinkedList devices = new LinkedList();
for(int i=0; i> getTopology(){
LinkedList> topology = new LinkedList>();
switch (getTopologyType()){
case STAR:
Collection devices = getDevicesWithRole(0);
Port router = null;
if(devices.size() == 1)
router = devices.iterator().next();
for (Iterator iterator = getDevices().iterator(); iterator.hasNext();) {
Port right = (Port) (iterator.next());
if(router != right)
topology.add(new Pair(router, right));
}
break;
case FULLY_CONNECTED:
case CUSTOM:
default:
for (Iterator iterator = getDevices().iterator(); iterator.hasNext();) {
Port left = (Port) (iterator.next());
boolean samePosition = false;
for(Iterator iterator2 = getDevices().iterator(); iterator2.hasNext();){
Port right = iterator2.next();
if(!samePosition){
if(left==right)
samePosition=true;
}else{
topology.add(new Pair(left, right));
}
}
}
break;
}
return topology;
}
/**
* Returns the deleted
*
* @return deleted connection parts
*/
public Collection> getDeletedTopology();
}