Class: Rinda::NotifyTemplateEntry

Inherits:
TemplateEntry show all
Defined in:
lib/rinda/tuplespace.rb

Overview

A NotifyTemplateEntry is returned by TupleSpace#notify and is notified of TupleSpace changes. You may receive either your subscribed event or the 'close' event when iterating over notifications.

See TupleSpace#notify_event for valid notification types.

Example

ts = Rinda::TupleSpace.new
observer = ts.notify 'write', [nil]

Thread.start do
  observer.each { |t| p t }
end

3.times { |i| ts.write [i] }

Outputs:

['write', [0]]
['write', [1]]
['write', [2]]

Instance Attribute Summary

Attributes inherited from TupleEntry

#expires

Instance Method Summary collapse

Methods inherited from TemplateEntry

#make_tuple, #match

Methods inherited from TupleEntry

#[], #alive?, #cancel, #canceled?, #expired?, #fetch, #make_expires, #make_tuple, #renew, #size, #value

Constructor Details

#initialize(place, event, tuple, expires = nil) ⇒ NotifyTemplateEntry

Creates a new NotifyTemplateEntry that watches place for events that match tuple.


246
247
248
249
250
251
# File 'lib/rinda/tuplespace.rb', line 246

def initialize(place, event, tuple, expires=nil)
  ary = [event, Rinda::Template.new(tuple)]
  super(ary, expires)
  @queue = Queue.new
  @done = false
end

Instance Method Details

#eachObject

Yields event/tuple pairs until this NotifyTemplateEntry expires.


274
275
276
277
278
279
280
281
282
# File 'lib/rinda/tuplespace.rb', line 274

def each # :yields: event, tuple
  while !@done
    it = pop
    yield(it)
  end
rescue
ensure
  cancel
end

#notify(ev) ⇒ Object

Called by TupleSpace to notify this NotifyTemplateEntry of a new event.


256
257
258
# File 'lib/rinda/tuplespace.rb', line 256

def notify(ev)
  @queue.push(ev)
end

#popObject

Retrieves a notification. Raises RequestExpiredError when this NotifyTemplateEntry expires.


264
265
266
267
268
269
# File 'lib/rinda/tuplespace.rb', line 264

def pop
  raise RequestExpiredError if @done
  it = @queue.pop
  @done = true if it[0] == 'close'
  return it
end