|
@@ -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);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|