Class: Cosmos::StreamInterface

Inherits:
Interface show all
Defined in:
lib/cosmos/interfaces/stream_interface.rb

Overview

Stream interfaces use stream protocols to interface with the target. This class simply passes through each method to identically named methods in the stream protocol class. This class is an abstract class and should not be used directly. It should be subclassed and the connect method implemented.

Direct Known Subclasses

SerialInterface, TcpipClientInterface

Constant Summary

Constants included from Extract

Extract::SCANNING_REGULAR_EXPRESSION

Instance Attribute Summary

Attributes inherited from Interface

#auto_reconnect, #connect_on_startup, #disable_disconnect, #interfaces, #name, #num_clients, #packet_log_writer_pairs, #raw_logger_pair, #read_count, #read_queue_size, #reconnect_delay, #routers, #target_names, #thread, #write_count, #write_queue_size

Instance Method Summary collapse

Methods inherited from Interface

#connect, #copy_to, #post_identify_packet, #read_allowed?, #start_raw_logging, #stop_raw_logging, #write_allowed?, #write_raw_allowed?

Methods included from Api

#cmd, #cmd_no_checks, #cmd_no_hazardous_check, #cmd_no_range_check, #cmd_raw, #cmd_raw_no_checks, #cmd_raw_no_hazardous_check, #cmd_raw_no_range_check, #connect_interface, #connect_router, #disable_limits, #disable_limits_group, #disconnect_interface, #disconnect_router, #enable_limits, #enable_limits_group, #get_cmd_hazardous, #get_cmd_list, #get_cmd_log_filename, #get_cmd_param_list, #get_interface_names, #get_limits, #get_limits_event, #get_limits_groups, #get_limits_set, #get_limits_sets, #get_out_of_limits, #get_overall_limits_state, #get_packet_data, #get_router_names, #get_server_message_log_filename, #get_target_list, #get_tlm_details, #get_tlm_item_list, #get_tlm_list, #get_tlm_log_filename, #get_tlm_packet, #get_tlm_values, #interface_state, #limits_enabled?, #map_target_to_interface, #router_state, #send_raw, #set_limits, #set_limits_set, #set_tlm, #set_tlm_raw, #start_cmd_log, #start_logging, #start_new_server_message_log, #start_raw_logging_interface, #start_raw_logging_router, #start_tlm_log, #stop_cmd_log, #stop_logging, #stop_raw_logging_interface, #stop_raw_logging_router, #stop_tlm_log, #subscribe_limits_events, #subscribe_packet_data, #tlm, #tlm_formatted, #tlm_raw, #tlm_variable, #tlm_with_units, #unsubscribe_limits_events, #unsubscribe_packet_data

Constructor Details

#initialize(stream_protocol_type, *stream_protocol_args) ⇒ StreamInterface

Returns a new instance of StreamInterface.

Parameters:

  • stream_protocol_type (String)

    Combined with 'StreamProtocol' this should resolve to a COSMOS stream protocol class

  • stream_protocol_args (Array)

    Arguments to pass to the stream protocol class constructor



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/cosmos/interfaces/stream_interface.rb', line 33

def initialize(stream_protocol_type, *stream_protocol_args)
  super()

  stream_protocol_class = stream_protocol_type.to_s.capitalize << 'StreamProtocol'
  klass = stream_protocol_class.to_class
  unless klass
    begin
      require stream_protocol_class.class_name_to_filename
    # If the stream protocol doesn't exist require will throw a LoadError
    rescue LoadError => err
      msg = "Unable to require " \
        "#{stream_protocol_class.class_name_to_filename} due to #{err.message}. " \
        "Ensure #{stream_protocol_class.class_name_to_filename} "\
        "is in the COSMOS lib directory."
      Logger.instance.error msg
      raise msg
    # If the stream protocol exists but has problems we rescue those here
    rescue => err
      msg = "Unable to require " \
        "#{stream_protocol_class.class_name_to_filename} due to #{err.message}."
      Logger.instance.error msg
      raise msg
    end
  end

  @stream_protocol = klass.new(*stream_protocol_args)
  @stream_protocol.interface = self
end

Instance Method Details

#bytes_readInteger

Returns The number of bytes read by the stream protocol.

Returns:

  • (Integer)

    The number of bytes read by the stream protocol



118
119
120
# File 'lib/cosmos/interfaces/stream_interface.rb', line 118

def bytes_read
  @stream_protocol.bytes_read
end

#bytes_read=(bytes_read) ⇒ Integer

Returns Sets the number of bytes read by the stream protocol.

Returns:

  • (Integer)

    Sets the number of bytes read by the stream protocol



123
124
125
# File 'lib/cosmos/interfaces/stream_interface.rb', line 123

def bytes_read=(bytes_read)
  @stream_protocol.bytes_read = bytes_read
end

#bytes_writtenInteger

Returns The number of bytes written to the stream protocol.

Returns:

  • (Integer)

    The number of bytes written to the stream protocol



128
129
130
# File 'lib/cosmos/interfaces/stream_interface.rb', line 128

def bytes_written
  @stream_protocol.bytes_written
end

#bytes_written=(bytes_written) ⇒ Integer

Returns Sets the number of bytes written to the stream protocol.

Returns:

  • (Integer)

    Sets the number of bytes written to the stream protocol



133
134
135
# File 'lib/cosmos/interfaces/stream_interface.rb', line 133

def bytes_written=(bytes_written)
  @stream_protocol.bytes_written = bytes_written
end

#connected?Boolean

Returns Whether the stream protocol is connected to the target.

Returns:

  • (Boolean)

    Whether the stream protocol is connected to the target



65
66
67
# File 'lib/cosmos/interfaces/stream_interface.rb', line 65

def connected?
  @stream_protocol.connected?
end

#disconnectObject

Disconnect the stream protocol from the target



70
71
72
# File 'lib/cosmos/interfaces/stream_interface.rb', line 70

def disconnect
  @stream_protocol.disconnect
end

#post_read_data(packet_data) ⇒ Object

These methods do not exist in StreamInterface but can be implemented by subclasses and will be called by the Cosmos::StreamProtocol when processing the data in the Cosmos::Stream.

Subclasses of Cosmos::StreamProtocol can implement the same method. However, if the callback method is implemented in the interface then the subclass method is not called.

Thus if you are implementing an Interface that uses a Cosmos::StreamProtocol and choose to implement this method, you must be aware of any processing that the Cosmos::StreamProtocol does in the same method and re-implement it (or call @stream_protocol.post_read_data(packet_data), etc) in yours.



# File 'lib/cosmos/interfaces/stream_interface.rb', line 137

#post_read_packet(packet) ⇒ Object

These methods do not exist in StreamInterface but can be implemented by subclasses and will be called by the Cosmos::StreamProtocol when processing the data in the Cosmos::Stream.

Subclasses of Cosmos::StreamProtocol can implement the same method. However, if the callback method is implemented in the interface then the subclass method is not called.

Thus if you are implementing an Interface that uses a Cosmos::StreamProtocol and choose to implement this method, you must be aware of any processing that the Cosmos::StreamProtocol does in the same method and re-implement it (or call @stream_protocol.post_read_data(packet_data), etc) in yours.



# File 'lib/cosmos/interfaces/stream_interface.rb', line 137

#pre_write_packet(packet) ⇒ Object

These methods do not exist in StreamInterface but can be implemented by subclasses and will be called by the Cosmos::StreamProtocol when processing the data in the Cosmos::Stream.

Subclasses of Cosmos::StreamProtocol can implement the same method. However, if the callback method is implemented in the interface then the subclass method is not called.

Thus if you are implementing an Interface that uses a Cosmos::StreamProtocol and choose to implement this method, you must be aware of any processing that the Cosmos::StreamProtocol does in the same method and re-implement it (or call @stream_protocol.post_read_data(packet_data), etc) in yours.



# File 'lib/cosmos/interfaces/stream_interface.rb', line 137

#readObject

Read a packet from the stream protocol



75
76
77
78
79
# File 'lib/cosmos/interfaces/stream_interface.rb', line 75

def read
  packet = @stream_protocol.read
  @read_count += 1 if packet
  packet
end

#write(packet) ⇒ Object

Write a packet to the stream protocol

Parameters:



84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/cosmos/interfaces/stream_interface.rb', line 84

def write(packet)
  if connected?()
    begin
      @stream_protocol.write(packet)
      @write_count += 1
    rescue Exception => err
      Logger.instance.error("Error writing to interface : #{@name}")
      disconnect()
      raise err
    end
  else
    raise "Interface not connected for write : #{@name}"
  end
end

#write_raw(data) ⇒ Object

Write a raw binary string to the stream protocol

Parameters:

  • data (String)

    Raw binary string



102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/cosmos/interfaces/stream_interface.rb', line 102

def write_raw(data)
  if connected?()
    begin
      @stream_protocol.write_raw(data)
      @write_count += 1
    rescue Exception => err
      Logger.instance.error("Error writing raw data to interface : #{@name}")
      disconnect()
      raise err
    end
  else
    raise "Interface not connected for write_raw : #{@name}"
  end
end