123456789_123456789_123456789_123456789_123456789_

Class: Rinda::RingFinger

Relationships & Source Files
Inherits: Object
Defined in: lib/rinda/ring.rb

Overview

RingFinger is used by RingServer clients to discover the RingServer's TupleSpace. Typically, all a client needs to do is call .primary to retrieve the remote TupleSpace, which it can then begin using.

To find the first available remote TupleSpace:

Rinda::RingFinger.primary

To create a RingFinger that broadcasts to a custom list:

rf = Rinda::RingFinger.new  ['localhost', '192.0.2.1']
rf.primary

RingFinger also understands multicast addresses and sets them up properly. This allows you to run multiple RingServers on the same host:

rf = Rinda::RingFinger.new ['239.0.0.1']
rf.primary

You can set the hop count (or TTL) for multicast searches using #multicast_hops.

If you use IPv6 multicast you may need to set both an address and the outbound interface index:

rf = Rinda::RingFinger.new ['ff02::1']
rf.multicast_interface = 1
rf.primary

At this time there is no easy way to get an interface index by name.

Class Method Summary

Instance Attribute Summary

  • #broadcast_list rw

    The list of addresses where RingFinger will send query packets.

  • #multicast_hops rw

    Maximum number of hops for sent multicast packets (if using a multicast address in the broadcast list).

  • #multicast_interface rw

    The interface index to send IPv6 multicast packets from.

  • #port rw

    The port that RingFinger will send query packets to.

  • #primary rw

    Contain the first advertised TupleSpace after lookup_ring_any is called.

Instance Method Summary

Constructor Details

.new(broadcast_list = @@broadcast_list, port = Ring_PORT) ⇒ RingFinger

Creates a new RingFinger that will look for RingServers at #port on the addresses in #broadcast_list.

If #broadcast_list contains a multicast address then multicast queries will be made using the given multicast_hops and multicast_interface.

[ GitHub ]

  
# File 'lib/rinda/ring.rb', line 341

def initialize(broadcast_list=@@broadcast_list, port=Ring_PORT)
  @broadcast_list = broadcast_list || ['localhost']
  @port = port
  @primary = nil
  @rings = []

  @multicast_hops = 1
  @multicast_interface = 0
end

Class Method Details

.finger

Creates a singleton RingFinger and looks for a RingServer. Returns the created RingFinger.

[ GitHub ]

  
# File 'lib/rinda/ring.rb', line 285

def self.finger
  unless @@finger
    @@finger = self.new
    @@finger.lookup_ring_any
  end
  @@finger
end

.primary

Returns the first advertised TupleSpace.

[ GitHub ]

  
# File 'lib/rinda/ring.rb', line 296

def self.primary
  finger.primary
end

.to_a

Contains all discovered TupleSpaces except for the primary.

[ GitHub ]

  
# File 'lib/rinda/ring.rb', line 303

def self.to_a
  finger.to_a
end

Instance Attribute Details

#broadcast_list (rw)

The list of addresses where RingFinger will send query packets.

[ GitHub ]

  
# File 'lib/rinda/ring.rb', line 310

attr_accessor :broadcast_list

#multicast_hops (rw)

Maximum number of hops for sent multicast packets (if using a multicast address in the broadcast list). The default is 1 (same as UDP broadcast).

[ GitHub ]

  
# File 'lib/rinda/ring.rb', line 317

attr_accessor :multicast_hops

#multicast_interface (rw)

The interface index to send IPv6 multicast packets from.

[ GitHub ]

  
# File 'lib/rinda/ring.rb', line 322

attr_accessor :multicast_interface

#port (rw)

The port that RingFinger will send query packets to.

[ GitHub ]

  
# File 'lib/rinda/ring.rb', line 327

attr_accessor :port

#primary (rw)

Contain the first advertised TupleSpace after lookup_ring_any is called.

[ GitHub ]

  
# File 'lib/rinda/ring.rb', line 332

attr_accessor :primary

Instance Method Details

#each {|@primary| ... }

Iterates over all discovered TupleSpaces starting with the primary.

Yields:

[ GitHub ]

  
# File 'lib/rinda/ring.rb', line 361

def each
  lookup_ring_any unless @primary
  return unless @primary
  yield(@primary)
  @rings.each { |x| yield(x) }
end

#lookup_ring(timeout = 5, &block)

Looks up RingServers waiting timeout seconds. RingServers will be given block as a callback, which will be called with the remote TupleSpace.

[ GitHub ]

  
# File 'lib/rinda/ring.rb', line 373

def lookup_ring(timeout=5, &block)
  return lookup_ring_any(timeout) unless block_given?

  msg = Marshal.dump([[:lookup_ring, DRbObject.new(block)], timeout])
  @broadcast_list.each do |it|
    send_message(it, msg)
  end
  sleep(timeout)
end

#lookup_ring_any(timeout = 5)

Returns the first found remote TupleSpace. Any further recovered TupleSpaces can be found by calling .to_a.

[ GitHub ]

  
# File 'lib/rinda/ring.rb', line 387

def lookup_ring_any(timeout=5)
  queue = Queue.new

  Thread.new do
    self.lookup_ring(timeout) do |ts|
      queue.push(ts)
    end
    queue.push(nil)
  end

  @primary = queue.pop
  raise('RingNotFound') if @primary.nil?

  Thread.new do
    while it = queue.pop
      @rings.push(it)
    end
  end

  @primary
end

#to_a

Contains all discovered TupleSpaces except for the primary.

[ GitHub ]

  
# File 'lib/rinda/ring.rb', line 354

def to_a
  @rings
end