mqtt-ccutrer 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: fbcc93715636fd1a6f3491005dccf8c9805c519145145154a9895d5fb3df0670
4
+ data.tar.gz: f978d8185287327b475f69881546dde182fbbd3c9d665253d9625bc48df1f9a9
5
+ SHA512:
6
+ metadata.gz: 53cb56f81cd0eed1d41d6a0c5a7b7fcb88e53f5fcea755cdf544715d38fd698b931c7b5d23bbf161709a0f46fe72ce14a0f0107f2bfe5f59e92225c72d8d0556
7
+ data.tar.gz: 845ec674c88c7be5b7835adeb1d6b52397661f3e02ae775240cb2621058eba72956327ca961c0ce82172a13485faee720d9b0c9c37e99f03643f1918d5d0d203
data/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2009-2013 Nicholas J Humfrey
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/NEWS.md ADDED
@@ -0,0 +1,173 @@
1
+ Ruby MQTT NEWS
2
+ ==============
3
+
4
+ Ruby MQTT Version 1.0.0 (2021-02-19) (CCutrer fork)
5
+ ---------------------------------------------------
6
+ * Increase minimum Ruby version to 2.5
7
+ * Change several methods to take kwargs
8
+ * Remove `get` vs. `get_packet`; just always return packets from get
9
+ * `get` no longer takes an optional topic to subscribe to; just call
10
+ `subscribe`
11
+ * Remove `last_ping_response` since it's not recorded anymore
12
+ * Remove deprecated `remote_host` and `remote_port`
13
+ * `connect` no longer takes an optional client id; just set it beforehand
14
+ * Add `batch_publish` method for improving performance of QoS 1 publishes
15
+ * Add `flush` method to ensure writes have finished, since writes are
16
+ now done in a background thread
17
+ * Add `wait_for_ack` param to `subscribe` and `unsubscribe`
18
+ * Automatic reconnects
19
+ * Re-send Pub, Sub, and Unsub packets that don't get acked, even across
20
+ server disconnects
21
+ * Only send keepalive pings if we haven't received any other packets,
22
+ and measure from the last packet received, instead of the last ping
23
+ sent
24
+ * Improve CPU usage by only checking for timeouts at the time of the
25
+ timeout, instead of polling
26
+
27
+ Ruby MQTT Version 0.5.0 (2017-04-16)
28
+ ------------------------------------
29
+
30
+ * Switched default protocol version to 3.1.1
31
+ * Added support for Server Name Identification (SNI)
32
+ * Fix for unescaping user/password in URI
33
+ * Fix for bug in MQTT::Proxy class
34
+ * Add the ability to ignore retained packets when subscribed.
35
+ * Fix problem of wrong Puback packet ID
36
+ * Don't keepalive ping if disconnected
37
+ * Immediately close socket after failed Connack
38
+ * FakeServer improvements
39
+ * Fix for working with mathn library.
40
+
41
+
42
+ Ruby MQTT Version 0.4.0 (2016-06-27)
43
+ ------------------------------------
44
+
45
+ * Added puback handling for QoS level 1
46
+ * Low-level MQTT-SN packet parsing support
47
+ * Allow certs to be set directly instead of just by file
48
+ * Allow keyphrase for certs to be passed through
49
+ * Put 'disconnect' inside an 'ensure' block
50
+ * Fix for error on publish with frozen payload
51
+ * Fix for packets always getting id 1
52
+ * Improvements to tests
53
+
54
+
55
+ Ruby MQTT Version 0.3.1 (2014-10-10)
56
+ ------------------------------------
57
+
58
+ * Added ```last_ping_response``` to attribute to ```MQTT::Client```
59
+
60
+
61
+ Ruby MQTT Version 0.3.0 (2014-08-26)
62
+ ------------------------------------
63
+
64
+ * Added support for MQTT protocol version 3.1.1
65
+ * Renamed a number of methods/attributes:
66
+ - Renamed ```:granted_qos``` to ```:return_codes```
67
+ - Renamed ```:remote_port``` to ```:port```
68
+ - Renamed ```:remote_host``` to ```:host```
69
+ - Renamed ```:message_id``` to ```:id```
70
+ - Renamed ```:protocol_version``` to ```:protocol_level```
71
+ - Renamed ```MQTT_BROKER``` environment variable to ```MQTT_SERVER```
72
+ * Added more checks to ensure that the 3.1.1 protocol specs are adhered to
73
+ * Added a Library Overview section to the README
74
+ * Added links to the protocol specification to README
75
+ * Improvements to the YARD API documentation
76
+ * Don't display payload in inspect if it contains non-visible ASCII characters
77
+ * Upgraded to rspec 3
78
+ * Various minor bug fixes and corrections
79
+
80
+
81
+ Ruby MQTT Version 0.2.0 (2014-04-02)
82
+ ------------------------------------
83
+
84
+ * Added SSL/TLS support
85
+ * Added support for passing connection details using a URI
86
+ * Added support for using the ```MQTT_BROKER``` environment variable
87
+ * Allow passing array of topics to Client#unsubscribe
88
+ * Allow more combinations of arguments to be passed to a new Client
89
+ * No longer defaults to ‘localhost’ if there is no server configured
90
+ * Fixed more 'unused variable' warnings
91
+ * Documentation improvements
92
+ * Ruby 1.8 fixes
93
+ * Ruby 2 fixes
94
+
95
+
96
+ Ruby MQTT Version 0.1.0 (2013-09-07)
97
+ ------------------------------------
98
+
99
+ * Changed license to MIT, to simplify licensing concerns
100
+ * Improvements for UTF-8 handling under Ruby 1.9
101
+ * Added ```get_packet``` method
102
+ * Added support for a keep-alive value of 0
103
+ * Added a #inspect method to the Packet classes
104
+ * Added checks for the protocol name and version
105
+ * Added check to ensure that packet body isn't too big
106
+ * Added validation of QoS value
107
+ * Added example of using authentication
108
+ * Fixed 'unused variable' warnings
109
+ * Reduced duplicated code in packet parsing
110
+ * Improved testing
111
+ - Created fake server and integration tests
112
+ - Better test coverage
113
+ - Added more tests for error states
114
+
115
+
116
+ Ruby MQTT Version 0.0.9 (2012-12-21)
117
+ ------------------------------------
118
+
119
+ * Fixes for Ruby 1.9.3 by Mike English
120
+ * Fix for ```client_id``` typo by Anubisss
121
+ * Added methods to inspect the incoming message queue: ```queue_empty?``` and ```queue_length```
122
+ * Fixed incorrect implementation of the parsing and serialising of Subscription Acknowledgement packets
123
+ * Changed test mocking from Mocha to rspec-mocks
124
+
125
+
126
+ Ruby MQTT Version 0.0.8 (2011-02-04)
127
+ ------------------------------------
128
+
129
+ * Implemented Last Will and Testament feature
130
+ * Renamed dup attribute to duplicate to avoid method name clash
131
+ * Made the random ```client_id``` generator a public class method
132
+
133
+
134
+ Ruby MQTT Version 0.0.7 (2011-01-19)
135
+ ------------------------------------
136
+
137
+ * You can now pass a topic and block to client.get
138
+ * Added MQTT::Client.connect class method
139
+
140
+
141
+ Ruby MQTT Version 0.0.5 (2011-01-18)
142
+ ------------------------------------
143
+
144
+ * Implemented setting username and password (MQTT 3.1)
145
+ * Renamed ```clean_start``` to ``clean_session```
146
+ * Started using autoload to load classes
147
+ * Modernised Gem building mechanisms
148
+
149
+
150
+ Ruby MQTT Version 0.0.4 (2009-02-22)
151
+ ------------------------------------
152
+
153
+ * Re-factored packet encoding/decoding into one class per packet type
154
+ * Added MQTT::Proxy class for implementing an MQTT proxy
155
+
156
+
157
+ Ruby MQTT Version 0.0.3 (2009-02-08)
158
+ ------------------------------------
159
+
160
+ * Added checking of Connection Acknowledgement
161
+ * Automatic client identifier generation
162
+
163
+
164
+ Ruby MQTT Version 0.0.2 (2009-02-03)
165
+ ------------------------------------
166
+
167
+ * Added support for packets longer than 127 bytes
168
+
169
+
170
+ Ruby MQTT Version 0.0.1 (2009-02-01)
171
+ ------------------------------------
172
+
173
+ * Initial Release
data/README.md ADDED
@@ -0,0 +1,218 @@
1
+ [![Build Status](https://travis-ci.org/njh/ruby-mqtt.svg)](https://travis-ci.org/ccutrer/ruby-mqtt)
2
+
3
+ ruby-mqtt
4
+ =========
5
+
6
+ Pure Ruby gem that implements the [MQTT] protocol, a lightweight protocol for publish/subscribe messaging.
7
+
8
+ Also includes a class for parsing and generating [MQTT-SN] packets.
9
+
10
+
11
+ Table of Contents
12
+ -----------------
13
+ * [Installation](#installation)
14
+ * [Quick Start](#quick-start)
15
+ * [Library Overview](#library-overview)
16
+ * [Resources](#resources)
17
+ * [License](#license)
18
+ * [Contact](#contact)
19
+
20
+
21
+ Installation
22
+ ------------
23
+
24
+ You may get the latest stable version from [Rubygems]:
25
+
26
+ $ gem install mqtt-ccutrer
27
+
28
+ Quick Start
29
+ -----------
30
+
31
+ ~~~ ruby
32
+ require 'mqtt'
33
+
34
+ # Publish example
35
+ MQTT::Client.connect('test.mosquitto.org') do |c|
36
+ c.publish('test', 'message')
37
+ end
38
+
39
+ # Subscribe example
40
+ MQTT::Client.connect('test.mosquitto.org') do |c|
41
+ # If you pass a block to the get method, then it will loop
42
+ c.subscribe('test')
43
+ c.get do |packet|
44
+ puts "#{packet.topic}: #{packet.payload}"
45
+ end
46
+ end
47
+ ~~~
48
+
49
+
50
+ Library Overview
51
+ ----------------
52
+
53
+ ### Connecting ###
54
+
55
+ A new client connection can be created by passing either a [MQTT URI], a host and port or by passing a hash of attributes.
56
+
57
+ ~~~ ruby
58
+ client = MQTT::Client.connect('mqtt://myserver.example.com')
59
+ client = MQTT::Client.connect('mqtts://user:pass@myserver.example.com')
60
+ client = MQTT::Client.connect('myserver.example.com')
61
+ client = MQTT::Client.connect('myserver.example.com', 18830)
62
+ client = MQTT::Client.connect(host: 'myserver.example.com', port: 1883 ... )
63
+ ~~~
64
+
65
+ TLS/SSL is not enabled by default, to enabled it, pass `ssl: true`:
66
+
67
+ ~~~ ruby
68
+ client = MQTT::Client.connect(
69
+ host: 'test.mosquitto.org',
70
+ port: 8883,
71
+ ssl: true
72
+ )
73
+ ~~~
74
+
75
+ Alternatively you can create a new Client object and then configure it by setting attributes. This example shows setting up client certificate based authentication:
76
+
77
+ ~~~ ruby
78
+ client = MQTT::Client.new
79
+ client.host = 'myserver.example.com'
80
+ client.ssl = true
81
+ client.cert_file = path_to('client.pem')
82
+ client.key_file = path_to('client.key')
83
+ client.ca_file = path_to('root-ca.pem')
84
+ client.connect
85
+ ~~~
86
+
87
+ The connection can either be made without the use of a block:
88
+
89
+ ~~~ ruby
90
+ client = MQTT::Client.connect('test.mosquitto.org')
91
+ # perform operations
92
+ client.disconnect
93
+ ~~~
94
+
95
+ Or, if using a block, with an implicit disconnection at the end of the block.
96
+
97
+ ~~~ ruby
98
+ MQTT::Client.connect('test.mosquitto.org') do |client|
99
+ # perform operations
100
+ end
101
+ ~~~
102
+
103
+ For more information, see the list of attributes for the [MQTT::Client] class and the [MQTT::Client.connect] method.
104
+
105
+
106
+ ### Publishing ###
107
+
108
+ To send a message to a topic, use the ```publish``` method:
109
+
110
+ ~~~ ruby
111
+ client.publish(topic, payload, retain: false, qos: 0)
112
+ ~~~
113
+
114
+ The method will return once the message has been sent to the MQTT server for QoS 0,
115
+ or once an Ack has been received from the server for QoS 1.
116
+
117
+ For more information see the [MQTT::Client#publish] method.
118
+
119
+
120
+ ### Subscribing ###
121
+
122
+ You can send a subscription request to the MQTT server using the subscribe method. One or more [Topic Filters] may be passed in:
123
+
124
+ ~~~ ruby
125
+ client.subscribe( 'topic1' )
126
+ client.subscribe( 'topic1', 'topic2' )
127
+ client.subscribe( 'foo/#' )
128
+ ~~~
129
+
130
+ For more information see the [MQTT::Client#subscribe] method.
131
+
132
+
133
+ ### Receiving Messages ###
134
+
135
+ To receive a message, use the get method. This method will block until a message is available. You can access details
136
+ about the packet such as if it was retained, the topic it was sent to, and the payload.
137
+
138
+ ~~~ ruby
139
+ packet = client.get
140
+ packet.retained?
141
+ packet.topic
142
+ packet.payload
143
+ ~~~
144
+
145
+ Alternatively, you can give the get method a block, which will be called for every message received and loop forever:
146
+
147
+ ~~~ ruby
148
+ client.get do |packet|
149
+ # Block is executed for every message received
150
+ end
151
+ ~~~
152
+
153
+ For more information see the [MQTT::Client#get] method.
154
+
155
+
156
+ ### Parsing and serialising of packets ###
157
+
158
+ The parsing and serialising of MQTT and MQTT-SN packets is a separate lower-level API.
159
+ You can use it to build your own clients and servers, without using any of the rest of the
160
+ code in this gem.
161
+
162
+ ~~~ ruby
163
+ # Parse a string containing a binary packet into an object
164
+ packet_obj = MQTT::Packet.parse(binary_packet)
165
+
166
+ # Write a PUBACK packet to an IO handle
167
+ ios << MQTT::Packet::Puback(id: 20)
168
+
169
+ # Write an MQTT-SN Publish packet with QoS -1 to a UDP socket
170
+ socket = UDPSocket.new
171
+ socket.connect('localhost', MQTT::SN::DEFAULT_PORT)
172
+ socket << MQTT::SN::Packet::Publish.new(
173
+ topic_id: 'TT',
174
+ topic_id_type: :short,
175
+ data: "The time is: #{Time.now}",
176
+ qos: -1
177
+ )
178
+ socket.close
179
+ ~~~
180
+
181
+ Limitations
182
+ -----------
183
+
184
+ * QoS 2 is not currently supported by client
185
+ * Pending publishes are only persisted as long as a reconnect occurs within
186
+ the configured timeout
187
+
188
+ Resources
189
+ ---------
190
+
191
+ * API Documentation: http://rubydoc.info/gems/mqtt
192
+ * Protocol Specification v3.1.1: http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/mqtt-v3.1.1.html
193
+ * Protocol Specification v3.1: http://public.dhe.ibm.com/software/dw/webservices/ws-mqtt/mqtt-v3r1.html
194
+ * MQTT-SN Protocol Specification v1.2: http://mqtt.org/new/wp-content/uploads/2009/06/MQTT-SN_spec_v1.2.pdf
195
+ * MQTT Homepage: http://www.mqtt.org/
196
+ * GitHub Project: http://github.com/njh/ruby-mqtt
197
+
198
+
199
+ License
200
+ -------
201
+
202
+ The mqtt ruby gem is licensed under the terms of the MIT license.
203
+ See the file LICENSE for details.
204
+
205
+
206
+ [MQTT]: http://www.mqtt.org/
207
+ [MQTT-SN]: http://mqtt.org/2013/12/mqtt-for-sensor-networks-mqtt-sn
208
+ [Rubygems]: http://rubygems.org/
209
+ [Bundler]: http://bundler.io/
210
+ [MQTT URI]: https://github.com/mqtt/mqtt.github.io/wiki/URI-Scheme
211
+ [Topic Filters]: http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/mqtt-v3.1.1.html#_Toc388534397
212
+
213
+ [MQTT::Client]: http://rubydoc.info/gems/mqtt-ccutrer/MQTT/Client#instance_attr_details
214
+ [MQTT::Client.connect]: http://rubydoc.info/gems/mqtt-ccutrer/MQTT/Client.connect
215
+ [MQTT::Client#publish]: http://rubydoc.info/gems/mqtt-ccutrer/MQTT/Client:publish
216
+ [MQTT::Client#subscribe]: http://rubydoc.info/gems/mqtt-ccutrer/MQTT/Client:subscribe
217
+ [MQTT::Client#get]: http://rubydoc.info/gems/mqtt-ccutrer/MQTT/Client:get
218
+
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'mqtt'
data/lib/mqtt.rb ADDED
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'logger'
4
+ require 'socket'
5
+ require 'timeout'
6
+
7
+ require 'mqtt/version'
8
+
9
+ module MQTT
10
+ # Default port number for unencrypted connections
11
+ DEFAULT_PORT = 1883
12
+
13
+ # Default port number for TLS/SSL encrypted connections
14
+ DEFAULT_SSL_PORT = 8883
15
+
16
+ # Super-class for other MQTT related exceptions
17
+ class Exception < RuntimeError
18
+ end
19
+
20
+ # A ProtocolException will be raised if there is a
21
+ # problem with data received from a remote host
22
+ class ProtocolException < Exception
23
+ end
24
+
25
+ class KeepAliveTimeout < ProtocolException
26
+ end
27
+
28
+ # A NotConnectedException will be raised when trying to
29
+ # perform a function but no connection has been
30
+ # established
31
+ class NotConnectedException < Exception
32
+ end
33
+
34
+ # A ConnectionClosedException will be raised when the
35
+ # connection has been closed while waiting for an operation
36
+ # to complete
37
+ class ConnectionClosedException < Exception
38
+ end
39
+
40
+ # A ResendLimitExceededException will be raised when a packet
41
+ # has timed out without an ack too many times
42
+ class ResendLimitExceededException < Exception
43
+ end
44
+
45
+ autoload :Client, 'mqtt/client'
46
+ autoload :Packet, 'mqtt/packet'
47
+ autoload :Proxy, 'mqtt/proxy'
48
+
49
+ # MQTT-SN
50
+ module SN
51
+ # Default port number for unencrypted connections
52
+ DEFAULT_PORT = 1883
53
+
54
+ # A ProtocolException will be raised if there is a
55
+ # problem with data received from a remote host
56
+ class ProtocolException < MQTT::Exception
57
+ end
58
+
59
+ autoload :Packet, 'mqtt/sn/packet'
60
+ end
61
+ end