module EventMachine

EventMachine emulation for Cool.io:

require 'coolio/eventmachine'

Drawbacks: slightly slower than EM. Benefits: timers are more accurate using libev than using EM TODO: some things like connection timeouts aren't implemented yet DONE: timers and normal socket functions are implemented.

Public Class Methods

add_timer(interval, proc = nil, &block) click to toggle source

ltodo: use Coolio's PeriodicTimer to wrap EM's two similar to it todo: close all connections on 'stop', I believe

# File lib/cool.io/eventmachine.rb, line 43
def add_timer(interval, proc = nil, &block)
  block ||= proc
  t = OneShotEMTimer.new(interval, false) # non repeating
  t.setup(block)

  # fire 'er off ltodo: do we keep track of these timers in memory?
  t.attach(Coolio::Loop.default)
  t
end
cancel_timer(t) click to toggle source
# File lib/cool.io/eventmachine.rb, line 53
def cancel_timer(t)
  # guess there's a case where EM you can say 'cancel' but it's already fired?
  # kind of odd but it happens
 t.detach if t.attached?
end
connect(addr, port, handler = Connection, *args) { |conn| ... } click to toggle source

Make an outgoing connection

# File lib/cool.io/eventmachine.rb, line 62
def connect(addr, port, handler = Connection, *args, &block)
  block = args.pop if Proc === args[-1]

  # make sure we're a 'real' class here
  klass = if (handler and handler.is_a?(Class))
    handler
  else
    Class.new( Connection ) {handler and include handler}
  end

  wrapped_child = CallsBackToEM.connect(addr, port, *args) # ltodo: args? what? they're used? also TODOC TODO FIX
  conn = klass.new(wrapped_child) # ltodo [?] addr, port, *args)
  wrapped_child.attach(Coolio::Loop.default) # necessary
  conn.heres_your_socket(wrapped_child)
  wrapped_child.call_back_to_this(conn) # calls post_init for us
  yield conn if block_given?
end
epoll() click to toggle source

Compatibility noop. Handled automatically by libev

# File lib/cool.io/eventmachine.rb, line 109
def epoll; end
kqueue() click to toggle source

Compatibility noop. Handled automatically by libev

# File lib/cool.io/eventmachine.rb, line 112
def kqueue; end
run() { || ... } click to toggle source

Start the Reactor loop

# File lib/cool.io/eventmachine.rb, line 20
def run
  yield if block_given?
  Coolio::Loop.default.run
end
set_comm_inactivity_timeout(*args) click to toggle source
# File lib/cool.io/eventmachine.rb, line 59
def set_comm_inactivity_timeout(*args); end
set_descriptor_table_size(nfds) click to toggle source

Set the maximum number of descriptors available to this process

# File lib/cool.io/eventmachine.rb, line 104
def set_descriptor_table_size(nfds)
  Coolio::Utils.maxfds = nfds
end
start_server(addr, port, handler = Connection, *args, &block) click to toggle source

Start a TCP server on the given address and port

# File lib/cool.io/eventmachine.rb, line 81
def start_server(addr, port, handler = Connection, *args, &block)
  # make sure we're a 'real' class here
  klass = if (handler and handler.is_a?(Class))
    handler
  else
    Class.new( Connection ) {handler and include handler}
  end

  server = Coolio::TCPServer.new(addr, port, CallsBackToEM, *args) do |wrapped_child|
    conn = klass.new(wrapped_child)
    conn.heres_your_socket(wrapped_child) # ideally NOT have this :)
    wrapped_child.call_back_to_this(conn)
    block.call(conn) if block
  end

  server.attach(Coolio::Loop.default)
end
stop_event_loop() click to toggle source

Stop the Reactor loop

# File lib/cool.io/eventmachine.rb, line 26
def stop_event_loop
  Coolio::Loop.default.stop
end
stop_server(server) click to toggle source
# File lib/cool.io/eventmachine.rb, line 99
def stop_server(server)
  server.close
end