Class: Rinda::RingServer

Inherits:
Object
  • Object
show all
Includes:
DRbUndumped
Defined in:
lib/rinda/ring.rb

Overview

A RingServer allows a Rinda::TupleSpace to be located via UDP broadcasts. Service location uses the following steps:

  1. A RingServer begins listening on the broadcast UDP address.

  2. A RingFinger sends a UDP packet containing the DRb URI where it will listen for a reply.

  3. The RingServer receives the UDP packet and connects back to the provided DRb URI with the DRb service.

Instance Method Summary collapse

Constructor Details

#initialize(ts, port = Ring_PORT) ⇒ RingServer

Advertises ts on the UDP broadcast address at port.



32
33
34
35
36
37
38
# File 'lib/rinda/ring.rb', line 32

def initialize(ts, port=Ring_PORT)
  @ts = ts
  @soc = UDPSocket.open
  @soc.bind('', port)
  @w_service = write_service
  @r_service = reply_service
end

Instance Method Details

#do_replyObject

Pulls lookup tuples out of the TupleSpace and sends their DRb object the address of the local TupleSpace.



82
83
84
85
86
# File 'lib/rinda/ring.rb', line 82

def do_reply
  tuple = @ts.take([:lookup_ring, nil])
  Thread.new { tuple[1].call(@ts) rescue nil}
rescue
end

#do_write(msg) ⇒ Object

Extracts the response URI from msg and adds it to TupleSpace where it will be picked up by reply_service for notification.



57
58
59
60
61
62
63
64
65
# File 'lib/rinda/ring.rb', line 57

def do_write(msg)
  Thread.new do
	begin
	  tuple, sec = Marshal.load(msg)
	  @ts.write(tuple, sec)
	rescue
	end
  end
end

#reply_serviceObject

Creates a thread that notifies waiting clients from the TupleSpace.



70
71
72
73
74
75
76
# File 'lib/rinda/ring.rb', line 70

def reply_service
  Thread.new do
	loop do
	  do_reply
	end
  end
end

#write_serviceObject

Creates a thread that picks up UDP packets and passes them to do_write for decoding.



44
45
46
47
48
49
50
51
# File 'lib/rinda/ring.rb', line 44

def write_service
  Thread.new do
	loop do
	  msg = @soc.recv(1024)
	  do_write(msg)
	end
  end
end