Class: SimplePubSub::Server
- Inherits:
-
Object
- Object
- SimplePubSub::Server
- Defined in:
- lib/simplepubsub.rb
Instance Attribute Summary collapse
-
#bridges ⇒ Object
readonly
Returns the value of attribute bridges.
-
#subscribers ⇒ Object
readonly
Returns the value of attribute subscribers.
Instance Method Summary collapse
- #add_bridge(topic, hostname, address) ⇒ Object
- #bridge_deliver(topic, message, excluded_host = nil) ⇒ Object
- #delete_bridge(topic, hostname) ⇒ Object
- #deliver(topic, msg) ⇒ Object
-
#initialize(raw_reg = 'simplepubsub.xml') ⇒ Server
constructor
A new instance of Server.
- #start ⇒ Object
- #subscribe(topic, uri) ⇒ Object
Constructor Details
#initialize(raw_reg = 'simplepubsub.xml') ⇒ Server
Returns a new instance of Server.
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 |
# File 'lib/simplepubsub.rb', line 70 def initialize(raw_reg='simplepubsub.xml') h = {DWSRegistry: ->{raw_reg}, String: ->{DWSRegistry.new raw_reg}} @reg = h[raw_reg.class.to_s.to_sym].call # try to read the subscribers topics = @reg.get_key 'hkey_apps/simplepubsub/subscription_topics' @hostname = Socket.gethostname @subscribers, @bridges = {'#' => []}, {'#' => {}} if topics then topics.elements.each do |topic_element| topic = topic_element.name @subscribers[topic] ||= [] @subscribers[topic] = topic_element.elements[0].elements.map(&:value) end end all_topics = @reg.get_key 'hkey_apps/simplepubsub/subscription_all_topics' if all_topics then @subscribers['#'] = all_topics.elements[0].elements.map(&:value) end bridge_topics = @reg.get_key 'hkey_apps/simplepubsub/bridge_topics' if bridge_topics then bridge_topics.elements.each do |topic_element| topic = topic_element.name @bridges[topic] = topic_element.elements[0].elements.inject({}) do |r,x| r.merge({x.name.to_s => x.text('address')}) end end end bridge_all_topics = @reg.get_key 'hkey_apps/simplepubsub/bridge_all_topics' if bridge_all_topics then @bridges['#'] = bridge_all_topics.elements[0].elements.inject({}) do |r,x| r.merge({x.name.to_s => x.text('address')}) end end 'done' end |
Instance Attribute Details
#bridges ⇒ Object (readonly)
Returns the value of attribute bridges.
68 69 70 |
# File 'lib/simplepubsub.rb', line 68 def bridges @bridges end |
#subscribers ⇒ Object (readonly)
Returns the value of attribute subscribers.
68 69 70 |
# File 'lib/simplepubsub.rb', line 68 def subscribers @subscribers end |
Instance Method Details
#add_bridge(topic, hostname, address) ⇒ Object
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/simplepubsub.rb', line 213 def add_bridge(topic, hostname, address) if topic == '#' then key = "hkey_apps/simplepubsub/bridge_all_topics/webserver/%s/address" % \ [hostname] else topic.sub!('/','_') @bridges[topic] ||= {} key = "hkey_apps/simplepubsub/bridge_topics/%s/webserver/%s/address" % \ [topic, hostname] end @bridges[topic].merge!(hostname => address) @reg.set_key key, address end |
#bridge_deliver(topic, message, excluded_host = nil) ⇒ Object
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/simplepubsub.rb', line 246 def bridge_deliver(topic, , excluded_host=nil) return 'no matching topic' unless @bridges.has_key? topic if excluded_host then bridges = @bridges[topic].select{|x| x != excluded_host} else bridges = @bridges[topic] end bridges.values.each do |address| url = "http://%s/do/simplepubsub/bridgepub?topic=%s&hostname=%s&message=%s" % \ [address, URI.escape(topic), @hostname, URI.escape()] r = open(url, 'UserAgent' => USER_AGENT) end 'bridge delivered' end |
#delete_bridge(topic, hostname) ⇒ Object
231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/simplepubsub.rb', line 231 def delete_bridge(topic, hostname) @bridges[topic].delete hostname if @bridges[topic].empty? and topic != '#' then @bridges.delete_key(topic) key = "hkey_apps/simplepubsub/bridge_topics/%s" % [topic] else key = "hkey_apps/simplepubsub/bridge_topics/%s/webserver/%s/address" % \ [topic, hostname] end @reg.delete_key key end |
#deliver(topic, msg) ⇒ Object
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 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/simplepubsub.rb', line 150 def deliver(topic, msg) topic.sub!('/','_') if not @subscribers.include?(topic) and \ not @subscribers.include?('#') then return 'no topic subscribers' end DRb.start_service topic_subscribers = @subscribers[topic] if topic_subscribers then topic_subscribers.each do |uri| next if @subscribers['#'].include? uri Thread.new { echo = DRbObject.new nil, uri begin echo. topic, msg rescue DRb::DRbConnError => e @subscribers[topic].delete uri if @subscribers[topic].empty? then @subscribers.delete topic key = "hkey_apps/simplepubsub/subscription_topics/%s" % [topic] else key = "hkey_apps/simplepubsub/subscription_topics/%s/subscribers/%s" % \ [topic, uri[/[^\/]+$/].sub(':','')] end @reg.delete_key key end } end end @subscribers['#'].each do |uri| Thread.new { echo = DRbObject.new nil, uri begin echo. topic, msg rescue DRb::DRbConnError => e @subscribers['#'].delete uri key = "hkey_apps/simplepubsub/subscription_all_topics/subscribers/%s" % \ [uri[/[^\/]+$/].sub(':','')] @reg.delete_key key end } end end |
#start ⇒ Object
120 121 122 123 124 125 126 127 128 129 |
# File 'lib/simplepubsub.rb', line 120 def start() # start up the DRb service DRb.start_service 'druby://:59000', self # wait for the DRb service to finish before exiting DRb.thread.join 'done' end |
#subscribe(topic, uri) ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/simplepubsub.rb', line 131 def subscribe(topic, uri) topic.sub!('/','_') @subscribers[topic] ||= [] @subscribers[topic] << uri # e.g. 'hkey_apps/simplepubsub/subscription_topics/magic/subscribers/niko', # 'druby://niko:353524' if topic == '#' then key = "hkey_apps/simplepubsub/subscription_all_topics/subscribers/%s" % \ [uri[/[^\/]+$/].sub(':','')] else key = "hkey_apps/simplepubsub/subscription_topics/%s/subscribers/%s" % \ [topic, uri[/[^\/]+$/].sub(':','')] end @reg.set_key key, uri end |