Class: Cosmos::FixedStreamProtocol

Inherits:
StreamProtocol show all
Defined in:
lib/cosmos/streams/fixed_stream_protocol.rb

Overview

This StreamProtocol delineates packets by identifying them and then reading out their entire fixed length. Packets lengths can vary but they must all be fixed.

Instance Attribute Summary

Attributes inherited from StreamProtocol

#bytes_read, #bytes_written, #interface, #post_read_data_callback, #post_read_packet_callback, #post_write_data_callback, #pre_write_packet_callback, #stream

Instance Method Summary collapse

Methods inherited from StreamProtocol

#connect, #connected?, #disconnect, #post_read_data, #post_write_data, #pre_write_packet, #read, #write, #write_raw

Constructor Details

#initialize(min_id_size, discard_leading_bytes = 0, sync_pattern = nil, telemetry_stream = true, fill_sync_pattern = false) ⇒ FixedStreamProtocol

Returns a new instance of FixedStreamProtocol.

Parameters:

  • min_id_size (Integer)

    The minimum amount of data needed to identify a packet.

  • telemetry_stream (Boolean) (defaults to: true)

    Whether the stream is returning telemetry (true) or commands (false)

  • discard_leading_bytes (Integer) (defaults to: 0)

    The number of bytes to discard from the binary data after reading from the Stream. Note that this is often used to remove a sync pattern from the final packet data.

  • sync_pattern (String) (defaults to: nil)

    String representing a hex number ("0x1234") that will be searched for in the raw Stream. Bytes encountered before this pattern is found are discarded.

  • fill_sync_pattern (Boolean) (defaults to: false)

    Fill the sync pattern when writing packets



28
29
30
31
32
33
34
35
36
# File 'lib/cosmos/streams/fixed_stream_protocol.rb', line 28

def initialize(min_id_size,
               discard_leading_bytes = 0,
               sync_pattern = nil,
               telemetry_stream = true,
               fill_sync_pattern = false)
  super(discard_leading_bytes, sync_pattern, fill_sync_pattern)
  @min_id_size = Integer(min_id_size)
  @telemetry_stream = telemetry_stream
end

Instance Method Details

#post_read_packet(packet) ⇒ Object

Set the received_time, target_name and packet_name which we recorded when we identified this packet. The server will also do this but since we know the information here, we perform this optimization. See StreamProtocol#post_read_packet



42
43
44
45
46
47
# File 'lib/cosmos/streams/fixed_stream_protocol.rb', line 42

def post_read_packet(packet)
  packet.received_time = @received_time
  packet.target_name = @target_name
  packet.packet_name = @packet_name
  packet
end