EditPacketSniffer.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. package de.tu_darmstadt.tk.SmartHomeNetworkSim.view.popups;
  2. import javax.swing.JDialog;
  3. import javax.swing.JList;
  4. import javax.swing.JOptionPane;
  5. import javax.swing.JScrollPane;
  6. import javax.swing.ListSelectionModel;
  7. import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.ClassImportException;
  8. import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.Controller;
  9. import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.PacketCaptureController;
  10. import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.SimulationController;
  11. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Link;
  12. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.PacketCollector;
  13. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.PacketSniffer;
  14. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.SmartDevice;
  15. import de.tu_darmstadt.tk.SmartHomeNetworkSim.view.util.Utility;
  16. import weka.classifiers.functions.supportVector.RBFKernel;
  17. import java.awt.Container;
  18. import javax.swing.ButtonGroup;
  19. import javax.swing.JButton;
  20. import javax.swing.JLabel;
  21. import java.awt.event.ActionListener;
  22. import java.awt.event.WindowAdapter;
  23. import java.awt.event.WindowEvent;
  24. import java.util.Iterator;
  25. import java.util.Observable;
  26. import java.util.Observer;
  27. import java.awt.event.ActionEvent;
  28. import javax.swing.JRadioButton;
  29. import javax.swing.JCheckBox;
  30. /**
  31. * PopUp for creation or editing of packet Sniffers
  32. *
  33. * @author Andreas T. Meyer-Berg
  34. */
  35. public class EditPacketSniffer extends JDialog implements Observer {
  36. /**
  37. * serial
  38. */
  39. private static final long serialVersionUID = 1L;
  40. /**
  41. * Controller for manipulating the simulation and adding algorithms
  42. */
  43. private Controller controller;
  44. /**
  45. * Simulation controller for access to the algorithms
  46. */
  47. private SimulationController sim;
  48. /**
  49. * Controller for capturing packets
  50. */
  51. private PacketCaptureController captureController;
  52. /**
  53. * PacketCollector which is being edited
  54. */
  55. private PacketCollector collector;
  56. /**
  57. * Label to show the algorithm name
  58. */
  59. private JLabel lblAlgorithm;
  60. /**
  61. * Reference to this for inner classes
  62. */
  63. private EditPacketSniffer that = this;
  64. /**
  65. * Mutex to disable listeners, which GUI Refresh is active
  66. */
  67. private boolean mutex = false;
  68. /**
  69. * ScrollPane which contains a List of all registered Links
  70. */
  71. private JScrollPane scrollPaneLinks;
  72. /**
  73. * List for the links
  74. */
  75. private JList<String> listLinks;
  76. /**
  77. * ScrollPane which contains a List of all registered Links
  78. */
  79. private JScrollPane scrollPaneDevices;
  80. /**
  81. * List for the links
  82. */
  83. private JList<String> listDevices;
  84. /**
  85. * True if the collector is being edited
  86. */
  87. private boolean edit;
  88. /**
  89. * RadioButton marked, if in testing mode
  90. */
  91. private JRadioButton rdbtnTesting;
  92. /**
  93. * RadioButton for training mode
  94. */
  95. private JRadioButton rdbtnTraining;
  96. /**
  97. * Checkbox, which is checked if enabled
  98. */
  99. private JCheckBox chckbxEnabled;
  100. /**
  101. * Creates and shows a new EditAlgorithmPopUp
  102. * @param controller controller
  103. * @param parent parent container the location should be set relative to
  104. * @param edit true, if an sniffer is being edited
  105. */
  106. public EditPacketSniffer(Controller controller, Container parent, PacketCollector collector, boolean edit) {
  107. mutex = true;
  108. this.edit = edit;
  109. this.controller = controller;
  110. sim = this.controller.getSimulationController();
  111. captureController = sim.getPacketCaptureController();
  112. captureController.addObserver(this);
  113. this.collector = collector;
  114. this.setSize(400, 400);
  115. this.setResizable(false);
  116. setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
  117. setTitle((this.edit?"Edit":"Create")+" PacketCollector");
  118. this.setIconImage(Utility.loadFile("images/smartHome_icon.png"));
  119. getContentPane().setLayout(null);
  120. JButton btnImportPacketcapturealgorithm = new JButton("Import PacketAlgorithm");
  121. btnImportPacketcapturealgorithm.addActionListener(new ActionListener() {
  122. public void actionPerformed(ActionEvent e) {
  123. if(mutex)return;
  124. /**
  125. * PopUp for Importing
  126. */
  127. ImportPopUp<PacketSniffer> importPopUp = new ImportPopUp<PacketSniffer>(that, PacketSniffer.class);
  128. /**
  129. * Imported PacketSniffer Class
  130. */
  131. Class<? extends PacketSniffer> imported = null;
  132. try {
  133. imported = importPopUp.showPopUp();
  134. } catch (ClassImportException e1) {
  135. /**
  136. * Warning if Importing failed
  137. */
  138. JOptionPane.showMessageDialog(that, "Import failed: " + e1.getMessage());
  139. return;
  140. }
  141. if(imported!=null){
  142. try {
  143. /**
  144. * Instance of the PacketSniffer
  145. */
  146. PacketSniffer p = imported.newInstance();
  147. /**
  148. * Set as new PacketAlgorithm
  149. */
  150. that.collector.setPacketAlgorithm(p);
  151. update(null,null);
  152. } catch (InstantiationException | IllegalAccessException e1) {
  153. JOptionPane.showMessageDialog(that, "Instance creation failed: " + e1.getMessage());
  154. }
  155. }else{
  156. System.out.println("Imported null");
  157. }
  158. //Else import aborted
  159. }
  160. });
  161. btnImportPacketcapturealgorithm.setBounds(180, 10, 210, 30);
  162. getContentPane().add(btnImportPacketcapturealgorithm);
  163. lblAlgorithm = new JLabel();
  164. lblAlgorithm.setBounds(10, 10, 165, 30);
  165. getContentPane().add(lblAlgorithm);
  166. scrollPaneLinks = new JScrollPane();
  167. scrollPaneLinks.setBounds(12, 46, 165, 132);
  168. getContentPane().add(scrollPaneLinks);
  169. scrollPaneDevices = new JScrollPane();
  170. scrollPaneDevices.setBounds(12, 208, 165, 132);
  171. getContentPane().add(scrollPaneDevices);
  172. JButton btnStopCapturingLink = new JButton("Stop Capturing Link");
  173. btnStopCapturingLink.addActionListener(a->{
  174. if(mutex)
  175. return;
  176. int selectedIndex = listLinks.getSelectedIndex();
  177. if(selectedIndex!=-1 && selectedIndex<collector.getLinks().size()){
  178. Iterator<Link> it = collector.getLinks().iterator();
  179. for(int i=0; i<selectedIndex;i++){
  180. it.next();
  181. }
  182. if(it.hasNext()){
  183. Link linkToBeRemoved = it.next();
  184. captureController.removeLinkFromCollector(collector, linkToBeRemoved);
  185. update(null,null);
  186. }
  187. }
  188. });
  189. btnStopCapturingLink.setBounds(180, 150, 210, 30);
  190. getContentPane().add(btnStopCapturingLink);
  191. JButton btnStopCapturingDevice = new JButton("Stop Capturing Device");
  192. btnStopCapturingDevice.setBounds(180, 200, 210, 30);
  193. getContentPane().add(btnStopCapturingDevice);
  194. btnStopCapturingDevice.addActionListener(a->{
  195. if(mutex)
  196. return;
  197. int selectedIndex = listDevices.getSelectedIndex();
  198. if(selectedIndex!=-1 && selectedIndex<collector.getDevices().size()){
  199. Iterator<SmartDevice> it = collector.getDevices().iterator();
  200. for(int i=0; i<selectedIndex;i++){
  201. it.next();
  202. }
  203. if(it.hasNext()){
  204. SmartDevice deviceToBeRemoved = it.next();
  205. captureController.removeDeviceFromCollector(collector, deviceToBeRemoved);
  206. update(null,null);
  207. }
  208. }
  209. });
  210. JButton btnCreatePacketCollector = new JButton("Validate Collector");
  211. btnCreatePacketCollector.addActionListener(new ActionListener() {
  212. public void actionPerformed(ActionEvent e) {
  213. if(mutex)return;
  214. if(!captureController.getPacketCollectors().contains(collector)){
  215. captureController.addPacketCollector(collector);
  216. }
  217. captureController.removeObserver(that);
  218. that.setVisible(false);
  219. that.setEnabled(false);
  220. that.dispose();
  221. }
  222. });
  223. btnCreatePacketCollector.setBounds(180, 310, 210, 30);
  224. getContentPane().add(btnCreatePacketCollector);
  225. chckbxEnabled = new JCheckBox("Enabled");
  226. chckbxEnabled.setBounds(190, 50, 113, 25);
  227. getContentPane().add(chckbxEnabled);
  228. chckbxEnabled.addActionListener(a->{
  229. if(mutex)return;
  230. boolean active = chckbxEnabled.isSelected();
  231. captureController.setActive(collector, active);
  232. });
  233. rdbtnTraining = new JRadioButton("Training");
  234. rdbtnTraining.setBounds(185, 82, 127, 25);
  235. getContentPane().add(rdbtnTraining);
  236. rdbtnTraining.addActionListener(a->{
  237. if(mutex)return;
  238. captureController.setMode(collector, !rdbtnTraining.isSelected());
  239. });
  240. rdbtnTesting = new JRadioButton("Testing");
  241. rdbtnTesting.setBounds(185, 112, 127, 25);
  242. getContentPane().add(rdbtnTesting);
  243. rdbtnTesting.addActionListener(a->{
  244. if(mutex)return;
  245. captureController.setMode(collector, rdbtnTesting.isSelected());
  246. });
  247. this.addWindowListener(new WindowAdapter() {
  248. //Remove Observer, if window is closing
  249. @Override
  250. public void windowClosing(WindowEvent e) {
  251. captureController.removeObserver(that);
  252. super.windowClosing(e);
  253. }
  254. });
  255. update(null, null);
  256. this.setLocationRelativeTo(parent);
  257. ButtonGroup group = new ButtonGroup();
  258. group.add(rdbtnTesting);
  259. group.add(rdbtnTraining);
  260. }
  261. @Override
  262. public void update(Observable o, Object arg) {
  263. mutex = true;
  264. /**
  265. * Update algorithm name
  266. */
  267. if(collector.getPacketAlgorithm()==null)
  268. lblAlgorithm.setText("Algorithm: null");
  269. else
  270. lblAlgorithm.setText("<html>Algorithm:<br> "+collector.getPacketAlgorithm().getClass().getSimpleName()+"</html>");
  271. /**
  272. * Update List of Links
  273. */
  274. /**
  275. * Names of the Links
  276. */
  277. String[] linkNames = new String[collector.getLinks().size()];
  278. if(collector.getLinks().size()!=0){
  279. Iterator<Link> it = collector.getLinks().iterator();
  280. for(int i=0;i<collector.getLinks().size();i++){
  281. if(it.hasNext()){
  282. Link l = it.next();
  283. if(l!=null && l.getName()!=null)
  284. linkNames[i] = l.getName();
  285. else
  286. linkNames[i] = "null";
  287. }else{
  288. linkNames[i] = "invalid Size";
  289. }
  290. }
  291. }
  292. /**
  293. * New list for the Links
  294. */
  295. JList<String> newList = new JList<String>(linkNames);
  296. newList.setToolTipText("List of all links, which packets are currently being captured.");
  297. newList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  298. /**
  299. * ScrollPosition of the ScrollPaneLink
  300. */
  301. int pos = scrollPaneLinks.getVerticalScrollBar().getValue();
  302. if(listLinks!=null){
  303. scrollPaneLinks.remove(listLinks);
  304. newList.setSelectedIndex(listLinks.getSelectedIndex());
  305. }
  306. scrollPaneLinks.setViewportView(newList);
  307. scrollPaneLinks.getVerticalScrollBar().setValue(pos);
  308. listLinks = newList;
  309. /**
  310. * Update List of Devices
  311. */
  312. /**
  313. * Names of the Devices
  314. */
  315. String[] deviceNames = new String[collector.getDevices().size()];
  316. if(collector.getDevices().size()!=0){
  317. Iterator<SmartDevice> itD = collector.getDevices().iterator();
  318. for(int i=0;i<collector.getDevices().size();i++){
  319. if(itD.hasNext()){
  320. deviceNames[i] = itD.next().getName();
  321. }else{
  322. deviceNames[i] = "invalid Size";
  323. }
  324. }
  325. }
  326. /**
  327. * New list for the Devices
  328. */
  329. newList = new JList<String>(deviceNames);
  330. newList.setToolTipText("List of all SmartDevices, which packets are currently being captured.");
  331. newList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  332. /**
  333. * ScrollPosition of the ScrollPaneLink
  334. */
  335. pos = scrollPaneDevices.getVerticalScrollBar().getValue();
  336. if(listDevices!=null){
  337. scrollPaneDevices.remove(listDevices);
  338. newList.setSelectedIndex(listDevices.getSelectedIndex());
  339. }
  340. scrollPaneDevices.setViewportView(newList);
  341. scrollPaneDevices.getVerticalScrollBar().setValue(pos);
  342. listDevices = newList;
  343. /**
  344. * Manage checkboxes
  345. */
  346. chckbxEnabled.setSelected(collector.isActive());
  347. rdbtnTesting.setEnabled(collector.isActive());
  348. rdbtnTesting.setSelected(collector.getMode());
  349. rdbtnTraining.setEnabled(collector.isActive());
  350. rdbtnTraining.setSelected(!collector.getMode());
  351. if(this.edit&&!captureController.getPacketCollectors().contains(collector)){
  352. /**
  353. * If Collector was removed -> Close PopUp
  354. */
  355. this.setEnabled(false);
  356. this.setVisible(false);
  357. this.dispose();
  358. return;
  359. }
  360. this.repaint();
  361. mutex = false;
  362. }
  363. }