MQTT_packet.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. package de.tu_darmstadt.tk.SmartHomeNetworkSim.core.protocols.packets;
  2. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Packet;
  3. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Port;
  4. /**
  5. * Packet generated by the MQTT Protocol
  6. *
  7. *
  8. * @author Andreas T. Meyer-Berg
  9. */
  10. public class MQTT_packet extends Packet {
  11. /*
  12. * List of the different MQTT packet types, which are the bits 7 to 4 of the
  13. * first byte of the packet as specified in MQTT Specification 2.2.1 (MQTT
  14. * Control Packet type)<br> The first 4 bits contain the values 0 to F.
  15. */
  16. /*
  17. * The second part of the first byte are the Flag Bits. In most control
  18. * packets they are 0. The Publish Packet contains DUP, QoS, QoS, RETAIN
  19. * bits. PUBREL, SUBSCRIBE and UNSUBSCRIBE contain 0x2 as lower nibble. See
  20. * MQTT Section 2.2.2 for further infos.
  21. */
  22. /**
  23. * Reserved (forbidden)
  24. */
  25. public static final byte RESERVED = (byte) 0x00;
  26. /**
  27. * Client request to connect to Server (Client-{@literal >}Server)
  28. */
  29. public static final byte CONNECT = (byte) 0x10;
  30. /**
  31. * Connect acknowledgement (Client{@literal <}-Server)
  32. */
  33. public static final byte CONNACK = (byte) 0x20;
  34. /**
  35. * Publish message (Client{@literal <}-{@literal >}Server)
  36. */
  37. public static final byte PUBLISH = (byte) 0x30;
  38. /**
  39. * Publish acknowledgement (Client{@literal <}-{@literal >}Server)
  40. */
  41. public static final byte PUBACK = (byte) 0x40;
  42. /**
  43. * Publish received (assured delivery part 1) (Client{@literal <}-{@literal >}Server)
  44. */
  45. public static final byte PUBREC = (byte) 0x50;
  46. /**
  47. * Publish received (assured delivery part 2) (Client{@literal <}-{@literal >}Server)
  48. */
  49. public static final byte PUBREL = (byte) 0x62;
  50. /**
  51. * Publish received (assured delivery part 3) (Client{@literal <}-{@literal >}Server)
  52. */
  53. public static final byte PUBCOMP = (byte) 0x70;
  54. /**
  55. * Client subscribe request (Client-{@literal >}Server)
  56. */
  57. public static final byte SUBSCRIBE = (byte) 0x82;
  58. /**
  59. * Subscribe acknowledgement (Client{@literal <}-Server)
  60. */
  61. public static final byte SUBACK = (byte) 0x90;
  62. /**
  63. * Unsubscribe request (Client-{@literal >}Server)
  64. */
  65. public static final byte UNSUBSCRIBE = (byte) 0xA2;
  66. /**
  67. * Unsubscribe acknowledgement (Client{@literal <}-Server)
  68. */
  69. public static final byte UNSUBACK = (byte) 0xB0;
  70. /**
  71. * PING request (Client-{@literal >}Server)
  72. */
  73. public static final byte PINGREQ = (byte) 0xC0;
  74. /**
  75. * PING response (Client{@literal <}-Server)
  76. */
  77. public static final byte PINGRESP = (byte) 0xD0;
  78. /**
  79. * Client is disconnecting (Client-{@literal >}Server)
  80. */
  81. public static final byte DISCONNECT = (byte) 0xE0;
  82. /**
  83. * Reserved - forbidden
  84. */
  85. public static final byte RESERVED2 = (byte) 0xF0;
  86. /*
  87. * Packet Content
  88. */
  89. /**
  90. * First byte of the MQTT:Packet
  91. */
  92. protected byte controlByte;
  93. /**
  94. * Payload message
  95. */
  96. protected String message = "";
  97. /**
  98. * Creates a new MQTT Packet
  99. *
  100. * @param controlType
  101. * specifies the packetType. one of CONNECT, CONNACK, ...
  102. * @param timestamp
  103. * time the packet was sent
  104. * @param source
  105. * source port of the package
  106. * @param destination
  107. * destination port of the packet
  108. */
  109. public MQTT_packet(byte controlType, long timestamp, Port source, Port destination) {
  110. super(timestamp, source, destination);
  111. controlByte = controlType;
  112. }
  113. /**
  114. * Creates a new MQTT Packet
  115. *
  116. * @param controlType
  117. * specifies the packetType. one of CONNECT, CONNACK, ...
  118. * @param timestamp
  119. * time the packet was sent
  120. * @param source
  121. * source port of the package
  122. * @param destination
  123. * destination port of the packet
  124. * @param msg Message of the packet
  125. */
  126. public MQTT_packet(byte controlType, long timestamp, Port source, Port destination, String msg) {
  127. super(timestamp, source, destination);
  128. controlByte = controlType;
  129. message = msg;
  130. }
  131. @Override
  132. public byte[] dumpBytes() {
  133. return "not implemented".getBytes();
  134. }
  135. @Override
  136. public String getTextualRepresentation() {
  137. /**
  138. * Control type of the MQTT Packet as String
  139. */
  140. String packetType;
  141. switch (controlByte) {
  142. case CONNECT:
  143. packetType="CONNECT";
  144. break;
  145. case CONNACK:
  146. packetType="CONNACK";
  147. break;
  148. case PUBLISH:
  149. packetType="PUBLISH";
  150. break;
  151. case PUBACK:
  152. packetType="PUBACK";
  153. break;
  154. case PUBREC:
  155. packetType="PUBREC";
  156. break;
  157. case PUBREL:
  158. packetType="PUBREL";
  159. break;
  160. case PUBCOMP:
  161. packetType="PUBCOMP";
  162. break;
  163. case SUBSCRIBE:
  164. packetType="SUBSCRIBE";
  165. break;
  166. case SUBACK:
  167. packetType="SUBACK";
  168. break;
  169. case UNSUBSCRIBE:
  170. packetType="UNSUBSCRIBE";
  171. break;
  172. case UNSUBACK:
  173. packetType="UNSUBACK";
  174. break;
  175. case PINGREQ:
  176. packetType="PINGREQ";
  177. break;
  178. case PINGRESP:
  179. packetType="PINGRESP";
  180. break;
  181. case DISCONNECT:
  182. packetType="DISCONNECT";
  183. break;
  184. default:
  185. packetType="RESERVED";
  186. break;
  187. }
  188. return "[MQTT: "+packetType+"; time:"+timestamp+"; source:"+source.getOwner().getName()+":"+source.getPortNumber()+"; destination:"+destination.getOwner().getName()+":"+destination.getPortNumber()+(message==""?"":"; "+message)+"]";
  189. }
  190. @Override
  191. public String getPayload() {
  192. return "null";
  193. }
  194. @Override
  195. public String getProtocolName() {
  196. return "MQTT";
  197. }
  198. }