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