Class: Pwrake::NBIO::Writer

Inherits:
Object
  • Object
show all
Defined in:
lib/pwrake/nbio.rb

Overview


Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(selector, io) ⇒ Writer

Returns a new instance of Writer.



150
151
152
153
154
155
# File 'lib/pwrake/nbio.rb', line 150

def initialize(selector, io)
  @selector = selector
  @io = io
  @waiter = []
  @pool = []
end

Instance Attribute Details

#hostObject

Returns the value of attribute host.



157
158
159
# File 'lib/pwrake/nbio.rb', line 157

def host
  @host
end

#ioObject (readonly)

Returns the value of attribute io.



156
157
158
# File 'lib/pwrake/nbio.rb', line 156

def io
  @io
end

Instance Method Details

#callObject

call from Selector



160
161
162
163
164
165
166
# File 'lib/pwrake/nbio.rb', line 160

def call
  w = @waiter
  @waiter = []
  w.each{|f| f.resume}
ensure
  @selector.delete_writer(self) if @waiter.empty?
end

#error(e) ⇒ Object



182
183
184
185
# File 'lib/pwrake/nbio.rb', line 182

def error(e)
  @closed = true
  raise e
end

#flushObject



200
201
202
203
204
205
# File 'lib/pwrake/nbio.rb', line 200

def flush
  until @pool.empty?
    len = _write(@pool[0])
    pop(len)
  end
end

#haltObject



175
176
177
178
179
180
# File 'lib/pwrake/nbio.rb', line 175

def halt
  @halting = true
  call
ensure
  @halting = false
end

#put_line(line, ch = nil) ⇒ Object

call from Fiber context



169
170
171
172
173
# File 'lib/pwrake/nbio.rb', line 169

def put_line(line, ch=nil)
  line = line.chomp
  line = "#{ch}:#{line}" if ch
  write(line+"\n")
end

#puts(s) ⇒ Object



196
197
198
# File 'lib/pwrake/nbio.rb', line 196

def puts(s)
  write(s+"\n")
end

#select_ioObject



207
208
209
210
211
# File 'lib/pwrake/nbio.rb', line 207

def select_io
  @selector.add_writer(self) if @waiter.empty?
  @waiter.push(Fiber.current)
  Fiber.yield
end

#write(buf, buffered = false) ⇒ Object Also known as: print

from Bartender



189
190
191
192
# File 'lib/pwrake/nbio.rb', line 189

def write(buf, buffered=false)
  push(buf)
  flush unless buffered
end