Forum: Ruby Eventmachine questions?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Tim U. (Guest)
on 2009-01-11 06:24
(Received via mailing list)
I am starting to write a pretty simple UDP server using eventmachine.
So
far my code consists of a server which sleeps for a period of time and
then
echoes back what it got sent and a threaded client which sends a
thousand
udp packets consisting of the integer and prints the replies it gets.

The problem is that the server only processes 361 requests. The requests
are
processed in order (which is expected I guess) but clearly the rest of
the
udp messages are being lost to the ether.

I have included my server and client below and if somebody could explain
why
this is happening and how I can fix it I would appreciate it.

BTW is eventmachine still in development? Looks like everything on the
web
site is from 2006 and the mailing list doesn't work.

-------Code--------

-----Client--------
require 'socket'
UDP_RECV_TIMEOUT = 3

server_port = 7779
server_addr = '127.0.0.1'

def send_udp_message(msg, server_addr, server_port, respond = false)
  begin
    sock = UDPSocket.open
    sock.send(msg, 0, server_addr, server_port)
    if respond
      message, status = sock.recvfrom(65536)
      return message
    end
  rescue IOError, SystemCallError
  ensure
    sock.close if sock
  end

end
threads = []
1000.times do |i|
  threads << Thread.new do

    result = send_udp_message i.to_s, server_addr, server_port, true
    puts "Sent #{i}  got #{result}"
  end
end
threads.each {|t| t.join }


----------- Server ---------------
require 'eventmachine'

module UmmpServer
  def post_init
    puts "client connected!"
  end

  def receive_data(data)

      sleep_time = rand(5)
      message = "#{data} -- #{sleep_time}"
      puts "Sleeping for #{sleep_time}"
      sleep sleep_time
      p message
      send_data(message)       #Eventmachine will make this
return-to-sender
by default

  end
end

class Echo < EventMachine::Connection


  def initialize(*args)
    puts "starting server"
    super
    # stuff here...
  end

  def receive_data(data)
    # resp = EventMachine::D
    p data
    send_data data
    close_connection_after_writing
  end

  def unbind
    p ' connection totally closed'
  end


end



EventMachine::run do
  EventMachine.epoll
  EventMachine::open_datagram_socket('0.0.0.0', 7779, UmmpServer)
  EventMachine::add_periodic_timer(1) {
    EventMachine::stop_event_loop unless $running
  }

  EventMachine::add_periodic_timer( 10 ) { $stderr.write "*" }
  puts "Now accepting connections on address , port #7779..."


end

puts "All done for that....."
Chuck R. (Guest)
on 2009-01-12 00:43
(Received via mailing list)
On Jan 10, 2009, at 10:22 PM, Tim U. wrote:

> processed in order (which is expected I guess) but clearly the rest
> of the
> udp messages are being lost to the ether.
>
> I have included my server and client below and if somebody could
> explain why
> this is happening and how I can fix it I would appreciate it.
>
> BTW is eventmachine still in development? Looks like everything on
> the web
> site is from 2006 and the mailing list doesn't work.


Tim,

the mailing list is alive and well. Most of the action these days is
on IRC so check out the #eventmachine forum on irc.freenode.net.

The new home for eventmachine is http://rubyeventmachine.com. There is
a pretty active community but it's kind of like an iceberg... you only
see a little bit of it peek above the water line.

cr
This topic is locked and can not be replied to.