Class: RubyOnAcid::RindaFactory

Inherits:
Factory
  • Object
show all
Defined in:
lib/rubyonacid/factories/rinda.rb

Overview

Allows values to be sent over the network. For more information, see the Ruby standard library documentation for Rinda.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Factory

#boolean, #choose, #get, #within

Constructor Details

#initialize(uri = "druby://127.0.0.1:7632", timeout = 0) ⇒ RindaFactory

Returns a new instance of RindaFactory.



17
18
19
20
21
22
23
# File 'lib/rubyonacid/factories/rinda.rb', line 17

def initialize(uri = "druby://127.0.0.1:7632", timeout = 0)
  super
  @uri = uri
  @timeout = timeout
  @default_factory = nil
  @prior_values = {}
end

Instance Attribute Details

#default_factoryObject

A factory to pull requests from if retrieval of values via Rinda times out.



13
14
15
# File 'lib/rubyonacid/factories/rinda.rb', line 13

def default_factory
  @default_factory
end

#timeoutObject

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.



11
12
13
# File 'lib/rubyonacid/factories/rinda.rb', line 11

def timeout
  @timeout
end

#uriObject

The URI to connect to. Default is “druby://127.0.0.1:7632” (7632 == RNDA).



15
16
17
# File 'lib/rubyonacid/factories/rinda.rb', line 15

def uri
  @uri
end

Instance Method Details

#get_unit(key) ⇒ Object

Get key from Rinda server.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rubyonacid/factories/rinda.rb', line 32

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

#start_serviceObject

Create the Rinda TupleSpace for clients to write to.



26
27
28
29
# File 'lib/rubyonacid/factories/rinda.rb', line 26

def start_service
  DRb.start_service 
  @space = Rinda::TupleSpaceProxy.new(DRbObject.new(nil, @uri))
end