瀏覽代碼

Improves protocol interface to enable multiple participants

Allows for different Roles inside the protocol and representation.
Andreas T. Meyer-Berg 5 年之前
父節點
當前提交
000c82d6c0

+ 56 - 1
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/Protocol.java

@@ -1,7 +1,10 @@
 package de.tu_darmstadt.tk.SmartHomeNetworkSim.core;
 
+import java.util.Collection;
+
 /**
- * Protocol according to which the packages are created
+ * Protocol according to which the packages are created. Can contain different
+ * Roles.
  * 
  * @author Andreas T. Meyer-Berg
  */
@@ -9,7 +12,59 @@ public interface Protocol {
 
 	/**
 	 * Generates the next Packet
+	 * 
+	 * @param timestep
+	 *            Time the package should be sent
 	 * @return next Packet
 	 */
 	public Packet generateNextPaket(long timestep);
+
+	/**
+	 * Returns the number of different roles the participating SmartDevices
+	 * could have
+	 * 
+	 * @return number of different roles
+	 */
+	public int getNumberOfRoles();
+
+	/**
+	 * Returns the textual Representation of the different Roles. The Array
+	 * should contain {@codeNumberOfRoles} Strings. {@codeArray[role]} should be
+	 * a human-readable String representation of the Role at Position
+	 * {@code role}.
+	 * 
+	 * @return String Representations of the roles
+	 */
+	public String[] getRoles();
+
+	/**
+	 * Returns all SmartDevices of the given Role. Returns null, if the role
+	 * number was invalid.
+	 * 
+	 * @param role
+	 *            Position of the role in {@code getNumberOfRoles}
+	 * @return SmartDevices of Role with index {@code role}
+	 */
+	public Collection<SmartDevice> getDevicesWithRole(int role);
+
+	/**
+	 * Adds a new smartDevice to the role, returns {@code true} if if was
+	 * assigned successfully, {@code false} if it wasn't. (Either invalid role
+	 * number or maximum number of devices for the role reached)
+	 * 
+	 * @param device
+	 *            SmartDevice that should be assigned to the given role
+	 * @param role
+	 *            Position of the role in {@code getNumberOfRoles}
+	 * @return whether the Device was added
+	 */
+	public boolean addDeviceOfRole(SmartDevice device, int role);
+
+	/**
+	 * Remove a device from the Protocol
+	 * 
+	 * @param device
+	 *            device that should be removed
+	 */
+	public void removeDevice(SmartDevice device);
 }

+ 55 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/SimpleProtocol.java

@@ -1,5 +1,8 @@
 package de.tu_darmstadt.tk.SmartHomeNetworkSim.core;
 
+import java.util.Arrays;
+import java.util.Collection;
+
 /**
  * Simple Implementation of a protocol, which sends 
  *
@@ -30,5 +33,57 @@ public class SimpleProtocol implements Protocol {
 		else
 			return new SimplePacket(timestep, source, destination);
 	}
+	
+	@Override
+	public int getNumberOfRoles() {
+		// two different roles
+		return 2;
+	}
+	
+	@Override
+	public String[] getRoles() {
+		return new String[]{"Sender", "Receiver"};
+	}
+	
+	@Override
+	public Collection<SmartDevice> getDevicesWithRole(int role) {
+		SmartDevice ret = null;
+		switch (role) {
+		case 0:
+			ret = source;
+			break;
+		case 1:
+			ret = destination;
+			break;
+		default:
+			return null;
+		}
+		if(ret!=null) 
+			return Arrays.asList(ret);
+		else
+			return Arrays.asList();
+	}
+	
+	@Override
+	public boolean addDeviceOfRole(SmartDevice device, int role) {
+		if(role == 0 && source == null){
+			source = device;
+		}else if(role == 1 && destination == source){
+			destination = device;
+		}else{
+			return false;
+		}
+		return false;
+	}
+	
+	@Override
+	public void removeDevice(SmartDevice device) {
+		if(device == source)
+			source = null;
+		if(device == destination)
+			destination = null;
+		
+	}
+	
 
 }