Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Event Machine - callbacks from different connections

Reply
Thread Tools

Event Machine - callbacks from different connections

 
 
Tim Conner
Guest
Posts: n/a
 
      03-03-2008
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:pen_datagram_socket "192.168.0.2", 1700, ServerOne
EventMachine:pen_datagram_socket "192.168.0.2", 1800, ServerTwo
}


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

 
Reply With Quote
 
 
 
 
Jason Roelofs
Guest
Posts: n/a
 
      03-03-2008
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:pen_datagram_socket "192.168.0.2", 1700, ServerOne do |conn|
$one_conns << conn
end

EventMachine:pen_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 <(E-Mail Removed)> 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:pen_datagram_socket "192.168.0.2", 1700, ServerOne
> EventMachine:pen_datagram_socket "192.168.0.2", 1800, ServerTwo
> }
>
>
> Many thanks
> --
> Posted via http://www.ruby-forum.com/.
>
>


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Win32::OLE different connections methods produce different results HmJ Perl Misc 0 03-28-2008 02:46 PM
Directory structures different on development machine vs. install machine. UJ ASP .Net 2 10-24-2006 10:35 AM
Callbacks accross different threads Alfonso Morra C++ 10 09-20-2005 05:34 AM
COM event callbacks ivan_r_moore Python 0 11-05-2003 10:12 AM



Advertisments