Auto-Reconnect for Mogwai Clients

Eric Hodel | Fri, 26 Aug 2005 22:27:00 GMT

I want Mogwai clients to be able to reconnect to the server automatically of the server goes down, so I added a thread that periodically polls the server to see if we’re still registered there.

Here’s what it looks like:

  def run
    @thread = Thread.start do
      loop do
        begin
          server = Rinda::RingFinger.new BROADCAST_LIST, Mogwai::RING_PORT
          @ts = server.lookup_ring_any
          register unless registered?
        rescue RuntimeError # RingNotFound error (doesn't have its own class)
        end
        sleep 60
      end
    end 
  end

  def register
    @ts.write @registration, @renewer
  end

  def registered?
    reg = @ts.read [:name, :MogwaiClient, nil, @hostname], 1
    return reg[2].__drbref == self.object_id
  rescue Rinda::RequestExpiredError
    puts "request expired" 
    return false
  end

The second parameter to @ts.read is a timeout value. A read on a TupleSpace will block until there is a matching Tuple to read. Since I’m looking for myself and haven’t yet registered myself, I’ll never match.

I could construct a DRbObject for the template Tuple, but I didn’t think about it until just now, and it would look equally clumsy.

The @renewer is a way of letting the server ensure that the client is still alive.

Comments are disabled