Created
December 17, 2012 05:50
-
-
Save turboladen/4316067 to your computer and use it in GitHub Desktop.
EventMachine repeater
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Receiver < EventMachine::Connection | |
attr_reader :data_channel | |
def initialize | |
@data_channel = EM::Channel.new | |
end | |
def receive_data(data) | |
@data_channel << data | |
end | |
end | |
class Sender < EM::Connection | |
def initialize(data_channel, dest_ip, dest_port) | |
@data_channel = data_channel | |
@dest_ip = dest_ip | |
@dest_port = dest_port | |
end | |
def post_init | |
@data_channel.subscribe do |data| | |
send_datagram(data, @dest_ip, @dest_port) | |
end | |
end | |
end | |
EM.run do | |
# Give the IP and port to listen to UDP traffic on | |
receiver = EM.open_datagram_socket('127.0.0.1', 1234, Receiver) | |
20.times do |i| | |
EM.defer { | |
# Give the starting port to send on and the IP to send to | |
EM.open_datagram_socket('0.0.0.0', 1235 + i, Sender, receiver.data_channel, | |
'192.168.10.5', 1235 + i) | |
} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment