Class: RubyTorrent::Block

Inherits:
Object show all
Defined in:
lib/rubytorrent/package.rb

Overview

Blocks are very simple chunks of data which exist solely in memory. they are the basic currency of the bittorrent protocol. a Block can be divided into “chunks” (no intelligence there; it’s solely for the purposes of buffered reading/writing) and one or more Blocks comprises a Piece.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pindex, beginn, length) ⇒ Block

Returns a new instance of Block.



205
206
207
208
209
210
211
212
# File 'lib/rubytorrent/package.rb', line 205

def initialize(pindex, beginn, length)
  @pindex = pindex
  @begin = beginn
  @length = length
  @data = nil
  @requested = false
  @time = nil
end

Instance Attribute Details

#beginObject

Returns the value of attribute begin.



203
204
205
# File 'lib/rubytorrent/package.rb', line 203

def begin
  @begin
end

#dataObject

Returns the value of attribute data.



203
204
205
# File 'lib/rubytorrent/package.rb', line 203

def data
  @data
end

#lengthObject

Returns the value of attribute length.



203
204
205
# File 'lib/rubytorrent/package.rb', line 203

def length
  @length
end

#pindexObject

Returns the value of attribute pindex.



203
204
205
# File 'lib/rubytorrent/package.rb', line 203

def pindex
  @pindex
end

#requestedObject

Returns the value of attribute requested.



203
204
205
# File 'lib/rubytorrent/package.rb', line 203

def requested
  @requested
end

Instance Method Details

#==(o) ⇒ Object



241
242
243
244
# File 'lib/rubytorrent/package.rb', line 241

def ==(o)
  o.is_a?(Block) && (o.pindex == self.pindex) && (o.begin == self.begin) &&
    (o.length == self.length)
end

#add_chunk(chunk) ⇒ Object

chunk can only be added to blocks in order



225
226
227
228
229
230
# File 'lib/rubytorrent/package.rb', line 225

def add_chunk(chunk)
  @data = "" if @data.nil?
  raise "adding chunk would result in too much data (#{@data.length} + #{chunk.length} > #@length)" if (@data.length + chunk.length) > @length
  @data += chunk
  self
end

#complete?Boolean

Returns:

  • (Boolean)


216
# File 'lib/rubytorrent/package.rb', line 216

def complete?; @data && (@data.length == @length); end

#each_chunk(blocksize) ⇒ Object



232
233
234
235
236
237
238
239
# File 'lib/rubytorrent/package.rb', line 232

def each_chunk(blocksize)
  raise "each_chunk called on incomplete block" unless complete?
  start = 0
  while(start < @length)
    yield data[start, [blocksize, @length - start].min]
    start += blocksize
  end
end

#have_lengthObject



215
# File 'lib/rubytorrent/package.rb', line 215

def have_length; @data.length; end

#mark_timeObject



217
# File 'lib/rubytorrent/package.rb', line 217

def mark_time; @time = Time.now; end

#requested?Boolean

Returns:

  • (Boolean)


214
# File 'lib/rubytorrent/package.rb', line 214

def requested?; @requested; end

#time_elapsedObject



218
# File 'lib/rubytorrent/package.rb', line 218

def time_elapsed; Time.now - @time; end

#to_sObject



220
221
222
# File 'lib/rubytorrent/package.rb', line 220

def to_s
  "<block: p[#{@pindex}], #@begin + #@length #{(data.nil? || (data.length == 0) ? 'emp' : (complete? ? 'cmp' : 'inc'))}>"
end