module EmmyMachine::ClassMethods

Public Instance Methods

async(&b) click to toggle source
# File lib/emmy_machine/class_methods.rb, line 36
def async &b
  pool.checkout &b
end
await(&b) click to toggle source
# File lib/emmy_machine/class_methods.rb, line 40
def await &b
  Fiber.await &b
end
bind(url, *a, &b) click to toggle source

Bind server

EmmyMachine.bind(“tcp://localhost:5555”, ServerConnection) EmmyMachine.bind(“ipc://mypoint”, ServerConnection)

# File lib/emmy_machine/class_methods.rb, line 81
def bind(url, *a, &b)
  url = URI(url.to_s)
  handler = a.empty? ? Connection : a.shift
  b ||= a.shift if a.first.is_a?(Proc) || a.first.is_a?(Method)

  case url.scheme
  when "tcp" then
    EventMachine.start_server(url.host, url.port, handler, *a, &b)
  when "ipc" then
    EventMachine.start_unix_domain_server(url[6..-1], handler, *a, &b)
  else
    raise ArgumentError, "unsupported url scheme"
  end
end
connect(url, *a, &b) click to toggle source

Connect to remote server

EmmyMachine.connect(“tcp://localhost:5555”, handler, *args) EmmyMachine.connect(“ipc://mypoint”)

# File lib/emmy_machine/class_methods.rb, line 62
def connect(url, *a, &b)
  url = URI(url.to_s)
  handler = a.empty? ? Connection : a.shift
  b ||= a.shift if a.first.is_a?(Proc) || a.first.is_a?(Method)

  case url.scheme
  when "tcp" then
    EventMachine.connect(url.host, url.port, handler, *a, &b)
  when "ipc" then
    EventMachine.connect_unix_domain(url[6..-1], handler, *a, &b)
  else
    raise ArgumentError, "unsupported url scheme"
  end
end
init_pool(*a) click to toggle source
# File lib/emmy_machine/class_methods.rb, line 28
def init_pool(*a)
  Fibre.init_pool(*a)
end
loop() click to toggle source
# File lib/emmy_machine/class_methods.rb, line 7
def loop
  EventMachine.run
end
next_tick() click to toggle source
# File lib/emmy_machine/class_methods.rb, line 113
def next_tick
  fiber = Fiber.current
  EventMachine.next_tick { fiber.resume }
  Fiber.yield
end
pool() click to toggle source
# File lib/emmy_machine/class_methods.rb, line 32
def pool
  Fibre.pool
end
reconnect(url, connection, *a, &b) click to toggle source
# File lib/emmy_machine/class_methods.rb, line 108
def reconnect(url, connection, *a, &b)
  url = URI(url.to_s)
  EventMachine.reconnect(url.host, url.port, connection, &b)
end
run(&b) click to toggle source
# File lib/emmy_machine/class_methods.rb, line 3
def run(&b)
  EventMachine.reactor_running? ? b.call : EventMachine.run(&b)
end
run_once() { || ... } click to toggle source
# File lib/emmy_machine/class_methods.rb, line 19
def run_once &b
  run do
    async do
      yield
      stop
    end
  end
end
running?() click to toggle source
# File lib/emmy_machine/class_methods.rb, line 11
def running?
  EventMachine.reactor_running?
end
sleep(time) click to toggle source
# File lib/emmy_machine/class_methods.rb, line 119
def sleep(time)
  fiber = Fiber.current
  timeout(time) { fiber.resume }
  Fiber.yield
end
stop() click to toggle source
# File lib/emmy_machine/class_methods.rb, line 15
def stop
  EventMachine.stop
end
timeout(interval=1, &b) click to toggle source

Run the timer once

# File lib/emmy_machine/class_methods.rb, line 52
def timeout(interval=1, &b)
  EventMachine::Timer.new(interval) do
    async &b
  end
end
timer(interval=1, &b) click to toggle source

Run the timer periodically

# File lib/emmy_machine/class_methods.rb, line 45
def timer(interval=1, &b)
  EventMachine::PeriodicTimer.new(interval) do
    async &b
  end
end
watch(socket, handler, *a, notify_readable: true, notify_writable: false, &b) click to toggle source

Watch socket

EmmyMachine.watch(socket, ClientConnection) - notify read by default EmmyMachine.watch(socket, ClientConnection, notify_readable: false, notify_writable: true) - notify write only

# File lib/emmy_machine/class_methods.rb, line 100
def watch(socket, handler, *a, notify_readable: true, notify_writable: false, &b)
  b ||= a.shift if a.first.is_a?(Proc) || a.first.is_a?(Method)
  EventMachine.attach_io(socket, true, handler, *a, &b).tap do |conn|
    conn.notify_readable = true if notify_readable
    conn.notify_writable = true if notify_writable
  end
end