lib/client/utils.rb in stomp-1.2.11 vs lib/client/utils.rb in stomp-1.2.12

- old
+ new

@@ -19,57 +19,56 @@ @host = first_host[:host] @port = first_host[:port] || Connection::default_port(first_host[:ssl]) @reliable = true true end - private :parse_hash_params def parse_stomp_url(login) - regexp = /^stomp:\/\/#{url_regex}/ # e.g. stomp://login:passcode@host:port or stomp://host:port + regexp = /^stomp:\/\/#{URL_REPAT}/ return false unless login =~ regexp - @login = $2 || "" - @passcode = $3 || "" - @host = $4 - @port = $5.to_i + @login = $3 || "" + @passcode = $4 || "" + @host = $5 + @port = $6.to_i + @reliable = false true end - private :parse_stomp_url # e.g. failover://(stomp://login1:passcode1@localhost:61616,stomp://login2:passcode2@remotehost:61617)?option1=param def parse_failover_url(login) - regexp = /^failover:(\/\/)?\(stomp(\+ssl)?:\/\/#{url_regex}(,stomp(\+ssl)?:\/\/#{url_regex}\))+(\?(.*))?$/ - return false unless login =~ regexp - - first_host = {} - first_host[:ssl] = !$2.nil? - @login = first_host[:login] = $4 || "" - @passcode = first_host[:passcode] = $5 || "" - @host = first_host[:host] = $6 - @port = first_host[:port] = $7.to_i || Connection::default_port(first_host[:ssl]) - options = $16 || "" - parts = options.split(/&|=/) - options = Hash[*parts] - hosts = [first_host] + parse_hosts(login) - @parameters = {} - @parameters[:hosts] = hosts - @parameters.merge! filter_options(options) - @reliable = true - true + rval = nil + if md = FAILOVER_REGEX.match(login) + finhosts = parse_hosts(login) + # + @login = finhosts[0][:login] || "" + @passcode = finhosts[0][:passcode] || "" + @host = finhosts[0][:host] || "" + @port = finhosts[0][:port] || "" + # + options = {} + if md_last = md[md.size-1] + parts = md_last.split(/&|=/) + raise Stomp::Error::MalformedFailoverOptionsError unless (parts.size % 2 ) == 0 + options = Hash[*parts] + end + @parameters = {:hosts => finhosts}.merge! filter_options(options) + @reliable = true + rval = true + end + rval end - private :parse_failover_url def parse_positional_params(login, passcode, host, port, reliable) @login = login @passcode = passcode @host = host @port = port.to_i @reliable = reliable true end - private :parse_positional_params # Set a subscription id in the headers hash if one does not already exist. # For simplicities sake, all subscriptions have a subscription ID. # setting an id in the SUBSCRIPTION header is described in the stomp protocol docs: # http://stomp.github.com/ @@ -89,34 +88,26 @@ end @receipt_listeners[id] = listener id end - # url_regex defines a regex for e.g. login:passcode@host:port or host:port - def url_regex - '(([\w\.\-]*):(\w*)@)?([\w\.\-]+):(\d+)' - end +# Parse a stomp URL. +def parse_hosts(url) + hosts = [] + host_match = /stomp(\+ssl)?:\/\/#{URL_REPAT}/ + url.scan(host_match).each do |match| + host = {} + host[:ssl] = match[0] == "+ssl" ? true : false + host[:login] = match[3] || "" + host[:passcode] = match[4] || "" + host[:host] = match[5] + host[:port] = match[6].to_i + hosts << host + end + hosts +end - # Parse a stomp URL. - def parse_hosts(url) - hosts = [] - - host_match = /stomp(\+ssl)?:\/\/(([\w\.]*):(\w*)@)?([\w\.]+):(\d+)\)/ - url.scan(host_match).each do |match| - host = {} - host[:ssl] = !match[0].nil? - host[:login] = match[2] || "" - host[:passcode] = match[3] || "" - host[:host] = match[4] - host[:port] = match[5].to_i - - hosts << host - end - - hosts - end - # A very basic check of required arguments. def check_arguments!() raise ArgumentError if @host.nil? || @host.empty? raise ArgumentError if @port.nil? || @port == '' || @port < 1 || @port > 65535 raise ArgumentError unless @reliable.is_a?(TrueClass) || @reliable.is_a?(FalseClass) @@ -159,17 +150,19 @@ message = @connection.receive # AMQ specific behavior if message.nil? && (!@reliable) raise Stomp::Error::NilMessageError end + if message # message can be nil on rapid AMQ stop / start sequences # OK, we have some real data - if message.command == Stomp::CMD_MESSAGE - if listener = find_listener(message) - listener.call(message) - end - elsif message.command == Stomp::CMD_RECEIPT - if listener = @receipt_listeners[message.headers['receipt-id']] - listener.call(message) + if message.command == Stomp::CMD_MESSAGE + if listener = find_listener(message) + listener.call(message) + end + elsif message.command == Stomp::CMD_RECEIPT + if listener = @receipt_listeners[message.headers['receipt-id']] + listener.call(message) + end end end end # while true end end # method start_listeners