Class: LiveF1::Source::Live

Inherits:
LiveF1::Source show all
Defined in:
lib/live_f1/source/live.rb

Defined Under Namespace

Classes: ConnectionError, Log, LogProxy

Constant Summary collapse

HOST =
"80.231.178.249"
PORT =
4321

Instance Attribute Summary collapse

Attributes inherited from LiveF1::Source

#session

Instance Method Summary collapse

Methods inherited from LiveF1::Source

#decrypt, #read_packet

Constructor Details

#initialize(username, password) ⇒ Live

Returns a new instance of Live.



17
18
19
20
# File 'lib/live_f1/source/live.rb', line 17

def initialize username, password
  @username = username
  @password = password
end

Instance Attribute Details

#passwordObject (readonly)

Returns the value of attribute password.



15
16
17
# File 'lib/live_f1/source/live.rb', line 15

def password
  @password
end

#usernameObject (readonly)

Returns the value of attribute username.



15
16
17
# File 'lib/live_f1/source/live.rb', line 15

def username
  @username
end

Instance Method Details

#decryption_key(session_number) ⇒ Object

Raises:



44
45
46
47
48
49
# File 'lib/live_f1/source/live.rb', line 44

def decryption_key session_number
  return if session_number.zero?
  key = open("http://#{HOST}/reg/getkey/#{session_number}.asp?auth=#{auth}").read.to_i(16)
  raise ConnectionError, "Unable to access session key for session #{session_number}. This could indicate incorrect credentials or an issue with the formula1.com key server" if key.zero?
  key
end

#keyframe(number = nil) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/live_f1/source/live.rb', line 34

def keyframe number = nil
  io = open("http://#{HOST}/#{keyframe_filename(number)}")
  log.keyframe(number, io.read) if number
  log.flush
  io.rewind
  Source::Keyframe.new io, self
rescue SocketError
  raise ConnectionError, "Unable to connect to live timing server #{HOST}"
end

#logObject



82
83
84
# File 'lib/live_f1/source/live.rb', line 82

def log
  LogProxy
end

#log_dir=(log_directory) ⇒ Object

Sets the base directory for live data log files to be stored



52
53
54
# File 'lib/live_f1/source/live.rb', line 52

def log_dir= log_directory
  Log.dir = log_directory
end

#read_bytes(num) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/live_f1/source/live.rb', line 22

def read_bytes num
  begin
    Timeout.timeout(0.5) do
      socket.read(num) or raise EOFError
    end
  rescue Timeout::Error, Errno::ETIMEDOUT => e
    socket.write("\n")
    socket.flush
    retry
  end
end

#runObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/live_f1/source/live.rb', line 56

def run
  super do |packet|
    case packet
    when LiveF1::Packet::Sys::SessionStart
      log.start packet.session_number
      log.key session.decryption_key
    when LiveF1::Packet::Sys::KeyFrame
      keyframe(packet.number)
    end

    log.packet packet

    yield packet
  end
rescue Errno::ECONNRESET
  log.reset
  retry
rescue Exception
  log.reset
  raise
end

#session=(new_session) ⇒ Object



78
79
80
# File 'lib/live_f1/source/live.rb', line 78

def session= new_session
  super
end