Class: Fit4Ruby::FitMessageIdMapper

Inherits:
Object
  • Object
show all
Defined in:
lib/fit4ruby/FitMessageIdMapper.rb

Overview

The FIT file maps GlobalFitMessage numbers to local numbers. Due to restrictions in the format, only 16 local messages can be active at any point in the file. If a GlobalFitMessage is needed that is currently not mapped, a new entry is generated and the least recently used message is evicted. The FitMessageIdMapper is the objects that stores those 16 active entries and can map global to local message numbers.

Defined Under Namespace

Classes: Entry

Instance Method Summary collapse

Constructor Details

#initializeFitMessageIdMapper

Returns a new instance of FitMessageIdMapper.



27
28
29
# File 'lib/fit4ruby/FitMessageIdMapper.rb', line 27

def initialize
  @entries = Array.new(16, nil)
end

Instance Method Details

#add_global(message) ⇒ Object

Add a new GlobalFitMessage to the mapper and return the local message number.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/fit4ruby/FitMessageIdMapper.rb', line 33

def add_global(message)
  unless (slot = @entries.index { |e| e.nil? })
    # No more free slots. We have to find the least recently used one.
    slot = 0
    0.upto(15) do |i|
      if i != slot && @entries[slot].last_use > @entries[i].last_use
        slot = i
      end
    end
  end
  @entries[slot] = Entry.new(message, Time.now)

  slot
end

#get_local(message) ⇒ Object

Get the local message number for a given GlobalFitMessage. If there is no message number, nil is returned.



50
51
52
53
54
55
56
57
58
# File 'lib/fit4ruby/FitMessageIdMapper.rb', line 50

def get_local(message)
  0.upto(15) do |i|
    if (entry = @entries[i]) && entry.global_message == message
      entry.last_use = Time.now
      return i
    end
  end
  nil
end