/usr/lib/ruby/vendor_ruby/em/protocols/stomp.rb is in ruby-eventmachine 0.12.10-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 | #--
#
# Author:: Francis Cianfrocca (gmail: blackhedd)
# Homepage:: http://rubyeventmachine.com
# Date:: 15 November 2006
#
# See EventMachine and EventMachine::Connection for documentation and
# usage examples.
#
#----------------------------------------------------------------------------
#
# Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
# Gmail: blackhedd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of either: 1) the GNU General Public License
# as published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version; or 2) Ruby's License.
#
# See the file COPYING for complete licensing information.
#
#---------------------------------------------------------------------------
#
#
#
module EventMachine
module Protocols
# Implements Stomp (http://docs.codehaus.org/display/STOMP/Protocol).
#
# == Usage example
#
# module StompClient
# include EM::Protocols::Stomp
#
# def connection_completed
# connect :login => 'guest', :passcode => 'guest'
# end
#
# def receive_msg msg
# if msg.command == "CONNECTED"
# subscribe '/some/topic'
# else
# p ['got a message', msg]
# puts msg.body
# end
# end
# end
#
# EM.run{
# EM.connect 'localhost', 61613, StompClient
# }
#
module Stomp
include LineText2
class Message
# The command associated with the message, usually 'CONNECTED' or 'MESSAGE'
attr_accessor :command
# Hash containing headers such as destination and message-id
attr_accessor :header
alias :headers :header
# Body of the message
attr_accessor :body
def initialize # :nodoc:
@header = {}
@state = :precommand
@content_length = nil
end
def consume_line line # :nodoc:
if @state == :precommand
unless line =~ /\A\s*\Z/
@command = line
@state = :headers
end
elsif @state == :headers
if line == ""
if @content_length
yield( [:sized_text, @content_length+1] )
else
@state = :body
yield( [:unsized_text] )
end
elsif line =~ /\A([^:]+):(.+)\Z/
k = $1.dup.strip
v = $2.dup.strip
@header[k] = v
if k == "content-length"
@content_length = v.to_i
end
else
# This is a protocol error. How to signal it?
end
elsif @state == :body
@body = line
yield( [:dispatch] )
end
end
end
# :stopdoc:
def send_frame verb, headers={}, body=""
ary = [verb, "\n"]
headers.each {|k,v| ary << "#{k}:#{v}\n" }
ary << "content-length: #{body.to_s.length}\n"
ary << "content-type: text/plain; charset=UTF-8\n"
ary << "\n"
ary << body.to_s
ary << "\0"
send_data ary.join
end
def receive_line line
@stomp_initialized || init_message_reader
@stomp_message.consume_line(line) {|outcome|
if outcome.first == :sized_text
set_text_mode outcome[1]
elsif outcome.first == :unsized_text
set_delimiter "\0"
elsif outcome.first == :dispatch
receive_msg(@stomp_message) if respond_to?(:receive_msg)
init_message_reader
end
}
end
def receive_binary_data data
@stomp_message.body = data[0..-2]
receive_msg(@stomp_message) if respond_to?(:receive_msg)
init_message_reader
end
def init_message_reader
@stomp_initialized = true
set_delimiter "\n"
set_line_mode
@stomp_message = Message.new
end
# :startdoc:
# Invoked with an incoming Stomp::Message received from the STOMP server
def receive_msg msg
# stub, overwrite this in your handler
end
# CONNECT command, for authentication
#
# connect :login => 'guest', :passcode => 'guest'
#
def connect parms={}
send_frame "CONNECT", parms
end
# SEND command, for publishing messages to a topic
#
# send '/topic/name', 'some message here'
#
def send destination, body, parms={}
send_frame "SEND", parms.merge( :destination=>destination ), body.to_s
end
# SUBSCRIBE command, for subscribing to topics
#
# subscribe '/topic/name', false
#
def subscribe dest, ack=false
send_frame "SUBSCRIBE", {:destination=>dest, :ack=>(ack ? "client" : "auto")}
end
# ACK command, for acknowledging receipt of messages
#
# module StompClient
# include EM::P::Stomp
#
# def connection_completed
# connect :login => 'guest', :passcode => 'guest'
# # subscribe with ack mode
# subscribe '/some/topic', true
# end
#
# def receive_msg msg
# if msg.command == "MESSAGE"
# ack msg.headers['message-id']
# puts msg.body
# end
# end
# end
#
def ack msgid
send_frame "ACK", 'message-id'=> msgid
end
end
end
end
|