/
xmpp_pub_sub.rb
201 lines (173 loc) · 5.2 KB
/
xmpp_pub_sub.rb
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
200
201
require 'thread' # for Queue
require 'pp'
require 'xmpp4r'
require "xmpp4r/pubsub"
require "xmpp4r/pubsub/helper/servicehelper.rb"
require "xmpp4r/pubsub/helper/nodebrowser.rb"
#require "xmpp4r/pubsub/helper/nodehelper.rb"
Thread.abort_on_exception = true
module REXML
# derived from http://www.koders.com/ruby/fid3712196E67D4522F76EAD9F57078E4D0736746C5.aspx?s=update
class Element # :nodoc:
unless REXML::Element.public_instance_methods.include?(:inner_xml)
def inner_xml # :nodoc:
result = []
each_child do |child|
if child.kind_of?(REXML::Comment)
result << "<!--" + child.to_s + "-->"
else
result << child.to_s
end
end
result.join('').strip
end
else
warn("inner_xml method already exists.")
end
end
end
module SMQueue
class XMPPPubSubAdapter < Adapter
include Jabber
class Configuration < AdapterConfiguration
has :host, :kind => String, :default => "" do
doc <<-EDOC
The host that runs the broker you want to connect to.
EDOC
end
has :logger do
default { Logger.new(STDERR) }
doc <<-EDOC
The logger to use - must provide #info, #debug, #warn.
If this is not specified, a default Logger is set up
to log to STDERR.
EDOC
end
# note: this configured node is not identical to the one defined
# (and used) below - this defines the leaf path which is later
# expanded to the full path to the node below
has :node, :default => nil do
doc <<-EDOC
The node to publish/subscribe to.
EDOC
end
has :jid, :default => "" do
doc <<-end
The Jabber ID.
end
end
has :password, :kind => String, :default => "" do
doc <<-EDOC
The password to attempt to authenticate at the broker with.
If you are using an anonymous subscription, leave this blank.
EDOC
end
has :service
has :anonymous, :default => false do
doc <<-end
Set to true to use SASL anonymous authentication.
end
end
has :debug, :default => false, :doc => "set to true to get XMPP debug info" do
from Object do |s|
flag = !!s
if flag
Jabber.debug = flag
end
flag
end
end
has :xpath, :default => nil
end
has :connected, :default => false
has :subscribed, :default => false
has :node do
init { "home/#{configuration.host}/#{configuration.node}"}
end
has :service do
init { "#{configuration.service}.#{configuration.host}"}
end
has :logger do
init { configuration.logger }
end
def connect
pp self.doodle.key_values
@client = Jabber::Client.new(Jabber::JID.new(configuration.jid))
rv = @client.connect(configuration.host)
logger.info "client.connect: #{rv}"
if @client.supports_anonymous? && configuration.anonymous
logger.info "connecting anonymously"
@client.auth_anonymous_sasl
else
logger.info "connecting with password: #{!configuration.password.nil?}"
@client.auth(configuration.password)
end
@client.send(Jabber::Presence.new.set_type(:available))
@pubsub = Jabber::PubSub::ServiceHelper.new(@client, service)
connected true
end
def subscribe
logger.info "\n\nSubscribing\n\n"
@pubsub.subscribe_to(node)
subscribed true
@callbacks = @pubsub.add_event_callback do |event|
# logger.info "received #{event.inspect}"
logger.info "Received " + event.inspect
event.payload.each do |e|
logger.info "payload.e " + e.inspect
REXML::XPath.each(e, configuration.xpath) do |el|
logger.info "queuing: #{el.inner_xml}"
@event_queue.enq(el.inner_xml)
end
end
end
logger.debug "after callback definition"
end
def unsubscribe
@pubsub.unsubscribe_from(node) if subscribed
end
def disconnect
begin
unsubscribe
@client.close
ensure
connected false
end
end
def get(&block)
logger.debug "get"
m = nil
connect if !connected
@event_queue = Queue.new
subscribe if !subscribed
loop do
logger.debug "# queued events #{@event_queue.size}"
logger.info "waiting for msg"
payload = @event_queue.deq
logger.info "PAYLOAD: #{payload.inspect}"
m = SMQueue::Message.new(
:headers => {
:protocol => :xmpp
},
:body => payload
)
if block_given?
yield(m)
else
break
end
end
m
end
def put(body, headers = { })
body, headers = normalize_message(body, headers)
logger.debug "put #{body.inspect}"
connect if !connected
item = PubSub::Item.new
xml = REXML::Element.new("message")
xml.text = body
item.add(xml);
@pubsub.publish_item_to(node, item)
end
end
end