rubysl-rinda 1.0.0 → 2.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: 569d3924674613e8ef815b980e434f1f9083948c
4
- data.tar.gz: bd62bc505d9fe5770dd53c548898b07d3f3b7531
3
+ metadata.gz: c2507e6dc6b7aa120ba8b18eff4075268a27799f
4
+ data.tar.gz: 8c52201f4f499a4800524c544bf68e27b4453c14
5
5
  SHA512:
6
- metadata.gz: a9e02962e23175532cd263856f609c326ba754321e2922e4f86ae364af4a478c9c85fdd47c4279b8f205893b225d2968f10a8ee16c2d66bb27243ba8ee1f072a
7
- data.tar.gz: df2c144c6265a21fc8c64feefd49a08868229f91b4b15361efe9ec953c151517585210c51b14f14d73d7762c64e3c53e9d98b03f76a8029deb074e86d5609b69
6
+ metadata.gz: 450544d5bbf7343fce6771ffa4c4c6b5f9a3ba2e115e6e9c60dad78be1899c7ea2e5bf7826bef348fe9a64248e3f8fca620be0f8df017f5d23b0352295b8483e
7
+ data.tar.gz: 0d4a3223c481fe1cb842338199b5c5c927a1ee53247cb7396d5d90333a86fe8dbe07f40ab0b0e7ce12058e6028440a53b58bee7c394bd68a9efd7b3ea4813e39
@@ -1,8 +1,7 @@
1
1
  language: ruby
2
- before_install:
3
- - gem update --system
4
- - gem --version
5
- - gem install rubysl-bundler
6
- script: bundle exec mspec spec
2
+ env:
3
+ - RUBYLIB=lib
4
+ script: bundle exec mspec
7
5
  rvm:
8
- - rbx-nightly-18mode
6
+ - 1.9.3
7
+ - rbx-nightly-19mode
@@ -0,0 +1 @@
1
+ require "rubysl/rinda"
@@ -19,7 +19,7 @@ module Rinda
19
19
  # 1. A RingServer begins listening on the broadcast UDP address.
20
20
  # 2. A RingFinger sends a UDP packet containing the DRb URI where it will
21
21
  # listen for a reply.
22
- # 3. The RingServer recieves the UDP packet and connects back to the
22
+ # 3. The RingServer receives the UDP packet and connects back to the
23
23
  # provided DRb URI with the DRb service.
24
24
 
25
25
  class RingServer
@@ -43,24 +43,24 @@ module Rinda
43
43
 
44
44
  def write_service
45
45
  Thread.new do
46
- loop do
47
- msg = @soc.recv(1024)
48
- do_write(msg)
49
- end
46
+ loop do
47
+ msg = @soc.recv(1024)
48
+ do_write(msg)
49
+ end
50
50
  end
51
51
  end
52
-
52
+
53
53
  ##
54
54
  # Extracts the response URI from +msg+ and adds it to TupleSpace where it
55
55
  # will be picked up by +reply_service+ for notification.
56
56
 
57
57
  def do_write(msg)
58
58
  Thread.new do
59
- begin
60
- tuple, sec = Marshal.load(msg)
61
- @ts.write(tuple, sec)
62
- rescue
63
- end
59
+ begin
60
+ tuple, sec = Marshal.load(msg)
61
+ @ts.write(tuple, sec)
62
+ rescue
63
+ end
64
64
  end
65
65
  end
66
66
 
@@ -69,18 +69,18 @@ module Rinda
69
69
 
70
70
  def reply_service
71
71
  Thread.new do
72
- loop do
73
- do_reply
74
- end
72
+ loop do
73
+ do_reply
74
+ end
75
75
  end
76
76
  end
77
-
77
+
78
78
  ##
79
79
  # Pulls lookup tuples out of the TupleSpace and sends their DRb object the
80
80
  # address of the local TupleSpace.
81
81
 
82
82
  def do_reply
83
- tuple = @ts.take([:lookup_ring, nil])
83
+ tuple = @ts.take([:lookup_ring, DRbObject])
84
84
  Thread.new { tuple[1].call(@ts) rescue nil}
85
85
  rescue
86
86
  end
@@ -104,9 +104,9 @@ module Rinda
104
104
  # created RingFinger.
105
105
 
106
106
  def self.finger
107
- unless @@finger
108
- @@finger = self.new
109
- @@finger.lookup_ring_any
107
+ unless @@finger
108
+ @@finger = self.new
109
+ @@finger.lookup_ring_any
110
110
  end
111
111
  @@finger
112
112
  end
@@ -119,7 +119,7 @@ module Rinda
119
119
  end
120
120
 
121
121
  ##
122
- # Contains all discoverd TupleSpaces except for the primary.
122
+ # Contains all discovered TupleSpaces except for the primary.
123
123
 
124
124
  def self.to_a
125
125
  finger.to_a
@@ -178,15 +178,15 @@ module Rinda
178
178
 
179
179
  msg = Marshal.dump([[:lookup_ring, DRbObject.new(block)], timeout])
180
180
  @broadcast_list.each do |it|
181
- soc = UDPSocket.open
182
- begin
183
- soc.setsockopt(Socket::SOL_SOCKET, Socket::SO_BROADCAST, true)
184
- soc.send(msg, 0, it, @port)
185
- rescue
186
- nil
187
- ensure
188
- soc.close
189
- end
181
+ soc = UDPSocket.open
182
+ begin
183
+ soc.setsockopt(Socket::SOL_SOCKET, Socket::SO_BROADCAST, true)
184
+ soc.send(msg, 0, it, @port)
185
+ rescue
186
+ nil
187
+ ensure
188
+ soc.close
189
+ end
190
190
  end
191
191
  sleep(timeout)
192
192
  end
@@ -198,18 +198,22 @@ module Rinda
198
198
  def lookup_ring_any(timeout=5)
199
199
  queue = Queue.new
200
200
 
201
- th = Thread.new do
202
- self.lookup_ring(timeout) do |ts|
203
- queue.push(ts)
204
- end
205
- queue.push(nil)
206
- while it = queue.pop
207
- @rings.push(it)
208
- end
201
+ Thread.new do
202
+ self.lookup_ring(timeout) do |ts|
203
+ queue.push(ts)
204
+ end
205
+ queue.push(nil)
209
206
  end
210
-
207
+
211
208
  @primary = queue.pop
212
209
  raise('RingNotFound') if @primary.nil?
210
+
211
+ Thread.new do
212
+ while it = queue.pop
213
+ @rings.push(it)
214
+ end
215
+ end
216
+
213
217
  @primary
214
218
  end
215
219
 
@@ -252,19 +256,19 @@ if __FILE__ == $0
252
256
  when 's'
253
257
  require 'rinda/tuplespace'
254
258
  ts = Rinda::TupleSpace.new
255
- place = Rinda::RingServer.new(ts)
259
+ Rinda::RingServer.new(ts)
256
260
  $stdin.gets
257
261
  when 'w'
258
262
  finger = Rinda::RingFinger.new(nil)
259
- finger.lookup_ring do |ts|
260
- p ts
261
- ts.write([:hello, :world])
263
+ finger.lookup_ring do |ts2|
264
+ p ts2
265
+ ts2.write([:hello, :world])
262
266
  end
263
267
  when 'r'
264
268
  finger = Rinda::RingFinger.new(nil)
265
- finger.lookup_ring do |ts|
266
- p ts
267
- p ts.take([nil, nil])
269
+ finger.lookup_ring do |ts2|
270
+ p ts2
271
+ p ts2.take([nil, nil])
268
272
  end
269
273
  end
270
274
  end
@@ -2,6 +2,8 @@ require 'monitor'
2
2
  require 'thread'
3
3
  require 'drb/drb'
4
4
  require 'rinda/rinda'
5
+ require 'enumerator'
6
+ require 'forwardable'
5
7
 
6
8
  module Rinda
7
9
 
@@ -71,14 +73,14 @@ module Rinda
71
73
  end
72
74
 
73
75
  ##
74
- # Reset the expiry time according to +sec_or_renewer+.
76
+ # Reset the expiry time according to +sec_or_renewer+.
75
77
  #
76
78
  # +nil+:: it is set to expire in the far future.
77
79
  # +false+:: it has expired.
78
80
  # Numeric:: it will expire in that many seconds.
79
81
  #
80
82
  # Otherwise the argument refers to some kind of renewer object
81
- # which will reset its expiry time.
83
+ # which will reset its expiry time.
82
84
 
83
85
  def renew(sec_or_renewer)
84
86
  sec, @renewer = get_renewer(sec_or_renewer)
@@ -166,7 +168,7 @@ module Rinda
166
168
  def match(tuple)
167
169
  @tuple.match(tuple)
168
170
  end
169
-
171
+
170
172
  alias === match
171
173
 
172
174
  def make_tuple(ary) # :nodoc:
@@ -222,11 +224,11 @@ module Rinda
222
224
  #
223
225
  # ts = Rinda::TupleSpace.new
224
226
  # observer = ts.notify 'write', [nil]
225
- #
227
+ #
226
228
  # Thread.start do
227
229
  # observer.each { |t| p t }
228
230
  # end
229
- #
231
+ #
230
232
  # 3.times { |i| ts.write [i] }
231
233
  #
232
234
  # Outputs:
@@ -274,7 +276,7 @@ module Rinda
274
276
  it = pop
275
277
  yield(it)
276
278
  end
277
- rescue
279
+ rescue
278
280
  ensure
279
281
  cancel
280
282
  end
@@ -286,45 +288,70 @@ module Rinda
286
288
  # of Tuplespace.
287
289
 
288
290
  class TupleBag
291
+ class TupleBin
292
+ extend Forwardable
293
+ def_delegators '@bin', :find_all, :delete_if, :each, :empty?
294
+
295
+ def initialize
296
+ @bin = []
297
+ end
298
+
299
+ def add(tuple)
300
+ @bin.push(tuple)
301
+ end
302
+
303
+ def delete(tuple)
304
+ idx = @bin.rindex(tuple)
305
+ @bin.delete_at(idx) if idx
306
+ end
307
+
308
+ def find
309
+ @bin.reverse_each do |x|
310
+ return x if yield(x)
311
+ end
312
+ nil
313
+ end
314
+ end
289
315
 
290
316
  def initialize # :nodoc:
291
317
  @hash = {}
318
+ @enum = enum_for(:each_entry)
292
319
  end
293
320
 
294
321
  ##
295
322
  # +true+ if the TupleBag to see if it has any expired entries.
296
323
 
297
324
  def has_expires?
298
- @hash.each do |k, v|
299
- v.each do |tuple|
300
- return true if tuple.expires
301
- end
325
+ @enum.find do |tuple|
326
+ tuple.expires
302
327
  end
303
- false
304
328
  end
305
329
 
306
330
  ##
307
- # Add +ary+ to the TupleBag.
331
+ # Add +tuple+ to the TupleBag.
308
332
 
309
- def push(ary)
310
- size = ary.size
311
- @hash[size] ||= []
312
- @hash[size].push(ary)
333
+ def push(tuple)
334
+ key = bin_key(tuple)
335
+ @hash[key] ||= TupleBin.new
336
+ @hash[key].add(tuple)
313
337
  end
314
338
 
315
339
  ##
316
- # Removes +ary+ from the TupleBag.
340
+ # Removes +tuple+ from the TupleBag.
317
341
 
318
- def delete(ary)
319
- size = ary.size
320
- @hash.fetch(size, []).delete(ary)
342
+ def delete(tuple)
343
+ key = bin_key(tuple)
344
+ bin = @hash[key]
345
+ return nil unless bin
346
+ bin.delete(tuple)
347
+ @hash.delete(key) if bin.empty?
348
+ tuple
321
349
  end
322
350
 
323
351
  ##
324
352
  # Finds all live tuples that match +template+.
325
-
326
353
  def find_all(template)
327
- @hash.fetch(template.size, []).find_all do |tuple|
354
+ bin_for_find(template).find_all do |tuple|
328
355
  tuple.alive? && template.match(tuple)
329
356
  end
330
357
  end
@@ -333,7 +360,7 @@ module Rinda
333
360
  # Finds a live tuple that matches +template+.
334
361
 
335
362
  def find(template)
336
- @hash.fetch(template.size, []).find do |tuple|
363
+ bin_for_find(template).find do |tuple|
337
364
  tuple.alive? && template.match(tuple)
338
365
  end
339
366
  end
@@ -343,7 +370,7 @@ module Rinda
343
370
  # +tuple+ and are alive.
344
371
 
345
372
  def find_all_template(tuple)
346
- @hash.fetch(tuple.size, []).find_all do |template|
373
+ @enum.find_all do |template|
347
374
  template.alive? && template.match(tuple)
348
375
  end
349
376
  end
@@ -354,20 +381,39 @@ module Rinda
354
381
 
355
382
  def delete_unless_alive
356
383
  deleted = []
357
- @hash.keys.each do |size|
358
- ary = []
359
- @hash[size].each do |tuple|
384
+ @hash.each do |key, bin|
385
+ bin.delete_if do |tuple|
360
386
  if tuple.alive?
361
- ary.push(tuple)
387
+ false
362
388
  else
363
389
  deleted.push(tuple)
390
+ true
364
391
  end
365
392
  end
366
- @hash[size] = ary
367
393
  end
368
394
  deleted
369
395
  end
370
396
 
397
+ private
398
+ def each_entry(&blk)
399
+ @hash.each do |k, v|
400
+ v.each(&blk)
401
+ end
402
+ end
403
+
404
+ def bin_key(tuple)
405
+ head = tuple[0]
406
+ if head.class == Symbol
407
+ return head
408
+ else
409
+ false
410
+ end
411
+ end
412
+
413
+ def bin_for_find(template)
414
+ key = bin_key(template)
415
+ key ? @hash.fetch(key, []) : @enum
416
+ end
371
417
  end
372
418
 
373
419
  ##
@@ -403,8 +449,7 @@ module Rinda
403
449
  # Adds +tuple+
404
450
 
405
451
  def write(tuple, sec=nil)
406
- entry = TupleEntry.new(tuple, sec)
407
- start_keeper
452
+ entry = create_entry(tuple, sec)
408
453
  synchronize do
409
454
  if entry.expired?
410
455
  @read_waiter.find_all_template(entry).each do |template|
@@ -414,6 +459,7 @@ module Rinda
414
459
  notify_event('delete', entry.value)
415
460
  else
416
461
  @bag.push(entry)
462
+ start_keeper if entry.expires
417
463
  @read_waiter.find_all_template(entry).each do |template|
418
464
  template.read(tuple)
419
465
  end
@@ -439,7 +485,6 @@ module Rinda
439
485
  def move(port, tuple, sec=nil)
440
486
  template = WaitTemplateEntry.new(self, tuple, sec)
441
487
  yield(template) if block_given?
442
- start_keeper
443
488
  synchronize do
444
489
  entry = @bag.find(template)
445
490
  if entry
@@ -452,6 +497,7 @@ module Rinda
452
497
 
453
498
  begin
454
499
  @take_waiter.push(template)
500
+ start_keeper if template.expires
455
501
  while true
456
502
  raise RequestCanceledError if template.canceled?
457
503
  raise RequestExpiredError if template.expired?
@@ -476,7 +522,6 @@ module Rinda
476
522
  def read(tuple, sec=nil)
477
523
  template = WaitTemplateEntry.new(self, tuple, sec)
478
524
  yield(template) if block_given?
479
- start_keeper
480
525
  synchronize do
481
526
  entry = @bag.find(template)
482
527
  return entry.value if entry
@@ -484,6 +529,7 @@ module Rinda
484
529
 
485
530
  begin
486
531
  @read_waiter.push(template)
532
+ start_keeper if template.expires
487
533
  template.wait
488
534
  raise RequestCanceledError if template.canceled?
489
535
  raise RequestExpiredError if template.expired?
@@ -529,6 +575,10 @@ module Rinda
529
575
 
530
576
  private
531
577
 
578
+ def create_entry(tuple, sec)
579
+ TupleEntry.new(tuple, sec)
580
+ end
581
+
532
582
  ##
533
583
  # Removes dead tuples.
534
584
 
@@ -566,9 +616,12 @@ module Rinda
566
616
  def start_keeper
567
617
  return if @keeper && @keeper.alive?
568
618
  @keeper = Thread.new do
569
- while need_keeper?
570
- keep_clean
619
+ while true
571
620
  sleep(@period)
621
+ synchronize do
622
+ break unless need_keeper?
623
+ keep_clean
624
+ end
572
625
  end
573
626
  end
574
627
  end
@@ -1 +1,2 @@
1
+ require "rubysl/rinda/rinda"
1
2
  require "rubysl/rinda/version"
@@ -58,7 +58,7 @@ module Rinda
58
58
 
59
59
  ##
60
60
  # The number of elements in the tuple.
61
-
61
+
62
62
  def size
63
63
  @tuple.size
64
64
  end
@@ -162,7 +162,7 @@ module Rinda
162
162
  end
163
163
  return true
164
164
  end
165
-
165
+
166
166
  ##
167
167
  # Alias for #match.
168
168
 
@@ -171,7 +171,7 @@ module Rinda
171
171
  end
172
172
 
173
173
  end
174
-
174
+
175
175
  ##
176
176
  # <i>Documentation?</i>
177
177
 
@@ -184,7 +184,7 @@ module Rinda
184
184
  @drb_uri = uri
185
185
  @drb_ref = ref
186
186
  end
187
-
187
+
188
188
  ##
189
189
  # This DRbObjectTemplate matches +ro+ if the remote object's drburi and
190
190
  # drbref are the same. +nil+ is used as a wildcard.
@@ -213,14 +213,14 @@ module Rinda
213
213
  def initialize(ts)
214
214
  @ts = ts
215
215
  end
216
-
216
+
217
217
  ##
218
218
  # Adds +tuple+ to the proxied TupleSpace. See TupleSpace#write.
219
219
 
220
220
  def write(tuple, sec=nil)
221
221
  @ts.write(tuple, sec)
222
222
  end
223
-
223
+
224
224
  ##
225
225
  # Takes +tuple+ from the proxied TupleSpace. See TupleSpace#take.
226
226
 
@@ -229,14 +229,14 @@ module Rinda
229
229
  @ts.move(DRbObject.new(port), tuple, sec, &block)
230
230
  port[0]
231
231
  end
232
-
232
+
233
233
  ##
234
234
  # Reads +tuple+ from the proxied TupleSpace. See TupleSpace#read.
235
235
 
236
236
  def read(tuple, sec=nil, &block)
237
237
  @ts.read(tuple, sec, &block)
238
238
  end
239
-
239
+
240
240
  ##
241
241
  # Reads all tuples matching +tuple+ from the proxied TupleSpace. See
242
242
  # TupleSpace#read_all.
@@ -244,7 +244,7 @@ module Rinda
244
244
  def read_all(tuple)
245
245
  @ts.read_all(tuple)
246
246
  end
247
-
247
+
248
248
  ##
249
249
  # Registers for notifications of event +ev+ on the proxied TupleSpace.
250
250
  # See TupleSpace#notify
@@ -1,5 +1,5 @@
1
1
  module RubySL
2
2
  module Rinda
3
- VERSION = "1.0.0"
3
+ VERSION = "2.0.0"
4
4
  end
5
5
  end
@@ -19,5 +19,4 @@ Gem::Specification.new do |spec|
19
19
  spec.add_development_dependency "bundler", "~> 1.3"
20
20
  spec.add_development_dependency "rake", "~> 10.0"
21
21
  spec.add_development_dependency "mspec", "~> 1.5"
22
- spec.add_development_dependency "rubysl-prettyprint", "~> 1.0"
23
- end
22
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubysl-rinda
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian Shirai
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-26 00:00:00.000000000 Z
11
+ date: 2013-09-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,20 +52,6 @@ dependencies:
52
52
  - - ~>
53
53
  - !ruby/object:Gem::Version
54
54
  version: '1.5'
55
- - !ruby/object:Gem::Dependency
56
- name: rubysl-prettyprint
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - ~>
60
- - !ruby/object:Gem::Version
61
- version: '1.0'
62
- type: :development
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - ~>
67
- - !ruby/object:Gem::Version
68
- version: '1.0'
69
55
  description: Ruby standard library rinda.
70
56
  email:
71
57
  - brixen@gmail.com
@@ -79,10 +65,11 @@ files:
79
65
  - LICENSE
80
66
  - README.md
81
67
  - Rakefile
82
- - lib/rinda/rinda.rb
68
+ - lib/rinda.rb
83
69
  - lib/rinda/ring.rb
84
70
  - lib/rinda/tuplespace.rb
85
71
  - lib/rubysl/rinda.rb
72
+ - lib/rubysl/rinda/rinda.rb
86
73
  - lib/rubysl/rinda/version.rb
87
74
  - rubysl-rinda.gemspec
88
75
  homepage: https://github.com/rubysl/rubysl-rinda