michael.moen
7/25/2006 3:49:00 PM
Martin Boese wrote:
> I need to disconnect explicitly from a druby client.
>
> My code is like:
>
> DRb.start_service
> @drb = DRbObject.new(nil, 'druby://localhost:7777')
> @drb.dosomething(param1, param2, param3)
> ..(here I'd like to disconnect!!)...
>
>
> Background:
> I am using druby to communicate between a Rails application running on
> lighttpd/FreeBSD6 and a server-script running on the same box with different
> user rights.
Martin- I'm doing something similar but using Rinda as a middle man.
What I did was create a singleton class to handle the conenction, one
connection stays open and gets reused.
Here's what the client looks like.
#!/usr/bin/env ruby -w
require 'rinda/ring'
require 'uuidtools'
require 'rinda'
require 'search_result'
class SearchClient
private_class_method :new
@@tuple_space = nil
def self.create
if @@tuple_space.nil?
DRb.start_service
@@tuple_space = Rinda::TupleSpaceProxy.new(DRbObject.new(nil,
RINDA_MASTER))
end
@@tuple_space
end
def self.user_search(opts)
retries = 0
begin
opts[:uuid] = UUID.random_create.to_s
puts "outgoing => uuid: #{opts[:uuid]}\nopts: #{opts}"
SearchClient.create.write([:UserSearch, DRb.uri, opts],
RINDA_TIMEOUT)
tuple = SearchClient.create.take([:UserSearchResults,
opts[:uuid], nil], RINDA_TIMEOUT)
SearchResult.new tuple[2]
rescue DRb::DRbConnError
# handle connection closed
rescue
@@tuple_space = nil
raise 'Connection Error'
end
end
end