Module: EmmyMachine::ClassMethods
- Included in:
- EmmyMachine, EmmyMachine
- Defined in:
- lib/emmy_machine/class_methods.rb
Instance Method Summary collapse
-
#bind(url, *a, &b) ⇒ Object
Bind server.
-
#connect(url, *a, &b) ⇒ Object
Connect to remote server.
- #fiber_block(&b) ⇒ Object
- #loop ⇒ Object
- #next_tick ⇒ Object
- #reconnect(url, connection, *a, &b) ⇒ Object
- #run(&b) ⇒ Object
- #run_block(&b) ⇒ Object
- #running? ⇒ Boolean
- #sleep(time) ⇒ Object
- #stop ⇒ Object
-
#timeout(interval = 1, &b) ⇒ Object
One run timer.
-
#timer(interval = 1, &b) ⇒ Object
Periodic timer.
-
#watch(socket, handler, *a, notify_readable: true, notify_writable: false, &b) ⇒ Object
Watch socket.
Instance Method Details
#bind(url, *a, &b) ⇒ Object
Bind server
EmmyMachine.bind(“tcp://localhost:5555”, ServerConnection) EmmyMachine.bind(“ipc://mypoint”, ServerConnection)
69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/emmy_machine/class_methods.rb', line 69 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”)
50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/emmy_machine/class_methods.rb', line 50 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 |
#fiber_block(&b) ⇒ Object
28 29 30 |
# File 'lib/emmy_machine/class_methods.rb', line 28 def fiber_block &b Fibre.pool.checkout &b end |
#loop ⇒ Object
7 8 9 |
# File 'lib/emmy_machine/class_methods.rb', line 7 def loop EventMachine.run end |
#next_tick ⇒ Object
101 102 103 104 105 |
# File 'lib/emmy_machine/class_methods.rb', line 101 def next_tick fiber = Fiber.current EventMachine.next_tick { fiber.resume } Fiber.yield end |
#reconnect(url, connection, *a, &b) ⇒ Object
96 97 98 99 |
# File 'lib/emmy_machine/class_methods.rb', line 96 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_block(&b) ⇒ Object
19 20 21 22 23 24 25 26 |
# File 'lib/emmy_machine/class_methods.rb', line 19 def run_block &b EventMachine.run do fiber_block do yield stop end end end |
#running? ⇒ Boolean
11 12 13 |
# File 'lib/emmy_machine/class_methods.rb', line 11 def running? EventMachine.reactor_running? end |
#sleep(time) ⇒ Object
107 108 109 110 111 |
# File 'lib/emmy_machine/class_methods.rb', line 107 def sleep(time) fiber = Fiber.current timeout(time) { fiber.resume } Fiber.yield end |
#stop ⇒ Object
15 16 17 |
# File 'lib/emmy_machine/class_methods.rb', line 15 def stop EventMachine.stop end |
#timeout(interval = 1, &b) ⇒ Object
One run timer
40 41 42 43 44 |
# File 'lib/emmy_machine/class_methods.rb', line 40 def timeout(interval=1, &b) EventMachine::Timer.new(interval) do fiber_block &b end end |
#timer(interval = 1, &b) ⇒ Object
Periodic timer
33 34 35 36 37 |
# File 'lib/emmy_machine/class_methods.rb', line 33 def timer(interval=1, &b) EventMachine::PeriodicTimer.new(interval) do fiber_block &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
88 89 90 91 92 93 94 |
# File 'lib/emmy_machine/class_methods.rb', line 88 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 |