simplepubsub 0.5.3 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1805609c5dec321fbde1b6c835a93edef5448009
4
- data.tar.gz: e8b0604afbb9096bf109d34c65917360730a4801
3
+ metadata.gz: d900a96af9a7e45304f1cf25193a619b6a09ea69
4
+ data.tar.gz: 074182bcf709b23c432b45a1298f17d7eee8433f
5
5
  SHA512:
6
- metadata.gz: 70c9367e4e48284bb71e68d0dfd1ec45b7947973a00978f4d06c7096cc2759ceccfdf3c9881317d1858fe8d7f60f042874eae9c82888c1c245391b72994c3fc6
7
- data.tar.gz: 610a2d6be70cc96c9ab3d07b34729874d5bfba5b18b1781d72f3742c6dad9be0e1e0f659f4c40153fd5c529c4600100e48d58b1d7d436191d3ebd755445cf480
6
+ metadata.gz: 43fcd46034de9636033bf24ae6e40746201d547f8582b80d2f02232f112ba202c5701768b51787220525293505ed7d7d4979ff29e0ba2a466e2dadbee2c56369
7
+ data.tar.gz: 316b94f9395578f798b72eabc7a7aedf4aac58e18e8d9bfdd7a29db835af04bba166ed95f205a90380988ac26bb4de0fda566681dec31df764ca4d4bb8ebfe0a
checksums.yaml.gz.sig CHANGED
Binary file
data/lib/simplepubsub.rb CHANGED
@@ -2,264 +2,126 @@
2
2
 
3
3
  # file: simplepubsub.rb
4
4
 
5
- require 'open-uri'
6
- require 'drb'
7
- require 'dws-registry'
8
- require 'socket'
9
- require 'chronic_duration'
5
+ require 'websocket-eventmachine-server'
6
+ require 'websocket-eventmachine-client'
10
7
 
11
8
 
12
- USER_AGENT = 'SimplePbSub client 0.5'
13
-
14
9
  module SimplePubSub
15
10
 
16
- class Client
17
- class PubSub
11
+ class Server
18
12
 
19
- class Echo
13
+ def start(options={})
20
14
 
21
- def initialize(options, &get_proc)
22
-
23
- @interval = options[:interval]
24
- @t1 = Time.now
25
- @get_proc = get_proc
26
- end
15
+ opt = {host: '0.0.0.0', port: 59000}.merge options
16
+ host, port = opt[:host], opt[:port]
27
17
 
28
- def message(topic, message)
29
-
30
- return if @interval and @t1 + ChronicDuration.parse(@interval) > Time.now
31
- @t1 = Time.now
32
- @get_proc.call topic, message
33
- end
34
- end
18
+ EM.run do
35
19
 
36
- def initialize(hostname)
37
- @hostname = hostname
38
- end
20
+ subscribers = {}
39
21
 
40
- def get(topic, options={}, &get_proc)
22
+ WebSocket::EventMachine::Server.start(host: host, port: port) do |ws|
41
23
 
42
- DRb.start_service nil, Echo.new(options, &get_proc)
43
-
44
- obj = DRbObject.new nil, "druby://#{@hostname}:59000"
45
- obj.subscribe(topic, DRb.uri)
46
- DRb.thread.join
24
+ ws.onopen do
25
+ puts "Client connected"
26
+ end
47
27
 
48
- end
28
+ ws.onmessage do |msg, type|
49
29
 
50
- def publish(topic, message)
30
+ puts "Received message: #{msg}"
51
31
 
52
- DRb.start_service
53
- obj = DRbObject.new nil, "druby://#{@hostname}:59000"
54
- obj.deliver topic, message
55
- end
56
- end
32
+ a = msg.split(/\s*:\s*/,2)
33
+
34
+ if a.first == 'subscribe to topic' then
57
35
 
58
- attr_reader :remote_obj
36
+ topic = a.last
37
+ subscribers[topic] ||= []
38
+ subscribers[topic] << ws
59
39
 
60
- # generally used by the web server
61
- #
62
- def initialize(hostname)
63
-
64
- DRb.start_service
65
- # attach to the DRb server via a URI given on the command line
66
- @remote_obj = DRbObject.new nil, "druby://#{hostname}:59000"
67
- end
68
-
69
- def self.connect(base_url)
70
- yield(PubSub.new base_url)
40
+ elsif a.length > 1
41
+
42
+ puts "publish this %s: %s" % a
43
+ topic, message = a
44
+
45
+ if subscribers[topic] and subscribers[topic].any? then
46
+
47
+ connections = subscribers[topic]
48
+ connections += subscribers['#'] if subscribers['#']
49
+ connections.each {|c| c.send message }
50
+ end
51
+
52
+ end
53
+
54
+ ws.send msg, :type => type
55
+ end
56
+
57
+ ws.onclose do
58
+ puts "Client disconnected"
59
+ end
60
+ end
61
+
62
+ end
71
63
  end
72
64
  end
73
-
74
- class Server
75
-
76
- attr_reader :subscribers, :bridges
77
-
78
- def initialize(raw_reg='simplepubsub.xml')
79
65
 
66
+ class Client
80
67
 
81
- h = {DWSRegistry: ->{raw_reg}, String: ->{DWSRegistry.new raw_reg},:'RSC::Package' => ->{raw_reg}}
68
+ class PubSub
82
69
 
83
- @reg = h[raw_reg.class.to_s.to_sym].call
84
-
85
- doc = Rexle.new(@reg.xml('hkey_apps/simplepubsub'))
70
+ attr_reader :proc, :topic, :message
86
71
 
87
- root = doc.root
88
- # try to read the subscribers
89
- topics = root.xpath 'subscription_topics/*'
72
+ def get(topic, options={}, &get_proc)
90
73
 
91
- @hostname = Socket.gethostname
92
- @subscribers, @bridges = {'#' => []}, {'#' => {}}
93
-
94
- topics.each do |topic_element|
95
- topic = topic_element.name
96
- @subscribers[topic] ||= []
97
- @subscribers[topic] = topic_element.xpath 'subscribers/*/text()'
74
+ @topic = 'subscribe to topic'
75
+ @proc, @message = get_proc, topic
98
76
  end
99
77
 
100
- @subscribers['#'] = root.xpath 'subscription_all_topics' + \
101
- '/subscribers/*/text()'
78
+ def publish(topic, message)
102
79
 
103
- root.xpath('bridge_topics/*').each do |topic_element|
104
- topic = topic_element.name
105
- @bridges[topic] = topic_element.elements[0].elements.inject({}) do |r,x|
106
- r.merge({x.name.to_s => x.text('address')})
107
- end
80
+ @topic, @message = topic, message
81
+ @proc = ->(_,_){ :stop}
108
82
  end
83
+ end
84
+
85
+ def self.connect(hostname, port='59000')
109
86
 
87
+ pubsub = PubSub.new
88
+ yield(pubsub)
110
89
 
111
- @bridges['#'] = root.xpath('bridge_all_topics/subscribers/*')
112
- .inject({}) do |r,x|
113
- r.merge({x.name.to_s => x.text('address')})
114
- end
90
+ EM.run do
115
91
 
116
- 'done'
117
- end
92
+ address = hostname + ':' + port
118
93
 
119
- def start()
120
-
121
- # start up the DRb service
122
- DRb.start_service 'druby://:59000', self
94
+ ws = WebSocket::EventMachine::Client.connect(:uri => 'ws://' + address)
123
95
 
124
- # wait for the DRb service to finish before exiting
125
- DRb.thread.join
96
+ ws.onopen do
97
+ puts "Connected"
98
+ end
126
99
 
127
- 'done'
128
- end
129
-
130
- def subscribe(topic, uri)
131
-
132
- topic.sub!('/','_')
133
- @subscribers[topic] ||= []
134
- @subscribers[topic] << uri
135
-
136
- # e.g. 'hkey_apps/simplepubsub/subscription_topics/magic/subscribers/niko',
137
- # 'druby://niko:353524'
138
- if topic == '#' then
139
- key = "hkey_apps/simplepubsub/subscription_all_topics/subscribers/%s" % \
140
- [uri[/[^\/]+$/].sub(':','')]
141
- else
142
- key = "hkey_apps/simplepubsub/subscription_topics/%s/subscribers/%s" % \
143
- [topic, uri[/[^\/]+$/].sub(':','')]
144
- end
145
-
146
- @reg.set_key key, uri
147
- end
148
-
149
- def deliver(topic, msg)
150
-
151
- topic.sub!('/','_')
152
-
153
- if not @subscribers.include?(topic) and \
154
- not @subscribers.include?('#') then
155
- return 'no topic subscribers'
156
- end
157
-
158
-
159
- DRb.start_service
160
-
161
- topic_subscribers = @subscribers[topic]
162
-
163
- if topic_subscribers then
164
-
165
- topic_subscribers.each do |uri|
166
-
167
- next if @subscribers['#'].include? uri
168
-
169
- Thread.new {
170
- begin
171
-
172
- echo = DRbObject.new nil, uri
173
- echo.message topic, msg
174
-
175
- rescue DRb::DRbConnError => e
176
-
177
- @subscribers[topic].delete uri
178
-
179
- if @subscribers[topic].empty? then
180
- @subscribers.delete topic
181
- key = "hkey_apps/simplepubsub/subscription_topics/%s" % [topic]
182
- else
183
- key = "hkey_apps/simplepubsub/subscription_topics/%s/subscribers/%s" % \
184
- [topic, uri[/[^\/]+$/].sub(':','')]
185
- end
186
-
187
- @reg.delete_key key
188
- end
189
- }
190
-
100
+ ws.onmessage do |msg, type|
101
+
102
+ a = msg.split(/\s*,\s*/,2)
103
+ topic, message = a
104
+ r = pubsub.proc.call topic, message
105
+ (ws.close; EM.stop) if r == :stop
191
106
  end
192
- end
193
-
194
- @subscribers['#'].each do |uri|
195
- Thread.new {
196
-
197
- begin
198
- echo = DRbObject.new nil, uri
199
- echo.message topic, msg
200
- rescue DRb::DRbConnError => e
201
-
202
- @subscribers['#'].delete uri
203
- key = "hkey_apps/simplepubsub/subscription_all_topics/subscribers/%s" % \
204
- [uri[/[^\/]+$/].sub(':','')]
205
- @reg.delete_key key
206
- end
207
- }
107
+
108
+ ws.onclose do
109
+ puts "Disconnected"
110
+ end
111
+
112
+ EventMachine.next_tick do
113
+ ws.send pubsub.topic + ': ' + pubsub.message
114
+ end
115
+
208
116
 
209
117
  end
210
- end
211
-
212
- def add_bridge(topic, hostname, address)
213
-
214
- if topic == '#' then
215
-
216
- key = "hkey_apps/simplepubsub/bridge_all_topics/webserver/%s/address" % \
217
- [hostname]
218
- else
219
- topic.sub!('/','_')
220
- @bridges[topic] ||= {}
221
-
222
- key = "hkey_apps/simplepubsub/bridge_topics/%s/webserver/%s/address" % \
223
- [topic, hostname]
224
- end
225
-
226
- @bridges[topic].merge!(hostname => address)
227
- @reg.set_key key, address
228
- end
229
-
230
- def delete_bridge(topic, hostname)
231
-
232
- @bridges[topic].delete hostname
233
-
234
- if @bridges[topic].empty? and topic != '#' then
235
- @bridges.delete_key(topic)
236
- key = "hkey_apps/simplepubsub/bridge_topics/%s" % [topic]
237
- else
238
- key = "hkey_apps/simplepubsub/bridge_topics/%s/webserver/%s/address" % \
239
- [topic, hostname]
240
- end
241
-
242
- @reg.delete_key key
243
- end
244
-
245
- def bridge_deliver(topic, message, excluded_host=nil)
246
-
247
- return 'no matching topic' unless @bridges.has_key? topic
248
-
249
- if excluded_host then
250
- bridges = @bridges[topic].select{|x| x != excluded_host}
251
- else
252
- bridges = @bridges[topic]
253
- end
254
-
255
- bridges.values.each do |address|
256
- url = "http://%s/do/simplepubsub/bridgepub?topic=%s&hostname=%s&message=%s" % \
257
- [address, URI.escape(topic), @hostname, URI.escape(message)]
258
- r = open(url, 'UserAgent' => USER_AGENT)
259
- end
260
- 'bridge delivered'
261
- end
262
-
118
+ end
263
119
  end
264
-
120
+ end
121
+
122
+ if __FILE__ == $0 then
123
+
124
+ # Subscribe example
125
+ SimplePubSub::Server.new.start
126
+
265
127
  end
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simplepubsub
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.3
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Robertson
@@ -31,10 +31,10 @@ cert_chain:
31
31
  i0PBE6fLwlIMk8jNNUcX3J1csSMgLNTwE8CwrlSZE8sueHj0pQzICM7lF0gGREO6
32
32
  hQHD0LljU3MusQ==
33
33
  -----END CERTIFICATE-----
34
- date: 2013-10-30 00:00:00.000000000 Z
34
+ date: 2013-11-10 00:00:00.000000000 Z
35
35
  dependencies:
36
36
  - !ruby/object:Gem::Dependency
37
- name: dws-registry
37
+ name: websocket-eventmachine-server
38
38
  requirement: !ruby/object:Gem::Requirement
39
39
  requirements:
40
40
  - - ">="
@@ -48,7 +48,7 @@ dependencies:
48
48
  - !ruby/object:Gem::Version
49
49
  version: '0'
50
50
  - !ruby/object:Gem::Dependency
51
- name: chronic_duration
51
+ name: websocket-eventmachine-client
52
52
  requirement: !ruby/object:Gem::Requirement
53
53
  requirements:
54
54
  - - ">="
metadata.gz.sig CHANGED
Binary file