Sha256: 411dc1c1263e026c31e80d354ac00af0829f887854d53d2947898e101358ca87

Contents?: true

Size: 1.15 KB

Versions: 1

Compression:

Stored size: 1.15 KB

Contents

require 'rinda/rinda' 
require 'rubyonacid/factory'

module RubyOnAcid

class RindaFactory < Factory
  
  #Time in seconds to wait for a value before giving up and returning a default value for the given key.
  #Default is 0, which will return immediately.
  attr_accessor :timeout
  #A factory to pull requests from if retrieval of values via Rinda times out.
  attr_accessor :default_factory
  #The URI to connect to.  Default is "druby://127.0.0.1:7632" (7632 == RNDA).
  attr_accessor :uri
  
  def initialize(uri = "druby://127.0.0.1:7632", timeout = 0)
    super
    @uri = uri
    @timeout = timeout
    @default_factory = nil
    @prior_values = {}
  end
  
  def start_service
    DRb.start_service 
    @space = Rinda::TupleSpaceProxy.new(DRbObject.new(nil, @uri))
  end
  
  #Get key from Rinda server.
  def get_unit(key)
    @prior_values[key] ||= 0.0
    begin
      key, value = @space.take([key, Float], @timeout)
      @prior_values[key] = value
    rescue Rinda::RequestExpiredError => exception
      if @default_factory
        value = @default_factory.get_unit(key)
      else
        value = @prior_values[key]
      end
    end
    value
  end

end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rubyonacid-0.2.0 lib/rubyonacid/factories/rinda.rb