taichi / cool.io

Simple evented I/O for Ruby (but please check out Celluloid::IO instead)
http://coolio.github.com
MIT License
1 stars 1 forks source link

Cool.io

If you are interested in Celluloid based IO framework, please check out Celluloid::IO

Cool.io is an event library for Ruby, built on the libev event library which provides a cross-platform interface to high performance system calls . This includes the epoll system call for Linux, the kqueue system call for BSDs and OS X, and the completion ports interface for Solaris.

Cool.io also binds asynchronous wrappers to Ruby's core socket classes so you can use them in conjunction with Cool.io to build asynchronous event-driven applications.

You can include Cool.io in your programs with:

require 'cool.io'

Anatomy

Cool.io builds on two core classes which bind to the libev API:

Watchers

There are presently four types of watchers:

Using Watchers

Watchers have five important methods:

Asynchronous Wrappers

Several classes which provide asynchronous event-driven wrappers for Ruby's core socket classes are also provided. Among these are:

Example Program

Cool.io provides a Sinatra-like DSL for authoring event-driven programs:

require 'cool.io'

ADDR = '127.0.0.1'
PORT = 4321

cool.io.connection :echo_server_connection do
  on_connect do
    puts "#{remote_addr}:#{remote_port} connected"
  end

  on_close do
    puts "#{remote_addr}:#{remote_port} disconnected"
  end

  on_read do |data|
    write data
  end
end

puts "Echo server listening on #{ADDR}:#{PORT}"
cool.io.server ADDR, PORT, :echo_server_connection
cool.io.run

This creates a new connection class called :echo_server_connection and defines a set of callbacks for when various events occur.

We then create a new server on the given address and port. When this server receives new connections, it will create new instances of the given connection class for each connection.

Finally, we kick everything off with cool.io.run. Calling cool.io.run will block, listening for events on our server.

Using Cool.io subclasses directly

Below is an example of how to write an echo server using a subclass instead of the DSL:

require 'cool.io'
HOST = 'localhost'
PORT = 4321

class EchoServerConnection < Cool.io::TCPSocket
  def on_connect
    puts "#{remote_addr}:#{remote_port} connected"
  end

  def on_close
    puts "#{remote_addr}:#{remote_port} disconnected"
  end

  def on_read(data)
    write data
  end
end

server = Cool.io::TCPServer.new(HOST, PORT, EchoServerConnection)
server.attach(Cool.io::Loop.default)

puts "Echo server listening on #{HOST}:#{PORT}"
Cool.io::Loop.default.run

Here a new observer type (EchoServerConnection) is made by subclassing an existing one and adding new implementations to existing event handlers.

A new event loop is created, and a new Cool.io::TCPServer (whose base class is Cool.io::Watcher) is created and attached to the event loop.

Once this is done, the event loop is started with event_loop.run. This method will block until there are no active watchers for the loop or the loop is stopped explicitly with event_loop.stop.