Преглед изворни кода

Adds EditPacketSniffer functionalities

* Visual representation of Devices / Links which are being captured
* Stop Capturing of Devices / Links
* Creation of the PacketSniffer
Andreas T. Meyer-Berg пре 5 година
родитељ
комит
b888dba58a

+ 184 - 18
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/view/popups/EditPacketSniffer.java

@@ -10,24 +10,26 @@ import javax.swing.ListSelectionModel;
 
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.ClassImportException;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.Controller;
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.PacketCaptureController;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.SimulationController;
-import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.NetworkManipulationAlgorithm;
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Link;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.PacketCollector;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.PacketSniffer;
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.SmartDevice;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.view.util.Utility;
 
 import java.awt.Container;
-import java.awt.Toolkit;
 
 import javax.swing.JButton;
 import javax.swing.JLabel;
 
 import java.awt.event.ActionListener;
+import java.util.Iterator;
 import java.awt.event.ActionEvent;
 
 
 /**
- * PopUp for creation or editing of packet sniffers
+ * PopUp for creation or editing of packet Sniffers
  *
  * @author Andreas T. Meyer-Berg
  */
@@ -47,6 +49,11 @@ public class EditPacketSniffer extends JDialog {
 	 */
 	private SimulationController sim;
 	
+	/**
+	 * Controller for capturing packets
+	 */
+	private PacketCaptureController captureController;
+	
 	/**
 	 *	PacketCollector which is being edited 
 	 */
@@ -59,14 +66,37 @@ public class EditPacketSniffer extends JDialog {
 	 * Reference to this for inner classes
 	 */
 	private EditPacketSniffer that = this;
+	/**
+	 * Mutex to disable listeners, which GUI Refresh is active
+	 */
+	private boolean mutex = false;
+	/**
+	 * ScrollPane which contains a List of all registered Links
+	 */
+	private JScrollPane scrollPaneLinks;
+	/**
+	 * List for the links
+	 */
+	private JList<String> listLinks;
+	
+	/**
+	 * ScrollPane which contains a List of all registered Links
+	 */
+	private JScrollPane scrollPaneDevices;
+	/**
+	 * List for the links
+	 */
+	private JList<String> listDevices;
 	/**
 	 * Creates and shows a new EditAlgorithmPopUp
 	 * @param controller controller
 	 * @param parent parent container the location should be set relative to
 	 */
 	public EditPacketSniffer(Controller controller, Container parent, PacketCollector collector) {
+		mutex = true;
 		this.controller = controller;
-		this.sim = this.controller.getSimulationController();
+		sim = this.controller.getSimulationController();
+		captureController = sim.getPacketCaptureController();
 		this.collector = collector;
 		this.setSize(400, 400);
 		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
@@ -77,46 +107,77 @@ public class EditPacketSniffer extends JDialog {
 		JButton btnImportPacketcapturealgorithm = new JButton("Import PacketAlgorithm");
 		btnImportPacketcapturealgorithm.addActionListener(new ActionListener() {
 			public void actionPerformed(ActionEvent e) {
+				if(mutex)return;
+				/**
+				 * PopUp for Importing 
+				 */
 				ImportPopUp<PacketSniffer> importPopUp = new ImportPopUp<PacketSniffer>(that, PacketSniffer.class);
+				/**
+				 * Imported PacketSniffer Class
+				 */
 				Class<? extends PacketSniffer> imported = null;
 				try {
 					 imported = importPopUp.showPopUp();
 				} catch (ClassImportException e1) {
+					/**
+					 * Warning if Importing failed
+					 */
 					JOptionPane.showMessageDialog(that, "Import failed: " + e1.getMessage());
 					return;
 				}
 				if(imported!=null){
 					try {
+						/**
+						 * Instance of the PacketSniffer
+						 */
 						PacketSniffer p = imported.newInstance();
+						/**
+						 * Set as new PacketAlgorithm
+						 */
 						that.collector.setPacketAlgorithm(p);
+						updateThis(null);
 					} catch (InstantiationException | IllegalAccessException e1) {
 						JOptionPane.showMessageDialog(that, "Instance creation failed: " + e1.getMessage());
 					}
+				}else{
+					System.out.println("Imported null");
 				}
+				//Else import aborted 
 			}
 		});
 		btnImportPacketcapturealgorithm.setBounds(189, 13, 181, 25);
 		getContentPane().add(btnImportPacketcapturealgorithm);
 		
 		lblAlgorithm = new JLabel();
-		lblAlgorithm.setBounds(12, 17, 165, 16);
+		lblAlgorithm.setBounds(10, 10, 165, 30);
 		getContentPane().add(lblAlgorithm);
 		
-		JScrollPane scrollPane = new JScrollPane();
-		scrollPane.setBounds(12, 46, 165, 132);
-		getContentPane().add(scrollPane);
+		scrollPaneLinks = new JScrollPane();
+		scrollPaneLinks.setBounds(12, 46, 165, 132);
+		getContentPane().add(scrollPaneLinks);
 		
-		JList list_1 = new JList();
-		scrollPane.setViewportView(list_1);
-		
-		JScrollPane scrollPane_1 = new JScrollPane();
-		scrollPane_1.setBounds(12, 208, 165, 132);
-		getContentPane().add(scrollPane_1);
-		
-		JList list = new JList();
-		scrollPane_1.setViewportView(list);
+		scrollPaneDevices = new JScrollPane();
+		scrollPaneDevices.setBounds(12, 208, 165, 132);
+		getContentPane().add(scrollPaneDevices);
 		
 		JButton btnStopCapturingLink = new JButton("Stop Capturing Link");
+		btnStopCapturingLink.addActionListener(a->{
+			if(mutex)
+				return;
+			int selectedIndex = listLinks.getSelectedIndex();
+			if(selectedIndex!=-1 && selectedIndex<collector.getLinks().size()){
+				Iterator<Link> it = collector.getLinks().iterator();
+				for(int i=0; i<selectedIndex;i++){
+					it.next();
+				}
+				if(it.hasNext()){
+					Link linkToBeRemoved = it.next();
+					captureController.removeLinkFromCollector(collector, linkToBeRemoved);
+					updateThis(null);
+				}
+			}
+		});
+		
 		btnStopCapturingLink.setBounds(189, 153, 181, 25);
 		getContentPane().add(btnStopCapturingLink);
 		
@@ -124,11 +185,36 @@ public class EditPacketSniffer extends JDialog {
 		btnStopCapturingDevice.setBounds(189, 206, 181, 25);
 		getContentPane().add(btnStopCapturingDevice);
 		
+		btnStopCapturingDevice.addActionListener(a->{
+			if(mutex)
+				return;
+			int selectedIndex = listDevices.getSelectedIndex();
+			if(selectedIndex!=-1 && selectedIndex<collector.getDevices().size()){
+				Iterator<SmartDevice> it = collector.getDevices().iterator();
+				for(int i=0; i<selectedIndex;i++){
+					it.next();
+				}
+				if(it.hasNext()){
+					SmartDevice deviceToBeRemoved = it.next();
+					captureController.removeDeviceFromCollector(collector, deviceToBeRemoved);
+					updateThis(null);
+				}
+			}
+		});
+		
 		JButton btnCreatePacketCollector = new JButton("Create Packet Collector");
 		btnCreatePacketCollector.addActionListener(new ActionListener() {
 			public void actionPerformed(ActionEvent e) {
+				if(mutex)return;
+				if(!captureController.getPacketCollectors().contains(collector)){
+					captureController.addPacketCollector(collector);
+				}
+				that.setVisible(false);
+				that.setEnabled(false);
+				that.dispose();
 			}
 		});
+		
 		btnCreatePacketCollector.setBounds(189, 315, 181, 25);
 		getContentPane().add(btnCreatePacketCollector);
 		/**
@@ -146,9 +232,89 @@ public class EditPacketSniffer extends JDialog {
 	 * @param o object (unused)
 	 */
 	public void updateThis(Object o){
+		mutex = true;
+		/**
+		 * Update algorithm name
+		 */
 		if(collector.getPacketAlgorithm()==null)
 			lblAlgorithm.setText("Algorithm: null");
 		else
-			lblAlgorithm.setText("Algorithm: "+collector.getPacketAlgorithm().getClass().getSimpleName());
+			lblAlgorithm.setText("<html>Algorithm:<br> "+collector.getPacketAlgorithm().getClass().getSimpleName()+"</html>");
+		
+		/**
+		 * Update List of Links
+		 */
+		/**
+		 * Names of the Links
+		 */
+		String[] linkNames = new String[collector.getLinks().size()];
+		if(collector.getLinks().size()!=0){
+			Iterator<Link> it = collector.getLinks().iterator();
+			for(int i=0;i<collector.getLinks().size();i++){
+				if(it.hasNext()){
+					linkNames[i] = it.next().getName();
+				}else{
+					linkNames[i] = "invalid Size";
+				}
+			}
+		}
+		/**
+		 * New list for the Links
+		 */
+		JList<String> newList = new JList<String>(linkNames);
+		newList.setToolTipText("List of all links, which packets are currently being captured.");
+		newList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
+		/**
+		 * ScrollPosition of the ScrollPaneLink
+		 */
+		int pos = scrollPaneLinks.getVerticalScrollBar().getValue();
+
+		if(listLinks!=null){
+			scrollPaneLinks.remove(listLinks);
+			newList.setSelectedIndex(listLinks.getSelectedIndex());		
+		}
+		scrollPaneLinks.setViewportView(newList);
+		scrollPaneLinks.getVerticalScrollBar().setValue(pos);
+		listLinks = newList;
+		
+		/**
+		 * Update List of Devices
+		 */
+		/**
+		 * Names of the Devices
+		 */
+		String[] deviceNames = new String[collector.getDevices().size()];
+		if(collector.getDevices().size()!=0){
+			Iterator<SmartDevice> itD = collector.getDevices().iterator();
+			for(int i=0;i<collector.getDevices().size();i++){
+				if(itD.hasNext()){
+					deviceNames[i] = itD.next().getName();
+				}else{
+					deviceNames[i] = "invalid Size";
+				}
+			}
+		}
+		/**
+		 * New list for the Devices
+		 */
+		newList = new JList<String>(deviceNames);
+		newList.setToolTipText("List of all SmartDevices, which packets are currently being captured.");
+		newList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
+		/**
+		 * ScrollPosition of the ScrollPaneLink
+		 */
+		pos = scrollPaneDevices.getVerticalScrollBar().getValue();
+
+		if(listDevices!=null){
+			scrollPaneDevices.remove(listDevices);
+			newList.setSelectedIndex(listDevices.getSelectedIndex());		
+		}
+		scrollPaneDevices.setViewportView(newList);
+		scrollPaneDevices.getVerticalScrollBar().setValue(pos);
+		listDevices = newList;
+		
+		
+		this.repaint();
+		mutex = false;
 	}
 }