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

  • Object
    • Rumai::IXP::Agent::FidStream

Encapsulates I/O access over a file handle (fid). NOTE that this class is NOT thread-safe.

Attributes

Instance Attributes

eof [RW] public

Returns the value of attribute eof.

fid [RW] public

Returns the value of attribute fid.

pos [RW] public

Sets the attribute pos.

stat [RW] public

Returns the value of attribute stat.

Constructor Summary

public initialize(aAgent, aPathFid, aMessageSize)
[View source]


180
181
182
183
184
185
186
187
# File 'lib/rumai/ixp/transport.rb', line 180

def initialize aAgent, aPathFid, aMessageSize
  @agent  = aAgent
  @fid    = aPathFid
  @msize  = aMessageSize
  @stat   = @agent.stat_fid @fid
  @closed = false
  rewind
end

Public Visibility

Public Instance Method Summary

#close

Closes this stream.

#closed?

Returns true if this stream is closed.

#read(aPartial = false)

Reads some data from this stream at the current position.

#rewind

Rewinds the stream to the beginning.

#write(aContent) #<<

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

Public Instance Method Details

close

public close

Closes this stream.

[View source]


196
197
198
199
200
201
202
# File 'lib/rumai/ixp/transport.rb', line 196

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

closed?

public closed?

Returns true if this stream is closed.

[View source]


205
206
207
# File 'lib/rumai/ixp/transport.rb', line 205

def closed?
  @closed
end

read

public read(aPartial = false)

Reads some data from this stream at the current position.

aPartial: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.

If this stream corresponds to a directory, then an Array of Stat (one for each file in the directory) will be returned.

[View source]


219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/rumai/ixp/transport.rb', line 219

def read aPartial = 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 aPartial

  # 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

rewind

public rewind

Rewinds the stream to the beginning.

[View source]


190
191
192
193
# File 'lib/rumai/ixp/transport.rb', line 190

def rewind
  @pos = 0
  @eof = false
end

write

public write(aContent)

Also known as: <<

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

[View source]


251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/rumai/ixp/transport.rb', line 251

def write aContent
  raise 'closed streams cannot be written to' if @closed
  raise 'directories cannot be written to' if @stat.directory?

  data = aContent.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