Class: SimplePubSub::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/simplepubsub.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_reg = 'simplepubsub.xml') ⇒ Server

Returns a new instance of Server.



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
# File 'lib/simplepubsub.rb', line 71

def initialize(raw_reg='simplepubsub.xml')


  h = {DWSRegistry: ->{raw_reg}, String: ->{DWSRegistry.new raw_reg},:'RSC::Package' => ->{raw_reg}}

  @reg = h[raw_reg.class.to_s.to_sym].call
  
  doc = Rexle.new(@reg.xml('hkey_apps/simplepubsub'))

  root = doc.root
  # try to read the subscribers
  topics = root.xpath 'subscription_topics/*'

  @hostname = Socket.gethostname
  @subscribers, @bridges = {'#' => []}, {'#' => {}}     
        
  topics.each do |topic_element|
    topic = topic_element.name
    @subscribers[topic] ||= []
    @subscribers[topic] = topic_element.xpath 'subscribers/*/text()'
  end

  @subscribers['#'] = root.xpath 'subscription_all_topics' + \
      '/subscribers/*/text()'                            

  root.xpath('bridge_topics/*').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


  @bridges['#'] = root.xpath('bridge_all_topics/subscribers/*')
      .inject({}) do |r,x|
    r.merge({x.name.to_s => x.text('address')})
  end

  'done'
end

Instance Attribute Details

#bridges ⇒ Object (readonly)

Returns the value of attribute bridges.



69
70
71
# File 'lib/simplepubsub.rb', line 69

def bridges
  @bridges
end

#subscribers ⇒ Object (readonly)

Returns the value of attribute subscribers.



69
70
71
# File 'lib/simplepubsub.rb', line 69

def subscribers
  @subscribers
end

Instance Method Details

#add_bridge(topic, hostname, address) ⇒ Object



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/simplepubsub.rb', line 205

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



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/simplepubsub.rb', line 238

def bridge_deliver(topic, message, 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(message)]
    r = open(url, 'UserAgent' => USER_AGENT)
  end
  'bridge delivered'
end

#delete_bridge(topic, hostname) ⇒ Object



223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/simplepubsub.rb', line 223

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



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
202
203
# File 'lib/simplepubsub.rb', line 142

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 {
        begin          
          
          echo = DRbObject.new nil, uri            
          echo.message 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 {
      
      begin
        echo = DRbObject.new nil, uri        
        echo.message 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



112
113
114
115
116
117
118
119
120
121
# File 'lib/simplepubsub.rb', line 112

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



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/simplepubsub.rb', line 123

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