MenuBarNetworkExamples.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. package de.tu_darmstadt.tk.SmartHomeNetworkSim.view.popups;
  2. import java.util.HashMap;
  3. import java.util.Iterator;
  4. import java.util.LinkedList;
  5. import java.util.Map.Entry;
  6. import javax.swing.JMenu;
  7. import javax.swing.JMenuItem;
  8. import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.Controller;
  9. import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.NetworkController;
  10. import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.SettingsController;
  11. import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.SimulationController;
  12. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Connection;
  13. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.ConnectionPerformance;
  14. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.ConnectionPrecision;
  15. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Link;
  16. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Packet;
  17. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.PacketCollector;
  18. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Port;
  19. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.PrecisionLink;
  20. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Protocol;
  21. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.SmartDevice;
  22. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.devices.BoolCollectorDevice;
  23. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.devices.BoolSensorDevice;
  24. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.devices.FloatCollectorDevice;
  25. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.devices.FloatSensorDevice;
  26. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.protocols.MQTT_protocol;
  27. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.simpleImplementation.CountingMetric;
  28. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.simpleImplementation.Manipulation_RandomMove;
  29. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.simpleImplementation.SimpleLink;
  30. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.simpleImplementation.SimpleProtocol;
  31. public class MenuBarNetworkExamples extends JMenu{
  32. /**
  33. *
  34. */
  35. private static final long serialVersionUID = 1L;
  36. /**
  37. * Controller controller
  38. */
  39. private Controller controller;
  40. /**
  41. * Controller for editing the network
  42. */
  43. private NetworkController networkController;
  44. /**
  45. * Controller for configuration of the program
  46. */
  47. private SettingsController settingsController;
  48. /**
  49. * SimulationController for running the simulation
  50. */
  51. private SimulationController simulationController;
  52. /**
  53. * Creates the JMenu for network examples in the MenuBar
  54. * @param controller controller to create examples
  55. */
  56. public MenuBarNetworkExamples(Controller controller) {
  57. super("Create example Network");
  58. this.controller = controller;
  59. this.networkController = controller.getNetworkController();
  60. this.settingsController = controller.getSettingsController();
  61. this.simulationController = controller.getSimulationController();
  62. /**
  63. * MQTT example
  64. */
  65. JMenuItem mnMQTT = new JMenuItem("MQTT Example");
  66. mnMQTT.addActionListener(a->initializeMQTTTest());
  67. this.add(mnMQTT);
  68. /**
  69. * Small Network Test
  70. */
  71. JMenuItem mnTest = new JMenuItem("Network Example");
  72. mnTest.addActionListener(a->initializeTest());
  73. this.add(mnTest);
  74. /**
  75. * Performance Comparison Example
  76. */
  77. JMenuItem mnPerformance = new JMenuItem("Performane Evaluation Preset");
  78. mnPerformance.addActionListener(a->initializePerformanceTest());
  79. this.add(mnPerformance);
  80. /**
  81. * User Study Preset
  82. */
  83. JMenuItem mnUserStudy = new JMenuItem("User Study Preset");
  84. mnUserStudy.addActionListener(a->initializeUserStudy());
  85. this.add(mnUserStudy);
  86. /**
  87. * Packet Collection example
  88. */
  89. JMenuItem mnPacketCollectionExmample = new JMenuItem("Packet Collection Example");
  90. mnPacketCollectionExmample.addActionListener(a->testPackageCollection());
  91. this.add(mnPacketCollectionExmample);
  92. /**
  93. * Paper example
  94. */
  95. JMenuItem mnPaperExample = new JMenuItem("Paper Example");
  96. mnPaperExample.addActionListener(a->createPaperExample());
  97. this.add(mnPaperExample);
  98. }
  99. private void initializePerformanceTest(){
  100. SmartDevice tic = new SmartDevice("Tic");
  101. tic.setX(100);
  102. tic.setY(100);
  103. SmartDevice toc = new SmartDevice("Toc");
  104. toc.setX(100);
  105. toc.setY(250);
  106. networkController.addSmartDevice(tic);
  107. networkController.addSmartDevice(toc);
  108. Link l = new PrecisionLink("Channel");
  109. networkController.addLink(l);
  110. networkController.addLinkToDevice(l, tic);
  111. networkController.addLinkToDevice(l, toc);
  112. Port ticP = new Port(tic, (short)2, 200, (short)0, 0, (short)0);
  113. Port tocP = new Port(toc, (short)3, 200, (short)0, -100, (short)0);
  114. tic.addPort(ticP);
  115. toc.addPort(tocP);
  116. Connection con = new ConnectionPrecision(l, new SimpleProtocol());
  117. networkController.addConnection(con);
  118. networkController.addConnectionToLink(con, l);
  119. networkController.addDeviceToConnectionAndProtocol(ticP, con, 0);
  120. networkController.addDeviceToConnectionAndProtocol(tocP, con, 1);
  121. simulationController.setStepDuration(100000000);
  122. simulationController.setEndTime(1000000000);
  123. controller.notifyObservers();
  124. }
  125. /**
  126. * Initializes a basic test Network, which contains a few SmartDevices, one Link and one Connection
  127. */
  128. private void initializeUserStudy(){
  129. /**
  130. * Preset one SmartDevice
  131. */
  132. SmartDevice A = new SmartDevice("Homestation");
  133. A.setX(200);
  134. A.setY(200);
  135. networkController.addSmartDevice(A);
  136. /**
  137. * Packagecollector, which is registered at the Device
  138. */
  139. PacketCollector collector = new PacketCollector(new CountingMetric());
  140. simulationController.getPacketCaptureController().addPacketCollector(collector);
  141. simulationController.getPacketCaptureController().addDeviceToCollector(collector, A);
  142. simulationController.setPrintPackets(true);
  143. controller.notifyObservers();
  144. }
  145. /**
  146. * Test package collectors
  147. */
  148. private void testPackageCollection() {
  149. simulationController.addAlgorithm(new Manipulation_RandomMove(), controller);
  150. PacketCollector collector = new PacketCollector();
  151. simulationController.getSimulationManager().getPacketCollectionManager().addPacketCollector(collector);
  152. System.out.println("Collector 0-500 - nothing collected:");
  153. simulationController.getSimulationManager().simulateTimeIntervall(0, 500);
  154. HashMap<Link, LinkedList<Packet>> map = collector.getPackets();
  155. for(Entry<Link, LinkedList<Packet>> e:map.entrySet()){
  156. System.out.println("Link: "+e.getKey().getName());
  157. for(Packet p : e.getValue())
  158. System.out.println(p.getTextualRepresentation());
  159. }
  160. System.out.println("");
  161. System.out.println("");
  162. Iterator<SmartDevice> it = networkController.getSmartDevices().iterator();
  163. it.next();
  164. SmartDevice d = it.next();
  165. collector.addDevice(d);
  166. SmartDevice f = it.next();
  167. collector.addDevice(f);
  168. System.out.println("Collector 500-1000 - "+d.getName()+" & "+f.getName()+" collected:");
  169. simulationController.getSimulationManager().simulateTimeIntervall(500, 500);
  170. map = collector.getPackets();
  171. for(Entry<Link, LinkedList<Packet>> e:map.entrySet()){
  172. System.out.println("Link: "+e.getKey().getName());
  173. for(Packet p : e.getValue())
  174. System.out.println(p.getTextualRepresentation());
  175. }
  176. System.out.println("");
  177. System.out.println("");
  178. Link l = networkController.getLinks().iterator().next();
  179. collector.addLink(l);
  180. System.out.println("Collector 2000-3000 - "+l+" collected:");
  181. simulationController.getSimulationManager().simulateTimeIntervall(1000, 500);
  182. map = collector.getPackets();
  183. for(Entry<Link, LinkedList<Packet>> e:map.entrySet()){
  184. System.out.println("Link: "+e.getKey().getName());
  185. for(Packet p : e.getValue())
  186. System.out.println(p.getTextualRepresentation());
  187. }
  188. simulationController.resetSimulation();
  189. controller.notifyObservers();
  190. }
  191. /**
  192. * Initializes a basic test Network, which contains a few SmartDevices, one Link and one Connection
  193. */
  194. private void initializeTest(){
  195. SmartDevice A = null, B = null, C = null;
  196. for(int i = 0; i<5; i++){
  197. A = new SmartDevice("SmartTV"+i);
  198. A.setX((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  199. A.setY((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  200. B = new SmartDevice("SmartDoor"+i);
  201. B.setX((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  202. B.setY((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  203. C = new SmartDevice("SmartLight"+i);
  204. C.setX((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  205. C.setY((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  206. networkController.addSmartDevice(A);
  207. networkController.addSmartDevice(B);
  208. networkController.addSmartDevice(C);
  209. }
  210. Link link = new SimpleLink("SimpleWifi");
  211. link.addDevice(A);
  212. link.addDevice(B);
  213. link.addDevice(C);
  214. A.addLink(link);
  215. B.addLink(link);
  216. C.addLink(link);
  217. Port a = new Port(A, (short) 1, 68);
  218. a.setLastTrigger(0);
  219. a.setStatus(Port.SENDING);
  220. A.addPort(a);
  221. Port b = new Port(B, (short) 2, 102);
  222. b.setStatus(Port.SENDING);
  223. B.addPort(b);
  224. Connection s = new ConnectionPerformance(link, new SimpleProtocol(a, b));
  225. s.setPacketLossProbability(0.01);//1% Packet loss probability
  226. a.setConnection(s);
  227. b.setConnection(s);
  228. s.addSmartDevice(a);
  229. s.addSmartDevice(b);
  230. link.addConnection(s);
  231. networkController.addLink(link);
  232. networkController.addConnection(s);
  233. controller.notifyObservers();
  234. }
  235. /**
  236. * Initializes a basic test Network, which contains a few SmartDevices, one Link and one Connection
  237. */
  238. private void initializeMQTTTest(){
  239. SimpleLink link = new SimpleLink("WIFI");
  240. link.setFixedDelay(5);
  241. SmartDevice broker = new SmartDevice("MQTT-Broker");
  242. broker.setX((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  243. broker.setY((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  244. networkController.addSmartDevice(broker);
  245. Port brokerPort = new Port(broker, (short) 0, 987);
  246. brokerPort.setLastTrigger(0);
  247. brokerPort.setStatus(Port.OPEN);
  248. broker.addPort(brokerPort);
  249. link.addDevice(broker);
  250. broker.addLink(link);
  251. Protocol protocol = new MQTT_protocol(brokerPort);
  252. Connection con = new ConnectionPrecision(link, protocol);
  253. con.setPacketLossProbability(0.01);//1% Packet loss probability
  254. con.addSmartDevice(brokerPort);
  255. brokerPort.setConnection(con);
  256. con.setStatus(Connection.ACTIVE);
  257. networkController.addLink(link);
  258. link.addConnection(con);
  259. networkController.addConnection(con);
  260. SmartDevice A = null, A1 = null, A2 = null, B = null, C = null;
  261. Port aP,a1P,a2P,bP,cP;
  262. for(int i = 0; i<3; i++){
  263. FloatSensorDevice aS = new FloatSensorDevice("SmartThermostat"+i+"(Pub)");
  264. aS.setFSinfoName("room"+i+"/temperature");
  265. A=aS;
  266. A.setX((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  267. A.setY((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  268. link.addDevice(A);
  269. A.addLink(link);
  270. aP = new Port(A,((short) (3*i+1)),100+(int)(Math.random()*900));
  271. aP.setLastTrigger(0);
  272. aP.setJitter((short) (Math.random()*50));
  273. aP.setStatus(Port.SENDING);
  274. aP.setConnection(con);
  275. protocol.addDeviceOfRole(aP, 2);
  276. con.addSmartDevice(aP);
  277. A.addPort(aP);
  278. networkController.addSmartDevice(A);
  279. FloatCollectorDevice aS1 = new FloatCollectorDevice("SmartAirCondition"+i+"(Sub)");
  280. aS1.setFCinfoName("room"+i+"/temperature");
  281. A1=aS1;
  282. A1.setX((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  283. A1.setY((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  284. link.addDevice(A1);
  285. A1.addLink(link);
  286. a1P = new Port(A1,((short) (3*i+1)),100+(int)(Math.random()*900));
  287. a1P.setLastTrigger(0);
  288. a1P.setJitter((short) (Math.random()*50));
  289. a1P.setStatus(Port.SENDING);
  290. a1P.setConnection(con);
  291. protocol.addDeviceOfRole(a1P, 3);
  292. con.addSmartDevice(a1P);
  293. A1.addPort(a1P);
  294. networkController.addSmartDevice(A1);
  295. FloatCollectorDevice aS2 = new FloatCollectorDevice("SmartHeater"+i+"(Sub)");
  296. aS2.setFCinfoName("room"+i+"/temperature");
  297. A2=aS2;
  298. A2.setX((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  299. A2.setY((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  300. link.addDevice(A2);
  301. A2.addLink(link);
  302. a2P = new Port(A2,((short) (3*i+1)),100+(int)(Math.random()*900));
  303. a2P.setLastTrigger(0);
  304. a2P.setJitter((short) (Math.random()*50));
  305. a2P.setStatus(Port.SENDING);
  306. a2P.setConnection(con);
  307. protocol.addDeviceOfRole(a2P, 3);
  308. con.addSmartDevice(a2P);
  309. A2.addPort(a2P);
  310. networkController.addSmartDevice(A2);
  311. B = new BoolSensorDevice("SmartDoor"+i+"(Pub)");
  312. ((BoolSensorDevice)B).setBSinfoName("room"+i+"/DoorOpen");
  313. B.setX((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  314. B.setY((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  315. link.addDevice(B);
  316. B.addLink(link);
  317. bP = new Port(B,((short) (3*i+2)),10+(int)(Math.random()*190));
  318. bP.setLastTrigger(0);
  319. bP.setJitter((short) (Math.random()*50));
  320. bP.setStatus(Port.SENDING);
  321. bP.setConnection(con);
  322. protocol.addDeviceOfRole(bP, 2);
  323. con.addSmartDevice(bP);
  324. B.addPort(bP);
  325. networkController.addSmartDevice(B);
  326. C = new BoolCollectorDevice("SmartDoorStatusLight"+i+"(Sub)");
  327. ((BoolCollectorDevice)C).setBCinfoName("room"+i+"/DoorOpen");
  328. C.setX((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  329. C.setY((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  330. link.addDevice(C);
  331. C.addLink(link);
  332. cP = new Port(C,((short) (3*i+1)), 50+(int)(Math.random()*450));
  333. cP.setLastTrigger(0);
  334. cP.setJitter((short) (Math.random()*50));
  335. cP.setStatus(Port.SENDING);
  336. cP.setConnection(con);
  337. protocol.addDeviceOfRole(cP, 3);
  338. con.addSmartDevice(cP);
  339. C.addPort(cP);
  340. networkController.addSmartDevice(C);
  341. controller.notifyObservers();
  342. }
  343. }
  344. public void createPaperExample(){
  345. /*
  346. * Main networking devices
  347. */
  348. SmartDevice router = networkController.createSmartDevice("Wifi-Router", 500, 100, 50);
  349. SmartDevice zigBeeRouter = networkController.createSmartDevice("ZigBee-Router", 500, 300, 50);
  350. SmartDevice broker = networkController.createSmartDevice("Broker", 500, 500, 50);
  351. /*
  352. * Links/Networks
  353. */
  354. Link wifi = new PrecisionLink("Wifi");
  355. networkController.addLink(wifi);
  356. Link zigbee = new PrecisionLink("ZigBee");
  357. networkController.addLink(zigbee);
  358. /*
  359. * Connect Devices to Links
  360. */
  361. networkController.addLinkToDevice(wifi, router);
  362. networkController.addLinkToDevice(wifi, zigBeeRouter);
  363. networkController.addLinkToDevice(zigbee, zigBeeRouter);
  364. networkController.addLinkToDevice(zigbee, broker);
  365. /*
  366. * Internet Access Connection
  367. */
  368. Connection inetAcces = new ConnectionPrecision();
  369. inetAcces.setName("Cloud Access");
  370. networkController.addConnectionToLink(inetAcces, wifi);
  371. inetAcces.setProtocol(new SimpleProtocol());
  372. Port pRouter = new Port(router, (short)80, 500L);
  373. networkController.addDeviceToConnectionAndProtocol(pRouter, inetAcces, 0);
  374. Port pZigBee = new Port(zigBeeRouter, (short)80, 1000L);
  375. networkController.addDeviceToConnectionAndProtocol(pRouter, inetAcces, 0);
  376. networkController.addDeviceToConnectionAndProtocol(pZigBee, inetAcces, 1);
  377. networkController.addConnection(inetAcces);
  378. /*
  379. * Create MQTT Connection
  380. */
  381. Connection mqtt = new ConnectionPrecision();
  382. mqtt.setName("Automation (MQTT)");
  383. networkController.addConnectionToLink(mqtt, zigbee);
  384. mqtt.setProtocol(new MQTT_protocol());
  385. Port pBroker = new Port(broker, (short)1883);
  386. pBroker.setStatus(Port.OPEN);
  387. pBroker.setResponseTime((short)2);
  388. networkController.addDeviceToConnectionAndProtocol(pBroker, mqtt, 0);
  389. networkController.addConnection(mqtt);
  390. /*
  391. * Add some MQTT Devices
  392. */
  393. FloatSensorDevice floatSensor = new FloatSensorDevice("Thermostat 1");
  394. floatSensor.setFSinfoName("home/kitchen/temperature");
  395. networkController.addLinkToDevice(zigbee, floatSensor);
  396. networkController.moveSmartDevice(floatSensor, 300, 500, 50);
  397. floatSensor.setFSmin(15.0f);
  398. floatSensor.setFSmax(32.0f);
  399. networkController.addSmartDevice(floatSensor);
  400. Port pFloatSensor = new Port(floatSensor, (short)1883, 15000);
  401. pFloatSensor.setStatus(Port.SENDING);
  402. pFloatSensor.setLastTrigger(-357L);
  403. networkController.addDeviceToConnectionAndProtocol(pFloatSensor, mqtt,1);
  404. /*
  405. * Update visualization
  406. */
  407. controller.notifyObservers();
  408. }
  409. }