浏览代码

Adds MQTT test network

Andreas T. Meyer-Berg 6 年之前
父节点
当前提交
79fdced63c

+ 95 - 3
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/Main.java

@@ -1,18 +1,22 @@
 package de.tu_darmstadt.tk.SmartHomeNetworkSim;
 
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.Controller;
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Connection;
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.ConnectionImplementation;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Link;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Model;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Port;
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Protocol;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.SimulationManager;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.SmartDevice;
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.protocols.MQTT_protocol;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.simpleImplementation.SimpleConnection;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.simpleImplementation.SimpleLink;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.simpleImplementation.SimpleProtocol;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.view.MainFrame;
 
 /**
- * Main class which initialises and connects the different parts and starts the program
+ * Main class which initializes and connects the different parts and starts the program
  *
  * @author Andreas T. Meyer-Berg
  */
@@ -46,8 +50,10 @@ public class Main {
 		c = new Controller(m);
 	    v = new MainFrame(m, c);
 	    sim = new SimulationManager(m);
-	    initializeTest();
-	    sim.simulateTimeIntervall(0, 1000);
+	    //initializeTest();
+	    initializeMQTTTest();
+	    for(int i=0; i<10; i++)
+	    sim.simulateTimeIntervall(0+50*i, 50);
 	}
 	
 	/**
@@ -97,5 +103,91 @@ public class Main {
 		link.addConnection(s);
 		m.addConnection(s);
 	}
+	
+	/**
+	 * Initializes a basic test Network, which contains a few SmartDevices, one Link and one Connection
+	 */
+	public static void initializeMQTTTest(){
+		Link link = new SimpleLink("LossLess TCP-Network");
+		
+		SmartDevice broker = new SmartDevice("MQTT-Broker");
+		broker.setX((int)(Math.random()*m.getWidth()-2*m.getDevice_visualization_radius())+m.getDevice_visualization_radius());
+		broker.setY((int)(Math.random()*m.getWidth()-2*m.getDevice_visualization_radius())+m.getDevice_visualization_radius());
+		c.addSmartDevice(broker);
+		
+		Port brokerPort = new Port(broker, (short) 0);
+		brokerPort.setLastTrigger(0);
+		brokerPort.setTriggerInterval(987);
+		brokerPort.setStatus(Port.OPEN);
+		broker.addPort(brokerPort);
+		
+		link.addDevice(broker);
+		broker.addLink(link);
+		
+		Protocol protocol = new MQTT_protocol(brokerPort);
+		
+		Connection con = new ConnectionImplementation(link, protocol);
+		con.setPacketLossProbability(0.01);//1% Packet loss probability
+		con.addSmartDevice(brokerPort);
+		con.setStatus(Connection.ACTIVE);
+		m.addConnectionNetwork(link);
+		link.addConnection(con);
+		m.addConnection(con);
+		
+		SmartDevice A = null, B = null, C = null;
+		Port aP,bP,cP;
+		for(int i = 0; i<3; i++){
+			A = new SmartDevice("SmartTV"+i+"(Sub)");		
+			A.setX((int)(Math.random()*m.getWidth()-2*m.getDevice_visualization_radius())+m.getDevice_visualization_radius());
+			A.setY((int)(Math.random()*m.getWidth()-2*m.getDevice_visualization_radius())+m.getDevice_visualization_radius());
+			link.addDevice(A);
+			A.addLink(link);
+			
+			aP = new Port(A,((short) (3*i+1)));
+			aP.setLastTrigger(0);
+			aP.setTriggerInterval(100+(int)(Math.random()*900));
+			aP.setStatus(Port.SENDING);
+			protocol.addDeviceOfRole(aP, 3);
+			con.addSmartDevice(aP);
+			A.addPort(aP);
+			
+			c.addSmartDevice(A);
+			
+			
+			
+			B = new SmartDevice("SmartDoor"+i+"(Pub)");
+			B.setX((int)(Math.random()*m.getWidth()-2*m.getDevice_visualization_radius())+m.getDevice_visualization_radius());
+			B.setY((int)(Math.random()*m.getWidth()-2*m.getDevice_visualization_radius())+m.getDevice_visualization_radius());
+			link.addDevice(B);
+			B.addLink(link);
+			
+			bP = new Port(B,((short) (3*i+2)));
+			bP.setLastTrigger(0);
+			bP.setTriggerInterval(10+(int)(Math.random()*190));
+			bP.setStatus(Port.SENDING);
+			protocol.addDeviceOfRole(bP, 2);
+			con.addSmartDevice(bP);
+			B.addPort(bP);
+			
+			c.addSmartDevice(B);
+			
+			C = new SmartDevice("SmartLight"+i+"(Pub,Sub)");
+			C.setX((int)(Math.random()*m.getWidth()-2*m.getDevice_visualization_radius())+m.getDevice_visualization_radius());
+			C.setY((int)(Math.random()*m.getWidth()-2*m.getDevice_visualization_radius())+m.getDevice_visualization_radius());
+			link.addDevice(C);
+			C.addLink(link);
+			
+			cP = new Port(C,((short) (3*i+1)));
+			cP.setLastTrigger(0);
+			cP.setTriggerInterval(50+(int)(Math.random()*450));
+			cP.setStatus(Port.SENDING);
+			protocol.addDeviceOfRole(cP, 1);
+			con.addSmartDevice(cP);
+			C.addPort(cP);
+			
+			c.addSmartDevice(C);
+
+		}
+	}
 
 }

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

@@ -62,8 +62,13 @@ public class MQTT_protocol implements Protocol {
 	 */
 	public MQTT_protocol(Port broker) {
 		this.broker = broker;
-	    topics.add("temperature");
-	    topics.add("doorState");
+	    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
@@ -81,6 +86,9 @@ public class MQTT_protocol implements Protocol {
 		//remove packets from the old list
 		currentPackets.clear();
 		
+		//Return termination packets
+		if(port==null)return returnPackets;
+		
 		/**
 		 * Update the lastTime the port was triggered
 		 */