Browse Source

Adds visualisation of terminated connections to the new version

Andreas T. Meyer-Berg 5 years ago
parent
commit
8809afb379

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

@@ -175,4 +175,11 @@ public interface Protocol {
 		}
 		return topology;
 	}
+	
+	/**
+	 * Returns the deleted
+	 * 
+	 * @return deleted connection parts
+	 */
+	public Collection<Pair<Port,Port>> getDeletedTopology();
 }

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

@@ -52,6 +52,10 @@ public class MQTT_protocol implements Protocol {
 	 */
 	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
 	 */
@@ -103,7 +107,8 @@ public class MQTT_protocol implements Protocol {
 		returnPackets.addAll(currentPackets);
 		//remove packets from the old list
 		currentPackets.clear();
-		
+		//Clear deleted connections
+		deletedConnectionLinks.clear();
 		//Return termination packets
 		if(port==null)return returnPackets;
 		
@@ -225,6 +230,7 @@ public class MQTT_protocol implements Protocol {
 		if (broker==null) {
 			if (role == 0) {
 				broker = device;
+				updateBrokerOnDeletedConnections(null);
 				return true;
 			} else {
 				return false;
@@ -263,6 +269,8 @@ public class MQTT_protocol implements Protocol {
 		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);
@@ -270,9 +278,14 @@ public class MQTT_protocol implements Protocol {
 		//Remove Port from topics and clear its list
 		LinkedList<String> oldTopics = subbedTopics.remove(device);
 		if(oldTopics!=null)oldTopics.clear();
-		if(removedDevice && broker!=null){
-			//If not the broker and device was removed -> disconnect
-			currentPackets.add(new MQTT_packet(MQTT_packet.DISCONNECT, currentPackets.size()/2, device, broker));
+		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));
+			}
 		}
 	}
 	
@@ -320,6 +333,28 @@ public class MQTT_protocol implements Protocol {
 	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()){
@@ -330,19 +365,32 @@ public class MQTT_protocol implements Protocol {
 				}
 			}
 			if(noP==0)
-				return topology;
-			SmartDevice deletedBroker = new SmartDevice("DeltedBroker");
+				return;
 			deletedBroker.setX(((int)(x*1.0)/noP));
 			deletedBroker.setY(((int)(y*1.0)/noP));
-			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;
+	}
+	
+	/**
+	 * 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);
+				
+			}
+		}
 	}
 
 }

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

@@ -21,6 +21,16 @@ public class SimpleProtocol implements Protocol {
 	 */
 	Port source, destination;
 	
+	/**
+	 * Deleted Connection if participants of the connection were removed 
+	 */
+	private Pair<Port,Port> deletedConnection = null;
+	
+	/**
+	 * Package which is sent on termination 
+	 */
+	private Packet terminationPacket = null;
+	
 	/**
 	 * True if the next Package is send by source, false if the next one will be send by destination
 	 */
@@ -39,7 +49,12 @@ public class SimpleProtocol implements Protocol {
 	@Override
 	public Collection<Packet> generateNextPakets(Port p, long timestep, boolean packetLost) {
 		LinkedList<Packet> ret = new LinkedList<Packet>();
-		if(p==null)return ret;
+		if(terminationPacket!=null){
+			terminationPacket.setTimestamp(timestep);
+			ret.add(terminationPacket);
+			terminationPacket = null;
+		}
+		if(p==null) return ret;
 		p.setLastTrigger(timestep);
 		if(packetLost)ret.add(new SimplePacket(timestep, p, p==source?destination:source, "Lost"));
 		if(p == destination)
@@ -93,12 +108,27 @@ public class SimpleProtocol implements Protocol {
 	
 	@Override
 	public void removeDevice(Port device) {
-		if(device == source)
+		if(device == source){
+			if(destination!=null){
+				deletedConnection = new Pair<Port, Port>(new Port(source.getOwner(),(short)-1),destination);
+				terminationPacket = new SimplePacket(0, source, destination, "Disconnect");
+			}else if(deletedConnection!=null){
+				deletedConnection.setLeft(new Port(source.getOwner(),(short)-1));
+			}
 			source = null;
-		if(device == destination)
-			destination = null;
+		}
 		
+		if(device == destination){
+			if(source!=null){
+				deletedConnection = new Pair<Port, Port>(source, new Port(destination.getOwner(),(short)-1));
+				terminationPacket = new SimplePacket(0, destination, source, "Disconnect");
+			}else if(deletedConnection!=null){
+				deletedConnection.setRight(new Port(destination.getOwner(),(short)-1));
+			}
+			destination = null;
+		}
 	}
+	
 	@Override
 	public String getName() {
 		return "SimpleProtocol";
@@ -134,4 +164,10 @@ public class SimpleProtocol implements Protocol {
 			topology.add(new Pair<Port, Port>(source, destination));
 		return topology;
 	}
+	@Override
+	public Collection<Pair<Port, Port>> getDeletedTopology() {
+		LinkedList<Pair<Port, Port>> deleted = new LinkedList<Pair<Port,Port>>();
+		if(deletedConnection!=null)deleted.add(deletedConnection);
+		return deleted;
+	}
 }

+ 20 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/view/VisualisationPanel.java

@@ -248,6 +248,26 @@ public class VisualisationPanel extends JPanel implements Observer {
 					g.drawLine(xFrom, yFrom, xTo, yTo);
 				}
 			}
+			g.setColor(Color.RED);
+			for(Pair<Port,Port> p: c.getProtocol().getDeletedTopology() ){
+				if(p.getLeft() != null && p.getLeft().getOwner() != null && p.getRight() != null && p.getRight().getOwner() != null){
+					SmartDevice from = p.getLeft().getOwner();
+					SmartDevice to = p.getRight().getOwner();
+					int xFrom = from.getX();
+					int yFrom = from.getY();
+					int xTo = to.getX();
+					int yTo = to.getY();
+					if(interactor.selectedDevices.contains(from)^interactor.selectedDevicesDrag.contains(from)){
+						xFrom+=x_offset;
+						yFrom+=y_offset;
+					}
+					if(interactor.selectedDevices.contains(to)^interactor.selectedDevicesDrag.contains(to)){
+						xTo+=x_offset;
+						yTo+=y_offset;
+					}
+					g.drawLine(xFrom, yFrom, xTo, yTo);
+				}
+			}
 		}
 		// paint new in progress connection, if a connection or link is in
 		// creation