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.



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

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

Instance Method Details

#eachObject

Yields event/tuple pairs until this NotifyTemplateEntry expires.



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

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.



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

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

#popObject

Retrieves a notification. Raises RequestExpiredError when this NotifyTemplateEntry expires.



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

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