[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Drb and mystery ports (and Queue question

rubyfan

8/30/2006 6:31:00 PM

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

3 Answers

Joel VanderWerf

8/30/2006 10:38:00 PM

0

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

rubyfan

8/31/2006 12:08:00 AM

0

On 8/30/06, Joel VanderWerf <vjoel@path.berkeley.edu> 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

Joel VanderWerf

8/31/2006 12:20:00 AM

0

Phil Tomson wrote:
> On 8/30/06, Joel VanderWerf <vjoel@path.berkeley.edu> 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