Class: Green::Hub::Nio4r

Inherits:
Green::Hub show all
Defined in:
lib/green/hub/nio4r.rb

Defined Under Namespace

Classes: SocketWaiter, Timer

Constant Summary collapse

MIN_TIMEOUT =
0.0001
MAX_TIMEOUT =
0.01

Instance Attribute Summary collapse

Attributes inherited from Green::Hub

#g

Instance Method Summary collapse

Methods inherited from Green::Hub

#sleep, #start_hub, #switch, #wait

Constructor Details

#initialize(*args) ⇒ Nio4r

Returns a new instance of Nio4r.



55
56
57
58
59
60
61
62
63
# File 'lib/green/hub/nio4r.rb', line 55

def initialize(*args)
  require 'nio'
  require 'algorithms'

  @callbacks = []
  @timers = Containers::MinHeap.new
  @cancel_timers = {}
  super
end

Instance Attribute Details

#callbacksObject (readonly)

Returns the value of attribute callbacks.



54
55
56
# File 'lib/green/hub/nio4r.rb', line 54

def callbacks
  @callbacks
end

#cancel_timersObject (readonly)

Returns the value of attribute cancel_timers.



54
55
56
# File 'lib/green/hub/nio4r.rb', line 54

def cancel_timers
  @cancel_timers
end

#timersObject (readonly)

Returns the value of attribute timers.



54
55
56
# File 'lib/green/hub/nio4r.rb', line 54

def timers
  @timers
end

Instance Method Details

#callback(cb = nil, &blk) ⇒ Object



134
135
136
# File 'lib/green/hub/nio4r.rb', line 134

def callback(cb=nil, &blk)
  @callbacks << (cb || blk)
end

#cancel_timer(timer) ⇒ Object



126
127
128
# File 'lib/green/hub/nio4r.rb', line 126

def cancel_timer(timer)
  @cancel_timers[timer] = true
end

#reactor_running?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/green/hub/nio4r.rb', line 65

def reactor_running?
  @reactor_running
end

#runObject



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/green/hub/nio4r.rb', line 69

def run
  @reactor_running = true
  @selector = NIO::Selector.new
  while @reactor_running
    run_callbacks
    run_timers
    @selector.select(time_till_first_event) do |m|
      m.value.call
    end
  end
end

#run_callbacksObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/green/hub/nio4r.rb', line 97

def run_callbacks
  jobs, @callbacks = @callbacks, []
  begin
    i = 0
    while i < jobs.size
      job = jobs[i]
      jobs[i] = nil
      job.call
      i += 1
    end
  ensure
    @callbacks[0...0] = jobs[(i + 1)..-1]  if i < jobs.size
  end
end

#run_timersObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/green/hub/nio4r.rb', line 81

def run_timers
  now = Time.now
  while (t = @timers.next)
    if t.fire_at <= now
      @timers.pop
      if @cancel_timers[t]
        @cancel_timers.delete t
      else
        t.run
      end
    else
      break
    end
  end
end

#selectorObject



122
123
124
# File 'lib/green/hub/nio4r.rb', line 122

def selector
  @selector
end

#socket_waiter(socket) ⇒ Object



138
139
140
# File 'lib/green/hub/nio4r.rb', line 138

def socket_waiter(socket)
  SocketWaiter.new self, socket
end

#stopObject



142
143
144
# File 'lib/green/hub/nio4r.rb', line 142

def stop
  @reactor_running = false
end

#time_till_first_eventObject



112
113
114
115
116
117
118
119
120
# File 'lib/green/hub/nio4r.rb', line 112

def time_till_first_event
  if @callbacks.size > 0
    MIN_TIMEOUT
  elsif @timers.size > 0
    @timers.next.fire_at - Time.now + MIN_TIMEOUT
  else
    MAX_TIMEOUT
  end
end

#timer(n, &blk) ⇒ Object



130
131
132
# File 'lib/green/hub/nio4r.rb', line 130

def timer(n, &blk)
  @timers << Timer.new(self, Time.now + n, &blk)
end