Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Ruby (http://www.velocityreviews.com/forums/f66-ruby.html)
-   -   Event Machine - callbacks from different connections (http://www.velocityreviews.com/forums/t848564-event-machine-callbacks-from-different-connections.html)

Tim Conner 03-03-2008 11:11 AM

Event Machine - callbacks from different connections
 
In the RDoc for EventMachine, the following statement is made:
"You can also call send_data to write to a connection other than the one
whose callback you are calling send_data from. This is done by recording
the value of the connection in any callback function (the value self),
in any variable visible to other callback invocations on the same or
different connection objects. (Need an example to make that clear.)"

If I have the following code, how can I make it so that the 'send_data'
method for ServerOne is called when data is received for ServerTwo?
i.e. I want a packet of data to be sent out on port 1700 when a packet
is received on port 1800 (but i need the listener on port 1700 to stay
listening)

require 'rubygems'
require 'eventmachine'

module ServerOne
def receive_data data
sender_info = get_peername[2,6].unpack "nC4"
port = sender_info[0]
ip = sender_info[1..4].join(".").to_s
puts "IP = #{ip}"
puts "Port = #{port}"
end
end

module ServerTwo
def receive_data data
sender_info = get_peername[2,6].unpack "nC4"
port = sender_info[0]
ip = sender_info[1..4].join(".").to_s
puts "IP = #{ip}"
puts "Port = #{port}"
end
end

EventMachine::run {
EventMachine::open_datagram_socket "192.168.0.2", 1700, ServerOne
EventMachine::open_datagram_socket "192.168.0.2", 1800, ServerTwo
}


Many thanks
--
Posted via http://www.ruby-forum.com/.


Jason Roelofs 03-03-2008 12:32 PM

Re: Event Machine - callbacks from different connections
 
The idea is that there's a collection of the connection objects held
outside of the servers and used where necessary. In your case,
something like this would work:

$one_conns = []
$two_conns = []

module ServerOne
def receive_data data
sender_info = get_peername[2,6].unpack "nC4"
port = sender_info[0]
ip = sender_info[1..4].join(".").to_s
puts "IP = #{ip}"
puts "Port = #{port}"

# find your $two_conn and
$two_conns[i].send_data ...
end
end

module ServerTwo
def receive_data data
sender_info = get_peername[2,6].unpack "nC4"
port = sender_info[0]
ip = sender_info[1..4].join(".").to_s
puts "IP = #{ip}"
puts "Port = #{port}"

#Find your one conn and
$one_conns[i].send_data ...
end
end

EventMachine::run {
EventMachine::open_datagram_socket "192.168.0.2", 1700, ServerOne do |conn|
$one_conns << conn
end

EventMachine::open_datagram_socket "192.168.0.2", 1800, ServerTwo do |conn|
$two_conns << conn
end
}


Jason

On Mon, Mar 3, 2008 at 6:11 AM, Tim Conner <crofty_james@hotmail.com> wrote:
> In the RDoc for EventMachine, the following statement is made:
> "You can also call send_data to write to a connection other than the one
> whose callback you are calling send_data from. This is done by recording
> the value of the connection in any callback function (the value self),
> in any variable visible to other callback invocations on the same or
> different connection objects. (Need an example to make that clear.)"
>
> If I have the following code, how can I make it so that the 'send_data'
> method for ServerOne is called when data is received for ServerTwo?
> i.e. I want a packet of data to be sent out on port 1700 when a packet
> is received on port 1800 (but i need the listener on port 1700 to stay
> listening)
>
> require 'rubygems'
> require 'eventmachine'
>
> module ServerOne
> def receive_data data
> sender_info = get_peername[2,6].unpack "nC4"
> port = sender_info[0]
> ip = sender_info[1..4].join(".").to_s
> puts "IP = #{ip}"
> puts "Port = #{port}"
> end
> end
>
> module ServerTwo
> def receive_data data
> sender_info = get_peername[2,6].unpack "nC4"
> port = sender_info[0]
> ip = sender_info[1..4].join(".").to_s
> puts "IP = #{ip}"
> puts "Port = #{port}"
> end
> end
>
> EventMachine::run {
> EventMachine::open_datagram_socket "192.168.0.2", 1700, ServerOne
> EventMachine::open_datagram_socket "192.168.0.2", 1800, ServerTwo
> }
>
>
> Many thanks
> --
> Posted via http://www.ruby-forum.com/.
>
>




All times are GMT. The time now is 01:17 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57