Drb and mystery ports (and Queue question)

P

Phil Tomson

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
 
J

Joel VanderWerf

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.
 
P

Phil Tomson

Phil Tomson wrote:
...
^^^^^^^^^^^^^^^^^^^
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
 
J

Joel VanderWerf

Phil said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top