Line data Source code
1 : /*
2 : * Package : mqtt_client
3 : * Author : S. Hamblett <steve.hamblett@linux.com>
4 : * Date : 22/06/2017
5 : * Copyright : S.Hamblett
6 : */
7 :
8 : part of mqtt_client;
9 :
10 : /// State and logic used to read from the underlying network stream.
11 : class ReadWrapper {
12 : /// Creates a new ReadWrapper that wraps the state used to read a message from a stream.
13 3 : ReadWrapper() {
14 6 : this.messageBytes = new List<int>();
15 : }
16 :
17 : /// The bytes associated with the message being read.
18 : List<int> messageBytes;
19 : }
20 :
21 : /// The MQTT connection base class
22 : class MqttConnection extends Object with events.EventEmitter {
23 : /// The socket that maintains the connection to the MQTT broker.
24 : dynamic client;
25 :
26 : /// The read wrapper
27 : ReadWrapper readWrapper;
28 :
29 : /// Indicates if disconnect(onDone) has been requested or not
30 : bool disconnectRequested = false;
31 :
32 : /// Default constructor
33 3 : MqttConnection();
34 :
35 : /// Initializes a new instance of the MqttConnection class.
36 0 : MqttConnection.fromConnect(String server, int port) {
37 0 : connect(server, port);
38 : }
39 :
40 : /// Connect, must be overridden in connection classes
41 : Future connect(String server, int port) {
42 0 : final Completer completer = new Completer();
43 0 : return completer.future;
44 : }
45 :
46 : /// Create the listening stream subscription and subscribe the callbacks
47 : void _startListening() {
48 3 : MqttLogger.log("MqttConnection::_startListening");
49 15 : client.listen(_onData, onError: _onError, onDone: _onDone);
50 : }
51 :
52 : /// OnData listener callback
53 : void _onData(List<int> data) {
54 3 : MqttLogger.log("MqttConnection::_onData");
55 : // Protect against 0 bytes but should never happen.
56 6 : if (data.length == 0) {
57 : return;
58 : }
59 9 : readWrapper.messageBytes.addAll(data);
60 : // Attempt to create a message, if this works we have a full message
61 : // if not add the bytes to the read wrapper and wait for more bytes.
62 : bool messageIsValid = true;
63 : MqttMessage msg;
64 : try {
65 3 : final MqttByteBuffer messageStream = new MqttByteBuffer.fromList(data);
66 3 : msg = MqttMessage.createFrom(messageStream);
67 : } catch (exception) {
68 0 : MqttLogger.log("MqttConnection::_ondata - message is not valid");
69 : messageIsValid = false;
70 : }
71 : if (messageIsValid) {
72 6 : MqttLogger.log("MqttConnection::_onData - message received $msg");
73 6 : emitEvent(new MessageAvailable(msg));
74 3 : MqttLogger.log("MqttConnection::_onData - message processed");
75 : }
76 : }
77 :
78 : /// OnError listener callback
79 : void _onError(error) {
80 2 : _disconnect();
81 : }
82 :
83 : /// OnDone listener callback
84 : void _onDone() {
85 : // We should never be done unless requested
86 0 : _disconnect();
87 0 : if (!disconnectRequested) {
88 0 : throw new SocketException(
89 : "MqttConnection::On Done called by broker, disconnecting.");
90 : }
91 : }
92 :
93 : /// Disconnects from the message broker
94 : void _disconnect() {
95 2 : if (client != null) {
96 0 : client.close();
97 : }
98 : }
99 :
100 : /// Sends the message in the stream to the broker.
101 : void send(MqttByteBuffer message) {
102 6 : final typed.Uint8Buffer messageBytes = message.read(message.length);
103 9 : client.add(messageBytes.toList());
104 : }
105 :
106 : // User requested disconnection
107 : void disconnect() {
108 0 : disconnectRequested = true;
109 0 : _onDone();
110 : }
111 : }
|