Module: EmmyMachine::ClassMethods

Included in:
EmmyMachine, EmmyMachine
Defined in:
lib/emmy_machine/class_methods.rb

Instance Method Summary collapse

Instance Method Details

#async(&b) ⇒ Object



36
37
38
# File 'lib/emmy_machine/class_methods.rb', line 36

def async &b
  pool.checkout &b
end

#await(&b) ⇒ Object



40
41
42
# File 'lib/emmy_machine/class_methods.rb', line 40

def await &b
  Fiber.await &b
end

#bind(url, *a, &b) ⇒ Object

Bind server

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



81
82
83
84
85
86
87
88
89
90
91
92
93
94
# 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) ⇒ Object

Connect to remote server

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



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# 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) ⇒ Object



28
29
30
# File 'lib/emmy_machine/class_methods.rb', line 28

def init_pool(*a)
  Fibre.init_pool(*a)
end

#loopObject



7
8
9
# File 'lib/emmy_machine/class_methods.rb', line 7

def loop
  EventMachine.run
end

#next_tickObject



113
114
115
116
117
# File 'lib/emmy_machine/class_methods.rb', line 113

def next_tick
  fiber = Fiber.current
  EventMachine.next_tick { fiber.resume }
  Fiber.yield
end

#poolObject



32
33
34
# File 'lib/emmy_machine/class_methods.rb', line 32

def pool
  Fibre.pool
end

#reconnect(url, connection, *a, &b) ⇒ Object



108
109
110
111
# 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) ⇒ Object



3
4
5
# File 'lib/emmy_machine/class_methods.rb', line 3

def run(&b)
  EventMachine.reactor_running? ? b.call : EventMachine.run(&b)
end

#run_once(&b) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/emmy_machine/class_methods.rb', line 19

def run_once &b
  run do
    async do
      yield
      stop
    end
  end
end

#running?Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/emmy_machine/class_methods.rb', line 11

def running?
  EventMachine.reactor_running?
end

#sleep(time) ⇒ Object



119
120
121
122
123
# File 'lib/emmy_machine/class_methods.rb', line 119

def sleep(time)
  fiber = Fiber.current
  timeout(time) { fiber.resume }
  Fiber.yield
end

#stopObject



15
16
17
# File 'lib/emmy_machine/class_methods.rb', line 15

def stop
  EventMachine.stop
end

#timeout(interval = 1, &b) ⇒ Object

Run the timer once



52
53
54
55
56
# 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) ⇒ Object

Run the timer periodically



45
46
47
48
49
# 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) ⇒ Object

Watch socket

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



100
101
102
103
104
105
106
# 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