Module: Alo7::Net

Defined in:
lib/alo7/net.rb,
lib/alo7/net/defer.rb,
lib/alo7/net/error.rb,
lib/alo7/net/client.rb,
lib/alo7/net/server.rb,
lib/alo7/net/version.rb,
lib/alo7/net/connection.rb

Defined Under Namespace

Classes: Client, Connection, ConnectionLost, Defer, Error, Server

Constant Summary collapse

VERSION =
'0.1.0'

Class Method Summary collapse

Class Method Details

.await(defer) ⇒ Object

Wait the defer succeed and then return anything the defer returns. Or raise the exception from the defer.

Parameters:

  • defer (Defer)

    defer to wait

Returns:

  • anything the defer returns

Raises:

  • (Exception)

    the exception returns from defer


160
161
162
163
164
# File 'lib/alo7/net.rb', line 160

def self.await(defer)
  result = EM::Synchrony.sync defer
  raise result if result.is_a? Exception
  result
end

.connect(handler, host, port, *args) ⇒ handler

Note:

This requires event loop to be running. (see run).

Initiate a TCP connection to a remote server and set up event handling for the connection.

Parameters:

  • handler (ClassObject)

    a module or class that has an innerclass named `Impl`

  • host (String)

    host to connect to

  • port (Integer)

    port to connect to

Returns:

  • (handler)

    the initiated handler instance

Raises:

  • (ArgumentError)

    if the handler doesn't have a innerclass named Impl

See Also:


130
131
132
133
134
# File 'lib/alo7/net.rb', line 130

def self.connect(handler, host, port, *args)
  check_handler! handler
  connection = EM.connect host, port, handler::Impl, handler, *args
  connection.handler
end

.fiber_block(*args) {|*args| ... } ⇒ Object

Execute the block in a fiber.

Parameters:

  • args

    passed as arguments to the block that are executed

Yield Parameters:

  • *args

    arguments passed from outside

Yield Returns:

  • (Object)

    anything

Returns:

  • (Object)

    anything returned from the block


172
173
174
# File 'lib/alo7/net.rb', line 172

def self.fiber_block(*args)
  Fiber.new { yield(*args) }.resume
end

.listen(handler, host, port, *args) ⇒ Integer .listen(handler, port, *args) ⇒ Integer

Initiate a TCP server on the specified IP address and port.

Overloads:

  • .listen(handler, host, port, *args) ⇒ Integer

    Parameters:

    • handler (ClassObject)

      a module or class that has an innerclass named `Impl`

    • host (String)

      host to listen on

    • port (Integer)

      port to listen on

    • *args

      passed to the initializer of the handler

  • .listen(handler, port, *args) ⇒ Integer

    Parameters:

    • handler (ClassObject)

      a module or class that has an innerclass named `Impl`

    • port (Integer)

      port to listen on

    • *args

      passed to the initializer of the handler

Yields:

  • (handler)

    initiated when a connection is made

Returns:

  • (Integer)

    the internal signature

Raises:

  • (ArgumentError)

    if the handler doesn't have a innerclass named Impl

See Also:


103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/alo7/net.rb', line 103

def self.listen(handler, host_or_port, port = nil, *args)
  check_handler! handler
  if port
    host = host_or_port
  else
    host = 'localhost'
    port = host_or_port
  end
  EM.start_server host, port, handler::Impl, handler, *args do |server|
    yield server.handler if block_given?
  end
end

.reconnect(handler, host, port) ⇒ handler

Connect to a given host/port and re-use the provided handler instance.

Parameters:

  • handler (#impl)

    a instance that has a property named `impl`

  • host (String)

    host to reconnect to

  • port (Integer)

    port to reconnect to

Returns:

  • (handler)

    the previous handler instance

Raises:

  • (ArgumentError)

    if the handler doesn't implement a method named impl

See Also:


147
148
149
150
151
# File 'lib/alo7/net.rb', line 147

def self.reconnect(handler, host, port)
  raise ArgumentError, 'must provide a impl property' unless handler.respond_to? :impl
  connection = EM.reconnect host, port, handler.impl
  connection.handler
end

.run(block) ⇒ void .run(&block) ⇒ void

Note:

This method blocks the calling thread.

Note:

This method only returns if the event loop stopped.

This method returns an undefined value.

Run an event loop in the fiber way.

Examples:

Starting an event loop in the current thread to run the “Hello, world”-like Echo server example

require 'alo7-net'

class EchoServer < Alo7::Net::Server
  def receive_data(data)
    send_data data
  end
end

Alo7::Net.run do
  EchoServer.listen 3000
end

Parameters:

  • block (MethodObject)

    the block to run

See Also:


40
41
42
# File 'lib/alo7/net.rb', line 40

def self.run(blk = nil, &block)
  EM.synchrony(blk || block)
end

.run_once(&block) ⇒ void

This method returns an undefined value.

Run an event loop and then terminate the loop as soon as the block completes.

Parameters:

  • block (MethodObject)

    the block to run once


48
49
50
51
52
53
# File 'lib/alo7/net.rb', line 48

def self.run_once(&block)
  run do
    block.call
    stop
  end
end

.stopvoid

This method returns an undefined value.

Stop the running event loop.

Examples:

Stopping a running event loop.

require 'alo7-net'

class OnceClient < Alo7::Net::Client
  def post_init
    puts 'sending a dump HTTP request'
    send_data "GET / HTTP/1.1\r\nHost: www.google.com\r\n\r\n"
  end

  def receive_data(data)
    puts "data.length: #{data.length}"
    puts 'stopping the event loop now'
    Net.stop
  end
end

Alo7::Net.run do
  OnceClient.connect 'www.google.com', 80
end

See Also:


82
83
84
# File 'lib/alo7/net.rb', line 82

def self.stop
  EM.stop_event_loop
end