Class: RubyTorrent::Block
- Inherits:
-
Object
- Object
- RubyTorrent::Block
- 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
-
#begin ⇒ Object
Returns the value of attribute begin.
-
#data ⇒ Object
Returns the value of attribute data.
-
#length ⇒ Object
Returns the value of attribute length.
-
#pindex ⇒ Object
Returns the value of attribute pindex.
-
#requested ⇒ Object
Returns the value of attribute requested.
Instance Method Summary collapse
- #==(o) ⇒ Object
-
#add_chunk(chunk) ⇒ Object
chunk can only be added to blocks in order.
- #complete? ⇒ Boolean
- #each_chunk(blocksize) ⇒ Object
- #have_length ⇒ Object
-
#initialize(pindex, beginn, length) ⇒ Block
constructor
A new instance of Block.
- #mark_time ⇒ Object
- #requested? ⇒ Boolean
- #time_elapsed ⇒ Object
- #to_s ⇒ Object
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
#begin ⇒ Object
Returns the value of attribute begin.
203 204 205 |
# File 'lib/rubytorrent/package.rb', line 203 def begin @begin end |
#data ⇒ Object
Returns the value of attribute data.
203 204 205 |
# File 'lib/rubytorrent/package.rb', line 203 def data @data end |
#length ⇒ Object
Returns the value of attribute length.
203 204 205 |
# File 'lib/rubytorrent/package.rb', line 203 def length @length end |
#pindex ⇒ Object
Returns the value of attribute pindex.
203 204 205 |
# File 'lib/rubytorrent/package.rb', line 203 def pindex @pindex end |
#requested ⇒ Object
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
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_length ⇒ Object
215 |
# File 'lib/rubytorrent/package.rb', line 215 def have_length; @data.length; end |
#mark_time ⇒ Object
217 |
# File 'lib/rubytorrent/package.rb', line 217 def mark_time; @time = Time.now; end |
#requested? ⇒ Boolean
214 |
# File 'lib/rubytorrent/package.rb', line 214 def requested?; @requested; end |
#time_elapsed ⇒ Object
218 |
# File 'lib/rubytorrent/package.rb', line 218 def time_elapsed; Time.now - @time; end |
#to_s ⇒ Object
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 |