Class: SimplePubSub::Server

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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
# 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'
  
  @subscribers = {'#' => []}     
        
  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      
        
  @hostname = hostname

  @bridges = {'#' => []}
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



144
145
146
147
# File 'lib/simplepubsub.rb', line 144

def add_bridge(topic, hostname, address)
  @bridges[topic] ||= {}
  @bridges[topic].merge!(hostname => address)
end

#bridge_deliver(topic, message, excluded_host = nil) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/simplepubsub.rb', line 149

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

#deliver(topic, msg) ⇒ Object



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

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.message topic, msg
      rescue DRb::DRbConnError => e             
        @subscribers[topic].delete uri
        key = "hkey_apps/simplepubsub/subscription_topics/%s/subscribers/%s" % \
            [topic, uri[/[^\/]+$/].sub(':','')]
        @reg.delete_key key
      end          
      
    end
  end            

  @subscribers['#'].each do |uri|
  
    echo = DRbObject.new nil, uri
    
    begin
      echo.message topic, msg
    rescue DRb::DRbConnError => e             
      @subscribers['#'].delete uri
    end          

  end
end

#subscribe(topic, uri) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/simplepubsub.rb', line 85

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