123456789_123456789_123456789_123456789_123456789_

Class: Rinda::NotifyTemplateEntry

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Instance Chain:
self, TemplateEntry, TupleEntry, DRbUndumped
Inherits: Rinda::TemplateEntry
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]]

Class Method Summary

TupleEntry - Inherited

.new

Creates a TupleEntry based on ary with an optional renewer or expiry time sec.

Instance Attribute Summary

TupleEntry - Inherited

#alive?

A TupleEntry is dead when it is canceled or expired.

#canceled?

Returns the canceled status.

#expired?

Has this tuple expired? (true/false).

#expires

Instance Method Summary

  • #each

    Yields event/tuple pairs until this NotifyTemplateEntry expires.

  • #notify(ev)

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

  • #pop

    Retrieves a notification.

TemplateEntry - Inherited

#===
#match

Matches this TemplateEntry against tuple.

#make_tuple

TupleEntry - Inherited

#[]

Retrieves key from the tuple.

#cancel

Marks this TupleEntry as canceled.

#fetch

Fetches key from the tuple.

#make_expires
Returns an expiry Time based on sec which can be one of: Numeric
sec seconds into the future true
the expiry time is the start of 1970 (i.e. expired) nil

it is Tue Jan 19 03:14:07 GMT Standard Time 2038 (i.e. when.

#make_tuple

Creates a Tuple for ary.

#renew

Reset the expiry time according to sec_or_renewer.

#size

The size of the tuple.

#value

Return the object which makes up the tuple itself: the Array or Hash.

#get_renewer

Returns a valid argument to make_expires and the renewer or nil.

Constructor Details

.new(place, event, tuple, expires = nil) ⇒ NotifyTemplateEntry

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

[ GitHub ]

  
# 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

#each

Yields event/tuple pairs until this NotifyTemplateEntry expires.

[ GitHub ]

  
# 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)

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

[ GitHub ]

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

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

#pop

Retrieves a notification. Raises RequestExpiredError when this NotifyTemplateEntry expires.

[ GitHub ]

  
# 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