stomp.nim
author Mahlon E. Smith <mahlon@laika.com>
Thu, 24 Mar 2016 18:56:42 -0700
changeset 0 52e9f64937bf
permissions -rw-r--r--
Initial commit.
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
#
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
     3
# Copyright (c) 2016, Mahlon E. Smith <mahlon@martini.nu>
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
## Protocol support
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    53
## ----------------
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    54
## 
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    55
## Examples
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    56
## =========
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    57
##
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    58
## Connecting with SSL
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
##
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    61
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    62
import
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    63
    strutils,
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    64
    nativesockets,
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    65
    net,
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    66
    os,
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    67
    times,
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    68
    uri
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    69
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    70
const
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    71
    VERSION = "0.1.0" ## The current program version.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    72
    NULL    = "\x00"  ## The NULL character.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    73
    CR      = "\r"    ## The carriage return character.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    74
    CRLF    = "\r\n"  ## Carriage return + Line feed (EOL).
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    75
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    76
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    77
# Exceptions
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    78
#
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    79
type
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    80
    StompError* = object of ValueError ## A generic Stomp error state.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    81
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    82
    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
    83
        socket:         Socket ## The socket object attached to this client.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    84
        connected*:     bool ## Is the client currently connected?
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    85
        uri*:           Uri ## The URI used to instantiate this client.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    86
        username:       string ## The Stomp server user, if any.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    87
        password:       string ## The Stomp server password, if any.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    88
        host:           string ## The host or IP address to connect to.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    89
        port:           Port ## Optional, if Stomp is on a non-default port.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    90
        vhost:          string ## Parsed from the URI path, a Stomp "virtual host".
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    91
        timeout*:       int ## Global socket timeout.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    92
        last_msgtime*:  Time ## Timestamp of last seen server message.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    93
        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
    94
        subscriptions*: seq[ string ] ## Registered client subscriptions. Array position is the ID.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    95
        transactions*:  seq[ string ] ## Any currently open transactions.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    96
        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
    97
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    98
        connected_callback*:        proc ( client: StompClient, response: StompResponse ): void
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    99
        error_callback*:            proc ( client: StompClient, response: StompResponse ): void
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   100
        heartbeat_callback*:        proc ( client: StompClient, response: StompResponse ): void
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   101
        message_callback*:          proc ( client: StompClient, response: StompResponse ): void
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   102
        missed_heartbeat_callback*: proc ( client: StompClient ): void
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   103
        receipt_callback*:          proc ( client: StompClient, response: StompResponse ): void
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
    StompResponse* = ref object of RootObj ## A parsed packet from a Stomp server.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   106
        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
   107
        frame*:   string ## The Stomp frame type.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   108
        payload*: string ## The message body, if any.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   109
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   110
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   111
# convenience
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   112
proc printf( formatstr: cstring ) {.header: "<stdio.h>", varargs.}
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
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   115
#-------------------------------------------------------------------
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   116
# R E S P O N S E
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   117
#-------------------------------------------------------------------
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   118
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   119
proc is_eol( s: string ): bool =
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   120
    ## Convenience method, returns **true** if string is a Stomp EOF.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   121
    return s == CR or s == CRLF
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   122
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   123
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   124
proc parse_headers( response: StompResponse, c: StompClient ): int =
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   125
    ## Parse response headers from a stream.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   126
    ## Returns the content length of the response body, or 0.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   127
    result = 0
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   128
    var line = ""
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
    c.socket.readline( line, c.timeout )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   131
    while not line.is_eol:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   132
        if defined( debug ): printf " <-- %s\n", line
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   133
        var header = line.split( ":" )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   134
        if header.len < 2: break
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   135
        response.headers.add( (header[0], header[1]) )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   136
        if cmpIgnoreCase( header[0], "content-length" ) == 0: result = header[1].parse_int
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   137
        c.socket.readline( line, c.timeout )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   138
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
proc parse_payload( response: StompResponse, c: StompClient, bodylength = 0 ): void =
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   141
    ## Parse message payload from a stream.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   142
    let bufsize = 8192
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   143
    var
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   144
        buf  = ""
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   145
        data = ""
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   146
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   147
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   148
    # 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
   149
    #
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   150
    if bodylength > 0:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   151
        var
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   152
            readtotal = 0
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   153
            readamt   = 0
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   154
            remaining = 0
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   155
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   156
        while readtotal != bodylength:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   157
            remaining = bodylength - readtotal
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   158
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   159
            if remaining < bufsize:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   160
                readamt = remaining
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   161
            else:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   162
                readamt = bufsize
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   163
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   164
            buf = newString( readamt )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   165
            readtotal = readtotal + c.socket.recv( buf, readamt, 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
        # Eat the NULL terminator.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   169
        discard c.socket.recv( buf, 1, c.timeout )
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
    # Inefficient path.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   172
    #
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   173
    else:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   174
        while buf != NULL:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   175
            discard c.socket.recv( buf, 1, c.timeout )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   176
            data = data & buf
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
    response.payload = data
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
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   181
proc newStompResponse( c: StompClient ): StompResponse =
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   182
    ## Initialize a response object, which parses and contains
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   183
    ## the Stomp headers and any additional important data from
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   184
    ## the broker socket.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   185
    new( result )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   186
    result.headers = @[]
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   187
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   188
    # Get the frame type, record last seen server activity time.
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
    var line = ""
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   191
    c.socket.readline( line, c.timeout )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   192
    c.last_msgtime = get_time()
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
    # Heartbeat packets (empties.)
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
    # This could -also- parse optional EOLs from the prior
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   197
    # message (after the NULL separator), but since it is a no-op,
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   198
    # this seems harmless.
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
    if line.is_eol:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   201
        result.frame = "HEARTBEAT"
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   202
        return result
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   203
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   204
    # All other types.
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
    result.frame = line
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   207
    if defined( debug ):
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   208
        printf " <-- %s\n", line
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   209
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   210
    # Parse headers and body.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   211
    #
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   212
    var length = result.parse_headers( c )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   213
    if result.frame == "MESSAGE" or result.frame == "ERROR":
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   214
        result.parse_payload( c, length )
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
    # If the response -could- have a body, the NULL has already
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   217
    # been removed from the stream while we checked for one.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   218
    #
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   219
    if result.payload.len > 0:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   220
        if result.payload == NULL: # We checked for a body, but there was none.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   221
            result.payload = nil
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   222
            if defined( debug ): printf " <--\n <-- ^@\n\n"
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   223
        else:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   224
            if defined( debug ): printf " <--\n <-- (payload)^@\n\n"
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   225
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   226
    # Otherwise, pop off the NULL terminator now.
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
    else:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   229
        discard c.socket.recv( line, 1, c.timeout )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   230
        if defined( debug ): printf " <--\n <-- ^@\n\n"
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   231
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   232
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   233
proc `$`*( r: StompResponse ): string =
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   234
    ## Represent a Stomp response as a string.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   235
    result = r.frame & ": " & $r.headers
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
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   238
proc `[]`*( response: StompResponse, key: string ): string =
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   239
    ## Get a specific header from a Stomp response.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   240
    for header in response.headers:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   241
        if cmpIgnoreCase( key, header.name ) == 0:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   242
            return header.value
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   243
    return nil
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   244
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
#-------------------------------------------------------------------
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   247
# C A L L B A C K S
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   248
#-------------------------------------------------------------------
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
proc default_error_callback( c: StompClient, response: StompResponse ) =
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   251
    ## Something bad happened.  Disconnect from the server, build an error message,
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   252
    ## and raise an exception.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   253
    c.socket.close
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   254
    c.connected = false
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   255
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   256
    var detail = response.payload
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   257
    var msg    = response[ "message" ]
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   258
    if $detail[ ^1 ] == "\n": detail = detail[ 0 .. ^2 ] # chomp
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
    if detail.len > 0: msg = msg & " (" & detail & ")"
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   261
    raise newException( StompError, "ERROR: " & msg )
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
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   264
proc default_missed_heartbeat_callback( c: StompClient ) =
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   265
    ## Timeout while connected to the broker.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   266
    c.socket.close
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   267
    c.connected = false
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   268
    raise newException( StompError, "Heartbeat timeout.  Last activity: " & $c.last_msgtime )
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
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
#-------------------------------------------------------------------
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   273
# C L I E N T
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
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   276
proc newStompClient*( s: Socket, uri: string ): StompClient =
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   277
    ## Create a new Stomp client object from a preexisting **socket**,
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   278
    ## and a stomp **URI** string.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   279
    ##
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   280
    ## .. code-block:: nim
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   281
    ##
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   282
    ##    var socket = newSocket()
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   283
    ##    var stomp  = newStompClient( socket, "stomp://test:test@example.com/%2Fvhost" )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   284
    ##
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   285
    ## or if connecting with SSL, when compiled with -d:ssl:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   286
    ##
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   287
    ## .. code-block:: nim
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   288
    ##
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   289
    ##    var socket = newSocket()
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   290
    ##    let sslContext = newContext( verifyMode = CVerifyNone )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   291
    ##    sslContext.wrapSocket(socket)
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   292
    ##    var stomp = newStompClient( socket, "stomp+ssl://test:test@example.com/%2Fvhost" )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   293
    ##
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   294
    new( result )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   295
    result.socket        = s
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   296
    result.connected     = false
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   297
    result.uri           = parse_uri( uri )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   298
    result.username      = result.uri.username
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   299
    result.password      = result.uri.password
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   300
    result.host          = result.uri.hostname
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   301
    result.vhost         = result.uri.path
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   302
    result.timeout       = 500
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   303
    result.subscriptions = @[]
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   304
    result.transactions  = @[]
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   305
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   306
    # Parse any supported options in the query string.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   307
    #
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   308
    for pairs in result.uri.query.split( '&' ):
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   309
        let opt = pairs.split( '=' )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   310
        try:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   311
            case opt[0]:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   312
                of "heartbeat":
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   313
                    result.options.heartbeat = opt[1].parse_int
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   314
                else:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   315
                    discard
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   316
        except IndexError, ValueError:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   317
            discard
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   318
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   319
    # Set default STOMP port if otherwise unset.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   320
    #
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   321
    if not result.uri.scheme.contains( "stomp" ):
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   322
        raise newException( StompError, "Unknown scheme: " & result.uri.scheme  )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   323
    var port: int
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   324
    if result.uri.port == "":
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   325
        if result.uri.scheme.contains( "+ssl" ):
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   326
            port = 61614
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   327
        else:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   328
            port = 61613
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   329
    else:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   330
        port = result.uri.port.parse_int
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   331
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   332
    result.port = Port( port )
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
    # Decode URI encoded slashes for vhosts.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   335
    result.vhost = result.vhost.replace( "%2f", "/" ).replace( "%2F", "/" ).replace( "//", "/" )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   336
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   337
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   338
proc socksend( c: StompClient, data: string ): void =
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   339
    ## Send data on the connected socket with optional debug output.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   340
    c.socket.send( data )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   341
    if defined( debug ): printf " --> %s", data
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   342
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   343
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   344
proc finmsg( c: StompClient ): void =
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   345
    ## Send data on the connected socket with optional debug output.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   346
    c.socket.send( CRLF & NULL & CRLF )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   347
    if defined( debug ): printf " --> \n --> ^@\n\n"
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
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   350
proc `[]`*( c: StompClient, key: string ): string =
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   351
    ## Get a specific value from the server metadata, set during the initial connection.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   352
    if not c.connected: return nil
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   353
    for header in c.serverinfo:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   354
        if cmpIgnoreCase( key, header.name ) == 0:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   355
            return header.value
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   356
    return nil
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   357
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   358
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   359
proc `$`*( c: StompClient ): string =
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   360
    ## Represent the stomp client as a string, after masking the password.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   361
    let uri = ( $c.uri ).replace( ":" & c.uri.password & "@", "@" )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   362
    result = "(NimStomp v" & VERSION & ( if c.connected: " connected" else: " not connected" ) & " to " & uri
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   363
    if not c[ "server" ].is_nil:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   364
        result.add( " --> " & c["server"] )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   365
    result.add( ")" )
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
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   368
proc connect*( c: StompClient ): void =
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   369
    ## Establish a connection to the Stomp server.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   370
    if c.connected: return
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
    var headers: seq[ tuple[name: string, value: string] ] = @[]
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   373
    headers.add( ("accept-version", "1.2") )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   374
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   375
    # 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
   376
    # supplied, otherwise use the hostname of the server.
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
    if c.vhost != "":
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   379
        headers.add( ("host", c.vhost) )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   380
    else:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   381
        headers.add( ("host", c.host) )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   382
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   383
    if c.username != "": headers.add( ("login", c.username) )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   384
    if c.password != "": headers.add( ("passcode", c.password) )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   385
    if c.options.heartbeat > 0:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   386
        let heartbeat = c.options.heartbeat * 1000
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   387
        headers.add( ("heart-beat", "0," & $heartbeat) )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   388
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   389
    # Connect the socket and send the headers off.
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
    c.socket.connect( c.host, c.port )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   392
    c.socksend( "CONNECT" & CRLF )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   393
    for header in headers:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   394
        c.socksend( header.name & ":" & header.value & CRLF )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   395
    c.finmsg
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   396
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   397
    # Retreive and copy server metadata to client object.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   398
    #
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   399
    var response = newStompResponse( c )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   400
    c.serverinfo = response.headers
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
    if response.frame != "CONNECTED":
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   403
        if not isNil( c.error_callback ):
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   404
            c.error_callback( c, response )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   405
        else:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   406
            c.default_error_callback( response )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   407
    else:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   408
        c.connected = true
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   409
        if not isNil( c.connected_callback ):
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   410
            c.connected_callback( c, response )
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
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   413
proc disconnect*( c: StompClient ): void =
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   414
    ## Break down the connection to the Stomp server nicely.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   415
    if not c.connected: return
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   416
    c.socksend( "DISCONNECT" & CRLF )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   417
    c.finmsg
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   418
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   419
    c.socket.close
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   420
    c.connected = false
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   421
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   422
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   423
proc add_txn( c: StompClient ): void =
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   424
    ## Add a transaction header if there is only a single open txn.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   425
    if c.transactions.len != 1: return
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   426
    c.socksend( "transaction:" & c.transactions[0] & CRLF )
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
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   429
proc send*( c: StompClient,
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   430
            destination: string,
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   431
            message:     string = nil,
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   432
            contenttype: string = nil,
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   433
            headers:     seq[ tuple[name: string, value: string] ] = @[] ): void =
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   434
    ## Send a **message** to **destination**.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   435
    ##
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   436
    ## A Content-Length header is automatically and always included.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   437
    ## A **contenttype** is optional, but strongly recommended.
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
    ## Additionally, a transaction ID is automatically added if there is only
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   440
    ## 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
   441
    ## 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
   442
    ## **headers**.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   443
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   444
    if not c.connected: raise newException( StompError, "Client is not connected." )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   445
    c.socksend( "SEND" & CRLF )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   446
    c.socksend( "destination:" & destination & CRLF )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   447
    c.socksend( "content-length:" & $message.len & CRLF )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   448
    if not contenttype.is_nil: c.socksend( "content-type:" & contenttype & CRLF )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   449
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   450
    # Add custom headers.  Add transaction header if one isn't manually
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   451
    # present (and a transaction is open.)
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   452
    #
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   453
    var txn_seen = false
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   454
    for header in headers:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   455
        if header.name == "transaction": txn_seen = true
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   456
        c.socksend( header.name & ":" & header.value & CRLF )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   457
    if not txn_seen: c.add_txn
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   458
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   459
    if message.is_nil:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   460
        c.finmsg
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   461
    else:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   462
        c.socket.send( CRLF & message & NULL )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   463
        if defined( debug ): printf " -->\n --> (payload)^@\n\n"
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   464
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
proc subscribe*( c: StompClient,
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   467
            destination: string,
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   468
            ack        = "auto",
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   469
            headers:     seq[ tuple[name: string, value: string] ] = @[] ): void =
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   470
    ## Subscribe to messages at **destination**.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   471
    ##
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   472
    ## Setting **ack** to "client" or "client-individual" enables client ACK/NACK mode.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   473
    ## In this mode, incoming messages aren't considered processed by
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   474
    ## the server unless they receive ACK.  By default, the server
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   475
    ## considers the message processed if a client simply accepts it.
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
    ## You may optionally add any additional **headers** the server may support.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   478
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   479
    if not c.connected: raise newException( StompError, "Client is not connected." )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   480
    c.socksend( "SUBSCRIBE" & CRLF )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   481
    c.socksend( "destination:" & destination & CRLF )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   482
    c.socksend( "id:" & $c.subscriptions.len & CRLF )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   483
    if ack == "client" or ack == "client-individual":
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   484
        c.socksend( "ack:" & ack & CRLF )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   485
    else:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   486
        if ack != "auto": raise newException( StompError, "Unknown ack type: " & ack )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   487
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   488
    for header in headers:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   489
        c.socksend( header.name & ":" & header.value & CRLF )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   490
    c.finmsg
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   491
    c.subscriptions.add( destination )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   492
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   493
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   494
proc unsubscribe*( c: StompClient,
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   495
            destination: string,
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   496
            headers:     seq[ tuple[name: string, value: string] ] = @[] ): void =
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   497
    ## Unsubscribe from messages at **destination**.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   498
    ## You may optionally add any additional **headers** the server may support.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   499
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   500
    if not c.connected: raise newException( StompError, "Client is not connected." )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   501
    var
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   502
        sub_id: int
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   503
        i = 0
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   504
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   505
    # Find the ID of the subscription.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   506
    #
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   507
    for sub in c.subscriptions:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   508
        if sub == destination:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   509
            sub_id = i
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   510
            break
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   511
        i = i + 1
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   512
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   513
    c.socksend( "UNSUBSCRIBE" & CRLF )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   514
    c.socksend( "id:" & $sub_id & CRLF )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   515
    for header in headers:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   516
        c.socksend( header.name & ":" & header.value & CRLF )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   517
    c.finmsg
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   518
    c.subscriptions[ sub_id ] = ""
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   519
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   520
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   521
proc begin*( c: StompClient, txn: string ): void =
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   522
    ## Begin a new transaction on the broker, using **txn** as the identifier.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   523
    c.socksend( "BEGIN" & CRLF )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   524
    c.socksend( "transaction:" & txn & CRLF )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   525
    c.finmsg
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   526
    c.transactions.add( txn )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   527
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   528
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   529
proc commit*( c: StompClient, txn: string = nil ): void =
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   530
    ## Finish a specific transaction **txn**, or the most current if unspecified.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   531
    var transaction = txn
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   532
    if transaction.is_nil and c.transactions.len > 0: transaction = c.transactions.pop
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   533
    if transaction.is_nil: return
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   534
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   535
    c.socksend( "COMMIT" & CRLF )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   536
    c.socksend( "transaction:" & transaction & CRLF )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   537
    c.finmsg
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   538
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   539
    # Remove the transaction from the queue.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   540
    #
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   541
    var new_transactions: seq[ string ] = @[]
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   542
    for txn in c.transactions:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   543
        if txn != transaction: new_transactions.add( txn )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   544
    c.transactions = new_transactions
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   545
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   546
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   547
proc abort*( c: StompClient, txn: string = nil ): void =
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   548
    ## Cancel a specific transaction **txn**, or the most current if unspecified.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   549
    var transaction = txn
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   550
    if transaction.is_nil and c.transactions.len > 0: transaction = c.transactions.pop
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   551
    if transaction.is_nil: return
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   552
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   553
    c.socksend( "ABORT" & CRLF )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   554
    c.socksend( "transaction:" & transaction & CRLF )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   555
    c.finmsg
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   556
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   557
    # Remove the transaction from the queue.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   558
    #
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   559
    var new_transactions: seq[ string ] = @[]
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   560
    for txn in c.transactions:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   561
        if txn != transaction: new_transactions.add( txn )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   562
    c.transactions = new_transactions
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   563
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   564
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   565
proc ack*( c: StompClient, id: string, transaction: string = nil ): void =
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   566
    ## Acknowledge message **id**.  Optionally, attach this acknowledgement
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   567
    ## to a specific **transaction** -- if there's only one active, it is
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   568
    ## added automatically.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   569
    c.socksend( "ACK" & CRLF )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   570
    c.socksend( "id:" & id & CRLF )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   571
    if not transaction.is_nil:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   572
        c.socksend( "transaction:" & transaction & CRLF )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   573
    else:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   574
        c.add_txn
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   575
    c.finmsg
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   576
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   577
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   578
proc nack*( c: StompClient, id: string, transaction: string = nil ): void =
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   579
    ## Reject message **id**.  Optionally, attach this rejection to a
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   580
    ## specific **transaction** -- if there's only one active, it is
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   581
    ## added automatically.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   582
    ##
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   583
    ## Subscribe to a queue with ACK mode enabled, and reject the message
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   584
    ## on error:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   585
    ##
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   586
    ## .. code-block:: nim
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   587
    ##    
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   588
    ##  stomp.subscribe( "/queue/test", "client-individual" )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   589
    ##  FIXME: attach procs
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   590
    ##  stomp.wait_for_messages
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   591
    ##
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   592
    c.socksend( "NACK" & CRLF )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   593
    c.socksend( "id:" & id & CRLF )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   594
    if not transaction.is_nil:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   595
        c.socksend( "transaction:" & transaction & CRLF )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   596
    else:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   597
        c.add_txn
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   598
    c.finmsg
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   599
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   600
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   601
proc wait_for_messages*( c: StompClient, loop=true ) =
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   602
    ## Enter a blocking select loop, dispatching to the appropriate proc
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   603
    ## for the received message type.   Return after a single message
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   604
    ## is received if **loop** is set to **false**.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   605
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   606
    if not c.connected: raise newException( StompError, "Client is not connected." )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   607
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   608
    while true:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   609
        var
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   610
            timeout: int
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   611
            fds = @[ c.socket.get_fd ]
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   612
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   613
        # Check for missed heartbeats, with an additional second
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   614
        # of wiggle-room.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   615
        #
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   616
        if c.options.heartbeat > 0:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   617
             timeout = ( c.options.heartbeat + 1 ) * 1000
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   618
        else:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   619
            timeout = -1
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   620
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   621
        if select( fds, timeout ) == 0: # timeout, only happens if heartbeating missed
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   622
            if not isNil( c.missed_heartbeat_callback ):
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   623
                c.missed_heartbeat_callback( c )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   624
            else:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   625
                c.default_missed_heartbeat_callback
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   626
            if loop: continue else: break
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   627
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   628
        let response = newStompResponse( c )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   629
        case response.frame:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   630
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   631
            of "HEARTBEAT":
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   632
                if not isNil( c.heartbeat_callback ):
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   633
                    c.heartbeat_callback( c, response )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   634
                continue
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   635
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   636
            of "RECEIPT":
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   637
                if not isNil( c.receipt_callback ):
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   638
                    c.receipt_callback( c, response )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   639
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   640
            of "MESSAGE":
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   641
                if not isNil( c.message_callback ):
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   642
                    c.message_callback( c, response )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   643
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   644
            of "ERROR":
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   645
                if not isNil( c.error_callback ):
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   646
                    c.error_callback( c, response )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   647
                else:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   648
                    c.default_error_callback( response )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   649
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   650
            else:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   651
                if defined( debug ):
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   652
                    echo "Strange broker frame: " & response.repr
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   653
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   654
        if not loop: break
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   655
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   656
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   657
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   658
#-------------------------------------------------------------------
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   659
# T E S T S
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   660
#-------------------------------------------------------------------
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   661
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   662
# Functional (rather than unit) tests.  Requires a Stomp compatible broker.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   663
# This was tested against RabbitMQ 3.5.3 and 3.6.0.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   664
# 3.6.0 was -so- much faster.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   665
#
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   666
# First start up a message receiver:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   667
#   ./stomp receiver [stomp-uri] [subscription-destination]
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   668
#
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   669
# then run another process, to publish stuff:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   670
#   ./stomp publisher [stomp-uri] [publish-destination]
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   671
#
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   672
# An example with an AMQP "direct" exchange, and an exclusive queue:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   673
#   ./stomp publisher stomp://test:test@localhost/?heartbeat=10 /exchange/test
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   674
#   ./stomp receiver  stomp://test:test@localhost/?heartbeat=10 /exchange/test
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   675
#
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   676
# Then just let 'er run.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   677
#
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   678
# You can also run a nieve benchmark (deliveries/sec):
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   679
#
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   680
#   ./stomp benchmark stomp://test:test@localhost/ /exchange/test
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   681
#
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   682
# It will set messages to require acknowledgement, and nack everything, causing
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   683
# a delivery loop for 10 seconds.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   684
#
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   685
when isMainModule:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   686
    let expected = 8
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   687
    var
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   688
        socket   = newSocket()
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   689
        messages: seq[ StompResponse ] = @[]
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   690
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   691
    if paramCount() != 3: quit "See source comments for how to run functional tests."
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   692
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   693
    var stomp = newStompClient( socket, paramStr(2) )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   694
    stomp.connect
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   695
    echo stomp
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   696
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   697
    case paramStr(1):
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   698
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   699
        of "benchmark":
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   700
            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
   701
            var count = 0
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   702
            var start = get_time()
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
            proc incr( c: StompClient, r: StompResponse ) =
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   705
                let id = r["ack"]
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   706
                count = count + 1
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   707
                c.nack( id )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   708
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   709
            stomp.message_callback = incr
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   710
            stomp.subscribe( paramStr(3), "client" )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   711
            stomp.send( paramStr(3), "hi." )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   712
            while get_time() - start < 10:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   713
                stomp.wait_for_messages( false )
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
            printf "* Processed %d messages in 10 seconds.\n", count
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   716
            stomp.disconnect
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   717
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   718
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   719
        # Store incoming messages, ensure their contents match our expected behavior.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   720
        #
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   721
        of "receiver":
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   722
            var heartbeats = 0
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   723
            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
   724
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   725
            proc receive_message( c: StompClient, r: StompResponse ) =
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   726
                messages.add( r )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   727
                case r.frame:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   728
                    of "RECEIPT":
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   729
                        discard
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   730
                    of "MESSAGE":
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   731
                        let body = r.payload
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   732
                        let id   = r[ "ack" ]
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   733
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   734
            proc seen_heartbeat( c: StompClient, r: StompResponse ) =
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   735
                heartbeats = heartbeats + 1
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   736
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   737
            stomp.message_callback   = receive_message
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   738
            stomp.receipt_callback   = receive_message
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   739
            stomp.heartbeat_callback = seen_heartbeat
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   740
            stomp.subscribe( paramStr(3) )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   741
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   742
            # Populate the messages sequence with the count of expected messages.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   743
            for i in 1..expected: stomp.wait_for_messages( false )
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
            # Assertions on the results!
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   746
            #
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   747
            doAssert( messages.len == expected )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   748
            doAssert( messages[0].payload == nil )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   749
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   750
            doAssert( messages[1].payload == "Hello world!" )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   751
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   752
            doAssert( messages[2].payload == "Dumb.\n\n" )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   753
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   754
            doAssert( messages[3].payload == "Hello again." )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   755
            doAssert( messages[3][ "content-type" ] == "text/plain" )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   756
            doAssert( messages[3][ "Content-Type" ] == "text/plain" )
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[4][ "x-custom" ] == "yum" )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   759
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   760
            doAssert( messages[5][ "receipt" ] == "42" )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   761
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   762
            doAssert( messages[6].payload == "transaction!" )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   763
            doAssert( messages[7].payload == "transaction 2" )
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
            stomp.disconnect
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   766
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   767
            if heartbeats > 0:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   768
                printf "* Tests passed! %d heartbeats seen.", heartbeats
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   769
            else:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   770
                echo "* Tests passed!"
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   771
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
        # Publish a variety of messages with various options.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   774
        # Pause momentarily between sends(), as brokers -might- impose
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   775
        # rate limits and/or message dropping.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   776
        #
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   777
        of "publisher":
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   778
            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
   779
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   780
            # Simple, no frills event.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   781
            stomp.send( paramStr(3) )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   782
            sleep 500
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
            # Event with a body.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   785
            stomp.send( paramStr(3), "Hello world!" )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   786
            sleep 500
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
            # Event that doesn't contain a content-length.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   789
            # (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
   790
            # but invalidates this test.)
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   791
            stomp.socksend( "SEND" & CRLF )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   792
            stomp.socksend( "destination:" & paramStr(3) & CRLF & CRLF )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   793
            stomp.socksend( "Dumb.\n\n" & NULL )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   794
            sleep 500
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   795
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   796
            # Content-Type
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   797
            stomp.send( paramStr(3), "Hello again.", "text/plain" )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   798
            sleep 500
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   799
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   800
            # Custom headers.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   801
            var headers: seq[ tuple[ name: string, value: string ] ] = @[]
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   802
            headers.add( ("x-custom", "yum") )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   803
            stomp.send( paramStr(3), "Hello again.", "text/plain", headers )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   804
            sleep 500
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   805
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   806
            # Receipt requests.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   807
            proc receive_receipt( c: StompClient, r: StompResponse ) =
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   808
                messages.add( r )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   809
            headers = @[]
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   810
            headers.add( ("receipt", "42") )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   811
            stomp.send( paramStr(3), "Hello again.", "text/plain", headers )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   812
            stomp.receipt_callback = receive_receipt
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   813
            stomp.wait_for_messages( false )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   814
            doAssert( messages[0]["receipt-id"] == "42" )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   815
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   816
            # Aborted transaction.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   817
            stomp.begin( "test-abort" )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   818
            for i in 1..3:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   819
                stomp.send( paramStr(3), "Message: " & $i )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   820
            stomp.abort
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   821
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   822
            # Committed transaction.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   823
            stomp.begin( "test-commit" )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   824
            stomp.send( paramStr(3), "transaction!" )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   825
            stomp.commit
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
            # Mixed transactions.
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   828
            for i in 1..3:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   829
                headers = @[]
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   830
                headers.add( ("transaction", "test-" & $i ) )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   831
                stomp.begin( "test-" & $i )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   832
                stomp.send( paramStr(3), "transaction " & $i, nil, headers )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   833
                sleep 500
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   834
            stomp.abort( "test-1" )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   835
            sleep 500
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   836
            stomp.commit( "test-2" )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   837
            sleep 500
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   838
            stomp.abort( "test-3" )
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   839
            sleep 500
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   840
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   841
            stomp.disconnect
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   842
            echo "* Tests passed!"
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   843
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   844
        else:
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   845
            quit "See source comments for how to run functional tests."
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   846
52e9f64937bf Initial commit.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   847