T
Tim Conner
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
"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