em-mqtt-sn 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.
- checksums.yaml +7 -0
- data/LICENSE.md +22 -0
- data/NEWS.md +14 -0
- data/README.md +54 -0
- data/bin/em-mqtt-sn-gateway +6 -0
- data/lib/em/mqtt-sn.rb +25 -0
- data/lib/em/mqtt-sn/gateway.rb +81 -0
- data/lib/em/mqtt-sn/gateway_handler.rb +216 -0
- data/lib/em/mqtt-sn/packet.rb +348 -0
- data/lib/em/mqtt-sn/server_connection.rb +77 -0
- data/lib/em/mqtt-sn/version.rb +5 -0
- data/spec/em_mqtt-sn_packet_spec.rb +660 -0
- data/spec/em_mqtt-sn_version_spec.rb +21 -0
- metadata +171 -0
@@ -0,0 +1,77 @@
|
|
1
|
+
|
2
|
+
class EventMachine::MQTTSN::ServerConnection < EventMachine::MQTT::Connection
|
3
|
+
attr_accessor :gateway_handler
|
4
|
+
attr_accessor :client_address
|
5
|
+
attr_accessor :client_port
|
6
|
+
attr_accessor :client_id
|
7
|
+
attr_accessor :pending_requests
|
8
|
+
|
9
|
+
def initialize(gateway_handler, client_address, client_port)
|
10
|
+
@client_address = client_address
|
11
|
+
@client_port = client_port
|
12
|
+
@gateway_handler = gateway_handler
|
13
|
+
@topic_id = 0
|
14
|
+
@topic_map = {}
|
15
|
+
@pending_requests = {}
|
16
|
+
end
|
17
|
+
|
18
|
+
# TCP connection to server has closed
|
19
|
+
def unbind
|
20
|
+
@gateway_handler.disconnect(self)
|
21
|
+
end
|
22
|
+
|
23
|
+
# Incoming packet from server has been received
|
24
|
+
def process_packet(packet)
|
25
|
+
if packet.class == MQTT::Packet::Connack and packet.return_code == 0
|
26
|
+
@state = :connected
|
27
|
+
end
|
28
|
+
|
29
|
+
@gateway_handler.relay_from_server(self, packet)
|
30
|
+
end
|
31
|
+
|
32
|
+
# Get the topic ID for a topic name
|
33
|
+
def get_topic_id(name)
|
34
|
+
if name.length == 2
|
35
|
+
return :short, name
|
36
|
+
else
|
37
|
+
# FIXME: improve this
|
38
|
+
@topic_map.each_pair do |key,value|
|
39
|
+
if value == name
|
40
|
+
return :normal, key
|
41
|
+
end
|
42
|
+
end
|
43
|
+
@topic_id += 1
|
44
|
+
@topic_map[@topic_id] = name
|
45
|
+
return :normal, @topic_id
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
# Get the topic name for a topic ID
|
50
|
+
def get_topic_name(id)
|
51
|
+
@topic_map[id]
|
52
|
+
end
|
53
|
+
|
54
|
+
# Add a packet to a list of messages that we are expecting a reply to
|
55
|
+
def add_to_pending(packet)
|
56
|
+
@pending_requests[packet.id] = {
|
57
|
+
:packet => packet,
|
58
|
+
:time => Time.now
|
59
|
+
}
|
60
|
+
end
|
61
|
+
|
62
|
+
# Remove a packet that we have now received a reply to
|
63
|
+
def remove_from_pending(id)
|
64
|
+
if @pending_requests.has_key?(id)
|
65
|
+
request = @pending_requests[id]
|
66
|
+
@pending_requests.delete(id)
|
67
|
+
return request[:packet]
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
# Politely close the connection to the MQTT server
|
72
|
+
def disconnect
|
73
|
+
send_packet(MQTT::Packet::Disconnect.new)
|
74
|
+
@state = :disconnected
|
75
|
+
close_connection_after_writing
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,660 @@
|
|
1
|
+
$:.unshift(File.dirname(__FILE__))
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe EventMachine::MQTTSN::Packet do
|
6
|
+
|
7
|
+
describe "when creating a new packet" do
|
8
|
+
it "should allow you to set the packet dup flag as a hash parameter" do
|
9
|
+
packet = EventMachine::MQTTSN::Packet.new( :duplicate => true )
|
10
|
+
expect(packet.duplicate).to be_truthy
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should allow you to set the packet QOS level as a hash parameter" do
|
14
|
+
packet = EventMachine::MQTTSN::Packet.new( :qos => 2 )
|
15
|
+
expect(packet.qos).to eq(2)
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should allow you to set the packet retain flag as a hash parameter" do
|
19
|
+
packet = EventMachine::MQTTSN::Packet.new( :retain => true )
|
20
|
+
expect(packet.retain).to be_truthy
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe "getting the type id on a un-subclassed packet" do
|
25
|
+
it "should throw an exception" do
|
26
|
+
expect {
|
27
|
+
EventMachine::MQTTSN::Packet.new.type_id
|
28
|
+
}.to raise_error(
|
29
|
+
RuntimeError,
|
30
|
+
"Invalid packet type: EventMachine::MQTTSN::Packet"
|
31
|
+
)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe "Parsing a packet that does not match the packet length" do
|
36
|
+
it "should throw an exception" do
|
37
|
+
expect {
|
38
|
+
packet = EventMachine::MQTTSN::Packet.parse("\x02\x1834567")
|
39
|
+
}.to raise_error(
|
40
|
+
EventMachine::MQTTSN::ProtocolException,
|
41
|
+
"Length of packet is not the same as the length header"
|
42
|
+
)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
|
49
|
+
describe EventMachine::MQTTSN::Packet::Connect do
|
50
|
+
it "should have the right type id" do
|
51
|
+
packet = EventMachine::MQTTSN::Packet::Connect.new
|
52
|
+
expect(packet.type_id).to eq(0x04)
|
53
|
+
end
|
54
|
+
|
55
|
+
describe "when serialising a packet" do
|
56
|
+
it "should output the correct bytes for a packet with no flags" do
|
57
|
+
packet = EventMachine::MQTTSN::Packet::Connect.new(
|
58
|
+
:client_id => 'mqtt-sn-client-pub'
|
59
|
+
)
|
60
|
+
expect(packet.to_s).to eq("\x18\x04\x04\x01\x00\x0fmqtt-sn-client-pub")
|
61
|
+
end
|
62
|
+
|
63
|
+
it "should output the correct bytes for a packet with clean session turned off" do
|
64
|
+
packet = EventMachine::MQTTSN::Packet::Connect.new(
|
65
|
+
:client_id => 'myclient',
|
66
|
+
:clean_session => false
|
67
|
+
)
|
68
|
+
expect(packet.to_s).to eq("\016\004\000\001\000\017myclient")
|
69
|
+
end
|
70
|
+
|
71
|
+
it "should throw an exception when there is no client identifier" do
|
72
|
+
expect {
|
73
|
+
EventMachine::MQTTSN::Packet::Connect.new.to_s
|
74
|
+
}.to raise_error(
|
75
|
+
'Invalid client identifier when serialising packet'
|
76
|
+
)
|
77
|
+
end
|
78
|
+
|
79
|
+
it "should output the correct bytes for a packet with a will request" do
|
80
|
+
packet = EventMachine::MQTTSN::Packet::Connect.new(
|
81
|
+
:client_id => 'myclient',
|
82
|
+
:request_will => true,
|
83
|
+
:clean_session => true
|
84
|
+
)
|
85
|
+
expect(packet.to_s).to eq("\016\004\014\001\000\017myclient")
|
86
|
+
end
|
87
|
+
|
88
|
+
it "should output the correct bytes for with a custom keep alive" do
|
89
|
+
packet = EventMachine::MQTTSN::Packet::Connect.new(
|
90
|
+
:client_id => 'myclient',
|
91
|
+
:request_will => true,
|
92
|
+
:clean_session => true,
|
93
|
+
:keep_alive => 30
|
94
|
+
)
|
95
|
+
expect(packet.to_s).to eq("\016\004\014\001\000\036myclient")
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
describe "when parsing a simple Connect packet" do
|
100
|
+
before(:each) do
|
101
|
+
@packet = EventMachine::MQTTSN::Packet.parse(
|
102
|
+
"\x18\x04\x04\x01\x00\x00mqtt-sn-client-pub"
|
103
|
+
)
|
104
|
+
end
|
105
|
+
|
106
|
+
it "should correctly create the right type of packet object" do
|
107
|
+
expect(@packet.class).to eq(EventMachine::MQTTSN::Packet::Connect)
|
108
|
+
end
|
109
|
+
|
110
|
+
it "should not have the request will flag set" do
|
111
|
+
expect(@packet.request_will).to be_falsy
|
112
|
+
end
|
113
|
+
|
114
|
+
it "shoul have the clean session flag set" do
|
115
|
+
expect(@packet.clean_session).to be_truthy
|
116
|
+
end
|
117
|
+
|
118
|
+
it "should set the Keep Alive timer of the packet correctly" do
|
119
|
+
expect(@packet.keep_alive).to eq(0)
|
120
|
+
end
|
121
|
+
|
122
|
+
it "should set the Client Identifier of the packet correctly" do
|
123
|
+
expect(@packet.client_id).to eq('mqtt-sn-client-pub')
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
describe "when parsing a Connect packet with the clean session flag set" do
|
128
|
+
before(:each) do
|
129
|
+
@packet = EventMachine::MQTTSN::Packet.parse(
|
130
|
+
"\016\004\004\001\000\017myclient"
|
131
|
+
)
|
132
|
+
end
|
133
|
+
|
134
|
+
it "should set the clean session flag" do
|
135
|
+
expect(@packet.clean_session).to be_truthy
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
describe "when parsing a Connect packet with the will request flag set" do
|
140
|
+
before(:each) do
|
141
|
+
@packet = EventMachine::MQTTSN::Packet.parse(
|
142
|
+
"\016\004\014\001\000\017myclient"
|
143
|
+
)
|
144
|
+
end
|
145
|
+
|
146
|
+
it "should correctly create the right type of packet object" do
|
147
|
+
expect(@packet.class).to eq(EventMachine::MQTTSN::Packet::Connect)
|
148
|
+
end
|
149
|
+
it "should set the Client Identifier of the packet correctly" do
|
150
|
+
expect(@packet.client_id).to eq('myclient')
|
151
|
+
end
|
152
|
+
|
153
|
+
it "should set the clean session flag should be set" do
|
154
|
+
expect(@packet.clean_session).to be_truthy
|
155
|
+
end
|
156
|
+
|
157
|
+
it "should set the Will retain flag should be false" do
|
158
|
+
expect(@packet.request_will).to be_truthy
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
context "that has an invalid type identifier" do
|
163
|
+
it "should throw an exception" do
|
164
|
+
expect {
|
165
|
+
EventMachine::MQTTSN::Packet.parse( "\x02\xFF" )
|
166
|
+
}.to raise_error(
|
167
|
+
EventMachine::MQTTSN::ProtocolException,
|
168
|
+
"Invalid packet type identifier: 255"
|
169
|
+
)
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
describe "when parsing a Connect packet an unsupport protocol ID" do
|
174
|
+
it "should throw an exception" do
|
175
|
+
expect {
|
176
|
+
packet = EventMachine::MQTTSN::Packet.parse(
|
177
|
+
"\016\004\014\005\000\017myclient"
|
178
|
+
)
|
179
|
+
}.to raise_error(
|
180
|
+
EventMachine::MQTTSN::ProtocolException,
|
181
|
+
"Unsupported protocol ID number: 5"
|
182
|
+
)
|
183
|
+
end
|
184
|
+
end
|
185
|
+
end
|
186
|
+
|
187
|
+
describe EventMachine::MQTTSN::Packet::Connack do
|
188
|
+
it "should have the right type id" do
|
189
|
+
packet = EventMachine::MQTTSN::Packet::Connack.new
|
190
|
+
expect(packet.type_id).to eq(0x05)
|
191
|
+
end
|
192
|
+
|
193
|
+
describe "when serialising a packet" do
|
194
|
+
it "should output the correct bytes for a sucessful connection acknowledgement packet" do
|
195
|
+
packet = EventMachine::MQTTSN::Packet::Connack.new( :return_code => 0x00 )
|
196
|
+
expect(packet.to_s).to eq("\x03\x05\x00")
|
197
|
+
end
|
198
|
+
end
|
199
|
+
|
200
|
+
describe "when parsing a successful Connection Accepted packet" do
|
201
|
+
before(:each) do
|
202
|
+
@packet = EventMachine::MQTTSN::Packet.parse( "\x03\x05\x00" )
|
203
|
+
end
|
204
|
+
|
205
|
+
it "should correctly create the right type of packet object" do
|
206
|
+
expect(@packet.class).to eq(EventMachine::MQTTSN::Packet::Connack)
|
207
|
+
end
|
208
|
+
|
209
|
+
it "should set the return code of the packet correctly" do
|
210
|
+
expect(@packet.return_code).to eq(0x00)
|
211
|
+
end
|
212
|
+
|
213
|
+
it "should set the return message of the packet correctly" do
|
214
|
+
expect(@packet.return_msg).to match(/accepted/i)
|
215
|
+
end
|
216
|
+
end
|
217
|
+
|
218
|
+
describe "when parsing a congestion packet" do
|
219
|
+
before(:each) do
|
220
|
+
@packet = EventMachine::MQTTSN::Packet.parse( "\x03\x05\x01" )
|
221
|
+
end
|
222
|
+
|
223
|
+
it "should correctly create the right type of packet object" do
|
224
|
+
expect(@packet.class).to eq(EventMachine::MQTTSN::Packet::Connack)
|
225
|
+
end
|
226
|
+
|
227
|
+
it "should set the return code of the packet correctly" do
|
228
|
+
expect(@packet.return_code).to eq(0x01)
|
229
|
+
end
|
230
|
+
|
231
|
+
it "should set the return message of the packet correctly" do
|
232
|
+
expect(@packet.return_msg).to match(/rejected: congestion/i)
|
233
|
+
end
|
234
|
+
end
|
235
|
+
|
236
|
+
describe "when parsing a invalid topic ID packet" do
|
237
|
+
before(:each) do
|
238
|
+
@packet = EventMachine::MQTTSN::Packet.parse( "\x03\x05\x02" )
|
239
|
+
end
|
240
|
+
|
241
|
+
it "should correctly create the right type of packet object" do
|
242
|
+
expect(@packet.class).to eq(EventMachine::MQTTSN::Packet::Connack)
|
243
|
+
end
|
244
|
+
|
245
|
+
it "should set the return code of the packet correctly" do
|
246
|
+
expect(@packet.return_code).to eq(0x02)
|
247
|
+
end
|
248
|
+
|
249
|
+
it "should set the return message of the packet correctly" do
|
250
|
+
expect(@packet.return_msg).to match(/rejected: invalid topic ID/i)
|
251
|
+
end
|
252
|
+
end
|
253
|
+
|
254
|
+
describe "when parsing a 'not supported' packet" do
|
255
|
+
before(:each) do
|
256
|
+
@packet = EventMachine::MQTTSN::Packet.parse( "\x03\x05\x03" )
|
257
|
+
end
|
258
|
+
|
259
|
+
it "should correctly create the right type of packet object" do
|
260
|
+
expect(@packet.class).to eq(EventMachine::MQTTSN::Packet::Connack)
|
261
|
+
end
|
262
|
+
|
263
|
+
it "should set the return code of the packet correctly" do
|
264
|
+
expect(@packet.return_code).to eq(0x03)
|
265
|
+
end
|
266
|
+
|
267
|
+
it "should set the return message of the packet correctly" do
|
268
|
+
expect(@packet.return_msg).to match(/not supported/i)
|
269
|
+
end
|
270
|
+
end
|
271
|
+
|
272
|
+
describe "when parsing an unknown connection refused packet" do
|
273
|
+
before(:each) do
|
274
|
+
@packet = EventMachine::MQTTSN::Packet.parse( "\x03\x05\x10" )
|
275
|
+
end
|
276
|
+
|
277
|
+
it "should correctly create the right type of packet object" do
|
278
|
+
expect(@packet.class).to eq(EventMachine::MQTTSN::Packet::Connack)
|
279
|
+
end
|
280
|
+
|
281
|
+
it "should set the return code of the packet correctly" do
|
282
|
+
expect(@packet.return_code).to eq(0x10)
|
283
|
+
end
|
284
|
+
|
285
|
+
it "should set the return message of the packet correctly" do
|
286
|
+
expect(@packet.return_msg).to match(/rejected/i)
|
287
|
+
end
|
288
|
+
end
|
289
|
+
end
|
290
|
+
|
291
|
+
|
292
|
+
describe EventMachine::MQTTSN::Packet::Register do
|
293
|
+
it "should have the right type id" do
|
294
|
+
packet = EventMachine::MQTTSN::Packet::Register.new
|
295
|
+
expect(packet.type_id).to eq(0x0A)
|
296
|
+
end
|
297
|
+
|
298
|
+
describe "when serialising a packet" do
|
299
|
+
it "should output the correct bytes for a register packet" do
|
300
|
+
packet = EventMachine::MQTTSN::Packet::Register.new(
|
301
|
+
:id => 0x01,
|
302
|
+
:topic_id => 0x01,
|
303
|
+
:topic_name => 'test'
|
304
|
+
)
|
305
|
+
expect(packet.to_s).to eq("\x0A\x0A\x00\x01\x00\x01test")
|
306
|
+
end
|
307
|
+
end
|
308
|
+
|
309
|
+
describe "when parsing a Register packet" do
|
310
|
+
before(:each) do
|
311
|
+
@packet = EventMachine::MQTTSN::Packet.parse( "\x0A\x0A\x00\x01\x00\x01test" )
|
312
|
+
end
|
313
|
+
|
314
|
+
it "should correctly create the right type of packet object" do
|
315
|
+
expect(@packet.class).to eq(EventMachine::MQTTSN::Packet::Register)
|
316
|
+
end
|
317
|
+
|
318
|
+
it "should set the topic id type of the packet correctly" do
|
319
|
+
expect(@packet.topic_id_type).to eq(:normal)
|
320
|
+
end
|
321
|
+
|
322
|
+
it "should set the topic id of the packet correctly" do
|
323
|
+
expect(@packet.topic_id).to eq(0x01)
|
324
|
+
end
|
325
|
+
|
326
|
+
it "should set the message id of the packet correctly" do
|
327
|
+
expect(@packet.id).to eq(0x01)
|
328
|
+
end
|
329
|
+
|
330
|
+
it "should set the topic name of the packet correctly" do
|
331
|
+
expect(@packet.topic_name).to eq('test')
|
332
|
+
end
|
333
|
+
end
|
334
|
+
end
|
335
|
+
|
336
|
+
|
337
|
+
describe EventMachine::MQTTSN::Packet::Regack do
|
338
|
+
it "should have the right type id" do
|
339
|
+
packet = EventMachine::MQTTSN::Packet::Regack.new
|
340
|
+
expect(packet.type_id).to eq(0x0B)
|
341
|
+
end
|
342
|
+
|
343
|
+
describe "when serialising a packet" do
|
344
|
+
it "should output the correct bytes for a register packet" do
|
345
|
+
packet = EventMachine::MQTTSN::Packet::Regack.new(
|
346
|
+
:id => 0x02,
|
347
|
+
:topic_id => 0x01,
|
348
|
+
:return_code => 0x03
|
349
|
+
)
|
350
|
+
expect(packet.to_s).to eq("\x07\x0B\x00\x01\x00\x02\x03")
|
351
|
+
end
|
352
|
+
end
|
353
|
+
|
354
|
+
describe "when parsing a REGACK packet" do
|
355
|
+
before(:each) do
|
356
|
+
@packet = EventMachine::MQTTSN::Packet.parse( "\x07\x0B\x00\x01\x00\x02\x03" )
|
357
|
+
end
|
358
|
+
|
359
|
+
it "should correctly create the right type of packet object" do
|
360
|
+
expect(@packet.class).to eq(EventMachine::MQTTSN::Packet::Regack)
|
361
|
+
end
|
362
|
+
|
363
|
+
it "should set the topic id type of the packet correctly" do
|
364
|
+
expect(@packet.topic_id_type).to eq(:normal)
|
365
|
+
end
|
366
|
+
|
367
|
+
it "should set the topic id of the packet correctly" do
|
368
|
+
expect(@packet.topic_id).to eq(0x01)
|
369
|
+
end
|
370
|
+
|
371
|
+
it "should set the message id of the packet correctly" do
|
372
|
+
expect(@packet.id).to eq(0x02)
|
373
|
+
end
|
374
|
+
|
375
|
+
it "should set the topic name of the packet correctly" do
|
376
|
+
expect(@packet.return_code).to eq(0x03)
|
377
|
+
end
|
378
|
+
end
|
379
|
+
end
|
380
|
+
|
381
|
+
|
382
|
+
describe EventMachine::MQTTSN::Packet::Publish do
|
383
|
+
it "should have the right type id" do
|
384
|
+
packet = EventMachine::MQTTSN::Packet::Publish.new
|
385
|
+
expect(packet.type_id).to eq(0x0C)
|
386
|
+
end
|
387
|
+
|
388
|
+
describe "when serialising a packet with a normal topic id type" do
|
389
|
+
it "should output the correct bytes for a publish packet" do
|
390
|
+
packet = EventMachine::MQTTSN::Packet::Publish.new(
|
391
|
+
:topic_id => 0x01,
|
392
|
+
:topic_id_type => :normal,
|
393
|
+
:data => "Hello World"
|
394
|
+
)
|
395
|
+
expect(packet.to_s).to eq("\x12\x0C\x00\x00\x01\x00\x00Hello World")
|
396
|
+
end
|
397
|
+
end
|
398
|
+
|
399
|
+
describe "when serialising a packet with a short topic id type" do
|
400
|
+
it "should output the correct bytes for a publish packet" do
|
401
|
+
packet = EventMachine::MQTTSN::Packet::Publish.new(
|
402
|
+
:topic_id => 'tt',
|
403
|
+
:topic_id_type => :short,
|
404
|
+
:data => "Hello World"
|
405
|
+
)
|
406
|
+
expect(packet.to_s).to eq("\x12\x0C\x02tt\x00\x00Hello World")
|
407
|
+
end
|
408
|
+
end
|
409
|
+
|
410
|
+
describe "when parsing a Publish packet with a normal topic id" do
|
411
|
+
before(:each) do
|
412
|
+
@packet = EventMachine::MQTTSN::Packet.parse(
|
413
|
+
"\x12\x0C\x00\x00\x01\x00\x00Hello World"
|
414
|
+
)
|
415
|
+
end
|
416
|
+
|
417
|
+
it "should correctly create the right type of packet object" do
|
418
|
+
expect(@packet.class).to eq(EventMachine::MQTTSN::Packet::Publish)
|
419
|
+
end
|
420
|
+
|
421
|
+
it "should set the QOS of the packet correctly" do
|
422
|
+
expect(@packet.qos).to be === 0
|
423
|
+
end
|
424
|
+
|
425
|
+
it "should set the QOS of the packet correctly" do
|
426
|
+
expect(@packet.duplicate).to be === false
|
427
|
+
end
|
428
|
+
|
429
|
+
it "should set the retain flag of the packet correctly" do
|
430
|
+
expect(@packet.retain).to be === false
|
431
|
+
end
|
432
|
+
|
433
|
+
it "should set the topic id of the packet correctly" do
|
434
|
+
expect(@packet.topic_id_type).to be === :normal
|
435
|
+
end
|
436
|
+
|
437
|
+
it "should set the topic id of the packet correctly" do
|
438
|
+
expect(@packet.topic_id).to be === 0x01
|
439
|
+
end
|
440
|
+
|
441
|
+
it "should set the message id of the packet correctly" do
|
442
|
+
expect(@packet.id).to be === 0x0000
|
443
|
+
end
|
444
|
+
|
445
|
+
it "should set the topic name of the packet correctly" do
|
446
|
+
expect(@packet.data).to eq("Hello World")
|
447
|
+
end
|
448
|
+
end
|
449
|
+
|
450
|
+
describe "when parsing a Publish packet with a short topic id" do
|
451
|
+
before(:each) do
|
452
|
+
@packet = EventMachine::MQTTSN::Packet.parse(
|
453
|
+
"\x12\x0C\x02tt\x00\x00Hello World"
|
454
|
+
)
|
455
|
+
end
|
456
|
+
|
457
|
+
it "should correctly create the right type of packet object" do
|
458
|
+
expect(@packet.class).to eq(EventMachine::MQTTSN::Packet::Publish)
|
459
|
+
end
|
460
|
+
|
461
|
+
it "should set the QOS of the packet correctly" do
|
462
|
+
expect(@packet.qos).to be === 0
|
463
|
+
end
|
464
|
+
|
465
|
+
it "should set the QOS of the packet correctly" do
|
466
|
+
expect(@packet.duplicate).to be === false
|
467
|
+
end
|
468
|
+
|
469
|
+
it "should set the retain flag of the packet correctly" do
|
470
|
+
expect(@packet.retain).to be === false
|
471
|
+
end
|
472
|
+
|
473
|
+
it "should set the topic id type of the packet correctly" do
|
474
|
+
expect(@packet.topic_id_type).to be === :short
|
475
|
+
end
|
476
|
+
|
477
|
+
it "should set the topic id of the packet correctly" do
|
478
|
+
expect(@packet.topic_id).to be === 'tt'
|
479
|
+
end
|
480
|
+
|
481
|
+
it "should set the message id of the packet correctly" do
|
482
|
+
expect(@packet.id).to be === 0x0000
|
483
|
+
end
|
484
|
+
|
485
|
+
it "should set the topic name of the packet correctly" do
|
486
|
+
expect(@packet.data).to eq("Hello World")
|
487
|
+
end
|
488
|
+
end
|
489
|
+
end
|
490
|
+
|
491
|
+
|
492
|
+
describe EventMachine::MQTTSN::Packet::Subscribe do
|
493
|
+
it "should have the right type id" do
|
494
|
+
packet = EventMachine::MQTTSN::Packet::Subscribe.new
|
495
|
+
expect(packet.type_id).to eq(0x12)
|
496
|
+
end
|
497
|
+
|
498
|
+
describe "when serialising a packet" do
|
499
|
+
it "should output the correct bytes for a Subscribe packet" do
|
500
|
+
packet = EventMachine::MQTTSN::Packet::Subscribe.new(
|
501
|
+
:duplicate => false,
|
502
|
+
:qos => 0,
|
503
|
+
:id => 0x02,
|
504
|
+
:topic_name => 'test'
|
505
|
+
)
|
506
|
+
expect(packet.to_s).to eq("\x09\x12\x00\x00\x02test")
|
507
|
+
end
|
508
|
+
end
|
509
|
+
|
510
|
+
describe "when parsing a Subscribe packet" do
|
511
|
+
before(:each) do
|
512
|
+
@packet = EventMachine::MQTTSN::Packet.parse( "\x09\x12\x00\x00\x03test" )
|
513
|
+
end
|
514
|
+
|
515
|
+
it "should correctly create the right type of packet object" do
|
516
|
+
expect(@packet.class).to eq(EventMachine::MQTTSN::Packet::Subscribe)
|
517
|
+
end
|
518
|
+
|
519
|
+
it "should set the message id of the packet correctly" do
|
520
|
+
expect(@packet.id).to eq(0x03)
|
521
|
+
end
|
522
|
+
|
523
|
+
it "should set the message id of the packet correctly" do
|
524
|
+
expect(@packet.qos).to eq(0)
|
525
|
+
end
|
526
|
+
|
527
|
+
it "should set the message id of the packet correctly" do
|
528
|
+
expect(@packet.duplicate).to eq(false)
|
529
|
+
end
|
530
|
+
|
531
|
+
it "should set the topic name of the packet correctly" do
|
532
|
+
expect(@packet.topic_name).to eq('test')
|
533
|
+
end
|
534
|
+
end
|
535
|
+
end
|
536
|
+
|
537
|
+
|
538
|
+
describe EventMachine::MQTTSN::Packet::Suback do
|
539
|
+
it "should have the right type id" do
|
540
|
+
packet = EventMachine::MQTTSN::Packet::Suback.new
|
541
|
+
expect(packet.type_id).to eq(0x13)
|
542
|
+
end
|
543
|
+
|
544
|
+
describe "when serialising a packet" do
|
545
|
+
it "should output the correct bytes for a register packet" do
|
546
|
+
packet = EventMachine::MQTTSN::Packet::Suback.new(
|
547
|
+
:id => 0x02,
|
548
|
+
:qos => 0,
|
549
|
+
:topic_id => 0x01,
|
550
|
+
:return_code => 0x03
|
551
|
+
)
|
552
|
+
expect(packet.to_s).to eq("\x08\x13\x00\x00\x01\x00\x02\x03")
|
553
|
+
end
|
554
|
+
end
|
555
|
+
|
556
|
+
describe "when parsing a SUBACK packet" do
|
557
|
+
before(:each) do
|
558
|
+
@packet = EventMachine::MQTTSN::Packet.parse( "\x08\x13\x00\x00\x01\x00\x02\x03" )
|
559
|
+
end
|
560
|
+
|
561
|
+
it "should correctly create the right type of packet object" do
|
562
|
+
expect(@packet.class).to eq(EventMachine::MQTTSN::Packet::Suback)
|
563
|
+
end
|
564
|
+
|
565
|
+
it "should set the topic id of the packet correctly" do
|
566
|
+
expect(@packet.qos).to eq(0)
|
567
|
+
end
|
568
|
+
|
569
|
+
it "should set the topic id type of the packet correctly" do
|
570
|
+
expect(@packet.topic_id_type).to eq(:normal)
|
571
|
+
end
|
572
|
+
|
573
|
+
it "should set the topic id of the packet correctly" do
|
574
|
+
expect(@packet.topic_id).to eq(0x01)
|
575
|
+
end
|
576
|
+
|
577
|
+
it "should set the message id of the packet correctly" do
|
578
|
+
expect(@packet.id).to eq(0x02)
|
579
|
+
end
|
580
|
+
|
581
|
+
it "should set the topic name of the packet correctly" do
|
582
|
+
expect(@packet.return_code).to eq(0x03)
|
583
|
+
end
|
584
|
+
end
|
585
|
+
end
|
586
|
+
|
587
|
+
|
588
|
+
describe EventMachine::MQTTSN::Packet::Pingreq do
|
589
|
+
it "should have the right type id" do
|
590
|
+
packet = EventMachine::MQTTSN::Packet::Pingreq.new
|
591
|
+
expect(packet.type_id).to eq(0x16)
|
592
|
+
end
|
593
|
+
|
594
|
+
describe "when serialising a packet" do
|
595
|
+
it "should output the correct bytes for a pingreq packet" do
|
596
|
+
packet = EventMachine::MQTTSN::Packet::Pingreq.new
|
597
|
+
expect(packet.to_s).to eq("\x02\x16")
|
598
|
+
end
|
599
|
+
end
|
600
|
+
|
601
|
+
describe "when parsing a Pingreq packet" do
|
602
|
+
before(:each) do
|
603
|
+
@packet = EventMachine::MQTTSN::Packet.parse("\x02\x16")
|
604
|
+
end
|
605
|
+
|
606
|
+
it "should correctly create the right type of packet object" do
|
607
|
+
expect(@packet.class).to eq(EventMachine::MQTTSN::Packet::Pingreq)
|
608
|
+
end
|
609
|
+
end
|
610
|
+
end
|
611
|
+
|
612
|
+
|
613
|
+
describe EventMachine::MQTTSN::Packet::Pingresp do
|
614
|
+
it "should have the right type id" do
|
615
|
+
packet = EventMachine::MQTTSN::Packet::Pingresp.new
|
616
|
+
expect(packet.type_id).to eq(0x17)
|
617
|
+
end
|
618
|
+
|
619
|
+
describe "when serialising a packet" do
|
620
|
+
it "should output the correct bytes for a pingresp packet" do
|
621
|
+
packet = EventMachine::MQTTSN::Packet::Pingresp.new
|
622
|
+
expect(packet.to_s).to eq("\x02\x17")
|
623
|
+
end
|
624
|
+
end
|
625
|
+
|
626
|
+
describe "when parsing a Pingresp packet" do
|
627
|
+
before(:each) do
|
628
|
+
@packet = EventMachine::MQTTSN::Packet.parse("\x02\x17")
|
629
|
+
end
|
630
|
+
|
631
|
+
it "should correctly create the right type of packet object" do
|
632
|
+
expect(@packet.class).to eq(EventMachine::MQTTSN::Packet::Pingresp)
|
633
|
+
end
|
634
|
+
end
|
635
|
+
end
|
636
|
+
|
637
|
+
|
638
|
+
describe EventMachine::MQTTSN::Packet::Disconnect do
|
639
|
+
it "should have the right type id" do
|
640
|
+
packet = EventMachine::MQTTSN::Packet::Disconnect.new
|
641
|
+
expect(packet.type_id).to eq(0x18)
|
642
|
+
end
|
643
|
+
|
644
|
+
describe "when serialising a packet" do
|
645
|
+
it "should output the correct bytes for a disconnect packet" do
|
646
|
+
packet = EventMachine::MQTTSN::Packet::Disconnect.new
|
647
|
+
expect(packet.to_s).to eq("\x02\x18")
|
648
|
+
end
|
649
|
+
end
|
650
|
+
|
651
|
+
describe "when parsing a Disconnect packet" do
|
652
|
+
before(:each) do
|
653
|
+
@packet = EventMachine::MQTTSN::Packet.parse("\x02\x18")
|
654
|
+
end
|
655
|
+
|
656
|
+
it "should correctly create the right type of packet object" do
|
657
|
+
expect(@packet.class).to eq(EventMachine::MQTTSN::Packet::Disconnect)
|
658
|
+
end
|
659
|
+
end
|
660
|
+
end
|