-
Notifications
You must be signed in to change notification settings - Fork 3
/
mqpub.rex
executable file
·273 lines (209 loc) · 5.09 KB
/
mqpub.rex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#!/usr/bin/rxsock
/* rexx */
/*
See http://www.ibm.com/developerworks/webservices/library/ws-mqtt/index.html for the full protocol spec
(C) 2011 Dougie Lawson, all rights reserved.
*/
call RxFuncAdd 'SockLoadFuncs', 'rxsock', 'SockLoadFuncs'
call SockLoadFuncs
parse arg host port
if port = '' then port = 1883
if host = '' then host = 'localhost'
keepalive = 1 /* second */
id = 'REXX pub client'
qos = 1
rc = mqtt_connect(host, port, id, keepalive, qos)
if rc <> 0 then do
say "connect failed:" rc
exit 20
call SockClose s;
end
topic = 'examples/publishtest'
content = "Hello World" date() time()
/* content = content || copies('*',117400) || content */
say content
rc = mqtt_publish(topic,content,qos)
call SockClose s;
exit 0
mqtt_publish: procedure expose s
topic = arg(1)
content = arg(2)
qos = arg(3)
msgid = random()
msgid = x2c(d2x(msgid,4))
qos = qos * 2 /* shift 1 bit left */
qos1 = x2c(d2x(qos,2))
lth = hexlth(topic,4)
if qos < 1 then buf = lth || topic || content
else buf = lth || topic || msgid || content
hlth = mqtt_length_encode(length(buf))
pubcode = bitor('30'x,qos1)
/* PUBLISH encoded len */
header = pubcode || hlth
msg = header || buf
/*
say "---->"
say msg
say c2x(msg)
*/
if SockSend(s, msg) < 0 then do
say 'Send() failed'
say "SockErrno =" SockSock_Errno()
exit 301
end
if qos > 1 then do
if SockRecv(s, 'rsp1', 5) < 0 then do
say 'Recv() failed'
say "SockErrno =" SockSock_Errno()
exit 302
end
/*
say "<----"
say rsp1
say c2x(rsp1)
*/
rsp2 = ""
if length(rsp1) > 0 then do
parse var rsp1 1 verb 2 rlth
lth = mqtt_length_decode(rlth)
parse var lth rlth offset
rlth = rlth + 2
if SockRecv(s, 'rsp2', rlth) < 0 then do
say 'Recv() failed'
say "SockErrno =" SockSock_Errno()
exit 303
end
/*
say "<----"
say rsp2
say c2x(rsp2)
*/
rsp = rsp1 || rsp2
parse var rsp 1 verb 2 lth2 =(offset) rest
say "Msgid:" c2x(rest)
end
end
return 0
mqtt_connect: procedure expose s
/* set up the MQTT CONNECT and check the CONNACK */
host = arg(1)
port = arg(2)
id = arg(3)
keepalive = arg(4)
qos = arg(5)
qos = qos * 2
qos = x2c(d2x(qos,2))
s = tcp_connect(host, port)
ka = x2c(d2x(keepalive,4))
lth = hexlth(id,4)
clientid = lth || id
/* Set up connect struct. */
/* ll protocol version conflags */
/* clean */
buf = '0006'x || 'MQIsdp' || '03'x || '02'x || ka || clientid
hlth = mqtt_length_encode(length(buf))
/* CONNECT 1-byte total len */
concode = bitor('10'x,qos)
header = concode || hlth
msg = header || buf
/*
say "---->"
say msg
say c2x(msg)
*/
if SockSend(s, msg) < 0 then do
say 'Send() failed'
say "SockErrno =" SockSock_Errno()
exit 201
end
if SockRecv(s, 'rsp', 5, 'MSG_PEEK') < 0 then do
say 'Recv() failed'
say "SockErrno =" SockSock_Errno()
exit 202
end
/*
say "<----"
say rsp
say c2x(rsp)
*/
parse var rsp 1 verb 2 rlth
lth = mqtt_length_decode(rlth)
parse var lth rlth offset
rlth = rlth + 2
if rlth > 0 then do
if SockRecv(s, 'rsp', rlth) < 0 then do
say 'Recv() failed'
say "SockErrno =" SockSock_Errno()
exit 203
end
end
/*
say "<----"
say rsp
say c2x(rsp)
*/
parse var rsp 1 verb 2 lth =(offset) rsn
rsn = right(rsn,1)
if c2x(rsn) <> '00' then do
say "verb:" c2x(verb)
say "lth:" c2x(lth)
say "rsn:" c2x(rsn)
return 204
end
else return 0
tcp_connect: procedure
host = arg(1)
port = arg(2)
if SockGetHostByName(host, 'host.!') = 0 then do
say 'SockGetHostByName failed' sockpsock_errno()
say 'Errno' SockSock_Errno();
exit 101
end
/* Put server info into the server stem. */
server.!family = 'AF_INET'
server.!port = port
server.!addr = host.!addr
/* Get a stream socket. */
s = SockSocket('AF_INET', 'SOCK_STREAM', 0)
if s < 0 then do
call SockPSock_Errno 'Socket'
exit 102
end
/* Connect to the server. */
if SockConnect(s, 'server.!') < 0 then do
call SockPSock_Errno
exit 103
end
return s
hexlth: procedure
id = arg(1)
rlth = arg(2)
return x2c(d2x(length(id),rlth))
mqtt_length_encode: procedure
x = arg(1)
remlen = ""
do while(x > 0)
digit = x // 128
x = x % 128
if x > 0 then digit = digit + 128
remlen = remlen || d2x(digit,2)
end
return x2c(remlen)
mqtt_length_decode: procedure
remlen = arg(1)
digits = c2x(remlen)
multiplier = 1
value = 0
done = 0
offset = 3
do i = 1 to length(digits) by 2
digit = x2d(substr(digits,i,2))
if digit > 128 then digit = digit - 128
else done = 1
value = value + (digit * multiplier)
if done = 1 then leave
multiplier = multiplier * 128
offset = offset + 1
end
ret = value offset
return ret