Преглед на файлове

Fixes spelling mistake in Protocol.generateNextPackets()

Andreas T. Meyer-Berg преди 5 години
родител
ревизия
2680537430

+ 3 - 3
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/ConnectionPerformance.java

@@ -99,10 +99,10 @@ public class ConnectionPerformance implements Connection {
 		returnPackets.addAll(getTerminationPackages(startTime));
 		for(Port p:participants){
 			if(p.getLastTrigger()+p.getTriggerInterval()<startTime && p.getStatus()==Port.SENDING){
-				returnPackets.addAll(protocol.generateNextPakets(p, (long) (startTime+p.getJitter()*Math.random()),Math.random()<packetLossRate));
+				returnPackets.addAll(protocol.generateNextPackets(p, (long) (startTime+p.getJitter()*Math.random()),Math.random()<packetLossRate));
 			}
 			while(p.getLastTrigger()+p.getTriggerInterval()<startTime+duration &&p.getStatus()==Port.SENDING)
-				returnPackets.addAll(protocol.generateNextPakets(p, (long) Math.max((p.getLastTrigger()+p.getTriggerInterval()+p.getJitter()*(Math.random())),p.getLastTrigger()+p.getTriggerInterval()),Math.random()<packetLossRate));
+				returnPackets.addAll(protocol.generateNextPackets(p, (long) Math.max((p.getLastTrigger()+p.getTriggerInterval()+p.getJitter()*(Math.random())),p.getLastTrigger()+p.getTriggerInterval()),Math.random()<packetLossRate));
 		}
 		returnPackets.sort((a,b)->(Long.compare(a.getTimestamp(),b.getTimestamp())));
 		return returnPackets;
@@ -113,7 +113,7 @@ public class ConnectionPerformance implements Connection {
 		changed=!removedParticipants.isEmpty();
 		removedParticipants.clear();
 		if(status==TERMINATED)status = DONE;
-		return protocol.generateNextPakets(null, startTime, false);
+		return protocol.generateNextPackets(null, startTime, false);
 	}
 
 	@Override

+ 1 - 1
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/ConnectionPrecision.java

@@ -81,7 +81,7 @@ public class ConnectionPrecision extends ConnectionPerformance {
 					/**
 					 * Generate first package in the simulation interval (not before startTime though), and at least 1ms after the last Trigger Time
 					 */
-					returnPackets.addAll(protocol.generateNextPakets(p, (long)Math.max( p.getLastTrigger()+p.getTriggerInterval()+p.getJitter()*Math.random(),Math.max(p.getLastTrigger(),startTime)),Math.random()<packetLossRate));
+					returnPackets.addAll(protocol.generateNextPackets(p, (long)Math.max( p.getLastTrigger()+p.getTriggerInterval()+p.getJitter()*Math.random(),Math.max(p.getLastTrigger(),startTime)),Math.random()<packetLossRate));
 					/**
 					 * If Port should simulate again in this interval -> add back to the tree
 					 */

+ 1 - 1
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/Protocol.java

@@ -42,7 +42,7 @@ public interface Protocol {
 	 * @param packetLost True if the packet was lost
 	 * @return next Packet, which was sent
 	 */
-	public Collection<Packet> generateNextPakets(Port port, long timestep, boolean packetLost);
+	public Collection<Packet> generateNextPackets(Port port, long timestep, boolean packetLost);
 
 	/**
 	 * Returns the number of different roles the participating SmartDevice(Port)s

+ 417 - 417
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/protocols/MQTT_protocol.java

@@ -1,417 +1,417 @@
-package de.tu_darmstadt.tk.SmartHomeNetworkSim.core.protocols;
-
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.LinkedList;
-
-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;
-import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.protocols.packets.MQTT_packet;
-import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.util.Pair;
-
-/**
- * Implementation of the MQTT Protocol to generate packets for the simulation
- * 
- *
- * @author Andreas T. Meyer-Berg
- */
-public class MQTT_protocol implements Protocol {
-
-	/**
-	 * Broker which collects and distributes messages
-	 */
-	private Port broker;
-
-	/**
-	 * Publishers like sensors, which publish data
-	 */
-	private LinkedList<Port> pubs;
-
-	/**
-	 * Subscriber which subscribe to different Topics
-	 */
-	private LinkedList<Port> subs;
-	
-	/**
-	 * Topics subscribed by each Port
-	 */
-	private HashMap<Port,LinkedList<String>> subbedTopics = new HashMap<Port,LinkedList<String>>();
-
-	/**
-	 * Devices that are Publisher and Subscriber and therefore send and receive
-	 * messages
-	 */
-	private LinkedList<Port> pubSubs;
-
-	/**
-	 * Packets which are currently being generated or were generated after the
-	 * last generatePackets Call
-	 */
-	private LinkedList<Packet> currentPackets = new LinkedList<Packet>();
-	
-	private LinkedList<Pair<Port,Port>> deletedConnectionLinks = new LinkedList<Pair<Port,Port>>();
-	private SmartDevice deletedBroker = new SmartDevice("DeletedBroker");
-	private Port deletedBrokerPort = new Port(deletedBroker,(short)-1);
-	
-	/**
-	 * Topics of the MQTT Broker
-	 */
-	private LinkedList<String> topics = new LinkedList<String>();
-
-	/**
-	 * Creates a new MQTT Protocol
-	 */
-	public MQTT_protocol() {
-		this.broker = null;
-	    initialize();
-	    
-	}
-	
-	/**
-	 * Creates a new MQTT Protocol
-	 * 
-	 * @param broker broker of the protocol
-	 */
-	public MQTT_protocol(Port broker) {
-		this.broker = broker;
-		initialize();
-		
-	}
-	
-	/**
-	 * Initializes the different fields
-	 */
-	private void initialize() {
-		topics.add("/home/temperatureHot");
-	    topics.add("/home/doorOpen");
-	    topics.add("/home/lightOn");
-	    subs = new LinkedList<Port>();
-	    pubs = new LinkedList<Port>();
-	    pubSubs = new LinkedList<Port>();
-	}
-	
-	@Override
-	public Collection<Packet> generateNextPakets(Port port, long timestep, boolean packetLost) {
-		/**
-		 * Packets which will be generated
-		 */
-		LinkedList<Packet> returnPackets = new LinkedList<Packet>();
-		//Update all Timestamps of previous created Packets
-		for(Packet p: currentPackets){
-			p.setTimestamp(p.getTimestamp()+timestep);
-		}
-		//Add these packets to the return
-		returnPackets.addAll(currentPackets);
-		//remove packets from the old list
-		currentPackets.clear();
-		//Clear deleted connections
-		deletedConnectionLinks.clear();
-		//Return termination packets
-		if(port==null)return returnPackets;
-		
-		/**
-		 * Update the lastTime the port was triggered
-		 */
-		port.setLastTrigger(timestep);
-		/**
-		 * if port null, skip this step
-		 */
-		if(broker==null)return returnPackets;
-		
-		/**
-		 * Generate new Packets regarding to their class
-		 */
-		if(port==broker){
-			//Broker should not send new packages, without new messages
-			//But could perform some Ping request to random participants
-			Collection<Port> devices = getDevices();
-			devices.remove(port);
-			Port dest = null;
-			java.util.Iterator<Port> it = devices.iterator();
-			for(int i=0; i<(Math.random()*devices.size())&&it.hasNext();i++){
-				dest = it.next();
-			}
-			if(dest != null){
-				returnPackets.add(new MQTT_packet(MQTT_packet.PINGREQ, timestep, port, dest));
-				if(dest.getStatus()!=Port.CLOSED)
-					returnPackets.add(new MQTT_packet(MQTT_packet.PINGREQ, timestep, dest, port));
-			}
-		}
-		if(subs.contains(port)||pubSubs.contains(port)){
-			
-			//Subs can either subscribe to topics or unsubscribe
-			/**
-			 * Topics, the SmartDevice is subscribed to
-			 */
-			LinkedList<String> tops= subbedTopics.get(port);
-			
-			/**
-			 * Topic which should be subscribed or unsubscribed
-			 */
-			String newTopic;
-			//Subscribe if no subscriptions so far or not subscribed to all, with a probability of70%
-			if(tops.size()==0||(tops.size()!=topics.size()&&Math.random()<0.7)){
-				//Forced Subscribe
-				LinkedList<String> available = new LinkedList<String>();
-				available.addAll(topics);
-				available.removeAll(tops);
-				
-				newTopic = available.get((int) Math.floor(Math.random()*available.size()));
-				returnPackets.add(new MQTT_packet(MQTT_packet.SUBSCRIBE, timestep, port, broker, "topic:"+newTopic));
-				
-				if(broker.getStatus()!=Port.CLOSED){
-					timestep+=broker.getResponseTime();
-					returnPackets.add(new MQTT_packet(MQTT_packet.SUBACK, timestep, broker, port));
-					tops.add(newTopic);
-				}
-			}
-			else if(tops.size()==topics.size()){
-				//Forced Unsubscribe
-				newTopic = tops.get((int) Math.floor(Math.random()*tops.size()));
-				returnPackets.add(new MQTT_packet(MQTT_packet.UNSUBSCRIBE, timestep, port, broker, "topic:"+newTopic));
-				if(broker.getStatus()!=Port.CLOSED){
-					timestep+=broker.getResponseTime();
-					returnPackets.add(new MQTT_packet(MQTT_packet.UNSUBACK, timestep, broker, port));
-					tops.remove(newTopic);
-				}
-			}
-		}
-		if(pubs.contains(port)||pubSubs.contains(port)&&Math.random()<0.3){
-			String newTopic = topics.get((int) Math.floor(Math.random()*topics.size()));
-			/**
-			 * Message to be published (Should be further specialized by a Sensor Device) 
-			 */
-			String msg = "Topic:"+newTopic+":"+(Math.random()<0.5?"true":"false");
-			returnPackets.add(new MQTT_packet(MQTT_packet.PUBLISH, timestep, port, broker, msg));
-			//Response
-			timestep+=broker.getResponseTime();
-			returnPackets.add(new MQTT_packet(MQTT_packet.PUBACK, timestep, broker, port));
-			
-			//Publish to Subscribers
-			//Should be be improved to just notify Subs that are subscribed to the topic
-			if(broker.getStatus()!=Port.CLOSED){
-				for(Port p:subs){
-					if(!subbedTopics.get(p).contains(newTopic))continue;//Skip unsubbed ports
-					timestep+=broker.getResponseTime();
-					returnPackets.add(new MQTT_packet(MQTT_packet.PUBLISH, timestep, broker, p, msg));
-					returnPackets.add(new MQTT_packet(MQTT_packet.PUBACK, timestep+p.getResponseTime(), p, broker));
-				}
-				for(Port p:pubSubs){
-					if(!subbedTopics.get(p).contains(newTopic))continue;//skip unsubbed ports
-					timestep+=broker.getResponseTime();
-					returnPackets.add(new MQTT_packet(MQTT_packet.PUBLISH, timestep, broker, p, msg));
-					returnPackets.add(new MQTT_packet(MQTT_packet.PUBACK, timestep+p.getResponseTime(), p, broker));
-				}
-			}
-		}
-		if(Math.random() < 0.05 && port != broker){
-			returnPackets.add(new MQTT_packet(MQTT_packet.PINGREQ, timestep, port, broker));
-			if(port.getStatus()!=Port.CLOSED)
-				returnPackets.add(new MQTT_packet(MQTT_packet.PINGREQ, timestep, broker, port));
-		}
-		return returnPackets;
-	}
-
-	@Override
-	public int getNumberOfRoles() {
-		return 4;
-	}
-
-	@Override
-	public String[] getRoles() {
-		// PublisherSubscriber is Publisher as well as Subscriber
-		return new String[] { "Broker", "PublisherSubscriber", "Publisher", "Subscriber" };
-	}
-
-	@Override
-	public Collection<Port> getDevicesWithRole(int role) {
-		switch (role) {
-		case 0:
-			return new LinkedList<Port>(Arrays.asList(broker));
-		case 1:
-			return pubSubs;
-		case 2:
-			return pubs;
-		case 3:
-			return subs;
-
-		default:
-			return null;
-		}
-	}
-
-	@Override
-	public boolean addDeviceOfRole(Port device, int role) {
-		/*
-		 * First device has to be the Broker 
-		 */
-		if (broker==null) {
-			if (role == 0) {
-				broker = device;
-				updateBrokerOnDeletedConnections(null);
-				return true;
-			} else {
-				return false;
-			}
-		}
-		switch (role) {
-		case 0:
-			//Just one broker allowed.
-			return false;
-		case 1:
-			pubSubs.add(device);
-			subbedTopics.putIfAbsent(device, new LinkedList<String>());
-			break;
-		case 2:
-			pubs.add(device);
-			break;
-		case 3:
-			subs.add(device);
-			subbedTopics.putIfAbsent(device, new LinkedList<String>());
-			break;
-		default:
-			// invalid role
-			return false;
-		}
-		// Create packets for the connecting Client
-		currentPackets.add(new MQTT_packet(MQTT_packet.CONNECT, currentPackets.size()/2, device, broker));
-		currentPackets.add(new MQTT_packet(MQTT_packet.CONNACK, currentPackets.size()/2+1, broker, device));
-		return true;
-	}
-
-	@Override
-	public void removeDevice(Port device) {
-		/**
-		 * true if the device was removed
-		 */
-		boolean removedDevice=false;
-		if (broker == device){
-			broker = null;
-			deletedConnectionLinks.add(new Pair<Port, Port>(new Port(device.getOwner(), (short)-1), deletedBrokerPort));
-			updateBrokerOnDeletedConnections(device);
-		}
-		removedDevice|=pubSubs.remove(device);
-		removedDevice|=subs.remove(device);
-		removedDevice|=pubs.remove(device);
-		//Remove Port from topics and clear its list
-		LinkedList<String> oldTopics = subbedTopics.remove(device);
-		if(oldTopics!=null)oldTopics.clear();
-		if(removedDevice){
-			if(broker == null){
-				deletedConnectionLinks.add(new Pair<Port, Port>(deletedBrokerPort,device));
-			}else{
-				deletedConnectionLinks.add(new Pair<Port, Port>(broker, device));
-				//If not the broker and device was removed -> disconnect
-				currentPackets.add(new MQTT_packet(MQTT_packet.DISCONNECT, currentPackets.size()/2, device, broker));
-			}
-		}
-	}
-	
-	public void addTopic(String topic){
-		topics.add(topic);
-	}
-
-	@Override
-	public String getName() {
-		return "MQTT";
-	}
-	
-	@Override
-	public int getRoleOfDevice(Port device){
-		if(device == null)
-			return -1;
-		if(device == broker)
-			return 0;
-		if(pubSubs.contains(device))
-			return 1;
-		if(pubs.contains(device))
-			return 2;
-		if(subs.contains(device))
-			return 3;
-		return -1;
-	}
-	
-	@Override
-	public Collection<Port> getDevices(){
-		LinkedList<Port> returnDevices = new LinkedList<Port>();
-		if(broker!=null)
-			returnDevices.add(broker);
-		returnDevices.addAll(pubSubs);
-		returnDevices.addAll(pubs);
-		returnDevices.addAll(subs);
-		return returnDevices;
-	}
-	
-	@Override
-	public byte getTopologyType() {
-		return STAR;
-	}
-	
-	@Override
-	public Collection<Pair<Port,Port>> getTopology(){
-		LinkedList<Pair<Port,Port>> topology = new LinkedList<Pair<Port,Port>>();
-		Port center = broker;
-		calcDeletedBrokerPosition();
-		if(broker==null)
-			center = new Port(deletedBroker, (short)-1);
-		for(Port p: pubSubs)
-			topology.add(new Pair<Port, Port>(center, p));
-		for(Port p: pubs)
-			topology.add(new Pair<Port, Port>(center, p));
-		for(Port p: subs)
-			topology.add(new Pair<Port, Port>(center, p));
-		return topology;
-	}
-
-	@Override
-	public Collection<Pair<Port, Port>> getDeletedTopology() {
-		calcDeletedBrokerPosition();
-		return deletedConnectionLinks;
-	}
-	
-	/**
-	 * Calculate and update the position of the deleted Broker
-	 */
-	private void calcDeletedBrokerPosition(){
-		if(broker == null){
-			int x = 0, y = 0, noP = 0;
-			for(Port p: getDevices()){
-				if(p!=null && p.getOwner()!=null){
-					x += p.getOwner().getX();
-					y += p.getOwner().getY();
-					noP++;
-				}
-			}
-			if(noP==0)
-				return;
-			deletedBroker.setX(((int)(x*1.0)/noP));
-			deletedBroker.setY(((int)(y*1.0)/noP));
-		}
-	}
-	
-	/**
-	 * Update the broker port on the deleted Connections
-	 * 
-	 * @param device old broker
-	 */
-	private void updateBrokerOnDeletedConnections(Port device){
-		for(Pair<Port, Port> p: deletedConnectionLinks){
-			if(broker == null){
-				if(p.getLeft() == device)
-					p.setLeft(deletedBrokerPort);
-				if(p.getRight() == device)
-					p.setRight(deletedBrokerPort);
-			}else{
-				if(p.getLeft() == deletedBrokerPort)
-					p.setLeft(broker);
-				if(p.getRight() == deletedBrokerPort)
-					p.setRight(broker);
-				
-			}
-		}
-	}
-
-}
+package de.tu_darmstadt.tk.SmartHomeNetworkSim.core.protocols;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.LinkedList;
+
+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;
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.protocols.packets.MQTT_packet;
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.util.Pair;
+
+/**
+ * Implementation of the MQTT Protocol to generate packets for the simulation
+ * 
+ *
+ * @author Andreas T. Meyer-Berg
+ */
+public class MQTT_protocol implements Protocol {
+
+	/**
+	 * Broker which collects and distributes messages
+	 */
+	private Port broker;
+
+	/**
+	 * Publishers like sensors, which publish data
+	 */
+	private LinkedList<Port> pubs;
+
+	/**
+	 * Subscriber which subscribe to different Topics
+	 */
+	private LinkedList<Port> subs;
+	
+	/**
+	 * Topics subscribed by each Port
+	 */
+	private HashMap<Port,LinkedList<String>> subbedTopics = new HashMap<Port,LinkedList<String>>();
+
+	/**
+	 * Devices that are Publisher and Subscriber and therefore send and receive
+	 * messages
+	 */
+	private LinkedList<Port> pubSubs;
+
+	/**
+	 * Packets which are currently being generated or were generated after the
+	 * last generatePackets Call
+	 */
+	private LinkedList<Packet> currentPackets = new LinkedList<Packet>();
+	
+	private LinkedList<Pair<Port,Port>> deletedConnectionLinks = new LinkedList<Pair<Port,Port>>();
+	private SmartDevice deletedBroker = new SmartDevice("DeletedBroker");
+	private Port deletedBrokerPort = new Port(deletedBroker,(short)-1);
+	
+	/**
+	 * Topics of the MQTT Broker
+	 */
+	private LinkedList<String> topics = new LinkedList<String>();
+
+	/**
+	 * Creates a new MQTT Protocol
+	 */
+	public MQTT_protocol() {
+		this.broker = null;
+	    initialize();
+	    
+	}
+	
+	/**
+	 * Creates a new MQTT Protocol
+	 * 
+	 * @param broker broker of the protocol
+	 */
+	public MQTT_protocol(Port broker) {
+		this.broker = broker;
+		initialize();
+		
+	}
+	
+	/**
+	 * Initializes the different fields
+	 */
+	private void initialize() {
+		topics.add("/home/temperatureHot");
+	    topics.add("/home/doorOpen");
+	    topics.add("/home/lightOn");
+	    subs = new LinkedList<Port>();
+	    pubs = new LinkedList<Port>();
+	    pubSubs = new LinkedList<Port>();
+	}
+	
+	@Override
+	public Collection<Packet> generateNextPackets(Port port, long timestep, boolean packetLost) {
+		/**
+		 * Packets which will be generated
+		 */
+		LinkedList<Packet> returnPackets = new LinkedList<Packet>();
+		//Update all Timestamps of previous created Packets
+		for(Packet p: currentPackets){
+			p.setTimestamp(p.getTimestamp()+timestep);
+		}
+		//Add these packets to the return
+		returnPackets.addAll(currentPackets);
+		//remove packets from the old list
+		currentPackets.clear();
+		//Clear deleted connections
+		deletedConnectionLinks.clear();
+		//Return termination packets
+		if(port==null)return returnPackets;
+		
+		/**
+		 * Update the lastTime the port was triggered
+		 */
+		port.setLastTrigger(timestep);
+		/**
+		 * if port null, skip this step
+		 */
+		if(broker==null)return returnPackets;
+		
+		/**
+		 * Generate new Packets regarding to their class
+		 */
+		if(port==broker){
+			//Broker should not send new packages, without new messages
+			//But could perform some Ping request to random participants
+			Collection<Port> devices = getDevices();
+			devices.remove(port);
+			Port dest = null;
+			java.util.Iterator<Port> it = devices.iterator();
+			for(int i=0; i<(Math.random()*devices.size())&&it.hasNext();i++){
+				dest = it.next();
+			}
+			if(dest != null){
+				returnPackets.add(new MQTT_packet(MQTT_packet.PINGREQ, timestep, port, dest));
+				if(dest.getStatus()!=Port.CLOSED)
+					returnPackets.add(new MQTT_packet(MQTT_packet.PINGREQ, timestep, dest, port));
+			}
+		}
+		if(subs.contains(port)||pubSubs.contains(port)){
+			
+			//Subs can either subscribe to topics or unsubscribe
+			/**
+			 * Topics, the SmartDevice is subscribed to
+			 */
+			LinkedList<String> tops= subbedTopics.get(port);
+			
+			/**
+			 * Topic which should be subscribed or unsubscribed
+			 */
+			String newTopic;
+			//Subscribe if no subscriptions so far or not subscribed to all, with a probability of70%
+			if(tops.size()==0||(tops.size()!=topics.size()&&Math.random()<0.7)){
+				//Forced Subscribe
+				LinkedList<String> available = new LinkedList<String>();
+				available.addAll(topics);
+				available.removeAll(tops);
+				
+				newTopic = available.get((int) Math.floor(Math.random()*available.size()));
+				returnPackets.add(new MQTT_packet(MQTT_packet.SUBSCRIBE, timestep, port, broker, "topic:"+newTopic));
+				
+				if(broker.getStatus()!=Port.CLOSED){
+					timestep+=broker.getResponseTime();
+					returnPackets.add(new MQTT_packet(MQTT_packet.SUBACK, timestep, broker, port));
+					tops.add(newTopic);
+				}
+			}
+			else if(tops.size()==topics.size()){
+				//Forced Unsubscribe
+				newTopic = tops.get((int) Math.floor(Math.random()*tops.size()));
+				returnPackets.add(new MQTT_packet(MQTT_packet.UNSUBSCRIBE, timestep, port, broker, "topic:"+newTopic));
+				if(broker.getStatus()!=Port.CLOSED){
+					timestep+=broker.getResponseTime();
+					returnPackets.add(new MQTT_packet(MQTT_packet.UNSUBACK, timestep, broker, port));
+					tops.remove(newTopic);
+				}
+			}
+		}
+		if(pubs.contains(port)||pubSubs.contains(port)&&Math.random()<0.3){
+			String newTopic = topics.get((int) Math.floor(Math.random()*topics.size()));
+			/**
+			 * Message to be published (Should be further specialized by a Sensor Device) 
+			 */
+			String msg = "Topic:"+newTopic+":"+(Math.random()<0.5?"true":"false");
+			returnPackets.add(new MQTT_packet(MQTT_packet.PUBLISH, timestep, port, broker, msg));
+			//Response
+			timestep+=broker.getResponseTime();
+			returnPackets.add(new MQTT_packet(MQTT_packet.PUBACK, timestep, broker, port));
+			
+			//Publish to Subscribers
+			//Should be be improved to just notify Subs that are subscribed to the topic
+			if(broker.getStatus()!=Port.CLOSED){
+				for(Port p:subs){
+					if(!subbedTopics.get(p).contains(newTopic))continue;//Skip unsubbed ports
+					timestep+=broker.getResponseTime();
+					returnPackets.add(new MQTT_packet(MQTT_packet.PUBLISH, timestep, broker, p, msg));
+					returnPackets.add(new MQTT_packet(MQTT_packet.PUBACK, timestep+p.getResponseTime(), p, broker));
+				}
+				for(Port p:pubSubs){
+					if(!subbedTopics.get(p).contains(newTopic))continue;//skip unsubbed ports
+					timestep+=broker.getResponseTime();
+					returnPackets.add(new MQTT_packet(MQTT_packet.PUBLISH, timestep, broker, p, msg));
+					returnPackets.add(new MQTT_packet(MQTT_packet.PUBACK, timestep+p.getResponseTime(), p, broker));
+				}
+			}
+		}
+		if(Math.random() < 0.05 && port != broker){
+			returnPackets.add(new MQTT_packet(MQTT_packet.PINGREQ, timestep, port, broker));
+			if(port.getStatus()!=Port.CLOSED)
+				returnPackets.add(new MQTT_packet(MQTT_packet.PINGREQ, timestep, broker, port));
+		}
+		return returnPackets;
+	}
+
+	@Override
+	public int getNumberOfRoles() {
+		return 4;
+	}
+
+	@Override
+	public String[] getRoles() {
+		// PublisherSubscriber is Publisher as well as Subscriber
+		return new String[] { "Broker", "PublisherSubscriber", "Publisher", "Subscriber" };
+	}
+
+	@Override
+	public Collection<Port> getDevicesWithRole(int role) {
+		switch (role) {
+		case 0:
+			return new LinkedList<Port>(Arrays.asList(broker));
+		case 1:
+			return pubSubs;
+		case 2:
+			return pubs;
+		case 3:
+			return subs;
+
+		default:
+			return null;
+		}
+	}
+
+	@Override
+	public boolean addDeviceOfRole(Port device, int role) {
+		/*
+		 * First device has to be the Broker 
+		 */
+		if (broker==null) {
+			if (role == 0) {
+				broker = device;
+				updateBrokerOnDeletedConnections(null);
+				return true;
+			} else {
+				return false;
+			}
+		}
+		switch (role) {
+		case 0:
+			//Just one broker allowed.
+			return false;
+		case 1:
+			pubSubs.add(device);
+			subbedTopics.putIfAbsent(device, new LinkedList<String>());
+			break;
+		case 2:
+			pubs.add(device);
+			break;
+		case 3:
+			subs.add(device);
+			subbedTopics.putIfAbsent(device, new LinkedList<String>());
+			break;
+		default:
+			// invalid role
+			return false;
+		}
+		// Create packets for the connecting Client
+		currentPackets.add(new MQTT_packet(MQTT_packet.CONNECT, currentPackets.size()/2, device, broker));
+		currentPackets.add(new MQTT_packet(MQTT_packet.CONNACK, currentPackets.size()/2+1, broker, device));
+		return true;
+	}
+
+	@Override
+	public void removeDevice(Port device) {
+		/**
+		 * true if the device was removed
+		 */
+		boolean removedDevice=false;
+		if (broker == device){
+			broker = null;
+			deletedConnectionLinks.add(new Pair<Port, Port>(new Port(device.getOwner(), (short)-1), deletedBrokerPort));
+			updateBrokerOnDeletedConnections(device);
+		}
+		removedDevice|=pubSubs.remove(device);
+		removedDevice|=subs.remove(device);
+		removedDevice|=pubs.remove(device);
+		//Remove Port from topics and clear its list
+		LinkedList<String> oldTopics = subbedTopics.remove(device);
+		if(oldTopics!=null)oldTopics.clear();
+		if(removedDevice){
+			if(broker == null){
+				deletedConnectionLinks.add(new Pair<Port, Port>(deletedBrokerPort,device));
+			}else{
+				deletedConnectionLinks.add(new Pair<Port, Port>(broker, device));
+				//If not the broker and device was removed -> disconnect
+				currentPackets.add(new MQTT_packet(MQTT_packet.DISCONNECT, currentPackets.size()/2, device, broker));
+			}
+		}
+	}
+	
+	public void addTopic(String topic){
+		topics.add(topic);
+	}
+
+	@Override
+	public String getName() {
+		return "MQTT";
+	}
+	
+	@Override
+	public int getRoleOfDevice(Port device){
+		if(device == null)
+			return -1;
+		if(device == broker)
+			return 0;
+		if(pubSubs.contains(device))
+			return 1;
+		if(pubs.contains(device))
+			return 2;
+		if(subs.contains(device))
+			return 3;
+		return -1;
+	}
+	
+	@Override
+	public Collection<Port> getDevices(){
+		LinkedList<Port> returnDevices = new LinkedList<Port>();
+		if(broker!=null)
+			returnDevices.add(broker);
+		returnDevices.addAll(pubSubs);
+		returnDevices.addAll(pubs);
+		returnDevices.addAll(subs);
+		return returnDevices;
+	}
+	
+	@Override
+	public byte getTopologyType() {
+		return STAR;
+	}
+	
+	@Override
+	public Collection<Pair<Port,Port>> getTopology(){
+		LinkedList<Pair<Port,Port>> topology = new LinkedList<Pair<Port,Port>>();
+		Port center = broker;
+		calcDeletedBrokerPosition();
+		if(broker==null)
+			center = new Port(deletedBroker, (short)-1);
+		for(Port p: pubSubs)
+			topology.add(new Pair<Port, Port>(center, p));
+		for(Port p: pubs)
+			topology.add(new Pair<Port, Port>(center, p));
+		for(Port p: subs)
+			topology.add(new Pair<Port, Port>(center, p));
+		return topology;
+	}
+
+	@Override
+	public Collection<Pair<Port, Port>> getDeletedTopology() {
+		calcDeletedBrokerPosition();
+		return deletedConnectionLinks;
+	}
+	
+	/**
+	 * Calculate and update the position of the deleted Broker
+	 */
+	private void calcDeletedBrokerPosition(){
+		if(broker == null){
+			int x = 0, y = 0, noP = 0;
+			for(Port p: getDevices()){
+				if(p!=null && p.getOwner()!=null){
+					x += p.getOwner().getX();
+					y += p.getOwner().getY();
+					noP++;
+				}
+			}
+			if(noP==0)
+				return;
+			deletedBroker.setX(((int)(x*1.0)/noP));
+			deletedBroker.setY(((int)(y*1.0)/noP));
+		}
+	}
+	
+	/**
+	 * Update the broker port on the deleted Connections
+	 * 
+	 * @param device old broker
+	 */
+	private void updateBrokerOnDeletedConnections(Port device){
+		for(Pair<Port, Port> p: deletedConnectionLinks){
+			if(broker == null){
+				if(p.getLeft() == device)
+					p.setLeft(deletedBrokerPort);
+				if(p.getRight() == device)
+					p.setRight(deletedBrokerPort);
+			}else{
+				if(p.getLeft() == deletedBrokerPort)
+					p.setLeft(broker);
+				if(p.getRight() == deletedBrokerPort)
+					p.setRight(broker);
+				
+			}
+		}
+	}
+
+}

+ 118 - 118
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/protocols/Ping_protocol.java

@@ -1,118 +1,118 @@
-package de.tu_darmstadt.tk.SmartHomeNetworkSim.core.protocols;
-
-import java.util.Collection;
-import java.util.LinkedList;
-
-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.protocols.packets.Ping_packet;
-import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.util.Pair;
-
-/**
- * Ping Protocol which sends simple Ping requests to the target
- * 
- * @author Andreas T. Meyer-Berg
- */
-public class Ping_protocol implements Protocol {
-
-	/**
-	 * Sender / Source of the Ping Request
-	 */
-	private Port sender;
-	/**
-	 * Target / Destination of the Ping Request
-	 */
-	private Port target;
-	/**
-	 * Creates a new Ping Protocol
-	 */
-	public Ping_protocol(){
-		sender = null;
-		target = null;
-	}
-	short sequence = 0;
-	@Override
-	public Collection<Packet> generateNextPakets(Port port, long timestep, boolean packetLost) {
-		/**
-		 * Packets which are generated during this timestep
-		 */
-		LinkedList<Packet> packets = new LinkedList<>();
-		if(port == null || port.getStatus() != Port.SENDING)
-			return packets;
-		/**
-		 * Update trigger time
-		 */
-		port.setLastTrigger(timestep);
-		
-		if(port == sender && target != null){
-			packets.add(new Ping_packet(timestep, sender, target, Ping_packet.EchoRequest, sequence));
-			if(target.getStatus() >= Port.OPEN){
-				packets.add(new Ping_packet(timestep+target.getResponseTime()+(short)(target.getJitter()*Math.random()), target, sender, Ping_packet.EchoReply, sequence));
-			}
-		}
-		else if(port == target && sender != null){
-			packets.add(new Ping_packet(timestep, target, sender, Ping_packet.EchoRequest, sequence));
-			if(sender.getStatus() >= Port.OPEN){
-				packets.add(new Ping_packet(timestep+sender.getResponseTime()+(short)(sender.getJitter()*Math.random()), sender, target, Ping_packet.EchoReply, sequence));
-			}
-		}
-		return packets;
-	}
-
-	@Override
-	public String[] getRoles() {
-		return new String[]{"Sender", "Target"};
-	}
-
-	@Override
-	public Collection<Port> getDevicesWithRole(int role) {
-		/**
-		 * Devices of the given Role
-		 */
-		LinkedList<Port> devicesOfRole = new LinkedList<Port>();
-		switch (role) {
-		case 0:
-			if(sender != null)
-				devicesOfRole.add(sender);
-			break;
-		case 1:
-			if(target != null)
-				devicesOfRole.add(target);
-			break;
-		default:
-			return null; 
-		}
-		return devicesOfRole;
-	}
-
-	@Override
-	public boolean addDeviceOfRole(Port device, int role) {
-		if(role == 0 &&sender == null)
-			sender = device;
-		else if(role == 1 && target == null)
-			target = device;
-		else
-			return false;
-		return true;
-	}
-
-	@Override
-	public void removeDevice(Port device) {
-		if(sender == device)
-			sender = null;
-		if(target == device)
-			target = null;
-	}
-
-	@Override
-	public String getName() {
-		return "Ping";
-	}
-
-	@Override
-	public Collection<Pair<Port, Port>> getDeletedTopology() {
-		return new LinkedList<Pair<Port, Port>>();
-	}
-
-}
+package de.tu_darmstadt.tk.SmartHomeNetworkSim.core.protocols;
+
+import java.util.Collection;
+import java.util.LinkedList;
+
+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.protocols.packets.Ping_packet;
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.util.Pair;
+
+/**
+ * Ping Protocol which sends simple Ping requests to the target
+ * 
+ * @author Andreas T. Meyer-Berg
+ */
+public class Ping_protocol implements Protocol {
+
+	/**
+	 * Sender / Source of the Ping Request
+	 */
+	private Port sender;
+	/**
+	 * Target / Destination of the Ping Request
+	 */
+	private Port target;
+	/**
+	 * Creates a new Ping Protocol
+	 */
+	public Ping_protocol(){
+		sender = null;
+		target = null;
+	}
+	short sequence = 0;
+	@Override
+	public Collection<Packet> generateNextPackets(Port port, long timestep, boolean packetLost) {
+		/**
+		 * Packets which are generated during this timestep
+		 */
+		LinkedList<Packet> packets = new LinkedList<>();
+		if(port == null || port.getStatus() != Port.SENDING)
+			return packets;
+		/**
+		 * Update trigger time
+		 */
+		port.setLastTrigger(timestep);
+		
+		if(port == sender && target != null){
+			packets.add(new Ping_packet(timestep, sender, target, Ping_packet.EchoRequest, sequence));
+			if(target.getStatus() >= Port.OPEN){
+				packets.add(new Ping_packet(timestep+target.getResponseTime()+(short)(target.getJitter()*Math.random()), target, sender, Ping_packet.EchoReply, sequence));
+			}
+		}
+		else if(port == target && sender != null){
+			packets.add(new Ping_packet(timestep, target, sender, Ping_packet.EchoRequest, sequence));
+			if(sender.getStatus() >= Port.OPEN){
+				packets.add(new Ping_packet(timestep+sender.getResponseTime()+(short)(sender.getJitter()*Math.random()), sender, target, Ping_packet.EchoReply, sequence));
+			}
+		}
+		return packets;
+	}
+
+	@Override
+	public String[] getRoles() {
+		return new String[]{"Sender", "Target"};
+	}
+
+	@Override
+	public Collection<Port> getDevicesWithRole(int role) {
+		/**
+		 * Devices of the given Role
+		 */
+		LinkedList<Port> devicesOfRole = new LinkedList<Port>();
+		switch (role) {
+		case 0:
+			if(sender != null)
+				devicesOfRole.add(sender);
+			break;
+		case 1:
+			if(target != null)
+				devicesOfRole.add(target);
+			break;
+		default:
+			return null; 
+		}
+		return devicesOfRole;
+	}
+
+	@Override
+	public boolean addDeviceOfRole(Port device, int role) {
+		if(role == 0 &&sender == null)
+			sender = device;
+		else if(role == 1 && target == null)
+			target = device;
+		else
+			return false;
+		return true;
+	}
+
+	@Override
+	public void removeDevice(Port device) {
+		if(sender == device)
+			sender = null;
+		if(target == device)
+			target = null;
+	}
+
+	@Override
+	public String getName() {
+		return "Ping";
+	}
+
+	@Override
+	public Collection<Pair<Port, Port>> getDeletedTopology() {
+		return new LinkedList<Pair<Port, Port>>();
+	}
+
+}

+ 4 - 4
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/simpleImplementation/SimpleConnection.java

@@ -103,16 +103,16 @@ public class SimpleConnection implements Connection {
 			statusChanged = false;
 		//Generate packets by source
 		if(source.getLastTrigger()+source.getTriggerInterval()<startTime && source.getStatus()==Port.SENDING){
-			list.addAll(p.generateNextPakets(source, startTime,Math.random()<packetLossProbability));
+			list.addAll(p.generateNextPackets(source, startTime,Math.random()<packetLossProbability));
 		}
 		while(source.getLastTrigger()+source.getTriggerInterval()<startTime+duration &&source.getStatus()==Port.SENDING)
-			list.addAll(p.generateNextPakets(source, source.getLastTrigger()+source.getTriggerInterval(),Math.random()<packetLossProbability));
+			list.addAll(p.generateNextPackets(source, source.getLastTrigger()+source.getTriggerInterval(),Math.random()<packetLossProbability));
 		
 		if(destination.getLastTrigger()+destination.getTriggerInterval()<startTime && destination.getStatus()==Port.SENDING){
-			list.addAll(p.generateNextPakets(destination, startTime,Math.random()<packetLossProbability));
+			list.addAll(p.generateNextPackets(destination, startTime,Math.random()<packetLossProbability));
 		}
 		while(destination.getLastTrigger()+destination.getTriggerInterval()<startTime+duration &&destination.getStatus()==Port.SENDING)
-			list.addAll(p.generateNextPakets(destination, destination.getLastTrigger()+destination.getTriggerInterval(),Math.random()<packetLossProbability));
+			list.addAll(p.generateNextPackets(destination, destination.getLastTrigger()+destination.getTriggerInterval(),Math.random()<packetLossProbability));
 
 		list.sort((x,y) -> Long.compare(x.getTimestamp(),y.getTimestamp()));
 		return list;

+ 1 - 1
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/simpleImplementation/SimpleProtocol.java

@@ -47,7 +47,7 @@ public class SimpleProtocol implements Protocol {
 		srcSends = true;
 	}
 	@Override
-	public Collection<Packet> generateNextPakets(Port p, long timestep, boolean packetLost) {
+	public Collection<Packet> generateNextPackets(Port p, long timestep, boolean packetLost) {
 		LinkedList<Packet> ret = new LinkedList<Packet>();
 		if(terminationPacket!=null){
 			terminationPacket.setTimestamp(timestep);

+ 1 - 1
src/test/java/de/tu_darmstadt/tk/shNetSimTests/control/ValidProtocol.java

@@ -48,7 +48,7 @@ public class ValidProtocol implements Protocol {
 		srcSends = true;
 	}
 	@Override
-	public Collection<Packet> generateNextPakets(Port p, long timestep, boolean packetLost) {
+	public Collection<Packet> generateNextPackets(Port p, long timestep, boolean packetLost) {
 		LinkedList<Packet> ret = new LinkedList<Packet>();
 		if(terminationPacket!=null){
 			terminationPacket.setTimestamp(timestep);

+ 1 - 1
src/test/resources/control/testCompilation/packageTest/MQTT_invalidPackage.javaTest

@@ -94,7 +94,7 @@ public class MQTT_invalidPackage implements Protocol {
 	}
 	
 	@Override
-	public Collection<Packet> generateNextPakets(Port port, long timestep, boolean packetLost) {
+	public Collection<Packet> generateNextPackets(Port port, long timestep, boolean packetLost) {
 		/**
 		 * Packets which will be generated
 		 */

+ 1 - 1
src/test/resources/control/testCompilation/packageTest/MQTT_protocolPackageTest.javaTest

@@ -94,7 +94,7 @@ public class MQTT_protocolPackageTest implements Protocol {
 	}
 	
 	@Override
-	public Collection<Packet> generateNextPakets(Port port, long timestep, boolean packetLost) {
+	public Collection<Packet> generateNextPackets(Port port, long timestep, boolean packetLost) {
 		/**
 		 * Packets which will be generated
 		 */

+ 1 - 1
src/test/resources/control/testCompilation/packageTest/deepPackage/reallyDeepPackage/MQTT_protocolDeepPackageTest.javaTest

@@ -94,7 +94,7 @@ public class MQTT_protocolDeepPackageTest implements Protocol {
 	}
 	
 	@Override
-	public Collection<Packet> generateNextPakets(Port port, long timestep, boolean packetLost) {
+	public Collection<Packet> generateNextPackets(Port port, long timestep, boolean packetLost) {
 		/**
 		 * Packets which will be generated
 		 */