Class: Rumai::IXP::Agent::FidStream

Inherits:
Object
  • Object
show all
Defined in:
lib/rumai/ixp/transport.rb

Overview

Note:

this class is NOT thread safe!

Encapsulates I/O access over a file handle (fid).

Instance Attribute Summary

Instance Method Summary

Constructor Details

- (FidStream) initialize(agent, path_fid, message_size)

A new instance of FidStream



247
248
249
250
251
252
253
254
# File 'lib/rumai/ixp/transport.rb', line 247

def initialize agent, path_fid, message_size
  @agent  = agent
  @fid    = path_fid
  @msize  = message_size
  @stat   = @agent.stat_fid(@fid)
  @closed = false
  rewind
end

Instance Attribute Details

- (Object) eof (readonly) Also known as: eof?

Returns the value of attribute eof



241
242
243
# File 'lib/rumai/ixp/transport.rb', line 241

def eof
  @eof
end

- (Object) fid (readonly)

Returns the value of attribute fid



239
240
241
# File 'lib/rumai/ixp/transport.rb', line 239

def fid
  @fid
end

- (Object) pos Also known as: tell

Returns the value of attribute pos



244
245
246
# File 'lib/rumai/ixp/transport.rb', line 244

def pos
  @pos
end

- (Object) stat (readonly)

Returns the value of attribute stat



239
240
241
# File 'lib/rumai/ixp/transport.rb', line 239

def stat
  @stat
end

Instance Method Details

- (Object) close

Closes this stream.



267
268
269
270
271
272
273
# File 'lib/rumai/ixp/transport.rb', line 267

def close
  unless @closed
    @agent.clunk @fid
    @closed = true
    @eof = true
  end
end

- (Boolean) closed?

Returns true if this stream is closed.

Returns:

  • (Boolean)


278
279
280
# File 'lib/rumai/ixp/transport.rb', line 278

def closed?
  @closed
end

- (Object) read(partial = false)

Reads some data from this stream at the current position. If this stream corresponds to a directory, then an Array of Stat (one for each file in the directory) will be returned.

Parameters:

  • (Boolean) partial (defaults to: false)

    When false, the entire content of this stream is read and returned.

    When true, the maximum amount of content that can fit inside a single 9P2000 message is read and returned.



295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# File 'lib/rumai/ixp/transport.rb', line 295

def read partial = false
  raise 'cannot read from a closed stream' if @closed

  content = ''
  begin
    req = Tread.new(
      :fid    => @fid,
      :offset => @pos,
      :count  => @msize
    )
    rsp = @agent.talk(req)

    content << rsp.data
    count = rsp.count
    @pos += count
  end until @eof = count.zero? or partial

  # the content of a directory is a sequence
  # of Stat for all files in that directory
  if @stat.directory?
    buffer = StringIO.new(content)
    content = []

    until buffer.eof?
      content << Stat.from_9p(buffer)
    end
  end

  content
end

- (Object) rewind

Rewinds the stream to the beginning.



259
260
261
262
# File 'lib/rumai/ixp/transport.rb', line 259

def rewind
  @pos = 0
  @eof = false
end

- (Object) write(content) Also known as: <<

Writes the given content at the current position in this stream.



329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# File 'lib/rumai/ixp/transport.rb', line 329

def write content
  raise 'cannot write to a closed stream' if @closed
  raise 'cannot write to a directory' if @stat.directory?

  data = content.to_s
  limit = data.length + @pos

  while @pos < limit
    chunk = data[@pos, @msize]

    req = Twrite.new(
      :fid    => @fid,
      :offset => @pos,
      :count  => chunk.length,
      :data   => chunk
    )
    rsp = @agent.talk(req)

    @pos += rsp.count
  end
end