Kaynağa Gözat

Adds topology methods to protocol to enable any uknown topologies

Andreas T. Meyer-Berg 5 yıl önce
ebeveyn
işleme
36921a10da

+ 3 - 2
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/Main.java

@@ -10,7 +10,6 @@ import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Protocol;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.SimulationManager;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.SmartDevice;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.protocols.MQTT_protocol;
-import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.simpleImplementation.SimpleConnection;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.simpleImplementation.SimpleLink;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.simpleImplementation.SimpleProtocol;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.view.MainFrame;
@@ -96,10 +95,12 @@ public class Main {
 		b.setStatus(Port.SENDING);
 		B.addPort(b);
 		
-		SimpleConnection s = new SimpleConnection(a, b, link, new SimpleProtocol(a, b));
+		Connection s = new ConnectionImplementation(link, new SimpleProtocol(a, b));
 		s.setPacketLossProbability(0.01);//1% Packet loss probability
 		a.setConnection(s);
 		b.setConnection(s);
+		s.addSmartDevice(a);
+		s.addSmartDevice(b);
 		m.addConnectionNetwork(link);
 		link.addConnection(s);
 		m.addConnection(s);

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

@@ -1,8 +1,11 @@
 package de.tu_darmstadt.tk.SmartHomeNetworkSim.core;
 
 import java.util.Collection;
+import java.util.Iterator;
 import java.util.LinkedList;
 
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.util.Pair;
+
 /**
  * Protocol according to which the packages are created. Can contain different
  * Roles and multiple participating SmartDevice(Port)s.
@@ -10,6 +13,23 @@ import java.util.LinkedList;
  * @author Andreas T. Meyer-Berg
  */
 public interface Protocol {
+	/*
+	 * Topology options
+	 */
+	/**
+	 * Every Port/Device is connected to every other Port/Device
+	 */
+	public static final byte FULLY_CONNECTED = 0;
+	
+	/**
+	 * Every Port/Device is connected to the Device of Role 0. (Has to be exactly one device of Role 0)
+	 */
+	public static final byte STAR = 1;
+	
+	/**
+	 * Custom topology, getTopology has to be overwritten.
+	 */
+	public static final byte CUSTOM = 127;
 	
 	/**
 	 * Generates the next packets<br>
@@ -106,4 +126,53 @@ public interface Protocol {
 	 * @return name of the protocol 
 	 */
 	public String getName();
+	
+	/**
+	 * Returns the topology of this protocol as specified by the static final protocol fields.
+	 * 
+	 * @return used topology
+	 */
+	public default byte getTopologyType() {
+		return FULLY_CONNECTED;
+	}
+	
+	/**
+	 * Returns the different connections of this protocol, how the Ports are connected and transmitting packets.
+	 * 
+	 * @return Connections
+	 */
+	public default Collection<Pair<Port,Port>> getTopology(){
+		LinkedList<Pair<Port,Port>> topology = new LinkedList<Pair<Port,Port>>();
+		switch (getTopologyType()){			
+		case STAR:
+			Collection<Port> devices = getDevicesWithRole(0);
+			Port router = null;
+			if(devices.size() == 1)
+				router = devices.iterator().next();
+			for (Iterator<Port> iterator = getDevices().iterator(); iterator.hasNext();) {
+				Port right = (Port) (iterator.next());
+				if(router != right)
+					topology.add(new Pair<Port, Port>(router, right));
+			}
+			break;
+		case FULLY_CONNECTED:
+		case CUSTOM:
+		default:
+			for (Iterator<Port> iterator = getDevices().iterator(); iterator.hasNext();) {
+				Port left = (Port) (iterator.next());
+				boolean samePosition = false;
+				for(Iterator<Port> iterator2 = getDevices().iterator(); iterator2.hasNext();){
+					Port right = iterator2.next();
+					if(!samePosition){
+						if(left==right)
+							samePosition=true;
+					}else{
+						topology.add(new Pair<Port, Port>(left, right));						
+					}
+				}
+			}
+			break;
+		}
+		return topology;
+	}
 }

+ 44 - 2
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/protocols/MQTT_protocol.java

@@ -9,6 +9,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;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.protocols.packets.MQTT_packet;
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.util.Pair;
 
 /**
  * Implementation of the MQTT Protocol to generate packets for the simulation
@@ -131,7 +132,7 @@ public class MQTT_protocol implements Protocol {
 			 * Topic which should be subscribed or unsubscribed
 			 */
 			String newTopic;
-			//Subsribe if no subscriptions so far or not subscribed to all, with a probability of70%
+			//Subscribe if no subscriptions so far or not subscribed to all, with a probability of70%
 			if(tops.size()==0||(tops.size()!=subbedTopics.size()&&Math.random()<0.7)){
 				//Forced Subscribe
 				newTopic = topics.get((int) Math.floor(Math.random()*topics.size()));
@@ -234,7 +235,6 @@ public class MQTT_protocol implements Protocol {
 			return false;
 		case 1:
 			pubSubs.add(device);
-
 			subbedTopics.putIfAbsent(device, new LinkedList<String>());
 			break;
 		case 2:
@@ -283,5 +283,47 @@ public class MQTT_protocol implements Protocol {
 	public String getName() {
 		return "MQTT";
 	}
+	
+	@Override
+	public int getRoleOfDevice(Port device){
+		if(device == null)
+			return -1;
+		if(device == broker)
+			return 0;
+		if(pubSubs.contains(device))
+			return 1;
+		if(pubs.contains(device))
+			return 2;
+		if(subs.contains(device))
+			return 3;
+		return -1;
+	}
+	
+	@Override
+	public Collection<Port> getDevices(){
+		LinkedList<Port> returnDevices = new LinkedList<Port>();
+		returnDevices.add(broker);
+		returnDevices.addAll(pubSubs);
+		returnDevices.addAll(pubs);
+		returnDevices.addAll(subs);
+		return returnDevices;
+	}
+	
+	@Override
+	public byte getTopologyType() {
+		return STAR;
+	}
+	
+	@Override
+	public Collection<Pair<Port,Port>> getTopology(){
+		LinkedList<Pair<Port,Port>> topology = new LinkedList<Pair<Port,Port>>();
+		for(Port p: pubSubs)
+			topology.add(new Pair<Port, Port>(broker, p));
+		for(Port p: pubs)
+			topology.add(new Pair<Port, Port>(broker, p));
+		for(Port p: subs)
+			topology.add(new Pair<Port, Port>(broker, p));
+		return topology;
+	}
 
 }

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

@@ -7,6 +7,7 @@ import java.util.LinkedList;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Packet;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Port;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Protocol;
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.util.Pair;
 
 /**
  * Simple Implementation of a protocol, which sends 
@@ -110,4 +111,27 @@ public class SimpleProtocol implements Protocol {
 		else if(device == destination) return 1;
 		else return -1;
 	}
+	
+	@Override
+	public Collection<Port> getDevices(){
+		LinkedList<Port> returnDevices = new LinkedList<Port>();
+		if(source!=null)
+			returnDevices.add(source);
+		if(destination!=null)
+			returnDevices.add(destination);
+		return returnDevices;
+	}
+	
+	@Override
+	public byte getTopologyType() {
+		return FULLY_CONNECTED;
+	}
+	
+	@Override
+	public Collection<Pair<Port,Port>> getTopology(){
+		LinkedList<Pair<Port,Port>> topology = new LinkedList<Pair<Port,Port>>();
+		if(source!=null && destination!=null)
+			topology.add(new Pair<Port, Port>(source, destination));
+		return topology;
+	}
 }