MenuBarInsertAnomalies.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package de.tu_darmstadt.tk.SmartHomeNetworkSim.view.menuBar;
  2. import java.util.LinkedList;
  3. import java.util.List;
  4. import java.util.Observable;
  5. import java.util.Observer;
  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.ExampleAnomalyController;
  10. import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.NetworkController;
  11. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Connection;
  12. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.ConnectionPrecision;
  13. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Link;
  14. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Port;
  15. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.PrecisionLink;
  16. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Protocol;
  17. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.SmartDevice;
  18. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.devices.BoolSensor;
  19. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.devices.FloatSensor;
  20. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.distributionHandler.NormalDistributionHandler;
  21. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.simpleImplementation.SimpleProtocol;
  22. /**
  23. * Menu which allows simple insertion of anomalies into the network
  24. *
  25. * @author Andreas T. Meyer-Berg
  26. */
  27. public class MenuBarInsertAnomalies extends JMenu implements Observer {
  28. /**
  29. *
  30. */
  31. private static final long serialVersionUID = 2522406981845530518L;
  32. /**
  33. * Controller for manipulating the network
  34. */
  35. private Controller controller;
  36. /**
  37. * Menu for denial of service attacks
  38. */
  39. private JMenu mnDos;
  40. /**
  41. * Menu for distributed denial of service attacks
  42. */
  43. private JMenu mnDDos;
  44. /**
  45. * Menu for letting the device crash
  46. */
  47. private JMenu mnCrash;
  48. /**
  49. * Menu for Value Anomalies
  50. */
  51. private JMenu mnValueAnomaly;
  52. /**
  53. * Anomaly Controller
  54. */
  55. private ExampleAnomalyController anomController;
  56. /**
  57. * Create a new MenuBar for anomaly insertion
  58. * @param controller main Controller of the framework
  59. */
  60. public MenuBarInsertAnomalies(Controller controller) {
  61. super("Insert Example Anomaly");
  62. this.controller = controller;
  63. this.anomController = controller.getNetworkController().getAnomalyController();
  64. initialize();
  65. this.update(null, null);
  66. this.controller.addObserver(this);
  67. }
  68. private void initialize() {
  69. mnDos = new JMenu("Denial of Service");
  70. this.add(mnDos);
  71. mnDDos = new JMenu("Distributed Denial of Service");
  72. this.add(mnDDos);
  73. mnCrash = new JMenu("Crash Device");
  74. this.add(mnCrash);
  75. mnValueAnomaly = new JMenu("Value Anomaly");
  76. this.add(mnValueAnomaly);
  77. }
  78. @Override
  79. public void update(Observable o, Object arg) {
  80. mnDos.removeAll();
  81. mnDDos.removeAll();
  82. mnCrash.removeAll();
  83. mnValueAnomaly.removeAll();
  84. for(SmartDevice d : controller.getNetworkController().getVisibleSmartDevices()) {
  85. /**
  86. * DDos Target Menu Item, which opens a DDosCreation Menu
  87. */
  88. JMenuItem mntmDDosTarget = new JMenuItem("Destination: "+d.getName());
  89. mntmDDosTarget.addActionListener(a->anomController.openDDosCreationMenu(d, (List<SmartDevice>)controller.getNetworkController().getVisibleSmartDevices()));
  90. mnDDos.add(mntmDDosTarget);
  91. /**
  92. * Dos creation menus
  93. */
  94. JMenu mntmDosSource = new JMenu("Source: "+d.getName());
  95. for(SmartDevice t: controller.getNetworkController().getVisibleSmartDevices()){
  96. if(d==t)continue;
  97. JMenuItem mntmDosTarget = new JMenuItem("Destination: " +t.getName());
  98. mntmDosTarget.addActionListener(a->anomController.runDosAttack(d,t));
  99. mntmDosSource.add(mntmDosTarget);
  100. }
  101. mnDos.add(mntmDosSource);
  102. /**
  103. * Menus to crash a device
  104. */
  105. JMenuItem crashDevice = new JMenuItem(d.getName());
  106. crashDevice.addActionListener(a->anomController.crashDevice(d));
  107. mnCrash.add(crashDevice);
  108. /**
  109. * Value Anomalies menus
  110. */
  111. if(d instanceof BoolSensor) {
  112. BoolSensor sensor = (BoolSensor)d;
  113. JMenuItem itm = new JMenuItem(d.getName());
  114. itm.addActionListener(a->sensor.setBSval(!sensor.getBSval()));
  115. }else if(d instanceof FloatSensor) {
  116. FloatSensor sensor = (FloatSensor)d;
  117. JMenuItem itm = new JMenuItem(d.getName());
  118. itm.addActionListener(a->{
  119. sensor.setFSmax(100);
  120. sensor.setFSval(80);
  121. });
  122. mnValueAnomaly.add(itm);
  123. }
  124. }
  125. /**
  126. * Only enable not empty menus
  127. */
  128. mnCrash.setEnabled(mnCrash.getMenuComponentCount()!=0);
  129. mnDos.setEnabled(mnDos.getMenuComponentCount()!=0);
  130. mnDDos.setEnabled(mnDDos.getMenuComponentCount()!=0);
  131. mnValueAnomaly.setEnabled(mnValueAnomaly.getMenuComponentCount()!=0);
  132. }
  133. }