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(hostname, raw_reg = 'simplepubsub.xml') ⇒ Server
constructor
A new instance of Server.
- #subscribe(topic, uri) ⇒ Object
Constructor Details
#initialize(hostname, raw_reg = 'simplepubsub.xml') ⇒ Server
Returns a new instance of Server.
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 |
# File 'lib/simplepubsub.rb', line 60 def initialize(hostname, 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 = hostname @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.
58 59 60 |
# File 'lib/simplepubsub.rb', line 58 def bridges @bridges end |
#subscribers ⇒ Object (readonly)
Returns the value of attribute subscribers.
58 59 60 |
# File 'lib/simplepubsub.rb', line 58 def subscribers @subscribers end |
Instance Method Details
#add_bridge(topic, hostname, address) ⇒ Object
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/simplepubsub.rb', line 182 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
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/simplepubsub.rb', line 215 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
200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/simplepubsub.rb', line 200 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
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 |
# File 'lib/simplepubsub.rb', line 123 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 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| 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 |
#subscribe(topic, uri) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/simplepubsub.rb', line 110 def subscribe(topic, uri) topic.sub!('/','_') @subscribers[topic] ||= [] @subscribers[topic] << uri # e.g. 'hkey_apps/simplepubsub/subscription_topics/magic/subscribers/niko', # 'druby://niko:353524' key = "hkey_apps/simplepubsub/subscription_topics/%s/subscribers/%s" % \ [topic, uri[/[^\/]+$/].sub(':','')] @reg.set_key key, uri end |