author | Zach Smith <zd@zdsmith.com> |
Wed, 15 May 2019 14:53:03 -0700 | |
changeset 6 | 7d977f308c75 |
parent 4 | 2f4e88604125 |
child 7 | 9c5ce539b081 |
permissions | -rw-r--r-- |
0 | 1 |
# vim: set et nosta sw=4 ts=4 ft=nim : |
2 |
# |
|
4
2f4e88604125
Re-arrange for nimble, update to Nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents:
0
diff
changeset
|
3 |
# Copyright (c) 2016-2018, Mahlon E. Smith <mahlon@martini.nu> |
0 | 4 |
# All rights reserved. |
5 |
# Redistribution and use in source and binary forms, with or without |
|
6 |
# modification, are permitted provided that the following conditions are met: |
|
7 |
# |
|
8 |
# * Redistributions of source code must retain the above copyright |
|
9 |
# notice, this list of conditions and the following disclaimer. |
|
10 |
# |
|
11 |
# * Redistributions in binary form must reproduce the above copyright |
|
12 |
# notice, this list of conditions and the following disclaimer in the |
|
13 |
# documentation and/or other materials provided with the distribution. |
|
14 |
# |
|
15 |
# * Neither the name of Mahlon E. Smith nor the names of his |
|
16 |
# contributors may be used to endorse or promote products derived |
|
17 |
# from this software without specific prior written permission. |
|
18 |
# |
|
19 |
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY |
|
20 |
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|
21 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
|
22 |
# DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY |
|
23 |
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
|
24 |
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
|
25 |
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
|
26 |
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
27 |
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|
28 |
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
29 |
||
30 |
## Overview |
|
31 |
## ============ |
|
32 |
## |
|
33 |
## This is a pure-nim client library for interacting with Stomp 1.2 |
|
34 |
## compliant messaging brokers. |
|
35 |
## |
|
36 |
## https://stomp.github.io/stomp-specification-1.2.html |
|
37 |
## |
|
38 |
## Stomp is a simple protocol for message passing between clients, using a central |
|
39 |
## broker. It is a subset of other more elaborate protocols (like AMQP), supporting |
|
40 |
## only the most used features of common brokers. |
|
41 |
## |
|
42 |
## Because this library is pure-nim, there are no external dependencies. If you |
|
43 |
## can compile a nim binary, you can participate in advanced messaging between processes. |
|
44 |
## |
|
45 |
## A list of broker support for Stomp can be found here: |
|
46 |
## https://stomp.github.io/implementations.html. |
|
47 |
## |
|
48 |
## This library has been tested with recent versions of RabbitMQ. If it |
|
49 |
## works for you with another broker, please let the author know. |
|
50 |
## |
|
51 |
||
52 |
import |
|
53 |
strutils, |
|
54 |
nativesockets, |
|
55 |
net, |
|
56 |
os, |
|
57 |
times, |
|
58 |
uri |
|
59 |
||
60 |
const |
|
4
2f4e88604125
Re-arrange for nimble, update to Nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents:
0
diff
changeset
|
61 |
VERSION = "0.1.1" ## The current program version. |
0 | 62 |
NULL = "\x00" ## The NULL character. |
63 |
CR = "\r" ## The carriage return character. |
|
64 |
CRLF = "\r\n" ## Carriage return + Line feed (EOL). |
|
65 |
||
66 |
||
67 |
# Exceptions |
|
68 |
# |
|
69 |
type |
|
70 |
StompError* = object of ValueError ## A generic Stomp error state. |
|
71 |
||
72 |
StompClient* = ref object of RootObj ## An object that represents a connection to a Stomp compatible server. |
|
73 |
socket: Socket ## The socket object attached to this client. |
|
74 |
connected*: bool ## Is the client currently connected? |
|
75 |
uri*: Uri ## The URI used to instantiate this client. |
|
76 |
username: string ## The Stomp server user, if any. |
|
77 |
password: string ## The Stomp server password, if any. |
|
78 |
host: string ## The host or IP address to connect to. |
|
79 |
port: Port ## Optional, if Stomp is on a non-default port. |
|
80 |
vhost: string ## Parsed from the URI path, a Stomp "virtual host". |
|
81 |
timeout*: int ## Global socket timeout. |
|
82 |
last_msgtime*: Time ## Timestamp of last seen server message. |
|
83 |
options: tuple[ heartbeat: int ] ## Any supported client options, derived from the URI query string. |
|
84 |
subscriptions*: seq[ string ] ## Registered client subscriptions. Array position is the ID. |
|
85 |
transactions*: seq[ string ] ## Any currently open transactions. |
|
86 |
serverinfo: seq[ tuple[name: string, value: string] ] ## Server metadata, populated upon a successful connection. |
|
87 |
||
88 |
connected_callback*: proc ( client: StompClient, response: StompResponse ): void |
|
89 |
error_callback*: proc ( client: StompClient, response: StompResponse ): void |
|
90 |
heartbeat_callback*: proc ( client: StompClient, response: StompResponse ): void |
|
91 |
message_callback*: proc ( client: StompClient, response: StompResponse ): void |
|
92 |
missed_heartbeat_callback*: proc ( client: StompClient ): void |
|
93 |
receipt_callback*: proc ( client: StompClient, response: StompResponse ): void |
|
94 |
||
95 |
StompResponse* = ref object of RootObj ## A parsed packet from a Stomp server. |
|
96 |
headers: seq[ tuple[name: string, value: string] ] ## Any headers in the response. Access with the `[]` proc. |
|
97 |
frame*: string ## The Stomp frame type. |
|
98 |
payload*: string ## The message body, if any. |
|
99 |
||
100 |
||
101 |
# convenience |
|
102 |
proc printf( formatstr: cstring ) {.header: "<stdio.h>", varargs.} |
|
103 |
||
104 |
||
105 |
#------------------------------------------------------------------- |
|
106 |
# R E S P O N S E |
|
107 |
#------------------------------------------------------------------- |
|
108 |
||
109 |
proc is_eol( s: string ): bool = |
|
110 |
## Convenience method, returns **true** if string is a Stomp EOF. |
|
111 |
return s == CR or s == CRLF |
|
112 |
||
113 |
||
114 |
proc parse_headers( response: StompResponse, c: StompClient ): int = |
|
115 |
## Parse response headers from a stream. |
|
116 |
## Returns the content length of the response body, or 0. |
|
117 |
result = 0 |
|
118 |
var line = "" |
|
119 |
||
120 |
c.socket.readline( line, c.timeout ) |
|
121 |
while not line.is_eol: |
|
122 |
if defined( debug ): printf " <-- %s\n", line |
|
123 |
var header = line.split( ":" ) |
|
124 |
if header.len < 2: break |
|
125 |
response.headers.add( (header[0], header[1]) ) |
|
126 |
if cmpIgnoreCase( header[0], "content-length" ) == 0: result = header[1].parse_int |
|
127 |
c.socket.readline( line, c.timeout ) |
|
128 |
||
129 |
||
130 |
proc parse_payload( response: StompResponse, c: StompClient, bodylength = 0 ): void = |
|
131 |
## Parse message payload from a stream. |
|
132 |
let bufsize = 8192 |
|
133 |
var |
|
134 |
buf = "" |
|
135 |
data = "" |
|
136 |
||
137 |
||
138 |
# If we already know the length of the body, just perform a buffered read. |
|
139 |
# |
|
140 |
if bodylength > 0: |
|
141 |
var |
|
142 |
readtotal = 0 |
|
143 |
readamt = 0 |
|
144 |
remaining = 0 |
|
145 |
||
146 |
while readtotal != bodylength: |
|
147 |
remaining = bodylength - readtotal |
|
148 |
||
149 |
if remaining < bufsize: |
|
150 |
readamt = remaining |
|
151 |
else: |
|
152 |
readamt = bufsize |
|
153 |
||
154 |
buf = newString( readamt ) |
|
155 |
readtotal = readtotal + c.socket.recv( buf, readamt, c.timeout ) |
|
156 |
data = data & buf |
|
157 |
||
158 |
# Eat the NULL terminator. |
|
159 |
discard c.socket.recv( buf, 1, c.timeout ) |
|
160 |
||
161 |
# Inefficient path. |
|
162 |
# |
|
163 |
else: |
|
164 |
while buf != NULL: |
|
165 |
discard c.socket.recv( buf, 1, c.timeout ) |
|
166 |
data = data & buf |
|
167 |
||
168 |
response.payload = data |
|
169 |
||
170 |
||
171 |
proc newStompResponse( c: StompClient ): StompResponse = |
|
172 |
## Initialize a response object, which parses and contains |
|
173 |
## the Stomp headers and any additional important data from |
|
174 |
## the broker socket. |
|
175 |
new( result ) |
|
176 |
result.headers = @[] |
|
177 |
||
178 |
# Get the frame type, record last seen server activity time. |
|
179 |
# |
|
180 |
var line = "" |
|
181 |
c.socket.readline( line, c.timeout ) |
|
182 |
c.last_msgtime = get_time() |
|
183 |
||
184 |
# Heartbeat packets (empties.) |
|
185 |
# |
|
186 |
# This could -also- parse optional EOLs from the prior |
|
187 |
# message (after the NULL separator), but since it is a no-op, |
|
188 |
# this seems harmless. |
|
189 |
# |
|
190 |
if line.is_eol: |
|
191 |
result.frame = "HEARTBEAT" |
|
192 |
return result |
|
193 |
||
194 |
# All other types. |
|
195 |
# |
|
196 |
result.frame = line |
|
197 |
if defined( debug ): |
|
198 |
printf " <-- %s\n", line |
|
199 |
||
200 |
# Parse headers and body. |
|
201 |
# |
|
202 |
var length = result.parse_headers( c ) |
|
203 |
if result.frame == "MESSAGE" or result.frame == "ERROR": |
|
204 |
result.parse_payload( c, length ) |
|
205 |
||
206 |
# If the response -could- have a body, the NULL has already |
|
207 |
# been removed from the stream while we checked for one. |
|
208 |
# |
|
209 |
if result.payload.len > 0: |
|
210 |
if result.payload == NULL: # We checked for a body, but there was none. |
|
4
2f4e88604125
Re-arrange for nimble, update to Nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents:
0
diff
changeset
|
211 |
result.payload = "" |
0 | 212 |
if defined( debug ): printf " <--\n <-- ^@\n\n" |
213 |
else: |
|
214 |
if defined( debug ): printf " <--\n <-- (payload)^@\n\n" |
|
215 |
||
216 |
# Otherwise, pop off the NULL terminator now. |
|
217 |
# |
|
218 |
else: |
|
219 |
discard c.socket.recv( line, 1, c.timeout ) |
|
220 |
if defined( debug ): printf " <--\n <-- ^@\n\n" |
|
221 |
||
222 |
||
223 |
proc `$`*( r: StompResponse ): string = |
|
224 |
## Represent a Stomp response as a string. |
|
225 |
result = r.frame & ": " & $r.headers |
|
226 |
||
227 |
||
228 |
proc `[]`*( response: StompResponse, key: string ): string = |
|
229 |
## Get a specific header from a Stomp response. |
|
230 |
for header in response.headers: |
|
231 |
if cmpIgnoreCase( key, header.name ) == 0: |
|
232 |
return header.value |
|
4
2f4e88604125
Re-arrange for nimble, update to Nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents:
0
diff
changeset
|
233 |
return "" |
0 | 234 |
|
235 |
||
236 |
#------------------------------------------------------------------- |
|
237 |
# C A L L B A C K S |
|
238 |
#------------------------------------------------------------------- |
|
239 |
||
240 |
proc default_error_callback( c: StompClient, response: StompResponse ) = |
|
241 |
## Something bad happened. Disconnect from the server, build an error message, |
|
242 |
## and raise an exception. |
|
243 |
c.socket.close |
|
244 |
c.connected = false |
|
245 |
||
246 |
var detail = response.payload |
|
247 |
var msg = response[ "message" ] |
|
248 |
if $detail[ ^1 ] == "\n": detail = detail[ 0 .. ^2 ] # chomp |
|
249 |
||
250 |
if detail.len > 0: msg = msg & " (" & detail & ")" |
|
251 |
raise newException( StompError, "ERROR: " & msg ) |
|
252 |
||
253 |
||
254 |
proc default_missed_heartbeat_callback( c: StompClient ) = |
|
255 |
## Timeout while connected to the broker. |
|
256 |
c.socket.close |
|
257 |
c.connected = false |
|
258 |
raise newException( StompError, "Heartbeat timeout. Last activity: " & $c.last_msgtime ) |
|
259 |
||
260 |
||
261 |
||
262 |
#------------------------------------------------------------------- |
|
263 |
# C L I E N T |
|
264 |
#------------------------------------------------------------------- |
|
265 |
||
266 |
proc newStompClient*( s: Socket, uri: string ): StompClient = |
|
267 |
## Create a new Stomp client object from a preexisting **socket**, |
|
268 |
## and a stomp **URI** string. |
|
269 |
## |
|
270 |
## .. code-block:: nim |
|
271 |
## |
|
272 |
## var socket = newSocket() |
|
273 |
## var stomp = newStompClient( socket, "stomp://test:test@example.com/%2Fvhost" ) |
|
274 |
## |
|
275 |
## or if connecting with SSL, when compiled with -d:ssl: |
|
276 |
## |
|
277 |
## .. code-block:: nim |
|
278 |
## |
|
279 |
## var socket = newSocket() |
|
280 |
## let sslContext = newContext( verifyMode = CVerifyNone ) |
|
281 |
## sslContext.wrapSocket(socket) |
|
282 |
## var stomp = newStompClient( socket, "stomp+ssl://test:test@example.com/%2Fvhost" ) |
|
283 |
## |
|
6
7d977f308c75
Incorporate a series of patches from Zack Smith.
Zach Smith <zd@zdsmith.com>
parents:
4
diff
changeset
|
284 |
|
7d977f308c75
Incorporate a series of patches from Zack Smith.
Zach Smith <zd@zdsmith.com>
parents:
4
diff
changeset
|
285 |
let |
7d977f308c75
Incorporate a series of patches from Zack Smith.
Zach Smith <zd@zdsmith.com>
parents:
4
diff
changeset
|
286 |
uri = parse_uri( uri ) |
7d977f308c75
Incorporate a series of patches from Zack Smith.
Zach Smith <zd@zdsmith.com>
parents:
4
diff
changeset
|
287 |
vhost = if uri.path.len > 1: uri.path.strip( chars = {'/'}, trailing = false ) else: uri.path |
7d977f308c75
Incorporate a series of patches from Zack Smith.
Zach Smith <zd@zdsmith.com>
parents:
4
diff
changeset
|
288 |
|
0 | 289 |
new( result ) |
290 |
result.socket = s |
|
291 |
result.connected = false |
|
6
7d977f308c75
Incorporate a series of patches from Zack Smith.
Zach Smith <zd@zdsmith.com>
parents:
4
diff
changeset
|
292 |
result.uri = uri |
7d977f308c75
Incorporate a series of patches from Zack Smith.
Zach Smith <zd@zdsmith.com>
parents:
4
diff
changeset
|
293 |
result.username = uri.username |
7d977f308c75
Incorporate a series of patches from Zack Smith.
Zach Smith <zd@zdsmith.com>
parents:
4
diff
changeset
|
294 |
result.password = uri.password |
7d977f308c75
Incorporate a series of patches from Zack Smith.
Zach Smith <zd@zdsmith.com>
parents:
4
diff
changeset
|
295 |
result.host = uri.hostname |
7d977f308c75
Incorporate a series of patches from Zack Smith.
Zach Smith <zd@zdsmith.com>
parents:
4
diff
changeset
|
296 |
result.vhost = vhost |
0 | 297 |
result.timeout = 500 |
298 |
result.subscriptions = @[] |
|
299 |
result.transactions = @[] |
|
300 |
||
301 |
# Parse any supported options in the query string. |
|
302 |
# |
|
303 |
for pairs in result.uri.query.split( '&' ): |
|
304 |
let opt = pairs.split( '=' ) |
|
305 |
try: |
|
306 |
case opt[0]: |
|
307 |
of "heartbeat": |
|
308 |
result.options.heartbeat = opt[1].parse_int |
|
309 |
else: |
|
310 |
discard |
|
311 |
except IndexError, ValueError: |
|
312 |
discard |
|
313 |
||
314 |
# Set default STOMP port if otherwise unset. |
|
315 |
# |
|
316 |
if not result.uri.scheme.contains( "stomp" ): |
|
317 |
raise newException( StompError, "Unknown scheme: " & result.uri.scheme ) |
|
318 |
var port: int |
|
319 |
if result.uri.port == "": |
|
320 |
if result.uri.scheme.contains( "+ssl" ): |
|
321 |
port = 61614 |
|
322 |
else: |
|
323 |
port = 61613 |
|
324 |
else: |
|
325 |
port = result.uri.port.parse_int |
|
326 |
||
327 |
result.port = Port( port ) |
|
328 |
||
329 |
# Decode URI encoded slashes for vhosts. |
|
330 |
result.vhost = result.vhost.replace( "%2f", "/" ).replace( "%2F", "/" ).replace( "//", "/" ) |
|
331 |
||
332 |
||
333 |
proc socksend( c: StompClient, data: string ): void = |
|
334 |
## Send data on the connected socket with optional debug output. |
|
335 |
c.socket.send( data ) |
|
336 |
if defined( debug ): printf " --> %s", data |
|
337 |
||
338 |
||
339 |
proc finmsg( c: StompClient ): void = |
|
340 |
## Send data on the connected socket with optional debug output. |
|
341 |
c.socket.send( CRLF & NULL & CRLF ) |
|
342 |
if defined( debug ): printf " --> \n --> ^@\n\n" |
|
343 |
||
344 |
||
345 |
proc `[]`*( c: StompClient, key: string ): string = |
|
346 |
## Get a specific value from the server metadata, set during the initial connection. |
|
4
2f4e88604125
Re-arrange for nimble, update to Nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents:
0
diff
changeset
|
347 |
if not c.connected: return "" |
0 | 348 |
for header in c.serverinfo: |
349 |
if cmpIgnoreCase( key, header.name ) == 0: |
|
350 |
return header.value |
|
4
2f4e88604125
Re-arrange for nimble, update to Nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents:
0
diff
changeset
|
351 |
return "" |
0 | 352 |
|
353 |
||
354 |
proc `$`*( c: StompClient ): string = |
|
355 |
## Represent the stomp client as a string, after masking the password. |
|
356 |
let uri = ( $c.uri ).replace( ":" & c.uri.password & "@", "@" ) |
|
357 |
result = "(NimStomp v" & VERSION & ( if c.connected: " connected" else: " not connected" ) & " to " & uri |
|
4
2f4e88604125
Re-arrange for nimble, update to Nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents:
0
diff
changeset
|
358 |
if not ( c[ "server" ] == "" ): result.add( " --> " & c["server"] ) |
0 | 359 |
result.add( ")" ) |
360 |
||
361 |
||
362 |
proc connect*( c: StompClient ): void = |
|
363 |
## Establish a connection to the Stomp server. |
|
364 |
if c.connected: return |
|
365 |
||
366 |
var headers: seq[ tuple[name: string, value: string] ] = @[] |
|
367 |
headers.add( ("accept-version", "1.2") ) |
|
368 |
||
369 |
# Stomp 1.2 requires the Host: header. Use the path as a vhost if |
|
370 |
# supplied, otherwise use the hostname of the server. |
|
371 |
# |
|
372 |
if c.vhost != "": |
|
373 |
headers.add( ("host", c.vhost) ) |
|
374 |
else: |
|
375 |
headers.add( ("host", c.host) ) |
|
376 |
||
377 |
if c.username != "": headers.add( ("login", c.username) ) |
|
378 |
if c.password != "": headers.add( ("passcode", c.password) ) |
|
379 |
if c.options.heartbeat > 0: |
|
380 |
let heartbeat = c.options.heartbeat * 1000 |
|
381 |
headers.add( ("heart-beat", "0," & $heartbeat) ) |
|
382 |
||
383 |
# Connect the socket and send the headers off. |
|
384 |
# |
|
385 |
c.socket.connect( c.host, c.port ) |
|
386 |
c.socksend( "CONNECT" & CRLF ) |
|
387 |
for header in headers: |
|
388 |
c.socksend( header.name & ":" & header.value & CRLF ) |
|
389 |
c.finmsg |
|
390 |
||
391 |
# Retreive and copy server metadata to client object. |
|
392 |
# |
|
393 |
var response = newStompResponse( c ) |
|
394 |
c.serverinfo = response.headers |
|
395 |
||
396 |
if response.frame != "CONNECTED": |
|
397 |
if not isNil( c.error_callback ): |
|
398 |
c.error_callback( c, response ) |
|
399 |
else: |
|
400 |
c.default_error_callback( response ) |
|
401 |
else: |
|
402 |
c.connected = true |
|
403 |
if not isNil( c.connected_callback ): |
|
404 |
c.connected_callback( c, response ) |
|
405 |
||
406 |
||
407 |
proc disconnect*( c: StompClient ): void = |
|
408 |
## Break down the connection to the Stomp server nicely. |
|
409 |
if not c.connected: return |
|
410 |
c.socksend( "DISCONNECT" & CRLF ) |
|
411 |
c.finmsg |
|
412 |
||
413 |
c.socket.close |
|
414 |
c.connected = false |
|
415 |
||
416 |
||
417 |
proc add_txn( c: StompClient ): void = |
|
418 |
## Add a transaction header if there is only a single open txn. |
|
419 |
if c.transactions.len != 1: return |
|
420 |
c.socksend( "transaction:" & c.transactions[0] & CRLF ) |
|
421 |
||
422 |
||
423 |
proc send*( c: StompClient, |
|
424 |
destination: string, |
|
4
2f4e88604125
Re-arrange for nimble, update to Nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents:
0
diff
changeset
|
425 |
message: string = "", |
2f4e88604125
Re-arrange for nimble, update to Nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents:
0
diff
changeset
|
426 |
contenttype: string = "", |
0 | 427 |
headers: seq[ tuple[name: string, value: string] ] = @[] ): void = |
428 |
## Send a **message** to **destination**. |
|
429 |
## |
|
430 |
## A Content-Length header is automatically and always included. |
|
431 |
## A **contenttype** is optional, but strongly recommended. |
|
432 |
## |
|
433 |
## Additionally, a transaction ID is automatically added if there is only |
|
434 |
## one transaction active. If you need to attach this message to a particular |
|
435 |
## transaction ID, you'll need to add it yourself with the user defined |
|
436 |
## **headers**. |
|
437 |
||
438 |
if not c.connected: raise newException( StompError, "Client is not connected." ) |
|
439 |
c.socksend( "SEND" & CRLF ) |
|
440 |
c.socksend( "destination:" & destination & CRLF ) |
|
441 |
c.socksend( "content-length:" & $message.len & CRLF ) |
|
4
2f4e88604125
Re-arrange for nimble, update to Nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents:
0
diff
changeset
|
442 |
if not ( contenttype == "" ): c.socksend( "content-type:" & contenttype & CRLF ) |
0 | 443 |
|
444 |
# Add custom headers. Add transaction header if one isn't manually |
|
445 |
# present (and a transaction is open.) |
|
446 |
# |
|
447 |
var txn_seen = false |
|
448 |
for header in headers: |
|
449 |
if header.name == "transaction": txn_seen = true |
|
450 |
c.socksend( header.name & ":" & header.value & CRLF ) |
|
451 |
if not txn_seen: c.add_txn |
|
452 |
||
4
2f4e88604125
Re-arrange for nimble, update to Nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents:
0
diff
changeset
|
453 |
if message == "": |
0 | 454 |
c.finmsg |
455 |
else: |
|
456 |
c.socket.send( CRLF & message & NULL ) |
|
457 |
if defined( debug ): printf " -->\n --> (payload)^@\n\n" |
|
458 |
||
459 |
||
460 |
proc subscribe*( c: StompClient, |
|
461 |
destination: string, |
|
462 |
ack = "auto", |
|
6
7d977f308c75
Incorporate a series of patches from Zack Smith.
Zach Smith <zd@zdsmith.com>
parents:
4
diff
changeset
|
463 |
id: string = "", |
7d977f308c75
Incorporate a series of patches from Zack Smith.
Zach Smith <zd@zdsmith.com>
parents:
4
diff
changeset
|
464 |
headers: seq[ tuple[name: string, value: string] ] = @[] ): void = |
0 | 465 |
## Subscribe to messages at **destination**. |
466 |
## |
|
467 |
## Setting **ack** to "client" or "client-individual" enables client ACK/NACK mode. |
|
468 |
## In this mode, incoming messages aren't considered processed by |
|
469 |
## the server unless they receive ACK. By default, the server |
|
470 |
## considers the message processed if a client simply accepts it. |
|
471 |
## |
|
472 |
## You may optionally add any additional **headers** the server may support. |
|
473 |
||
474 |
if not c.connected: raise newException( StompError, "Client is not connected." ) |
|
475 |
c.socksend( "SUBSCRIBE" & CRLF ) |
|
476 |
c.socksend( "destination:" & destination & CRLF ) |
|
6
7d977f308c75
Incorporate a series of patches from Zack Smith.
Zach Smith <zd@zdsmith.com>
parents:
4
diff
changeset
|
477 |
|
7d977f308c75
Incorporate a series of patches from Zack Smith.
Zach Smith <zd@zdsmith.com>
parents:
4
diff
changeset
|
478 |
if id == "": |
7d977f308c75
Incorporate a series of patches from Zack Smith.
Zach Smith <zd@zdsmith.com>
parents:
4
diff
changeset
|
479 |
c.socksend( "id:" & $c.subscriptions.len & CRLF ) |
7d977f308c75
Incorporate a series of patches from Zack Smith.
Zach Smith <zd@zdsmith.com>
parents:
4
diff
changeset
|
480 |
else: |
7d977f308c75
Incorporate a series of patches from Zack Smith.
Zach Smith <zd@zdsmith.com>
parents:
4
diff
changeset
|
481 |
c.socksend( "id:" & id & CRLF ) |
7d977f308c75
Incorporate a series of patches from Zack Smith.
Zach Smith <zd@zdsmith.com>
parents:
4
diff
changeset
|
482 |
|
0 | 483 |
if ack == "client" or ack == "client-individual": |
484 |
c.socksend( "ack:" & ack & CRLF ) |
|
485 |
else: |
|
486 |
if ack != "auto": raise newException( StompError, "Unknown ack type: " & ack ) |
|
487 |
||
488 |
for header in headers: |
|
489 |
c.socksend( header.name & ":" & header.value & CRLF ) |
|
490 |
c.finmsg |
|
491 |
c.subscriptions.add( destination ) |
|
492 |
||
493 |
||
494 |
proc unsubscribe*( c: StompClient, |
|
495 |
destination: string, |
|
496 |
headers: seq[ tuple[name: string, value: string] ] = @[] ): void = |
|
497 |
## Unsubscribe from messages at **destination**. |
|
498 |
## You may optionally add any additional **headers** the server may support. |
|
499 |
||
500 |
if not c.connected: raise newException( StompError, "Client is not connected." ) |
|
501 |
var |
|
502 |
sub_id: int |
|
503 |
i = 0 |
|
504 |
||
505 |
# Find the ID of the subscription. |
|
506 |
# |
|
507 |
for sub in c.subscriptions: |
|
508 |
if sub == destination: |
|
509 |
sub_id = i |
|
510 |
break |
|
511 |
i = i + 1 |
|
512 |
||
513 |
c.socksend( "UNSUBSCRIBE" & CRLF ) |
|
514 |
c.socksend( "id:" & $sub_id & CRLF ) |
|
515 |
for header in headers: |
|
516 |
c.socksend( header.name & ":" & header.value & CRLF ) |
|
517 |
c.finmsg |
|
518 |
c.subscriptions[ sub_id ] = "" |
|
519 |
||
520 |
||
521 |
proc begin*( c: StompClient, txn: string ): void = |
|
522 |
## Begin a new transaction on the broker, using **txn** as the identifier. |
|
523 |
c.socksend( "BEGIN" & CRLF ) |
|
524 |
c.socksend( "transaction:" & txn & CRLF ) |
|
525 |
c.finmsg |
|
526 |
c.transactions.add( txn ) |
|
527 |
||
528 |
||
4
2f4e88604125
Re-arrange for nimble, update to Nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents:
0
diff
changeset
|
529 |
proc commit*( c: StompClient, txn: string = "" ): void = |
0 | 530 |
## Finish a specific transaction **txn**, or the most current if unspecified. |
531 |
var transaction = txn |
|
4
2f4e88604125
Re-arrange for nimble, update to Nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents:
0
diff
changeset
|
532 |
if transaction == "" and c.transactions.len > 0: transaction = c.transactions.pop |
2f4e88604125
Re-arrange for nimble, update to Nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents:
0
diff
changeset
|
533 |
if transaction == "": return |
0 | 534 |
|
535 |
c.socksend( "COMMIT" & CRLF ) |
|
536 |
c.socksend( "transaction:" & transaction & CRLF ) |
|
537 |
c.finmsg |
|
538 |
||
539 |
# Remove the transaction from the queue. |
|
540 |
# |
|
541 |
var new_transactions: seq[ string ] = @[] |
|
542 |
for txn in c.transactions: |
|
543 |
if txn != transaction: new_transactions.add( txn ) |
|
544 |
c.transactions = new_transactions |
|
545 |
||
546 |
||
4
2f4e88604125
Re-arrange for nimble, update to Nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents:
0
diff
changeset
|
547 |
proc abort*( c: StompClient, txn: string = "" ): void = |
0 | 548 |
## Cancel a specific transaction **txn**, or the most current if unspecified. |
549 |
var transaction = txn |
|
4
2f4e88604125
Re-arrange for nimble, update to Nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents:
0
diff
changeset
|
550 |
if transaction == "" and c.transactions.len > 0: transaction = c.transactions.pop |
2f4e88604125
Re-arrange for nimble, update to Nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents:
0
diff
changeset
|
551 |
if transaction == "": return |
0 | 552 |
|
553 |
c.socksend( "ABORT" & CRLF ) |
|
554 |
c.socksend( "transaction:" & transaction & CRLF ) |
|
555 |
c.finmsg |
|
556 |
||
557 |
# Remove the transaction from the queue. |
|
558 |
# |
|
559 |
var new_transactions: seq[ string ] = @[] |
|
560 |
for txn in c.transactions: |
|
561 |
if txn != transaction: new_transactions.add( txn ) |
|
562 |
c.transactions = new_transactions |
|
563 |
||
564 |
||
4
2f4e88604125
Re-arrange for nimble, update to Nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents:
0
diff
changeset
|
565 |
proc ack*( c: StompClient, id: string, transaction: string = "" ): void = |
0 | 566 |
## Acknowledge message **id**. Optionally, attach this acknowledgement |
567 |
## to a specific **transaction** -- if there's only one active, it is |
|
568 |
## added automatically. |
|
569 |
c.socksend( "ACK" & CRLF ) |
|
570 |
c.socksend( "id:" & id & CRLF ) |
|
4
2f4e88604125
Re-arrange for nimble, update to Nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents:
0
diff
changeset
|
571 |
if not ( transaction == "" ): |
0 | 572 |
c.socksend( "transaction:" & transaction & CRLF ) |
573 |
else: |
|
574 |
c.add_txn |
|
575 |
c.finmsg |
|
576 |
||
577 |
||
4
2f4e88604125
Re-arrange for nimble, update to Nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents:
0
diff
changeset
|
578 |
proc nack*( c: StompClient, id: string, transaction: string = "" ): void = |
0 | 579 |
## Reject message **id**. Optionally, attach this rejection to a |
580 |
## specific **transaction** -- if there's only one active, it is |
|
581 |
## added automatically. |
|
582 |
## |
|
583 |
## Subscribe to a queue with ACK mode enabled, and reject the message |
|
584 |
## on error: |
|
585 |
## |
|
586 |
## .. code-block:: nim |
|
587 |
## |
|
588 |
## stomp.subscribe( "/queue/test", "client-individual" ) |
|
589 |
## FIXME: attach procs |
|
590 |
## stomp.wait_for_messages |
|
591 |
## |
|
592 |
c.socksend( "NACK" & CRLF ) |
|
593 |
c.socksend( "id:" & id & CRLF ) |
|
4
2f4e88604125
Re-arrange for nimble, update to Nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents:
0
diff
changeset
|
594 |
if not ( transaction == "" ): |
0 | 595 |
c.socksend( "transaction:" & transaction & CRLF ) |
596 |
else: |
|
597 |
c.add_txn |
|
598 |
c.finmsg |
|
599 |
||
600 |
||
601 |
proc wait_for_messages*( c: StompClient, loop=true ) = |
|
602 |
## Enter a blocking select loop, dispatching to the appropriate proc |
|
603 |
## for the received message type. Return after a single message |
|
604 |
## is received if **loop** is set to **false**. |
|
605 |
||
606 |
if not c.connected: raise newException( StompError, "Client is not connected." ) |
|
607 |
||
608 |
while true: |
|
609 |
var |
|
610 |
timeout: int |
|
611 |
fds = @[ c.socket.get_fd ] |
|
612 |
||
613 |
# Check for missed heartbeats, with an additional second |
|
614 |
# of wiggle-room. |
|
615 |
# |
|
616 |
if c.options.heartbeat > 0: |
|
617 |
timeout = ( c.options.heartbeat + 1 ) * 1000 |
|
618 |
else: |
|
619 |
timeout = -1 |
|
620 |
||
4
2f4e88604125
Re-arrange for nimble, update to Nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents:
0
diff
changeset
|
621 |
if select_read( fds, timeout ) == 0: # timeout, only happens if heartbeating missed |
0 | 622 |
if not isNil( c.missed_heartbeat_callback ): |
623 |
c.missed_heartbeat_callback( c ) |
|
624 |
else: |
|
625 |
c.default_missed_heartbeat_callback |
|
626 |
if loop: continue else: break |
|
627 |
||
628 |
let response = newStompResponse( c ) |
|
629 |
case response.frame: |
|
630 |
||
631 |
of "HEARTBEAT": |
|
632 |
if not isNil( c.heartbeat_callback ): |
|
633 |
c.heartbeat_callback( c, response ) |
|
634 |
continue |
|
635 |
||
636 |
of "RECEIPT": |
|
637 |
if not isNil( c.receipt_callback ): |
|
638 |
c.receipt_callback( c, response ) |
|
639 |
||
640 |
of "MESSAGE": |
|
641 |
if not isNil( c.message_callback ): |
|
642 |
c.message_callback( c, response ) |
|
643 |
||
644 |
of "ERROR": |
|
645 |
if not isNil( c.error_callback ): |
|
646 |
c.error_callback( c, response ) |
|
647 |
else: |
|
648 |
c.default_error_callback( response ) |
|
649 |
||
650 |
else: |
|
651 |
if defined( debug ): |
|
652 |
echo "Strange broker frame: " & response.repr |
|
653 |
||
654 |
if not loop: break |
|
655 |
||
656 |
||
657 |
||
658 |
#------------------------------------------------------------------- |
|
659 |
# T E S T S |
|
660 |
#------------------------------------------------------------------- |
|
661 |
||
662 |
# Functional (rather than unit) tests. Requires a Stomp compatible broker. |
|
663 |
# This was tested against RabbitMQ 3.5.3 and 3.6.0. |
|
664 |
# 3.6.0 was -so- much faster. |
|
665 |
# |
|
666 |
# First start up a message receiver: |
|
667 |
# ./stomp receiver [stomp-uri] [subscription-destination] |
|
668 |
# |
|
669 |
# then run another process, to publish stuff: |
|
670 |
# ./stomp publisher [stomp-uri] [publish-destination] |
|
671 |
# |
|
672 |
# An example with an AMQP "direct" exchange, and an exclusive queue: |
|
6
7d977f308c75
Incorporate a series of patches from Zack Smith.
Zach Smith <zd@zdsmith.com>
parents:
4
diff
changeset
|
673 |
# ./stomp publisher stomp://test:test@localhost/%2F?heartbeat=10 /exchange/test |
7d977f308c75
Incorporate a series of patches from Zack Smith.
Zach Smith <zd@zdsmith.com>
parents:
4
diff
changeset
|
674 |
# ./stomp receiver stomp://test:test@localhost/%2F?heartbeat=10 /exchange/test |
0 | 675 |
# |
676 |
# Then just let 'er run. |
|
677 |
# |
|
6
7d977f308c75
Incorporate a series of patches from Zack Smith.
Zach Smith <zd@zdsmith.com>
parents:
4
diff
changeset
|
678 |
# You can also run a naive benchmark (deliveries/sec): |
0 | 679 |
# |
6
7d977f308c75
Incorporate a series of patches from Zack Smith.
Zach Smith <zd@zdsmith.com>
parents:
4
diff
changeset
|
680 |
# ./stomp benchmark stomp://test:test@localhost%2F /exchange/test |
0 | 681 |
# |
682 |
# It will set messages to require acknowledgement, and nack everything, causing |
|
683 |
# a delivery loop for 10 seconds. |
|
684 |
# |
|
685 |
when isMainModule: |
|
686 |
let expected = 8 |
|
687 |
var |
|
688 |
socket = newSocket() |
|
689 |
messages: seq[ StompResponse ] = @[] |
|
690 |
||
4
2f4e88604125
Re-arrange for nimble, update to Nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents:
0
diff
changeset
|
691 |
let usage = """ |
2f4e88604125
Re-arrange for nimble, update to Nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents:
0
diff
changeset
|
692 |
First start up a message receiver: |
2f4e88604125
Re-arrange for nimble, update to Nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents:
0
diff
changeset
|
693 |
./stomp receiver [stomp-uri] [subscription-destination] |
2f4e88604125
Re-arrange for nimble, update to Nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents:
0
diff
changeset
|
694 |
|
2f4e88604125
Re-arrange for nimble, update to Nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents:
0
diff
changeset
|
695 |
then run another process, to publish stuff: |
2f4e88604125
Re-arrange for nimble, update to Nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents:
0
diff
changeset
|
696 |
./stomp publisher [stomp-uri] [publish-destination] |
2f4e88604125
Re-arrange for nimble, update to Nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents:
0
diff
changeset
|
697 |
|
2f4e88604125
Re-arrange for nimble, update to Nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents:
0
diff
changeset
|
698 |
An example with an AMQP "direct" exchange, and an exclusive queue: |
6
7d977f308c75
Incorporate a series of patches from Zack Smith.
Zach Smith <zd@zdsmith.com>
parents:
4
diff
changeset
|
699 |
./stomp publisher stomp://test:test@localhost/%2F?heartbeat=10 /exchange/test |
7d977f308c75
Incorporate a series of patches from Zack Smith.
Zach Smith <zd@zdsmith.com>
parents:
4
diff
changeset
|
700 |
./stomp receiver stomp://test:test@localhost/%2F?heartbeat=10 /exchange/test |
4
2f4e88604125
Re-arrange for nimble, update to Nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents:
0
diff
changeset
|
701 |
|
2f4e88604125
Re-arrange for nimble, update to Nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents:
0
diff
changeset
|
702 |
Then just let 'er run. |
2f4e88604125
Re-arrange for nimble, update to Nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents:
0
diff
changeset
|
703 |
|
6
7d977f308c75
Incorporate a series of patches from Zack Smith.
Zach Smith <zd@zdsmith.com>
parents:
4
diff
changeset
|
704 |
You can also run a naive benchmark (deliveries/sec): |
4
2f4e88604125
Re-arrange for nimble, update to Nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents:
0
diff
changeset
|
705 |
|
6
7d977f308c75
Incorporate a series of patches from Zack Smith.
Zach Smith <zd@zdsmith.com>
parents:
4
diff
changeset
|
706 |
./stomp benchmark stomp://test:test@localhost/%2F /exchange/test |
4
2f4e88604125
Re-arrange for nimble, update to Nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents:
0
diff
changeset
|
707 |
|
2f4e88604125
Re-arrange for nimble, update to Nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents:
0
diff
changeset
|
708 |
It will set messages to require acknowledgement, and nack everything, causing |
2f4e88604125
Re-arrange for nimble, update to Nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents:
0
diff
changeset
|
709 |
a delivery loop for 10 seconds. |
6
7d977f308c75
Incorporate a series of patches from Zack Smith.
Zach Smith <zd@zdsmith.com>
parents:
4
diff
changeset
|
710 |
If your vhost requires slashes, use URI escaping: /%2Ftest |
4
2f4e88604125
Re-arrange for nimble, update to Nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents:
0
diff
changeset
|
711 |
""" |
2f4e88604125
Re-arrange for nimble, update to Nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents:
0
diff
changeset
|
712 |
|
2f4e88604125
Re-arrange for nimble, update to Nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents:
0
diff
changeset
|
713 |
|
2f4e88604125
Re-arrange for nimble, update to Nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents:
0
diff
changeset
|
714 |
if paramCount() != 3: quit usage |
0 | 715 |
|
716 |
var stomp = newStompClient( socket, paramStr(2) ) |
|
717 |
stomp.connect |
|
718 |
echo stomp |
|
719 |
||
720 |
case paramStr(1): |
|
721 |
||
722 |
of "benchmark": |
|
723 |
echo "* Running for 10 seconds. Compile with -d:debug to see the Stomp conversation." |
|
724 |
var count = 0 |
|
725 |
var start = get_time() |
|
726 |
||
727 |
proc incr( c: StompClient, r: StompResponse ) = |
|
728 |
let id = r["ack"] |
|
729 |
count = count + 1 |
|
730 |
c.nack( id ) |
|
731 |
||
732 |
stomp.message_callback = incr |
|
733 |
stomp.subscribe( paramStr(3), "client" ) |
|
734 |
stomp.send( paramStr(3), "hi." ) |
|
4
2f4e88604125
Re-arrange for nimble, update to Nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents:
0
diff
changeset
|
735 |
while get_time() < start + 10.seconds: |
0 | 736 |
stomp.wait_for_messages( false ) |
737 |
||
738 |
printf "* Processed %d messages in 10 seconds.\n", count |
|
739 |
stomp.disconnect |
|
740 |
||
741 |
||
742 |
# Store incoming messages, ensure their contents match our expected behavior. |
|
743 |
# |
|
744 |
of "receiver": |
|
745 |
var heartbeats = 0 |
|
746 |
echo "* Waiting on messages from publisher. Compile with -d:debug to see the Stomp conversation." |
|
747 |
||
748 |
proc receive_message( c: StompClient, r: StompResponse ) = |
|
749 |
messages.add( r ) |
|
750 |
case r.frame: |
|
751 |
of "RECEIPT": |
|
752 |
discard |
|
753 |
of "MESSAGE": |
|
754 |
let body = r.payload |
|
755 |
let id = r[ "ack" ] |
|
756 |
||
757 |
proc seen_heartbeat( c: StompClient, r: StompResponse ) = |
|
758 |
heartbeats = heartbeats + 1 |
|
759 |
||
760 |
stomp.message_callback = receive_message |
|
761 |
stomp.receipt_callback = receive_message |
|
762 |
stomp.heartbeat_callback = seen_heartbeat |
|
763 |
stomp.subscribe( paramStr(3) ) |
|
764 |
||
765 |
# Populate the messages sequence with the count of expected messages. |
|
766 |
for i in 1..expected: stomp.wait_for_messages( false ) |
|
767 |
||
768 |
# Assertions on the results! |
|
769 |
# |
|
770 |
doAssert( messages.len == expected ) |
|
4
2f4e88604125
Re-arrange for nimble, update to Nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents:
0
diff
changeset
|
771 |
doAssert( messages[0].payload == "" ) |
0 | 772 |
|
773 |
doAssert( messages[1].payload == "Hello world!" ) |
|
774 |
||
775 |
doAssert( messages[2].payload == "Dumb.\n\n" ) |
|
776 |
||
777 |
doAssert( messages[3].payload == "Hello again." ) |
|
778 |
doAssert( messages[3][ "content-type" ] == "text/plain" ) |
|
779 |
doAssert( messages[3][ "Content-Type" ] == "text/plain" ) |
|
780 |
||
781 |
doAssert( messages[4][ "x-custom" ] == "yum" ) |
|
782 |
||
783 |
doAssert( messages[5][ "receipt" ] == "42" ) |
|
784 |
||
785 |
doAssert( messages[6].payload == "transaction!" ) |
|
786 |
doAssert( messages[7].payload == "transaction 2" ) |
|
787 |
||
788 |
stomp.disconnect |
|
789 |
||
790 |
if heartbeats > 0: |
|
791 |
printf "* Tests passed! %d heartbeats seen.", heartbeats |
|
792 |
else: |
|
793 |
echo "* Tests passed!" |
|
794 |
||
795 |
||
796 |
# Publish a variety of messages with various options. |
|
797 |
# Pause momentarily between sends(), as brokers -might- impose |
|
798 |
# rate limits and/or message dropping. |
|
799 |
# |
|
800 |
of "publisher": |
|
801 |
echo "* Publishing to receiver. Compile with -d:debug to see the Stomp conversation." |
|
802 |
||
803 |
# Simple, no frills event. |
|
804 |
stomp.send( paramStr(3) ) |
|
805 |
sleep 500 |
|
806 |
||
807 |
# Event with a body. |
|
808 |
stomp.send( paramStr(3), "Hello world!" ) |
|
809 |
sleep 500 |
|
810 |
||
811 |
# Event that doesn't contain a content-length. |
|
812 |
# (Note, the broker may elect to add one on your behalf, which is a good thing... |
|
813 |
# but invalidates this test.) |
|
814 |
stomp.socksend( "SEND" & CRLF ) |
|
815 |
stomp.socksend( "destination:" & paramStr(3) & CRLF & CRLF ) |
|
816 |
stomp.socksend( "Dumb.\n\n" & NULL ) |
|
817 |
sleep 500 |
|
818 |
||
819 |
# Content-Type |
|
820 |
stomp.send( paramStr(3), "Hello again.", "text/plain" ) |
|
821 |
sleep 500 |
|
822 |
||
823 |
# Custom headers. |
|
824 |
var headers: seq[ tuple[ name: string, value: string ] ] = @[] |
|
825 |
headers.add( ("x-custom", "yum") ) |
|
826 |
stomp.send( paramStr(3), "Hello again.", "text/plain", headers ) |
|
827 |
sleep 500 |
|
828 |
||
829 |
# Receipt requests. |
|
830 |
proc receive_receipt( c: StompClient, r: StompResponse ) = |
|
831 |
messages.add( r ) |
|
832 |
headers = @[] |
|
833 |
headers.add( ("receipt", "42") ) |
|
834 |
stomp.send( paramStr(3), "Hello again.", "text/plain", headers ) |
|
835 |
stomp.receipt_callback = receive_receipt |
|
836 |
stomp.wait_for_messages( false ) |
|
837 |
doAssert( messages[0]["receipt-id"] == "42" ) |
|
838 |
||
839 |
# Aborted transaction. |
|
840 |
stomp.begin( "test-abort" ) |
|
841 |
for i in 1..3: |
|
842 |
stomp.send( paramStr(3), "Message: " & $i ) |
|
843 |
stomp.abort |
|
844 |
||
845 |
# Committed transaction. |
|
846 |
stomp.begin( "test-commit" ) |
|
847 |
stomp.send( paramStr(3), "transaction!" ) |
|
848 |
stomp.commit |
|
849 |
||
850 |
# Mixed transactions. |
|
851 |
for i in 1..3: |
|
852 |
headers = @[] |
|
853 |
headers.add( ("transaction", "test-" & $i ) ) |
|
854 |
stomp.begin( "test-" & $i ) |
|
4
2f4e88604125
Re-arrange for nimble, update to Nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents:
0
diff
changeset
|
855 |
stomp.send( paramStr(3), "transaction " & $i, "", headers ) |
0 | 856 |
sleep 500 |
857 |
stomp.abort( "test-1" ) |
|
858 |
sleep 500 |
|
859 |
stomp.commit( "test-2" ) |
|
860 |
sleep 500 |
|
861 |
stomp.abort( "test-3" ) |
|
862 |
sleep 500 |
|
863 |
||
864 |
stomp.disconnect |
|
865 |
echo "* Tests passed!" |
|
866 |
||
867 |
else: |
|
4
2f4e88604125
Re-arrange for nimble, update to Nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents:
0
diff
changeset
|
868 |
quit usage |
0 | 869 |