Line data Source code
1 : /*
2 : * Package : mqtt_client
3 : * Author : S. Hamblett <steve.hamblett@linux.com>
4 : * Date : 02/10/2017
5 : * Copyright : S.Hamblett
6 : */
7 :
8 : part of mqtt_client;
9 :
10 : /// The MQTT secure connection class
11 : class MqttSecureConnection extends MqttConnection {
12 : /// Trusted certificate file path for use in secure working
13 : String trustedCertPath;
14 :
15 : /// Private key file path
16 : String privateKeyFilePath;
17 :
18 : /// Default constructor
19 1 : MqttSecureConnection(this.trustedCertPath, this.privateKeyFilePath);
20 :
21 : /// Initializes a new instance of the MqttSecureConnection class.
22 0 : MqttSecureConnection.fromConnect(String server, int port) {
23 0 : connect(server, port);
24 : }
25 :
26 : /// Connect - overridden
27 : Future connect(String server, int port) {
28 1 : final Completer completer = new Completer();
29 1 : MqttLogger.log("MqttSecureConnection::connect");
30 : try {
31 : // Connect and save the socket.
32 1 : final SecurityContext context = SecurityContext.defaultContext;
33 1 : if (trustedCertPath != null) {
34 1 : MqttLogger.log(
35 2 : "MqttSecureConnection::connect - trusted cert path is $trustedCertPath");
36 2 : context.setTrustedCertificates(trustedCertPath);
37 : }
38 1 : if (privateKeyFilePath != null) {
39 0 : MqttLogger.log(
40 0 : "MqttSecureConnection::connect - private key file path is $privateKeyFilePath");
41 0 : context.usePrivateKey(privateKeyFilePath);
42 : }
43 2 : SecureSocket.connect(server, port).then((SecureSocket socket) {
44 1 : MqttLogger.log("MqttSecureConnection::connect - securing socket");
45 1 : client = socket;
46 2 : readWrapper = new ReadWrapper();
47 1 : MqttLogger.log("MqttSecureConnection::connect - start listening");
48 1 : _startListening();
49 1 : return completer.complete();
50 2 : }).catchError((e) => _onError(e));
51 0 : } on SocketException catch (e) {
52 : final String message =
53 : "MqttSecureConnection::The connection to the message broker {$server}:{$port} could not be made. Error is ${e
54 0 : .toString()}";
55 0 : throw new NoConnectionException(message);
56 0 : } on HandshakeException catch (e) {
57 : final String message =
58 : "MqttSecureConnection::Handshake exception to the message broker {$server}:{$port}. Error is ${e
59 0 : .toString()}";
60 0 : throw new NoConnectionException(message);
61 : }
62 1 : return completer.future;
63 : }
64 : }
|