Procházet zdrojové kódy

Fixes various Connection/Link Editing Bugs

Andreas T. Meyer-Berg před 5 roky
rodič
revize
e433475013

+ 51 - 3
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/control/Controller.java

@@ -357,15 +357,63 @@ public class Controller {
 	 * @param newRole new role of the device
 	 * @return true if new role was set, false if not
 	 */
-	public boolean changeRoleOfDevice(Protocol protocol, Port device, int newRole){
+	public boolean changeRoleOfDevice(Protocol protocol, Connection con, Port device, int newRole){
 		if(newRole < 0 || newRole >= protocol.getNumberOfRoles()){
 			protocol.removeDevice(device);
+			if(con.getParticipants().contains(device))
+				con.removeSmartDevice(device);
+			if(device.getConnection()!=null)
+				device.setConnection(null);
 			return false;
-		} else if(protocol.getDevicesWithRole(newRole).contains(device))
+		} else if(protocol.getDevicesWithRole(newRole).contains(device)){
+			if(!con.getParticipants().contains(device))
+				con.addSmartDevice(device);
+			if(device.getConnection()!=con)
+				device.setConnection(con);
 			return true;
+		}
 		else{
 			protocol.removeDevice(device);
-			return protocol.addDeviceOfRole(device, newRole);
+			boolean added = protocol.addDeviceOfRole(device, newRole);
+			if(added){
+				if(!con.getParticipants().contains(device))
+					con.addSmartDevice(device);
+				if(device.getConnection()!=con)
+					device.setConnection(con);
+				return true;
+			}else{
+				if(con.getParticipants().contains(device))
+					con.removeSmartDevice(device);
+				if(device.getConnection()!=null)
+					device.setConnection(null);
+				return false;
+			}
+		}
+	}
+	
+	public void removeDeviceFromConnection(Port p, Connection connection){
+		connection.removeSmartDevice(p);
+		connection.getProtocol().removeDevice(p);
+		p.setConnection(null);
+	}
+	
+	public boolean addDeviceToConnection(Port p, Connection connection, int role){
+		if(connection.getProtocol().getDevicesWithRole(role).contains(p) || connection.getProtocol().addDeviceOfRole(p, role)){
+			//If port already has the role, or it could be assigned - just check the fields
+			if(!connection.getParticipants().contains(p))
+				connection.addSmartDevice(p);
+			if(p.getConnection()!=connection)
+				p.setConnection(connection);
+			return true;
+		}else {
+			//Device could not be added -> Remove
+			if(p.getConnection()==connection)
+				p.setConnection(null);
+			if(connection.getParticipants().contains(p))
+				connection.removeSmartDevice(p);
+			if(connection.getProtocol().getDevices().contains(p))
+				connection.getProtocol().removeDevice(p);
+			return false;
 		}
 	}
 }

+ 13 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/Protocol.java

@@ -1,6 +1,7 @@
 package de.tu_darmstadt.tk.SmartHomeNetworkSim.core;
 
 import java.util.Collection;
+import java.util.LinkedList;
 
 /**
  * Protocol according to which the packages are created. Can contain different
@@ -87,6 +88,18 @@ public interface Protocol {
 	 */
 	public void removeDevice(Port device);
 
+	/**
+	 * Returns all Devices which participate in this Protocol
+	 * 
+	 * @return devices in this Protocol
+	 */
+	public default Collection<Port> getDevices(){
+		LinkedList<Port> devices = new LinkedList<Port>();
+		for(int i=0; i<getNumberOfRoles(); i++)
+			devices.addAll(getDevicesWithRole(i));
+		return devices;
+	} 
+	
 	/**
 	 * Returns name of the protocol
 	 * 

+ 2 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/simpleImplementation/SimpleConnection.java

@@ -10,6 +10,7 @@ import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Packet;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Port;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Protocol;
 
+@Deprecated
 public class SimpleConnection implements Connection {
 
 	/**
@@ -59,6 +60,7 @@ public class SimpleConnection implements Connection {
 	 */
 	private double packetLossProbability = 0.0;
 
+	
 	/**
 	 * Creates a new connection between two SmartDevice Ports on a given Link, which
 	 * communicate with a protocol p

+ 19 - 13
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/view/ConnectionCreationPanel.java

@@ -120,8 +120,7 @@ public class ConnectionCreationPanel extends JScrollPane {
 	public ConnectionCreationPanel(Collection<Port> ports, Link l,
 			Controller controller) {
 		this.controller = controller;
-		connection = new ConnectionImplementation(l, new SimpleProtocol(null,
-				null));
+		connection = new ConnectionImplementation(l, new SimpleProtocol());
 		this.ports = new Port[ports.size()];
 		int i = 0;
 		for (Port d : ports) {
@@ -188,8 +187,11 @@ public class ConnectionCreationPanel extends JScrollPane {
 		// Set Index to selected Protocol
 		for (int i = 0; i < availableProtocols.size(); i++)
 			if (connection.getProtocol().getClass()
-					.equals(availableProtocols.get(i)))
+					.equals(availableProtocols.get(i))){
+				//Select the right protocol and save last index
 				cmbLinkType.setSelectedIndex(i);
+				lastIndex = i;
+			}
 
 		// Add Functionality for changing protocol
 		cmbLinkType.addActionListener(new ActionListener() {
@@ -217,7 +219,7 @@ public class ConnectionCreationPanel extends JScrollPane {
 					cmbLinkType.setSelectedIndex(lastIndex);
 					System.out
 							.println("WARNING: Invalid Protocol Selected - restore last index");
-				} else {
+				} else if(connection.setProtocol(newProtocol)){
 					/**
 					 * New Roles as Strings
 					 */
@@ -228,6 +230,10 @@ public class ConnectionCreationPanel extends JScrollPane {
 					int oldDisconnected = oldProtocol.getNumberOfRoles();
 					//Update to new disconnected Index
 					disconnectedIndex = newProtocol.getNumberOfRoles();
+					//Update Protocol
+						
+					// Update lastIndex to new Index
+					lastIndex = cmbLinkType.getSelectedIndex();
 					//Update Boxes
 					for (int i = 0; i < boxes.length; i++) {
 						/**
@@ -243,22 +249,21 @@ public class ConnectionCreationPanel extends JScrollPane {
 						if (oldSelected < 0 || oldSelected >= disconnectedIndex
 								|| oldSelected == oldDisconnected) {
 							//Invalid Index -> Disconnected
+							controller.removeDeviceFromConnection(ports[i], connection);
 							boxes[i].setSelectedIndex(disconnectedIndex);
 						} else {
-							if (newProtocol.addDeviceOfRole(ports[i],
-									oldSelected)) {
+							if (controller.addDeviceToConnection(ports[i], connection, oldSelected)){
 								//Set to Box to display role
 								boxes[i].setSelectedIndex(oldSelected);
 							} else {
 								//Could not be added -> Disconnected
+								controller.removeDeviceFromConnection(ports[i], connection);
 								boxes[i].setSelectedIndex(disconnectedIndex);
 							}
 						}
 					}
-					// Update all Boxes, Roles etc.
-					connection.setProtocol(newProtocol);
-					// Update lastIndex to new Index
-					lastIndex = cmbLinkType.getSelectedIndex();
+				} else {
+					cmbLinkType.setSelectedIndex(lastIndex);
 				}
 				//Set Mutex back to false - allow changes
 				protocolChange = false;
@@ -394,16 +399,17 @@ public class ConnectionCreationPanel extends JScrollPane {
 				int n = 5;
 				for(int j = 0; j <= n; j++){
 					if(j == 0){//Try to add Router first
-						if(connection.getProtocol().addDeviceOfRole(currentPort, 0)){
+						if(controller.addDeviceToConnection(currentPort, connection, 0)){
 							boxes[pos].setSelectedIndex(0);
 							break;
 						}										
 					}else if(j == n){//If it could not be added
 						boxes[pos].setSelectedIndex(disconnectedIndex);
+						controller.removeDeviceFromConnection(currentPort, connection);
 						break;
 					}else{
 						int randomRole = ThreadLocalRandom.current().nextInt(0, disconnectedIndex);
-						if(connection.getProtocol().addDeviceOfRole(currentPort, randomRole)){
+						if(controller.addDeviceToConnection(currentPort, connection, randomRole)){
 							boxes[pos].setSelectedIndex(randomRole);
 							break;
 						}
@@ -424,7 +430,7 @@ public class ConnectionCreationPanel extends JScrollPane {
 					/**
 					 * True if role was successfully changed
 					 */
-					boolean successfullChange = controller.changeRoleOfDevice(connection.getProtocol(),
+					boolean successfullChange = controller.changeRoleOfDevice(connection.getProtocol(),connection,
 							ports[pos], selected);
 					/**
 					 * Set to Disconnected, if role could not be changed

+ 1 - 1
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/view/LinkCreationPanel.java

@@ -81,7 +81,7 @@ public class LinkCreationPanel extends JScrollPane{
 		
 		tfName = new JTextField();
 		tfName.setText(newLink.getName());
-		tfName.setBounds(162, 39, 116, 22);
+		tfName.setBounds(162, 39, 300, 22);
 		content.add(tfName);
 		tfName.setColumns(10);
 		tfName.addActionListener(new ActionListener() {

+ 5 - 1
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/view/PortEditorPanel.java

@@ -128,7 +128,11 @@ public class PortEditorPanel extends JPanel
         JButton btnEditConnection = new JButton("Edit");
         btnEditConnection.setBounds(200, 40, 60, 20);
         editPanel.add(btnEditConnection);
-        btnEditConnection.addActionListener(a->new ConnectionCreationDialog(list.getSelectedValue().getConnection(), controller, this));
+        btnEditConnection.addActionListener(a->{
+        	if(list.getSelectedValue()==null||list.getSelectedValue().getConnection()==null)
+        		return;
+        	new ConnectionCreationDialog(list.getSelectedValue().getConnection(), controller, this);
+        });
         
         JLabel lblStatus = new JLabel("Status:");
         lblStatus.setHorizontalAlignment(SwingConstants.RIGHT);