src/stomp.nim
author Mahlon E. Smith <mahlon@martini.nu>
Mon, 08 Oct 2018 12:11:54 -0700
changeset 4 2f4e88604125
parent 0 stomp.nim@52e9f64937bf
child 6 7d977f308c75
permissions -rw-r--r--
Re-arrange for nimble, update to Nim 0.19.
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
    ##
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   284
    new( result )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   285
    result.socket        = s
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   286
    result.connected     = false
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   287
    result.uri           = parse_uri( uri )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   288
    result.username      = result.uri.username
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   289
    result.password      = result.uri.password
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   290
    result.host          = result.uri.hostname
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   291
    result.vhost         = result.uri.path
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   292
    result.timeout       = 500
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   293
    result.subscriptions = @[]
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   294
    result.transactions  = @[]
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   295
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   296
    # Parse any supported options in the query string.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   297
    #
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   298
    for pairs in result.uri.query.split( '&' ):
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   299
        let opt = pairs.split( '=' )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   300
        try:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   301
            case opt[0]:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   302
                of "heartbeat":
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   303
                    result.options.heartbeat = opt[1].parse_int
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   304
                else:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   305
                    discard
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   306
        except IndexError, ValueError:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   307
            discard
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   308
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   309
    # Set default STOMP port if otherwise unset.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   310
    #
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   311
    if not result.uri.scheme.contains( "stomp" ):
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   312
        raise newException( StompError, "Unknown scheme: " & result.uri.scheme  )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   313
    var port: int
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   314
    if result.uri.port == "":
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   315
        if result.uri.scheme.contains( "+ssl" ):
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   316
            port = 61614
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   317
        else:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   318
            port = 61613
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   319
    else:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   320
        port = result.uri.port.parse_int
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   321
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   322
    result.port = Port( port )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   323
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   324
    # Decode URI encoded slashes for vhosts.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   325
    result.vhost = result.vhost.replace( "%2f", "/" ).replace( "%2F", "/" ).replace( "//", "/" )
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
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   328
proc socksend( c: StompClient, data: string ): void =
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   329
    ## Send data on the connected socket with optional debug output.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   330
    c.socket.send( data )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   331
    if defined( debug ): printf " --> %s", data
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
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   334
proc finmsg( c: StompClient ): void =
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   335
    ## Send data on the connected socket with optional debug output.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   336
    c.socket.send( CRLF & NULL & CRLF )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   337
    if defined( debug ): printf " --> \n --> ^@\n\n"
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
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   340
proc `[]`*( c: StompClient, key: string ): string =
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   341
    ## 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
   342
    if not c.connected: return ""
0
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   343
    for header in c.serverinfo:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   344
        if cmpIgnoreCase( key, header.name ) == 0:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   345
            return header.value
4
2f4e88604125 Re-arrange for nimble, update to Nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   346
    return ""
0
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   347
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   348
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   349
proc `$`*( c: StompClient ): string =
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   350
    ## Represent the stomp client as a string, after masking the password.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   351
    let uri = ( $c.uri ).replace( ":" & c.uri.password & "@", "@" )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   352
    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
   353
    if not ( c[ "server" ] == "" ): result.add( " --> " & c["server"] )
0
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   354
    result.add( ")" )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   355
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   356
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   357
proc connect*( c: StompClient ): void =
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   358
    ## Establish a connection to the Stomp server.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   359
    if c.connected: return
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
    var headers: seq[ tuple[name: string, value: string] ] = @[]
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   362
    headers.add( ("accept-version", "1.2") )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   363
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   364
    # 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
   365
    # supplied, otherwise use the hostname of the server.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   366
    #
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   367
    if c.vhost != "":
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   368
        headers.add( ("host", c.vhost) )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   369
    else:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   370
        headers.add( ("host", c.host) )
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.username != "": headers.add( ("login", c.username) )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   373
    if c.password != "": headers.add( ("passcode", c.password) )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   374
    if c.options.heartbeat > 0:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   375
        let heartbeat = c.options.heartbeat * 1000
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   376
        headers.add( ("heart-beat", "0," & $heartbeat) )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   377
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   378
    # Connect the socket and send the headers off.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   379
    #
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   380
    c.socket.connect( c.host, c.port )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   381
    c.socksend( "CONNECT" & CRLF )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   382
    for header in headers:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   383
        c.socksend( header.name & ":" & header.value & CRLF )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   384
    c.finmsg
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   385
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   386
    # Retreive and copy server metadata to client object.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   387
    #
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   388
    var response = newStompResponse( c )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   389
    c.serverinfo = response.headers
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
    if response.frame != "CONNECTED":
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   392
        if not isNil( c.error_callback ):
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   393
            c.error_callback( c, response )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   394
        else:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   395
            c.default_error_callback( response )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   396
    else:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   397
        c.connected = true
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   398
        if not isNil( c.connected_callback ):
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   399
            c.connected_callback( c, response )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   400
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   401
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   402
proc disconnect*( c: StompClient ): void =
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   403
    ## Break down the connection to the Stomp server nicely.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   404
    if not c.connected: return
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   405
    c.socksend( "DISCONNECT" & CRLF )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   406
    c.finmsg
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   407
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   408
    c.socket.close
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   409
    c.connected = false
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   410
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   411
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   412
proc add_txn( c: StompClient ): void =
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   413
    ## Add a transaction header if there is only a single open txn.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   414
    if c.transactions.len != 1: return
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   415
    c.socksend( "transaction:" & c.transactions[0] & CRLF )
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
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   418
proc send*( c: StompClient,
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   419
            destination: string,
4
2f4e88604125 Re-arrange for nimble, update to Nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   420
            message:     string = "",
2f4e88604125 Re-arrange for nimble, update to Nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   421
            contenttype: string = "",
0
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   422
            headers:     seq[ tuple[name: string, value: string] ] = @[] ): void =
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   423
    ## Send a **message** to **destination**.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   424
    ##
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   425
    ## A Content-Length header is automatically and always included.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   426
    ## A **contenttype** is optional, but strongly recommended.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   427
    ##
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   428
    ## Additionally, a transaction ID is automatically added if there is only
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   429
    ## 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
   430
    ## 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
   431
    ## **headers**.
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
    if not c.connected: raise newException( StompError, "Client is not connected." )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   434
    c.socksend( "SEND" & CRLF )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   435
    c.socksend( "destination:" & destination & CRLF )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   436
    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
   437
    if not ( contenttype == "" ): c.socksend( "content-type:" & contenttype & CRLF )
0
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   438
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   439
    # Add custom headers.  Add transaction header if one isn't manually
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   440
    # present (and a transaction is open.)
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   441
    #
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   442
    var txn_seen = false
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   443
    for header in headers:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   444
        if header.name == "transaction": txn_seen = true
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   445
        c.socksend( header.name & ":" & header.value & CRLF )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   446
    if not txn_seen: c.add_txn
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   447
4
2f4e88604125 Re-arrange for nimble, update to Nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   448
    if message == "":
0
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   449
        c.finmsg
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   450
    else:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   451
        c.socket.send( CRLF & message & NULL )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   452
        if defined( debug ): printf " -->\n --> (payload)^@\n\n"
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   453
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   454
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   455
proc subscribe*( c: StompClient,
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   456
            destination: string,
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   457
            ack        = "auto",
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   458
            headers:     seq[ tuple[name: string, value: string] ] = @[] ): void =
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   459
    ## Subscribe to messages at **destination**.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   460
    ##
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   461
    ## Setting **ack** to "client" or "client-individual" enables client ACK/NACK mode.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   462
    ## In this mode, incoming messages aren't considered processed by
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   463
    ## the server unless they receive ACK.  By default, the server
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   464
    ## considers the message processed if a client simply accepts it.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   465
    ##
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   466
    ## You may optionally add any additional **headers** the server may support.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   467
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   468
    if not c.connected: raise newException( StompError, "Client is not connected." )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   469
    c.socksend( "SUBSCRIBE" & CRLF )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   470
    c.socksend( "destination:" & destination & CRLF )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   471
    c.socksend( "id:" & $c.subscriptions.len & CRLF )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   472
    if ack == "client" or ack == "client-individual":
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   473
        c.socksend( "ack:" & ack & CRLF )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   474
    else:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   475
        if ack != "auto": raise newException( StompError, "Unknown ack type: " & ack )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   476
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   477
    for header in headers:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   478
        c.socksend( header.name & ":" & header.value & CRLF )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   479
    c.finmsg
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   480
    c.subscriptions.add( destination )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   481
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   482
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   483
proc unsubscribe*( c: StompClient,
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   484
            destination: string,
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   485
            headers:     seq[ tuple[name: string, value: string] ] = @[] ): void =
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   486
    ## Unsubscribe from messages at **destination**.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   487
    ## You may optionally add any additional **headers** the server may support.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   488
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   489
    if not c.connected: raise newException( StompError, "Client is not connected." )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   490
    var
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   491
        sub_id: int
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   492
        i = 0
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
    # Find the ID of the subscription.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   495
    #
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   496
    for sub in c.subscriptions:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   497
        if sub == destination:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   498
            sub_id = i
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   499
            break
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   500
        i = i + 1
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   501
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   502
    c.socksend( "UNSUBSCRIBE" & CRLF )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   503
    c.socksend( "id:" & $sub_id & CRLF )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   504
    for header in headers:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   505
        c.socksend( header.name & ":" & header.value & CRLF )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   506
    c.finmsg
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   507
    c.subscriptions[ sub_id ] = ""
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   508
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   509
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   510
proc begin*( c: StompClient, txn: string ): void =
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   511
    ## Begin a new transaction on the broker, using **txn** as the identifier.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   512
    c.socksend( "BEGIN" & CRLF )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   513
    c.socksend( "transaction:" & txn & CRLF )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   514
    c.finmsg
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   515
    c.transactions.add( txn )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   516
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   517
4
2f4e88604125 Re-arrange for nimble, update to Nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   518
proc commit*( c: StompClient, txn: string = "" ): void =
0
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   519
    ## Finish a specific transaction **txn**, or the most current if unspecified.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   520
    var transaction = txn
4
2f4e88604125 Re-arrange for nimble, update to Nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   521
    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
   522
    if transaction == "": return
0
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   523
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   524
    c.socksend( "COMMIT" & CRLF )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   525
    c.socksend( "transaction:" & transaction & CRLF )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   526
    c.finmsg
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
    # Remove the transaction from the queue.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   529
    #
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   530
    var new_transactions: seq[ string ] = @[]
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   531
    for txn in c.transactions:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   532
        if txn != transaction: new_transactions.add( txn )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   533
    c.transactions = new_transactions
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
4
2f4e88604125 Re-arrange for nimble, update to Nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   536
proc abort*( c: StompClient, txn: string = "" ): void =
0
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   537
    ## Cancel a specific transaction **txn**, or the most current if unspecified.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   538
    var transaction = txn
4
2f4e88604125 Re-arrange for nimble, update to Nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   539
    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
   540
    if transaction == "": return
0
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   541
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   542
    c.socksend( "ABORT" & CRLF )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   543
    c.socksend( "transaction:" & transaction & CRLF )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   544
    c.finmsg
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
    # Remove the transaction from the queue.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   547
    #
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   548
    var new_transactions: seq[ string ] = @[]
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   549
    for txn in c.transactions:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   550
        if txn != transaction: new_transactions.add( txn )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   551
    c.transactions = new_transactions
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
4
2f4e88604125 Re-arrange for nimble, update to Nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   554
proc ack*( c: StompClient, id: string, transaction: string = "" ): void =
0
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   555
    ## Acknowledge message **id**.  Optionally, attach this acknowledgement
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   556
    ## to a specific **transaction** -- if there's only one active, it is
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   557
    ## added automatically.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   558
    c.socksend( "ACK" & CRLF )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   559
    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
   560
    if not ( transaction == "" ):
0
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   561
        c.socksend( "transaction:" & transaction & CRLF )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   562
    else:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   563
        c.add_txn
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   564
    c.finmsg
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   565
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   566
4
2f4e88604125 Re-arrange for nimble, update to Nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   567
proc nack*( c: StompClient, id: string, transaction: string = "" ): void =
0
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   568
    ## Reject message **id**.  Optionally, attach this rejection to a
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   569
    ## specific **transaction** -- if there's only one active, it is
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   570
    ## added automatically.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   571
    ##
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   572
    ## Subscribe to a queue with ACK mode enabled, and reject the message
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   573
    ## on error:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   574
    ##
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   575
    ## .. code-block:: nim
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
    ##  stomp.subscribe( "/queue/test", "client-individual" )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   578
    ##  FIXME: attach procs
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   579
    ##  stomp.wait_for_messages
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   580
    ##
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   581
    c.socksend( "NACK" & CRLF )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   582
    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
   583
    if not ( transaction == "" ):
0
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   584
        c.socksend( "transaction:" & transaction & CRLF )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   585
    else:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   586
        c.add_txn
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   587
    c.finmsg
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   588
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   589
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   590
proc wait_for_messages*( c: StompClient, loop=true ) =
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   591
    ## Enter a blocking select loop, dispatching to the appropriate proc
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   592
    ## for the received message type.   Return after a single message
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   593
    ## is received if **loop** is set to **false**.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   594
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   595
    if not c.connected: raise newException( StompError, "Client is not connected." )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   596
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   597
    while true:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   598
        var
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   599
            timeout: int
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   600
            fds = @[ c.socket.get_fd ]
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   601
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   602
        # Check for missed heartbeats, with an additional second
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   603
        # of wiggle-room.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   604
        #
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   605
        if c.options.heartbeat > 0:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   606
             timeout = ( c.options.heartbeat + 1 ) * 1000
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   607
        else:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   608
            timeout = -1
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   609
4
2f4e88604125 Re-arrange for nimble, update to Nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   610
        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
   611
            if not isNil( c.missed_heartbeat_callback ):
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   612
                c.missed_heartbeat_callback( c )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   613
            else:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   614
                c.default_missed_heartbeat_callback
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   615
            if loop: continue else: break
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   616
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   617
        let response = newStompResponse( c )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   618
        case response.frame:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   619
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   620
            of "HEARTBEAT":
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   621
                if not isNil( c.heartbeat_callback ):
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   622
                    c.heartbeat_callback( c, response )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   623
                continue
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   624
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   625
            of "RECEIPT":
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   626
                if not isNil( c.receipt_callback ):
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   627
                    c.receipt_callback( c, response )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   628
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   629
            of "MESSAGE":
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   630
                if not isNil( c.message_callback ):
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   631
                    c.message_callback( c, response )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   632
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   633
            of "ERROR":
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   634
                if not isNil( c.error_callback ):
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   635
                    c.error_callback( c, response )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   636
                else:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   637
                    c.default_error_callback( response )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   638
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   639
            else:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   640
                if defined( debug ):
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   641
                    echo "Strange broker frame: " & response.repr
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   642
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   643
        if not loop: break
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   644
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   645
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   646
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   647
#-------------------------------------------------------------------
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   648
# T E S T S
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
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   651
# Functional (rather than unit) tests.  Requires a Stomp compatible broker.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   652
# This was tested against RabbitMQ 3.5.3 and 3.6.0.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   653
# 3.6.0 was -so- much faster.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   654
#
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   655
# First start up a message receiver:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   656
#   ./stomp receiver [stomp-uri] [subscription-destination]
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
# then run another process, to publish stuff:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   659
#   ./stomp publisher [stomp-uri] [publish-destination]
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
# An example with an AMQP "direct" exchange, and an exclusive queue:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   662
#   ./stomp publisher stomp://test:test@localhost/?heartbeat=10 /exchange/test
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   663
#   ./stomp receiver  stomp://test:test@localhost/?heartbeat=10 /exchange/test
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   664
#
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   665
# Then just let 'er run.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   666
#
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   667
# You can also run a nieve benchmark (deliveries/sec):
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
#   ./stomp benchmark stomp://test:test@localhost/ /exchange/test
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   670
#
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   671
# It will set messages to require acknowledgement, and nack everything, causing
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   672
# a delivery loop for 10 seconds.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   673
#
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   674
when isMainModule:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   675
    let expected = 8
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   676
    var
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   677
        socket   = newSocket()
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   678
        messages: seq[ StompResponse ] = @[]
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   679
4
2f4e88604125 Re-arrange for nimble, update to Nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   680
    let usage = """
2f4e88604125 Re-arrange for nimble, update to Nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   681
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
   682
  ./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
   683
2f4e88604125 Re-arrange for nimble, update to Nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   684
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
   685
  ./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
   686
2f4e88604125 Re-arrange for nimble, update to Nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   687
An example with an AMQP "direct" exchange, and an exclusive queue:
2f4e88604125 Re-arrange for nimble, update to Nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   688
  ./stomp publisher stomp://test:test@localhost/?heartbeat=10 /exchange/test
2f4e88604125 Re-arrange for nimble, update to Nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   689
  ./stomp receiver  stomp://test:test@localhost/?heartbeat=10 /exchange/test
2f4e88604125 Re-arrange for nimble, update to Nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   690
2f4e88604125 Re-arrange for nimble, update to Nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   691
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
   692
2f4e88604125 Re-arrange for nimble, update to Nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   693
You can also run a nieve benchmark (deliveries/sec):
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
  ./stomp benchmark stomp://test:test@localhost/ /exchange/test
2f4e88604125 Re-arrange for nimble, update to Nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   696
2f4e88604125 Re-arrange for nimble, update to Nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   697
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
   698
a delivery loop for 10 seconds.
2f4e88604125 Re-arrange for nimble, update to Nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   699
"""
2f4e88604125 Re-arrange for nimble, update to Nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   700
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
    if paramCount() != 3: quit usage
0
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   703
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   704
    var stomp = newStompClient( socket, paramStr(2) )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   705
    stomp.connect
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   706
    echo stomp
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   707
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   708
    case paramStr(1):
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   709
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   710
        of "benchmark":
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   711
            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
   712
            var count = 0
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   713
            var start = get_time()
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   714
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   715
            proc incr( c: StompClient, r: StompResponse ) =
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   716
                let id = r["ack"]
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   717
                count = count + 1
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   718
                c.nack( id )
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
            stomp.message_callback = incr
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   721
            stomp.subscribe( paramStr(3), "client" )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   722
            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
   723
            while get_time() < start + 10.seconds:
0
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   724
                stomp.wait_for_messages( false )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   725
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   726
            printf "* Processed %d messages in 10 seconds.\n", count
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   727
            stomp.disconnect
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   728
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   729
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   730
        # Store incoming messages, ensure their contents match our expected behavior.
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
        of "receiver":
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   733
            var heartbeats = 0
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   734
            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
   735
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   736
            proc receive_message( c: StompClient, r: StompResponse ) =
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   737
                messages.add( r )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   738
                case r.frame:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   739
                    of "RECEIPT":
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   740
                        discard
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   741
                    of "MESSAGE":
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   742
                        let body = r.payload
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   743
                        let id   = r[ "ack" ]
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   744
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   745
            proc seen_heartbeat( c: StompClient, r: StompResponse ) =
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   746
                heartbeats = heartbeats + 1
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
            stomp.message_callback   = receive_message
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   749
            stomp.receipt_callback   = receive_message
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   750
            stomp.heartbeat_callback = seen_heartbeat
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   751
            stomp.subscribe( paramStr(3) )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   752
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   753
            # Populate the messages sequence with the count of expected messages.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   754
            for i in 1..expected: stomp.wait_for_messages( false )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   755
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   756
            # Assertions on the results!
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   757
            #
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   758
            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
   759
            doAssert( messages[0].payload == "" )
0
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   760
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   761
            doAssert( messages[1].payload == "Hello world!" )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   762
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   763
            doAssert( messages[2].payload == "Dumb.\n\n" )
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
            doAssert( messages[3].payload == "Hello again." )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   766
            doAssert( messages[3][ "content-type" ] == "text/plain" )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   767
            doAssert( messages[3][ "Content-Type" ] == "text/plain" )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   768
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   769
            doAssert( messages[4][ "x-custom" ] == "yum" )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   770
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   771
            doAssert( messages[5][ "receipt" ] == "42" )
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[6].payload == "transaction!" )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   774
            doAssert( messages[7].payload == "transaction 2" )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   775
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   776
            stomp.disconnect
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   777
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   778
            if heartbeats > 0:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   779
                printf "* Tests passed! %d heartbeats seen.", heartbeats
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   780
            else:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   781
                echo "* Tests passed!"
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
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   784
        # Publish a variety of messages with various options.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   785
        # Pause momentarily between sends(), as brokers -might- impose
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   786
        # rate limits and/or message dropping.
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
        of "publisher":
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   789
            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
   790
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   791
            # Simple, no frills event.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   792
            stomp.send( paramStr(3) )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   793
            sleep 500
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
            # Event with a body.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   796
            stomp.send( paramStr(3), "Hello world!" )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   797
            sleep 500
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   798
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   799
            # Event that doesn't contain a content-length.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   800
            # (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
   801
            # but invalidates this test.)
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   802
            stomp.socksend( "SEND" & CRLF )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   803
            stomp.socksend( "destination:" & paramStr(3) & CRLF & CRLF )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   804
            stomp.socksend( "Dumb.\n\n" & NULL )
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
            # Content-Type
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   808
            stomp.send( paramStr(3), "Hello again.", "text/plain" )
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
            # Custom headers.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   812
            var headers: seq[ tuple[ name: string, value: string ] ] = @[]
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   813
            headers.add( ("x-custom", "yum") )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   814
            stomp.send( paramStr(3), "Hello again.", "text/plain", headers )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   815
            sleep 500
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   816
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   817
            # Receipt requests.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   818
            proc receive_receipt( c: StompClient, r: StompResponse ) =
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   819
                messages.add( r )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   820
            headers = @[]
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   821
            headers.add( ("receipt", "42") )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   822
            stomp.send( paramStr(3), "Hello again.", "text/plain", headers )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   823
            stomp.receipt_callback = receive_receipt
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   824
            stomp.wait_for_messages( false )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   825
            doAssert( messages[0]["receipt-id"] == "42" )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   826
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   827
            # Aborted transaction.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   828
            stomp.begin( "test-abort" )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   829
            for i in 1..3:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   830
                stomp.send( paramStr(3), "Message: " & $i )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   831
            stomp.abort
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   832
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   833
            # Committed transaction.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   834
            stomp.begin( "test-commit" )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   835
            stomp.send( paramStr(3), "transaction!" )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   836
            stomp.commit
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   837
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   838
            # Mixed transactions.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   839
            for i in 1..3:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   840
                headers = @[]
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   841
                headers.add( ("transaction", "test-" & $i ) )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   842
                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
   843
                stomp.send( paramStr(3), "transaction " & $i, "", headers )
0
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   844
                sleep 500
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   845
            stomp.abort( "test-1" )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   846
            sleep 500
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   847
            stomp.commit( "test-2" )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   848
            sleep 500
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   849
            stomp.abort( "test-3" )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   850
            sleep 500
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   851
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   852
            stomp.disconnect
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   853
            echo "* Tests passed!"
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   854
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   855
        else:
4
2f4e88604125 Re-arrange for nimble, update to Nim 0.19.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
   856
            quit usage
0
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   857