mqtt 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/NEWS CHANGED
@@ -1,5 +1,9 @@
1
1
  = Ruby MQTT NEWS
2
2
 
3
- == Ruby MQTT Version 0.0.1 (2009-01-??)
3
+ == Ruby MQTT Version 0.0.2 (2009-02-03)
4
+
5
+ Added support for packets longer than 127 bytes.
6
+
7
+ == Ruby MQTT Version 0.0.1 (2009-02-01)
4
8
 
5
9
  Initial Release.
data/README CHANGED
@@ -22,8 +22,8 @@ You may get the latest stable version from Rubyforge. Source gems are also avail
22
22
 
23
23
  == TODO
24
24
 
25
- * Support payloads longer than 128 bytes
26
25
  * Process acknowledgement packets
26
+ * Automatic uninque client identifier if none given
27
27
  * Create classes for each type of packet?
28
28
  * More validations of data/parameters
29
29
  * Implement exception throwing
data/Rakefile CHANGED
@@ -8,7 +8,7 @@ require 'spec/rake/verify_rcov'
8
8
 
9
9
 
10
10
  NAME = "mqtt"
11
- VERS = "0.0.1"
11
+ VERS = "0.0.2"
12
12
  CLEAN.include ['pkg', 'rdoc']
13
13
 
14
14
  spec = Gem::Specification.new do |s|
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $:.unshift File.dirname(__FILE__)+'/../lib'
4
+
5
+ require 'mqtt'
6
+
7
+
8
+ client = MQTT::Client.new('hadrian.aelius.com')
9
+ client.connect('livetextclient')
10
+
11
+ client.subscribe('livetext/#')
12
+
13
+ loop do
14
+ topic,message = client.get
15
+ puts "#{topic}: #{message}"
16
+ end
17
+
18
+ client.disconnect
@@ -8,7 +8,7 @@ require 'mqtt'
8
8
  client = MQTT::Client.new('mqtt.example.com')
9
9
  client.connect('simple_get')
10
10
 
11
- client.subscribe('$SYS/#')
11
+ client.subscribe('test')
12
12
 
13
13
  loop do
14
14
  topic,message = client.get
@@ -7,6 +7,6 @@ require 'mqtt'
7
7
  client = MQTT::Client.new('mqtt.example.com')
8
8
  client.connect('simple_publish_example') do |c|
9
9
 
10
- c.publish('test', "The time is: #{Time.now}")
10
+ c.publish('test', 'x' * 300)
11
11
 
12
12
  end
data/lib/mqtt/packet.rb CHANGED
@@ -6,7 +6,7 @@ module MQTT
6
6
 
7
7
  # Class representing a MQTT Packet
8
8
  # Performs binary encoding and decoding of headers
9
- class Packet #:nodoc: all
9
+ class Packet
10
10
  attr_reader :type # The packet type
11
11
  attr_reader :dup # Duplicate delivery flag
12
12
  attr_reader :retain # Retain flag
@@ -14,20 +14,29 @@ module MQTT
14
14
  attr_reader :body # Packet's body (everything after fixed header)
15
15
 
16
16
  # Read in a packet from a socket
17
- def self.read(sock)
18
- header = sock.read(2)
19
- raise MQTT::ProtocolException if header.nil?
20
- byte1,byte2 = header.unpack('C*')
21
-
22
- # FIXME: support decoding of multi-byte length header
17
+ def self.read(socket)
23
18
 
19
+ # Create a packet object
20
+ header = read_byte(socket)
24
21
  packet = MQTT::Packet.new(
25
- :type => ((byte1 & 0xF0) >> 4),
26
- :dup => ((byte1 & 0x08) >> 3),
27
- :qos => ((byte1 & 0x06) >> 1),
28
- :retain => ((byte1 & 0x01) >> 0)
22
+ :type => ((header & 0xF0) >> 4),
23
+ :dup => ((header & 0x08) >> 3),
24
+ :qos => ((header & 0x06) >> 1),
25
+ :retain => ((header & 0x01) >> 0)
29
26
  )
30
- packet.body = sock.read(byte2)
27
+
28
+ # Read in the packet length
29
+ multiplier = 1
30
+ body_len = 0
31
+ begin
32
+ digit = read_byte(socket)
33
+ body_len += ((digit & 0x7F) * multiplier)
34
+ multiplier *= 0x80
35
+ end while ((digit & 0x80) != 0x00)
36
+ # FIXME: only allow 4 bytes?
37
+
38
+ # Read in the packet body
39
+ packet.body = socket.read(body_len)
31
40
 
32
41
  return packet
33
42
  end
@@ -41,6 +50,10 @@ module MQTT
41
50
  self.body = args[:body] || ''
42
51
  end
43
52
 
53
+ # Set the packet type
54
+ # Can either by the packet type id (integer)
55
+ # Or the packet type as a symbol/string
56
+ # See the MQTT module for an enumeration of packet types.
44
57
  def type=(arg)
45
58
  if arg.kind_of?(Integer)
46
59
  # Convert type identifier to symbol
@@ -59,6 +72,7 @@ module MQTT
59
72
  return index
60
73
  end
61
74
 
75
+ # Set the dup flag (true/false)
62
76
  def dup=(arg)
63
77
  if arg.kind_of?(Integer)
64
78
  @dup = (arg != 0 ? true : false)
@@ -67,6 +81,7 @@ module MQTT
67
81
  end
68
82
  end
69
83
 
84
+ # Set the retain flag (true/false)
70
85
  def retain=(arg)
71
86
  if arg.kind_of?(Integer)
72
87
  @retain = (arg != 0 ? true : false)
@@ -75,11 +90,14 @@ module MQTT
75
90
  end
76
91
  end
77
92
 
93
+ # Set the Quality of Service level (0/1/2)
78
94
  def qos=(arg)
79
95
  @qos = arg.to_i
80
96
  end
81
97
 
98
+ # Set (replace) the packet body
82
99
  def body=(arg)
100
+ # FIXME: only allow 268435455 bytes?
83
101
  @body = arg.to_s
84
102
  end
85
103
 
@@ -137,15 +155,25 @@ module MQTT
137
155
 
138
156
  # Serialise the packet
139
157
  def to_s
140
- # Encode the 2-byte fixed header
158
+ # Encode the fixed header
141
159
  header = [
142
160
  ((type_id.to_i & 0x0F) << 4) |
143
161
  ((dup ? 0x1 : 0x0) << 3) |
144
162
  ((qos.to_i & 0x03) << 1) |
145
- (retain ? 0x1 : 0x0),
146
- (@body.length & 0x7F)
163
+ (retain ? 0x1 : 0x0)
147
164
  ]
148
- # FIXME: support multi-byte length header
165
+
166
+ # Build up the body length field bytes
167
+ body_size = @body.size
168
+ begin
169
+ digit = (body_size % 128)
170
+ body_size = (body_size / 128)
171
+ # if there are more digits to encode, set the top bit of this digit
172
+ digit |= 0x80 if (body_size > 0)
173
+ header.push(digit)
174
+ end while (body_size > 0)
175
+
176
+ # Convert header to binary and add on body
149
177
  header.pack('C*') + @body
150
178
  end
151
179
 
@@ -155,6 +183,15 @@ module MQTT
155
183
  "qos=#{@qos}, body.size=#{@body.size}>"
156
184
  end
157
185
 
186
+ private
187
+
188
+ # Read and unpack a single byte from socket
189
+ def self.read_byte(socket)
190
+ byte = socket.read(1)
191
+ raise MQTT::ProtocolException if byte.nil?
192
+ byte.unpack('C').first
193
+ end
194
+
158
195
  end
159
196
 
160
197
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mqtt
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nicholas J Humfrey
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-02-01 00:00:00 +00:00
12
+ date: 2009-02-03 00:00:00 +00:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -28,6 +28,7 @@ files:
28
28
  - lib/mqtt.rb
29
29
  - lib/mqtt/client.rb
30
30
  - lib/mqtt/packet.rb
31
+ - examples/livetext.rb
31
32
  - examples/simple_get.rb
32
33
  - examples/simple_publish.rb
33
34
  - README