Drb and mystery ports (and Queue question)

Discussion in 'Ruby' started by Phil Tomson, Aug 30, 2006.

  1. Phil Tomson

    Phil Tomson Guest

    I'm trying to make a simple message queue. Messages can be added to
    the queue by drb clients and the main queue class just sits in a
    different thread and goes through the queue trying to send them out.
    I figured I'd use the Queue class from Thread.

    Here's the server:

    #server
    require 'thread'
    require 'drb'

    class Message
    @@num = 0
    def initialize message = "Time is now: #{Time.now}",&b
    @num = @@num
    @message = message
    if b
    @block = b
    else
    @block = lambda{rand > 0.5}
    end
    @@num +=1
    end

    def try_send
    @block.call
    end

    def to_s
    "#{self.class} number: #{@num} message: #{@message}"
    end
    end

    class MsgQueue
    include DRb::DRbUndumped
    attr_reader :thread
    def initialize sleeptime = rand
    @queue = Queue.new
    @sleeptime = sleeptime
    end

    def method_missing methid, *args, &b
    @queue.send(methid, *args, &b)
    end

    def start
    @thread = Thread.new {
    loop do
    if @queue.size != 0
    msg = @queue.shift
    if msg.try_send
    puts "sending of message: #{msg} succeeded!"
    else
    puts "sending failed...."
    @queue << msg #put it back in queue
    end
    else
    puts "...queue empty..."
    end
    sleep @sleeptime
    end
    }
    end

    def start_works
    @thread = Thread.new {
    loop do
    puts "queue size is now: #{@queue.size}"
    sleep 0.1
    end
    }
    end
    end

    if __FILE__ == $0
    mq = MsgQueue.new 0.5
    DRb.start_service('druby://localhost:9759', mq)
    mq.start

    mq.thread.join
    DRb.thread.join

    end # server


    And here's the client:

    #client:
    require 'msgqueue'
    require 'drb'

    DRb.start_service()
    mq = DRbObject.new(nil, 'druby://localhost:9759')
    20.times do
    puts "send message..."
    mq << Message.new( "message from client...")
    sleep 0.1
    end #client


    When I run the server and then the client I usually end up with something like:
    $> ruby msgqueue.rb
    ...queue empty...
    ...queue empty...
    sending failed....
    sending of message: Message number: 1 message: message from
    client... succeeded!
    sending of message: Message number: 2 message: message from client... succeeded!
    sending failed....
    /usr/local/lib/ruby/1.8/drb/drb.rb:733:in `open':
    druby://phpe-dev-10.hf.intel.com:33135 - #<Errno::ECONNREFUSED:
    Connection refused - connect
    ^^^^^^^
    (2)> (DRb::DRbConnError)
    from msgqueue.rb:73:in `join'
    from msgqueue.rb:73


    So what's with the port number 33135? It's not a port I'm using.

    also, is Queue (the one that comes in with require 'thread' ) good for
    this kind of use? It's advertised as being safe for multiple thread
    access, but in this case there are also multiple processes involved as
    well.

    Phil
    Phil Tomson, Aug 30, 2006
    #1
    1. Advertising

  2. Phil Tomson wrote:
    ...
    > #client:
    > require 'msgqueue'
    > require 'drb'
    >
    > DRb.start_service()

    ^^^^^^^^^^^^^^^^^^^
    This will request a free port from the OS, so that the client can
    respond to requests from the server. (DRb is really pretty symmetrical.)
    A request to the client is generated when, for example, the client calls
    a method with a block, and the server yields to the block.

    ...
    > So what's with the port number 33135? It's not a port I'm using.


    That's just what the OS gave you. If you give the client's start_service
    call an explicit URL that you chose, you'd see that there.

    > also, is Queue (the one that comes in with require 'thread' ) good for
    > this kind of use? It's advertised as being safe for multiple thread
    > access, but in this case there are also multiple processes involved as
    > well.


    Outside processes send their requests into DRb, which starts threads to
    process them. So, as far as the Queue is concerned, there are multiple
    threads accessing it, but it doesn't know about other processes. So it
    should be ok.

    --
    vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
    Joel VanderWerf, Aug 30, 2006
    #2
    1. Advertising

  3. Phil Tomson

    Phil Tomson Guest

    On 8/30/06, Joel VanderWerf <> wrote:
    > Phil Tomson wrote:
    > ...
    > > #client:
    > > require 'msgqueue'
    > > require 'drb'
    > >
    > > DRb.start_service()

    > ^^^^^^^^^^^^^^^^^^^
    > This will request a free port from the OS, so that the client can
    > respond to requests from the server. (DRb is really pretty symmetrical.)
    > A request to the client is generated when, for example, the client calls
    > a method with a block, and the server yields to the block.
    >


    but doesn't this tell it to use port 9759?:
    mq = DRbObject.new(nil, 'druby://localhost:9759')


    Phil
    Phil Tomson, Aug 31, 2006
    #3
  4. Phil Tomson wrote:
    > On 8/30/06, Joel VanderWerf <> wrote:
    >> Phil Tomson wrote:
    >> ...
    >> > #client:
    >> > require 'msgqueue'
    >> > require 'drb'
    >> >
    >> > DRb.start_service()

    >> ^^^^^^^^^^^^^^^^^^^
    >> This will request a free port from the OS, so that the client can
    >> respond to requests from the server. (DRb is really pretty symmetrical.)
    >> A request to the client is generated when, for example, the client calls
    >> a method with a block, and the server yields to the block.
    >>

    >
    > but doesn't this tell it to use port 9759?:
    > mq = DRbObject.new(nil, 'druby://localhost:9759')


    That's the port on the server side, unless I'm confused.

    Your client code didn't have an explicit port, so DRb will request a
    free one from the OS. The client-side port number will be sent to the
    server, so that the server can call back to the client.

    --
    vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
    Joel VanderWerf, Aug 31, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Russell Warren

    Is Queue.Queue.queue.clear() thread-safe?

    Russell Warren, Jun 22, 2006, in forum: Python
    Replies:
    4
    Views:
    678
    Russell Warren
    Jun 27, 2006
  2. mreister
    Replies:
    1
    Views:
    3,150
    mreister
    May 25, 2010
  3. Kris
    Replies:
    0
    Views:
    477
  4. Miles Keaton
    Replies:
    3
    Views:
    179
    Miles Keaton
    Mar 30, 2005
  5. Kirk Haines

    More DRb; SSL & DRB & errors

    Kirk Haines, Jul 1, 2005, in forum: Ruby
    Replies:
    0
    Views:
    123
    Kirk Haines
    Jul 1, 2005
Loading...

Share This Page