Forum: Ruby Logging DRb source IP address

822363de5019f21e2ab34a862f557499?d=identicon&s=25 Anthony Wright (anthonywright)
on 2011-10-10 12:33
(Received via mailing list)
I use Drb to talk from a number of clients to a central server. I have
logging on the server that records the method called and the arguments
that it was called with. I'd like to include the IP address that the Drb
request came from, but I can't find any way to get that information. Is
the information available? If so how do I get it? If not, does anybody
know why not?

thanks,

Anthony Wright.
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2011-10-11 22:22
Anthony Wright wrote in post #1025900:
> I use Drb to talk from a number of clients to a central server. I have
> logging on the server that records the method called and the arguments
> that it was called with. I'd like to include the IP address that the Drb
> request came from, but I can't find any way to get that information. Is
> the information available? If so how do I get it? If not, does anybody
> know why not?

The nice thing about DRb is it's all pure ruby. Digging about a bit:

    def main_loop
      Thread.start(@protocol.accept) do |client|
        @grp.add Thread.current
        Thread.current['DRb'] = { 'client' => client ,
                                  'server' => self }

So in your method handler, try looking at
Thread.current['DRb']['client']. I think that will be a DRbTCPSocket
object with a peeraddr method.

Simple demo (client and server in the same code):

require "drb"
class Foo
  def doit
    return "You are #{Thread.current['DRb']['client'].peeraddr.inspect}"
  end
end

DRb.start_service("druby://0.0.0.0:9999", Foo.new)

o = DRbObject.new_with_uri("druby://127.0.0.1:9999")
puts o.doit
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.