Forum: Ruby Logging DRb source IP address

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
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
This topic is locked and can not be replied to.