Class: PDTP::Server::ChunkInfo

Inherits:
Object
  • Object
show all
Defined in:
lib/pdtp/server/chunk_info.rb

Overview

Stores information about the chunks requested or provided by a client FIXME this entire class is in need of a rewrite

Defined Under Namespace

Classes: FileStats

Instance Method Summary collapse

Constructor Details

#initializeChunkInfo

Returns a new instance of ChunkInfo.



30
31
32
# File 'lib/pdtp/server/chunk_info.rb', line 30

def initialize
  @files = {}
end

Instance Method Details

#get_file_statsObject

Returns an array of FileStats objects for debug output



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/pdtp/server/chunk_info.rb', line 62

def get_file_stats
  @files.map do |name, chunkmap|
    fs = FileStats.new
    fs.file_chunks = chunkmap.last.end + 1
    fs.url = name
    
    chunkmap.each do |range, state|
      length = range.end - range.begin + 1
      
      case state
      when :requested    then fs.chunks_requested += length
      when :provided     then fs.chunks_provided += length
      when :transferring then fs.chunks_transferring += length
      end
    end
    
    fs
  end
end

#high_priority_chunkObject

returns a high priority requested chunk



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/pdtp/server/chunk_info.rb', line 49

def high_priority_chunk
  #right now return any chunk
  @files.each do |name, chunkmap|
    range, _ = chunkmap.find { |_, state| state == :requested }
    next unless range
    
    return name, range.begin
  end  

  nil
end

#provided?(filename, chunk) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/pdtp/server/chunk_info.rb', line 40

def provided?(filename, chunk)
  get(filename, chunk) == :provided
end

#requested?(filename, chunk) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/pdtp/server/chunk_info.rb', line 44

def requested?(filename,chunk)
  get(filename, chunk) == :requested
end

#set(state, filename, range) ⇒ Object

Set the state for a given chunk range



35
36
37
38
# File 'lib/pdtp/server/chunk_info.rb', line 35

def set(state, filename, range)        
  chunks = @files[filename] ||= RangeMap.new
  chunks[range] = state
end