Class: Cosmos::PreidentifiedStreamProtocol

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

Overview

This StreamProtocol delineates packets using the COSMOS preidentification system

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

Constructor Details

#initialize(sync_pattern = nil, max_length = nil) ⇒ PreidentifiedStreamProtocol

Returns a new instance of PreidentifiedStreamProtocol.

Parameters:

  • max_length (Integer) (defaults to: nil)

    The maximum allowed value of the length field

  • 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.



21
22
23
24
25
# File 'lib/cosmos/streams/preidentified_stream_protocol.rb', line 21

def initialize(sync_pattern = nil, max_length = nil)
  super(0, sync_pattern)
  @max_length = ConfigParser.handle_nil(max_length)
  @max_length = Integer(@max_length) if @max_length
end

Instance Method Details

#post_read_packet(packet) ⇒ Object

See StreamProtocol#post_read_packet



28
29
30
31
32
33
# File 'lib/cosmos/streams/preidentified_stream_protocol.rb', line 28

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

#pre_write_packet(packet) ⇒ Object

See StreamProtocol#pre_write_packet



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/cosmos/streams/preidentified_stream_protocol.rb', line 36

def pre_write_packet(packet)
  received_time = packet.received_time
  received_time = Time.now unless received_time
  time_seconds = [received_time.tv_sec].pack('N') # UINT32
  time_microseconds = [received_time.tv_usec].pack('N') # UINT32
  target_name = packet.target_name
  target_name = 'UNKNOWN' unless target_name
  packet_name = packet.packet_name
  packet_name = 'UNKNOWN' unless packet_name
  data = packet.buffer
  data_length = [data.length].pack('N') # UINT32
  data_to_send = ''
  data_to_send << @sync_pattern if @sync_pattern
  data_to_send << time_seconds
  data_to_send << time_microseconds
  data_to_send << target_name.length
  data_to_send << target_name
  data_to_send << packet_name.length
  data_to_send << packet_name
  data_to_send << data_length
  data_to_send << data
  data_to_send
end