Browse Source

Adds simple visualisation of MQTT Networks

Andreas T. Meyer-Berg 5 years ago
parent
commit
f96bcb423d

+ 5 - 1
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/Main.java

@@ -118,7 +118,7 @@ public class Main {
 		Port brokerPort = new Port(broker, (short) 0);
 		brokerPort.setLastTrigger(0);
 		brokerPort.setTriggerInterval(987);
-		brokerPort.setStatus(Port.OPEN);
+		brokerPort.setStatus(Port.OPEN);		
 		broker.addPort(brokerPort);
 		
 		link.addDevice(broker);
@@ -129,6 +129,7 @@ public class Main {
 		Connection con = new ConnectionImplementation(link, protocol);
 		con.setPacketLossProbability(0.01);//1% Packet loss probability
 		con.addSmartDevice(brokerPort);
+		brokerPort.setConnection(con);
 		con.setStatus(Connection.ACTIVE);
 		m.addConnectionNetwork(link);
 		link.addConnection(con);
@@ -147,6 +148,7 @@ public class Main {
 			aP.setLastTrigger(0);
 			aP.setTriggerInterval(100+(int)(Math.random()*900));
 			aP.setStatus(Port.SENDING);
+			aP.setConnection(con);
 			protocol.addDeviceOfRole(aP, 3);
 			con.addSmartDevice(aP);
 			A.addPort(aP);
@@ -165,6 +167,7 @@ public class Main {
 			bP.setLastTrigger(0);
 			bP.setTriggerInterval(10+(int)(Math.random()*190));
 			bP.setStatus(Port.SENDING);
+			bP.setConnection(con);
 			protocol.addDeviceOfRole(bP, 2);
 			con.addSmartDevice(bP);
 			B.addPort(bP);
@@ -181,6 +184,7 @@ public class Main {
 			cP.setLastTrigger(0);
 			cP.setTriggerInterval(50+(int)(Math.random()*450));
 			cP.setStatus(Port.SENDING);
+			cP.setConnection(con);
 			protocol.addDeviceOfRole(cP, 1);
 			con.addSmartDevice(cP);
 			C.addPort(cP);

+ 3 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/control/Controller.java

@@ -201,7 +201,10 @@ public class Controller {
 			//Skip ports that are not connected
 			if(p.getConnection()==null)continue;
 			Connection c = p.getConnection();
+			//remove from connection
 			c.removeSmartDevice(p);
+			//remove from protocol
+			if(c.getProtocol()!=null)c.getProtocol().removeDevice(p);
 			if (c.getStatus()==Connection.FINISHED||c.getStatus()==Connection.TERMINATED) {
 				//if connection terminated - remove rest of ports
 				for (Port sd : c.getParticipants()) {

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

@@ -238,7 +238,7 @@ public class MQTT_protocol implements Protocol {
 	public void removeDevice(Port device) {
 		/**
 		 * true if the device was removed
-		 */
+		 */System.out.println("Halllo????");
 		boolean removedDevice=false;
 		if (broker == device){
 			broker = null;
@@ -247,7 +247,8 @@ public class MQTT_protocol implements Protocol {
 		removedDevice|=subs.remove(device);
 		removedDevice|=pubs.remove(device);
 		//Remove Port from topics and clear its list
-		subbedTopics.remove(device).clear();
+		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));

+ 31 - 1
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/view/VisualisationPanel.java

@@ -13,6 +13,7 @@ import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Connection;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Model;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Port;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.SmartDevice;
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.protocols.MQTT_protocol;
 
 @SuppressWarnings("serial")
 public class VisualisationPanel extends JPanel {
@@ -153,7 +154,36 @@ public class VisualisationPanel extends JPanel {
 			 * All Devices that are part of the connection
 			 */
 			Collection<Port> d = c.getParticipants();
-			if (d.size() == 2) {
+			if(c.getProtocol() instanceof MQTT_protocol){
+				Port broker = c.getProtocol().getDevicesWithRole(0).iterator().next();
+				for(Port sd:d){
+					if(!model.getDevices().contains(sd.getOwner())){
+						SmartDevice removed = sd.getOwner();
+						g.fillOval(removed.getX()-visualisationRadius/4, removed.getY()-visualisationRadius/4, visualisationRadius/2, visualisationRadius/2);
+					}
+					
+					if (broker!= sd) {
+						// Check if dragged object
+						int s_x, s_y, sd_x, sd_y;
+						if (broker.getOwner() == interactor.dragged) {
+							s_x = interactor.dragged_x;
+							s_y = interactor.dragged_y;
+						} else {
+							s_x = broker.getOwner().getX();
+							s_y = broker.getOwner().getY();
+						}
+						if (sd.getOwner() == interactor.dragged) {
+							sd_x = interactor.dragged_x;
+							sd_y = interactor.dragged_y;
+						} else {
+							sd_x = sd.getOwner().getX();
+							sd_y = sd.getOwner().getY();
+						}
+						g.drawLine(s_x, s_y, sd_x, sd_y);
+					}
+				}
+			}
+			else if (d.size() == 2) {
 				Port s = c.getSource();
 				for (Port sd : d) {
 					if(!model.getDevices().contains(sd.getOwner())){