Class: LightIO::Core::Backend::NIO

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/lightio/core/backend/nio.rb

Overview

LightIO use NIO as default event-driving backend

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeNIO

Returns a new instance of NIO.



58
59
60
61
62
63
64
65
# File 'lib/lightio/core/backend/nio.rb', line 58

def initialize
  # @selector = NIO::Selector.new
  @current_loop_time = nil
  @running = false
  @timers = Timers.new
  @callbacks = []
  @selector = ::NIO::Selector.new(env_backend)
end

Instance Attribute Details

#runningObject (readonly)

Returns the value of attribute running.



56
57
58
# File 'lib/lightio/core/backend/nio.rb', line 56

def running
  @running
end

Instance Method Details

#add_callback(&blk) ⇒ Object



79
80
81
# File 'lib/lightio/core/backend/nio.rb', line 79

def add_callback(&blk)
  @callbacks << blk
end

#add_io_wait(io, interests, &blk) ⇒ Object



91
92
93
94
95
# File 'lib/lightio/core/backend/nio.rb', line 91

def add_io_wait(io, interests, &blk)
  monitor = @selector.register(io, interests)
  monitor.value = blk
  monitor
end

#add_timer(timer) ⇒ Object



83
84
85
# File 'lib/lightio/core/backend/nio.rb', line 83

def add_timer(timer)
  timer.uuid = @timers.add_timer(timer)
end

#cancel_io_wait(io) ⇒ Object



97
98
99
# File 'lib/lightio/core/backend/nio.rb', line 97

def cancel_io_wait(io)
  @selector.deregister(io)
end

#cancel_timer(timer) ⇒ Object



87
88
89
# File 'lib/lightio/core/backend/nio.rb', line 87

def cancel_timer(timer)
  @timers.cancel_timer(timer)
end

#env_backendObject



109
110
111
112
# File 'lib/lightio/core/backend/nio.rb', line 109

def env_backend
  key = 'LIGHTIO_BACKEND'.freeze
  ENV.has_key?(key) ? ENV[key].to_sym : nil
end

#runObject

Raises:



67
68
69
70
71
72
73
74
75
76
# File 'lib/lightio/core/backend/nio.rb', line 67

def run
  raise Error, "already running" if @running
  @running = true
  loop do
    @current_loop_time = Time.now
    run_timers
    run_callbacks
    handle_selectables
  end
end

#stopObject Also known as: close



101
102
103
104
105
# File 'lib/lightio/core/backend/nio.rb', line 101

def stop
  return if closed?
  @running = false
  @selector.close
end