Browse Source

Adds new packages for protocols and packets, first MQTT template

Andreas T. Meyer-Berg 5 years ago
parent
commit
2ddbabd50c

+ 3 - 5
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/Connection.java

@@ -57,7 +57,7 @@ public interface Connection {
 	 * Removes the SmartDevice from the Connection. Should create terminating
 	 * packages, that are returned on the next call of simulateTimeIntervall.<br> If
 	 * the Connection will be fully closed, status should be changed to
-	 * {@link Connection#FINISHED} or {@link Connection#TERMINATED} and
+	 * FINISHED or TERMINATED and
 	 * getTerminationPackages should return the lastPackages that were sent to
 	 * terminate the connection. If the connection still continues and the
 	 * source was removed, a new Device should become the source. The Calling
@@ -120,8 +120,7 @@ public interface Connection {
 	 * Returns the current transmission status of the connection.
 	 * 
 	 * @return transmission status
-	 * @see {@link Connection#FINISHED}, {@link Connection#ACTIVE},
-	 *      {@link Connection#TERMINATED} and {@link Connection#HALTED}
+	 * @see FINISHED, ACTIVE, TERMINATED and HALTED
 	 */
 	public byte getStatus();
 
@@ -130,8 +129,7 @@ public interface Connection {
 	 * 
 	 * @param status
 	 *            transmission status
-	 * @see {@link Connection#FINISHED}, {@link Connection#ACTIVE},
-	 *      {@link Connection#TERMINATED} and {@link Connection#HALTED}
+	 * @see FINISHED, ACTIVE, TERMINATED and HALTED
 	 */
 	public void setStatus(byte status);
 	

+ 17 - 3
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/Packet.java

@@ -7,11 +7,23 @@ package de.tu_darmstadt.tk.SmartHomeNetworkSim.core;
  */
 public abstract class Packet {
 
+	/**
+	 * Time when the packet was sent
+	 */
+	protected long timestamp;
+	
+	/**
+	 * Creates a new packet with the given timestamp
+	 * @param timestamp time the packet was sent
+	 */
+	protected Packet(long timestamp){
+		this.timestamp = timestamp;
+	}
 	/**
 	 * 
 	 * @return Byte representation of the current packet
 	 */
-	public abstract Byte[] dumpBytes();
+	public abstract byte[] dumpBytes();
 
 	/**
 	 * 
@@ -26,7 +38,9 @@ public abstract class Packet {
 	public abstract String getPayload();
 
 	/**
-	 * Returns the Timestamp, wehn the packet was sent
+	 * Returns the Timestamp, at which the packet was sent
 	 */
-	public abstract long getTimestamp();
+	public long getTimestamp(){
+		return timestamp;
+	}
 }

+ 101 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/protocols/MQTT_protocol.java

@@ -0,0 +1,101 @@
+package de.tu_darmstadt.tk.SmartHomeNetworkSim.core.protocols;
+
+import java.util.Arrays;
+import java.util.Collection;
+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.protocols.packets.MQTT_packet;
+
+/**
+ * Implementation of the MQTT Protocol to generate packets for the simulation
+ * 
+ *
+ * @author Andreas T. Meyer-Berg
+ */
+public class MQTT_protocol implements Protocol {
+
+	/**
+	 * Broker which collects and distributes messages
+	 */
+	private Port broker;
+	/**
+	 * Publishers like sensors, which publish data
+	 */
+	private LinkedList<Port> pubs;
+	/**
+	 * Subscriber which subscribe to different Topics
+	 */
+	private LinkedList<Port> subs;
+	/**
+	 * Devices that are Publisher and Subscriber and therefore send and receive messages
+	 */
+	private LinkedList<Port> pubSubs;
+	@Override
+	public Packet generateNextPaket(Port port, long timestep, boolean packetLost) {
+		port.setLastTrigger(timestep);
+		return new MQTT_packet(timestep);
+	}
+
+	@Override
+	public int getNumberOfRoles() {
+		return 4;
+	}
+
+	@Override
+	public String[] getRoles() {
+		//PublisherSubscriber is Publisher as well as Subscriber
+		return new String[]{"Broker", "PublisherSubsriber","Publisher","Subscriber"};
+	}
+
+	@Override
+	public Collection<Port> getDevicesWithRole(int role) {
+		switch (role) {
+		case 0:
+			return new LinkedList<Port>(Arrays.asList(broker));
+		case 1:
+			return pubSubs;
+		case 2:
+			return pubs;
+		case 3:
+			return subs;
+
+		default:
+			return null;
+		}
+	}
+
+	@Override
+	public boolean addDeviceOfRole(Port device, int role) {
+		switch (role) {
+		case 0:
+			if(broker != null)
+				return false;
+			else{
+				broker = device;
+			}
+			break;
+		case 1:
+			pubSubs.add(device);
+		case 2:
+			pubs.add(device);
+		case 3:
+			subs.add(device);
+		default:
+			return false;
+		}
+		return true;
+	}
+
+	@Override
+	public void removeDevice(Port device) {
+		if(broker == device)
+			broker = null;
+		pubSubs.remove(device);
+		subs.remove(device);
+		pubs.remove(device);
+	}
+
+}

+ 7 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/protocols/package-info.java

@@ -0,0 +1,7 @@
+
+/**
+ * Contains implementations of protocols, which can be used to generate packets. 
+ *
+ * @author Andreas T. Meyer-Berg
+ */
+package de.tu_darmstadt.tk.SmartHomeNetworkSim.core.protocols;

+ 35 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/protocols/packets/MQTT_packet.java

@@ -0,0 +1,35 @@
+package de.tu_darmstadt.tk.SmartHomeNetworkSim.core.protocols.packets;
+
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Packet;
+
+/**
+ * Packet generated by the MQTT Protocol
+ * 
+ *
+ * @author Andreas T. Meyer-Berg
+ */
+public class MQTT_packet extends Packet {
+
+	/**
+	 * Creates a new MQTT Packet
+	 * @param timestamp time the packet was sent
+	 */
+	public MQTT_packet(long timestamp) {
+		super(timestamp);
+	}
+	
+	@Override
+	public byte[] dumpBytes() {
+		return "not implemented".getBytes();
+	}
+
+	@Override
+	public String getTextualRepresentation() {
+		return "[Packet: MQTT]";
+	}
+
+	@Override
+	public String getPayload() {
+		return "{Test: this}";
+	}
+}

+ 6 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/protocols/packets/package-info.java

@@ -0,0 +1,6 @@
+/**
+ * Package contains Packet implementations which are sent by protocols of the parent package protocols. 
+ *
+ * @author Andreas T. Meyer-Berg
+ */
+package de.tu_darmstadt.tk.SmartHomeNetworkSim.core.protocols.packets;

+ 3 - 9
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/simpleImplementation/SimplePacket.java

@@ -41,7 +41,7 @@ public class SimplePacket extends Packet {
 	 *            SmartDevice which should receive this packet
 	 */
 	public SimplePacket(long time, SmartDevice source, SmartDevice destination) {
-		this.time = time;
+		super(time);
 		this.source = source;
 		this.destination = destination;
 		this.payload = "";
@@ -61,14 +61,14 @@ public class SimplePacket extends Packet {
 	 *            this packet
 	 */
 	public SimplePacket(long time, SmartDevice source, SmartDevice destination, String payload) {
-		this.time = time;
+		super(time);
 		this.source = source;
 		this.destination = destination;
 		this.payload = payload;
 	}
 
 	@Override
-	public Byte[] dumpBytes() {
+	public byte[] dumpBytes() {
 		return this.dumpBytes();
 	}
 
@@ -90,10 +90,4 @@ public class SimplePacket extends Packet {
 	public String getPayload() {
 		return payload == null ? "" : payload;
 	}
-
-	@Override
-	public long getTimestamp() {
-		return time;
-	}
-
 }