Class: LiveF1::Packet

Inherits:
Object
  • Object
show all
Defined in:
lib/live_f1/packet.rb,
lib/live_f1/packet/car.rb,
lib/live_f1/packet/sys.rb,
lib/live_f1/packet/header.rb,
lib/live_f1/packet/car/gap.rb,
lib/live_f1/packet/sys/reset.rb,
lib/live_f1/packet/sys/speed.rb,
lib/live_f1/packet/car/driver.rb,
lib/live_f1/packet/car/number.rb,
lib/live_f1/packet/sys/notice.rb,
lib/live_f1/packet/decryptable.rb,
lib/live_f1/packet/sector_time.rb,
lib/live_f1/packet/sys/weather.rb,
lib/live_f1/packet/car/interval.rb,
lib/live_f1/packet/car/lap_time.rb,
lib/live_f1/packet/car/num_pits.rb,
lib/live_f1/packet/car/period_1.rb,
lib/live_f1/packet/car/period_2.rb,
lib/live_f1/packet/car/period_3.rb,
lib/live_f1/packet/car/position.rb,
lib/live_f1/packet/car/sector_1.rb,
lib/live_f1/packet/car/sector_2.rb,
lib/live_f1/packet/car/sector_3.rb,
lib/live_f1/packet/car/lap_count.rb,
lib/live_f1/packet/car/pit_count.rb,
lib/live_f1/packet/car/pit_lap_1.rb,
lib/live_f1/packet/car/pit_lap_2.rb,
lib/live_f1/packet/car/pit_lap_3.rb,
lib/live_f1/packet/sys/copyright.rb,
lib/live_f1/packet/sys/key_frame.rb,
lib/live_f1/packet/sys/timestamp.rb,
lib/live_f1/packet/sys/commentary.rb,
lib/live_f1/packet/sys/track_status.rb,
lib/live_f1/packet/car/best_lap_time.rb,
lib/live_f1/packet/sys/session_start.rb,
lib/live_f1/packet/car/position_update.rb,
lib/live_f1/packet/car/position_history.rb

Overview

A Packet represents a raw instruction sent from the live timing server to the live timing applet.

It is represented in the data stream by a variable-length series of bytes, always starting with a 2-byte “header” and then a number of other bytes depending on the specific data being represented.

Direct Known Subclasses

Car, Sys, Unknown

Defined Under Namespace

Modules: Decryptable, SectorTime, Type Classes: Car, Header, Sys, Unknown

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, header) ⇒ Packet

Returns a new instance of Packet.



83
84
85
86
# File 'lib/live_f1/packet.rb', line 83

def initialize source, header
  @source = source
  @header = header
end

Instance Attribute Details

#dataObject Also known as: bytes

Returns the value of attribute data.



69
70
71
# File 'lib/live_f1/packet.rb', line 69

def data
  @data
end

#headerObject (readonly)

Returns the value of attribute header.



68
69
70
# File 'lib/live_f1/packet.rb', line 68

def header
  @header
end

#sourceObject (readonly)

Returns the value of attribute source.



67
68
69
# File 'lib/live_f1/packet.rb', line 67

def source
  @source
end

Class Method Details

.from_source(source, event_type) ⇒ Object

First extracts a Header from the given source and then extracts the packet it represents, based on the given event type.



75
76
77
78
79
80
81
# File 'lib/live_f1/packet.rb', line 75

def self.from_source source, event_type
  header = Header.from_source(source, event_type)
  packet = header.packet_klass.new source, header
  data = source.read_bytes(packet.length)
  packet.data = data
  packet
end

Instance Method Details

#inspectObject



102
103
104
105
106
107
108
# File 'lib/live_f1/packet.rb', line 102

def inspect
  if LiveF1.debug
    "[%7s] %-23s %s" % [spare_data, leader, to_s]
  else
    "%-23s %s" % [leader, to_s]
  end
end

#leaderObject



110
111
112
# File 'lib/live_f1/packet.rb', line 110

def leader
  self.class.name.sub(/LiveF1::Packet::/, '')
end

#spare_bitsObject



92
93
94
# File 'lib/live_f1/packet.rb', line 92

def spare_bits
  0
end

#spare_dataObject

Returns the bits of the header data which aren’t used to determine the packet length



98
99
100
# File 'lib/live_f1/packet.rb', line 98

def spare_data
  "%0#{spare_bits}b" % (header.data & (2 ** spare_bits - 1))
end

#to_sObject



88
89
90
# File 'lib/live_f1/packet.rb', line 88

def to_s
  data.inspect
end