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