Class: Interlnk::InterIO

Inherits:
IO
  • Object
show all
Defined in:
lib/interlnk/interio.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(protocol:, unit_nbr:) ⇒ InterIO

Returns a new instance of InterIO.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/interlnk/interio.rb', line 18

def initialize(protocol:, unit_nbr:)
  @protocol = protocol
  @unit_nbr = unit_nbr

  # TODO: support non-512-byte sectors
  @sector_size = 512
  @cur_location = 0

  @sector_cache_size = 64
  # keeps the actual cached sector data
  @sector_cache = {}
  # keeps a list of cached sectors, used to remove
  # the oldest from the cache when we need to cache
  # something new
  @cached_sectors = []
  #@refresh_cache
end

Instance Attribute Details

#cached_sectorsObject (readonly)

Returns the value of attribute cached_sectors.



16
17
18
# File 'lib/interlnk/interio.rb', line 16

def cached_sectors
  @cached_sectors
end

Instance Method Details

#read(length) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/interlnk/interio.rb', line 41

def read(length)
  #STDERR.puts "InterIO: Fetching #{length} bytes starting at #{@cur_location}"

  start_byte = @cur_location
  end_byte = @cur_location + length
  start_sector = (start_byte / @sector_size).to_i
  end_sector = ((end_byte-1) / @sector_size).to_i
  #STDERR.puts "SS: #{start_sector} ES: #{end_sector} SL: #{end_sector-start_sector+1}"
  if(end_sector-start_sector > @sector_cache_size) then
    STDERR.puts "Single reads spanning more than the entire sector cache size are not yet supported."
    Kernel.exit 1
  end
  cache_sectors start_sector..end_sector
  
  buf = ""
  (0..length-1).each do |byte_nbr|
    sector_nbr = (@cur_location / @sector_size).to_i
    byte_within_sector = @cur_location - (sector_nbr * @sector_size)
    if(!@sector_cache.include? sector_nbr) then
      STDERR.puts "Uncached sector fetch attempted, aborting."
      Kernel.exit 1
    end
    buf += @sector_cache[sector_nbr][byte_within_sector]
    @cur_location += 1
    #refresh_cache
  end

  buf
end

#seek(offset) ⇒ Object



36
37
38
39
# File 'lib/interlnk/interio.rb', line 36

def seek(offset)
  @cur_location = offset
  #refresh_cache
end