Class: Cosmos::TerminatedStreamProtocol

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

Overview

This StreamProtocol delineates packets using termination characters at the end of the stream.

Direct Known Subclasses

TemplateStreamProtocol

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_read_packet, #post_write_data, #read, #write, #write_raw

Constructor Details

#initialize(write_termination_characters, read_termination_characters, strip_read_termination = true, discard_leading_bytes = 0, sync_pattern = nil, fill_sync_pattern = false) ⇒ TerminatedStreamProtocol

Returns a new instance of TerminatedStreamProtocol.

Parameters:

  • write_termination_characters (String)

    The characters to write to the stream after writing the Packet buffer. Must be given as a hexadecimal string such as '0xABCD'.

  • read_termination_characters (String)

    The characters at the end of the stream which delineate the end of a Packet. Must be given as a hexadecimal string such as '0xABCD'.

  • strip_read_termination (Boolean) (defaults to: true)

    Whether to remove the read_termination_characters before turning the stream data into a Packet.

  • 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



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/cosmos/streams/terminated_stream_protocol.rb', line 32

def initialize(write_termination_characters,
               read_termination_characters,
               strip_read_termination = true,
               discard_leading_bytes = 0,
               sync_pattern = nil,
               fill_sync_pattern = false)
  @write_termination_characters = write_termination_characters.hex_to_byte_string
  @read_termination_characters  = read_termination_characters.hex_to_byte_string
  @strip_read_termination       = ConfigParser.handle_true_false(strip_read_termination)

  super(discard_leading_bytes, sync_pattern, fill_sync_pattern)
end

Instance Method Details

#pre_write_packet(packet) ⇒ Object

See StreamProtocol#pre_write_packet



46
47
48
49
50
51
52
53
54
55
# File 'lib/cosmos/streams/terminated_stream_protocol.rb', line 46

def pre_write_packet(packet)
  data = super(packet)
  raise "Packet contains termination characters!" if data.index(@write_termination_characters)

  data = data.clone # Don't want to modify the actual packet buffer with the termination characters
  @write_termination_characters.each_byte do |byte|
    data << byte
  end
  data
end