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 : /// This class provides shared connection functionality to connection handler implementations.
11 : abstract class MqttConnectionHandler implements IMqttConnectionHandler {
12 : dynamic connection;
13 :
14 : /// Registry of message processors
15 : Map<MqttMessageType, MessageCallbackFunction> messageProcessorRegistry =
16 : new Map<MqttMessageType, MessageCallbackFunction>();
17 :
18 : /// Registry of sent message callbacks
19 : List<MessageCallbackFunction> sentMessageCallbacks =
20 : new List<MessageCallbackFunction>();
21 :
22 : /// Connection state
23 : ConnectionState connectionState = ConnectionState.disconnected;
24 :
25 : /// Use a websocket rather than TCP
26 : bool useWebSocket = false;
27 :
28 : /// If set use a secure connection, note TCP only, not websocket.
29 : bool secure = false;
30 :
31 : /// Trusted certificate file path for use in secure working
32 : String trustedCertPath;
33 :
34 : /// Private key file path
35 : String privateKeyFilePath;
36 :
37 : /// Initializes a new instance of the MqttConnectionHandler class.
38 5 : MqttConnectionHandler();
39 :
40 : /// Connect to the specific Mqtt Connection.
41 : Future connect(String server, int port, MqttConnectMessage message) async {
42 3 : final Completer completer = new Completer();
43 : try {
44 6 : await internalConnect(server, port, message);
45 6 : return completer.complete(this.connectionState);
46 : } catch (ConnectionException) {
47 1 : this.connectionState = ConnectionState.faulted;
48 : rethrow;
49 : }
50 1 : }
51 :
52 : /// Connect to the specific Mqtt Connection.
53 : Future<ConnectionState> internalConnect(String hostname, int port,
54 : MqttConnectMessage message);
55 :
56 : /// Sends a message to the broker through the current connection.
57 : void sendMessage(MqttMessage message) {
58 6 : MqttLogger.log("MqttConnectionHandler::sendMessage - $message");
59 6 : if ((connectionState == ConnectionState.connected) ||
60 6 : (connectionState == ConnectionState.connecting)) {
61 3 : final typed.Uint8Buffer buff = new typed.Uint8Buffer();
62 3 : final MqttByteBuffer stream = new MqttByteBuffer(buff);
63 3 : message.writeTo(stream);
64 3 : stream.seek(0);
65 6 : connection.send(stream);
66 : // Let any registered people know we're doing a message.
67 6 : for (MessageCallbackFunction callback in sentMessageCallbacks) {
68 3 : callback(message);
69 : }
70 : } else {
71 3 : MqttLogger.log("MqttConnectionHandler::sendMessage - not connected");
72 : }
73 : }
74 :
75 : /// Runs the disconnection process to stop communicating with a message broker.
76 : ConnectionState disconnect();
77 :
78 : /// Closes the connection to the Mqtt message broker.
79 : void close() {
80 4 : if (connectionState == ConnectionState.connected) {
81 2 : disconnect();
82 : }
83 : }
84 :
85 : /// Registers for the receipt of messages when they arrive.
86 : void registerForMessage(MqttMessageType msgType,
87 : MessageCallbackFunction callback) {
88 10 : messageProcessorRegistry[msgType] = callback;
89 : }
90 :
91 : /// UnRegisters for the receipt of messages when they arrive.
92 : void unRegisterForMessage(MqttMessageType msgType) {
93 0 : messageProcessorRegistry.remove(msgType);
94 : }
95 :
96 : /// Registers a callback to be called whenever a message is sent.
97 : void registerForAllSentMessages(MessageCallbackFunction sentMsgCallback) {
98 6 : sentMessageCallbacks.add(sentMsgCallback);
99 : }
100 :
101 : /// UnRegisters a callback that is called whenever a message is sent.
102 : void unRegisterForAllSentMessages(MessageCallbackFunction sentMsgCallback) {
103 0 : sentMessageCallbacks.remove(sentMsgCallback);
104 : }
105 :
106 : /// Handles the Message Available event of the connection control for handling non connection messages
107 : void messageAvailable(events.Event<MessageAvailable> event) {
108 : final MessageCallbackFunction callback =
109 18 : messageProcessorRegistry[event.data.message.header.messageType];
110 9 : callback(event.data.message);
111 : }
112 : }
|