Skip to content

Instantly share code, notes, and snippets.

@turboladen
Created December 17, 2012 05:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save turboladen/4316067 to your computer and use it in GitHub Desktop.
Save turboladen/4316067 to your computer and use it in GitHub Desktop.
EventMachine repeater
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