LCOV - code coverage report
Current view: top level - src/messages/subscribe - mqtt_client_mqtt_subscribe_payload.dart (source / functions) Hit Total Coverage
Test: coverage.lcov Lines: 26 26 100.0 %
Date: 2017-10-09 Functions: 0 0 -

          Line data    Source code
       1             : /*
       2             :  * Package : mqtt_client
       3             :  * Author : S. Hamblett <steve.hamblett@linux.com>
       4             :  * Date   : 19/06/2017
       5             :  * Copyright :  S.Hamblett
       6             :  */
       7             : 
       8             : part of mqtt_client;
       9             : 
      10             : /// Class that contains details related to an MQTT Subscribe messages payload
      11             : class MqttSubscribePayload extends MqttPayload {
      12             :   MqttVariableHeader variableHeader;
      13             :   MqttHeader header;
      14             : 
      15             :   /// The collection of subscriptions, Key is the topic, Value is the qos
      16             :   Map<String, MqttQos> subscriptions = new Map<String, MqttQos>();
      17             : 
      18             :   /// Initializes a new instance of the MqttSubscribePayload class.
      19           2 :   MqttSubscribePayload();
      20             : 
      21             :   /// Initializes a new instance of the MqttSubscribePayload class.
      22             :   MqttSubscribePayload.fromByteBuffer(MqttHeader header,
      23             :       MqttSubscribeVariableHeader variableHeader,
      24           1 :       MqttByteBuffer payloadStream) {
      25           1 :     this.header = header;
      26           1 :     this.variableHeader = variableHeader;
      27           1 :     readFrom(payloadStream);
      28             :   }
      29             : 
      30             :   /// Writes the payload to the supplied stream.
      31             :   void writeTo(MqttByteBuffer payloadStream) {
      32           2 :     subscriptions.forEach((String key, MqttQos value) {
      33           1 :       payloadStream.writeMqttStringM(key);
      34           2 :       payloadStream.writeByte(value.index);
      35             :     });
      36             :   }
      37             : 
      38             :   /// Creates a payload from the specified header stream.
      39             :   void readFrom(MqttByteBuffer payloadStream) {
      40             :     int payloadBytesRead = 0;
      41           5 :     final int payloadLength = header.messageSize - variableHeader.length;
      42             :     // Read all the topics and qos subscriptions from the message payload
      43           1 :     while (payloadBytesRead < payloadLength) {
      44           1 :       final String topic = payloadStream.readMqttStringM();
      45           2 :       final MqttQos qos = MqttQos.values[payloadStream.readByte()];
      46           1 :       payloadBytesRead +=
      47           2 :           topic.length + 3; // +3 = Mqtt string length bytes + qos byte
      48           1 :       addSubscription(topic, qos);
      49             :     }
      50             :   }
      51             : 
      52             :   /// Gets the length of the payload in bytes when written to a stream.
      53             :   int getWriteLength() {
      54             :     int length = 0;
      55           1 :     final MqttEncoding enc = new MqttEncoding();
      56           2 :     subscriptions.forEach((String key, MqttQos value) {
      57           2 :       length += enc.getByteCount(key);
      58           1 :       length += 1;
      59             :     });
      60             :     return length;
      61             :   }
      62             : 
      63             :   /// Adds a new subscription to the collection of subscriptions.
      64             :   void addSubscription(String topic, MqttQos qos) {
      65           4 :     subscriptions[topic] = qos;
      66             :   }
      67             : 
      68             :   /// Clears the subscriptions.
      69             :   void clearSubscriptions() {
      70           2 :     subscriptions.clear();
      71             :   }
      72             : 
      73             :   String toString() {
      74           2 :     final StringBuffer sb = new StringBuffer();
      75           8 :     sb.writeln("Payload: Subscription [{${subscriptions.length}}]");
      76           4 :     subscriptions.forEach((String key, MqttQos value) {
      77           4 :       sb.writeln("{{ Topic={$key}, Qos={$value} }}");
      78             :     });
      79           2 :     return sb.toString();
      80             :   }
      81             : }

Generated by: LCOV version 1.10