# Drb and mystery ports (and Queue question)

**URL:** <https://www.ruby-forum.com/t/drb-and-mystery-ports-and-queue-question/66998>\
**Category:** Ruby\
**Created:** [August 30, 2006, 8:35pm UTC](https://www.ruby-forum.com/t/drb-and-mystery-ports-and-queue-question/66998 "2006-08-30T20:35:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![philthompson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed8c4c/32.png) [@philthompson](https://www.ruby-forum.com/u/philthompson)\
**Post date:** [August 30, 2006, 8:35pm UTC](https://www.ruby-forum.com/t/drb-and-mystery-ports-and-queue-question/66998/1 "2006-08-30T20:35:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![philthompson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed8c4c/32.png) [@philthompson](https://www.ruby-forum.com/u/philthompson)\
**Post date:** [August 31, 2006, 12:42am UTC](https://www.ruby-forum.com/t/drb-and-mystery-ports-and-queue-question/66998/2 "2006-08-31T00:42:28Z")

</div>

Phil T. 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.

---

<div class="post-metadata">

**Author:** ![philthompson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed8c4c/32.png) [@philthompson](https://www.ruby-forum.com/u/philthompson)\
**Post date:** [August 31, 2006, 2:08am UTC](https://www.ruby-forum.com/t/drb-and-mystery-ports-and-queue-question/66998/3 "2006-08-31T02:08:38Z")

</div>

On 8/30/06, Joel VanderWerf [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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

---

<div class="post-metadata">

**Author:** ![philthompson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed8c4c/32.png) [@philthompson](https://www.ruby-forum.com/u/philthompson)\
**Post date:** [August 31, 2006, 2:20am UTC](https://www.ruby-forum.com/t/drb-and-mystery-ports-and-queue-question/66998/4 "2006-08-31T02:20:39Z")

</div>

Phil T. wrote:

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