Kaynağa Gözat

Extends network Controller

link/connection change still buggy ...
Andreas T. Meyer-Berg 5 yıl önce
ebeveyn
işleme
bb21dbd9c7

+ 70 - 22
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/control/NetworkController.java

@@ -104,7 +104,7 @@ public class NetworkController {
 			//Skip ports that are not connected
 			if(p.getConnection()==null)
 				continue;
-			removeDeviceFromConnection(p, p.getConnection());
+			removeDeviceFromConnectionAndProtocol(p, p.getConnection());
 		}
 		
 		// Remove from Links
@@ -160,7 +160,7 @@ public class NetworkController {
 	 * @param link link to add
 	 */
 	public void addLink(Link link){
-		if(link!=null)
+		if(link!=null && !model.getConnectionNetworks().contains(link))
 			model.addConnectionNetwork(link);
 	}
 
@@ -185,7 +185,7 @@ public class NetworkController {
 	 * @param connection connection to be added
 	 */
 	public void addConnection(Connection connection){
-		if(connection!=null)
+		if(connection!=null && !getConnections().contains(connection))
 			model.addConnection(connection);
 	}
 
@@ -251,7 +251,7 @@ public class NetworkController {
 	public boolean changeRoleOfDevice(Protocol protocol, Connection con, Port device, int newRole){
 		if(newRole < 0 || newRole >= protocol.getNumberOfRoles()){
 			protocol.removeDevice(device);
-			removeDeviceFromConnection(device, con);
+			removeDeviceFromConnectionAndProtocol(device, con);
 			return false;
 		} else if(protocol.getDevicesWithRole(newRole).contains(device)){
 			if(!con.getParticipants().contains(device))
@@ -280,11 +280,11 @@ public class NetworkController {
 	}
 
 	/**
-	 * Removes Device p from the Connection
+	 * Removes Device p from the Connection & Protocol
 	 * @param p Port/Device to remove
 	 * @param connection connection, which should remove the device
 	 */
-	public void removeDeviceFromConnection(Port p, Connection connection){
+	public void removeDeviceFromConnectionAndProtocol(Port p, Connection connection){
 		if(connection != null){
 			connection.removeSmartDevice(p);
 			connection.getProtocol().removeDevice(p);
@@ -297,6 +297,22 @@ public class NetworkController {
 		if(p!=null)
 			p.setConnection(null);
 	}
+	
+	/**
+	 * Removes Device p from the Connection & Protocol
+	 * @param p Port/Device to remove
+	 * @param connection connection, which should remove the device
+	 */
+	public void removeDeviceFromConnection(Port p, Connection connection){
+		if(connection != null){
+			connection.removeSmartDevice(p);
+			p.setConnection(null);
+			if(connection.getParticipants().isEmpty())
+				deleteConnection(connection);
+		}
+		if(p != connection)
+			p.setConnection(null);
+	}
 
 	/**
 	 * Adds Device p to the connection with the specified role
@@ -305,7 +321,7 @@ public class NetworkController {
 	 * @param role new role of the device (See {@link Protocol} Roles)
 	 * @return true, if the device was added
 	 */
-	public boolean addDeviceToConnection(Port p, Connection connection, int role){
+	public boolean addDeviceToConnectionAndProtocol(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))
@@ -324,6 +340,19 @@ public class NetworkController {
 			return false;
 		}
 	}
+	
+	/**
+	 * Adds Device p to the connection
+	 * @param p Port/Device to be added
+	 * @param connection Connection
+	 */
+	public void addDeviceToConnection(Port p, Connection connection){
+		if(p==null || connection == null)
+			return;
+		if(!connection.getParticipants().contains(p))
+			connection.addSmartDevice(p);
+		p.setConnection(connection);
+	}
 
 	/**
 	 * Changes the link of the given connection to the new link. Returns true if it was successfully changed, false if it could not be changed.
@@ -356,7 +385,8 @@ public class NetworkController {
 		 * Add Connection to new Link
 		 */
 		connection.setLink(link);
-		link.addConnection(connection);
+		if(!link.getConnections().contains(connection))
+			link.addConnection(connection);
 		/**
 		 * 
 		 */
@@ -379,15 +409,14 @@ public class NetworkController {
 		try{
 			newCon = newConnectionClass.newInstance();
 			newCon.setProtocol(connection.getProtocol());
-			newCon.setLink(connection.getLink());
-			connection.getLink().addConnection(newCon);
+			addConnectionToLink(newCon, connection.getLink());
 			newCon.setStatus(connection.getStatus());
 			newCon.setPacketLossProbability(connection.getPacketLossProbability());
 			newCon.setName(connection.getName());
 			for (Iterator<Port> p = connection.getParticipants().iterator(); p.hasNext();) {
 				Port type = (Port) p.next();
-				newCon.addSmartDevice(type);
-				type.setConnection(newCon);
+				removeDeviceFromConnection(type, connection);
+				addDeviceToConnection(type, connection);
 				
 			}
 		}catch(Exception e){
@@ -471,7 +500,7 @@ public class NetworkController {
 		if(c == null)return;
 		LinkedList<Port> ports = new LinkedList<Port>(c.getParticipants());
 		for(Port p:ports)
-			removeDeviceFromConnection(p, c);
+			removeDeviceFromConnectionAndProtocol(p, c);
 		ports.clear();
 		removeConnectionFromLink(c, c.getLink());
 		c.setStatus(Connection.TERMINATED);
@@ -519,6 +548,8 @@ public class NetworkController {
 	public Link changeLinkType(Link oldLink, Class<? extends Link> newType) {
 		if(newType == null)
 			return null;
+		System.out.println("Change Link:");
+		printLinkHelp(oldLink);
 		/**
 		 * New Link which was created
 		 */
@@ -533,10 +564,10 @@ public class NetworkController {
 		}else {
 			// Set old Name
 			newLink.setName(oldLink.getName());
-			// Add to Mode
-			if(getLinks().contains(oldLink)){
-				removeLink(oldLink);
-				addLink(newLink);
+			
+			//Connection to the new Link
+			for(Connection c: new LinkedList<Connection>(oldLink.getConnections())){
+				addConnectionToLink(c, newLink);
 			}
 			// Add devices to the new Link
 			LinkedList<SmartDevice> devices= new LinkedList<>(oldLink.getDevices());
@@ -544,12 +575,29 @@ public class NetworkController {
 				removeLinkFromDevice(oldLink, device);
 				addLinkToDevice(newLink, device);
 			}
-			//Connection to the new Link
-			for(Connection c: new LinkedList<Connection>(oldLink.getConnections())){
-				removeConnectionFromLink(c, oldLink);
-				addConnectionToLink(c, newLink);
+			
+			System.out.println("oldLink:");
+			printLinkHelp(oldLink);
+			System.out.println("new Link:");
+			printLinkHelp(newLink);
+			
+			// Add to Mode
+			if(getLinks().contains(oldLink)){
+				removeLink(oldLink);
+				addLink(newLink);
 			}
-		return newLink;
+			return newLink;
+		}
+	}
+	
+	private void printLinkHelp(Link l){
+		System.out.println("Link: "+l.getName());
+		for(SmartDevice d:l.getDevices()){
+			System.out.println(d.getName());
+		}
+		System.out.println("Connections:");
+		for(Connection c:l.getConnections()){
+			System.out.println(c.getName());
 		}
 	}
 

+ 7 - 7
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/view/popups/ConnectionCreationPanel.java

@@ -383,15 +383,15 @@ public class ConnectionCreationPanel extends JScrollPane {
 						// Try to update the Roles
 						if (oldSelected < 0 || oldSelected >= disconnectedIndex || oldSelected == oldDisconnected) {
 							// Invalid Index -> Disconnected
-							network.removeDeviceFromConnection(ports[i], connection);
+							network.removeDeviceFromConnectionAndProtocol(ports[i], connection);
 							cmbPortRoles[i].setSelectedIndex(disconnectedIndex);
 						} else {
-							if (network.addDeviceToConnection(ports[i], connection, oldSelected)) {
+							if (network.addDeviceToConnectionAndProtocol(ports[i], connection, oldSelected)) {
 								// Set to Box to display role
 								cmbPortRoles[i].setSelectedIndex(oldSelected);
 							} else {
 								// Could not be added -> Disconnected
-								network.removeDeviceFromConnection(ports[i], connection);
+								network.removeDeviceFromConnectionAndProtocol(ports[i], connection);
 								cmbPortRoles[i].setSelectedIndex(disconnectedIndex);
 							}
 						}
@@ -563,7 +563,7 @@ public class ConnectionCreationPanel extends JScrollPane {
 						/**
 						 * Else remove from COnnection
 						 */
-						network.removeDeviceFromConnection(ports[i], connection);
+						network.removeDeviceFromConnectionAndProtocol(ports[i], connection);
 						if (ports[i].getOwner().getPorts().contains(ports[i]))
 							ports[i].getOwner().removePort(ports[i]);
 
@@ -631,17 +631,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 (network.addDeviceToConnection(currentPort, connection, 0)) {
+						if (network.addDeviceToConnectionAndProtocol(currentPort, connection, 0)) {
 							cmbPortRoles[pos].setSelectedIndex(0);
 							break;
 						}
 					} else if (j == n) {// If it could not be added
 						cmbPortRoles[pos].setSelectedIndex(disconnectedIndex);
-						network.removeDeviceFromConnection(currentPort, connection);
+						network.removeDeviceFromConnectionAndProtocol(currentPort, connection);
 						break;
 					} else {
 						int randomRole = ThreadLocalRandom.current().nextInt(0, disconnectedIndex);
-						if (network.addDeviceToConnection(currentPort, connection, randomRole)) {
+						if (network.addDeviceToConnectionAndProtocol(currentPort, connection, randomRole)) {
 							cmbPortRoles[pos].setSelectedIndex(randomRole);
 							break;
 						}

+ 4 - 3
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/view/popups/LinkCreationPanel.java

@@ -39,6 +39,8 @@ public class LinkCreationPanel extends JScrollPane{
 	
 	private Link newLink;
 	private SmartDevice[] devices;
+	
+	@SuppressWarnings("unused")
 	private boolean edit;
 	private int lastIndex = -1;
 	
@@ -221,10 +223,9 @@ public class LinkCreationPanel extends JScrollPane{
 			
 			@Override
 			public void actionPerformed(ActionEvent e) {
-				if(!edit){
-					network.addLink(newLink);
-				}
+				network.addLink(newLink);
 				newLink.setName(tfName.getText());
+				
 				content.setVisible(false);
 				setVisible(false);
 				controller.notifyObservers();