Class: Cosmos::TcpipServerInterface

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

Overview

Base class for interfaces that act as a TCP/IP server

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, #options, #packet_log_writer_pairs, #raw_logger_pair, #read_count, #reconnect_delay, #routers, #target_names, #thread, #write_count

Instance Method Summary collapse

Methods inherited from Interface

#copy_to, #post_identify_packet, #read_allowed?, #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_cmd_time, #get_cmd_value, #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_stale, #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(write_port, read_port, write_timeout, read_timeout, stream_protocol_type, *stream_protocol_args) ⇒ TcpipServerInterface

Returns a new instance of TcpipServerInterface.

Parameters:

  • write_port (Integer)

    Port that clients should write commands to

  • read_port (Integer)

    Port that clients should read telemetry from

  • write_timeout (Integer)

    Seconds to wait before aborting writes

  • read_timeout (Integer)

    Seconds to wait before aborting reads

  • 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



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/cosmos/interfaces/tcpip_server_interface.rb', line 25

def initialize(write_port,
               read_port,
               write_timeout,
               read_timeout,
               stream_protocol_type,
               *stream_protocol_args)
  super()

  @tcpip_server = TcpipServer.new(write_port,
                                  read_port,
                                  write_timeout,
                                  read_timeout,
                                  stream_protocol_type,
                                  *stream_protocol_args)
  @tcpip_server.interface = self
  @read_allowed = false unless ConfigParser.handle_nil(read_port)
  @write_allowed = false unless ConfigParser.handle_nil(write_port)
  @write_raw_allowed = false unless ConfigParser.handle_nil(write_port)
end

Instance Method Details

#bytes_readObject

The number of bytes read from all connected sockets



108
109
110
# File 'lib/cosmos/interfaces/tcpip_server_interface.rb', line 108

def bytes_read
  @tcpip_server.bytes_read
end

#bytes_read=(bytes_read) ⇒ Object

The number of bytes read from all connected sockets



113
114
115
# File 'lib/cosmos/interfaces/tcpip_server_interface.rb', line 113

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

#bytes_writtenObject

The number of bytes written to the TcpipServer. This number does not vary with the number of clients connected to the write port.



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

def bytes_written
  @tcpip_server.bytes_written
end

#bytes_written=(bytes_written) ⇒ Object

The number of bytes written to the TcpipServer. This number does not vary with the number of clients connected to the write port.



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

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

#connectObject

Create the read and write port listen threads. Incoming connections will spawn separate threads to process the reads and writes.



46
47
48
49
# File 'lib/cosmos/interfaces/tcpip_server_interface.rb', line 46

def connect
  @tcpip_server.raw_logger_pair = @raw_logger_pair
  @tcpip_server.connect
end

#connected?Boolean

Returns Whether the server is listening for connections.

Returns:

  • (Boolean)

    Whether the server is listening for connections



52
53
54
# File 'lib/cosmos/interfaces/tcpip_server_interface.rb', line 52

def connected?
  @tcpip_server.connected?
end

#disconnectObject

Shutdowns the listener threads for both the read and write ports as well as any client connections. As a part of shutting down client connections, the StreamProtocol#disconnect method is called.



57
58
59
# File 'lib/cosmos/interfaces/tcpip_server_interface.rb', line 57

def disconnect
  @tcpip_server.disconnect
end

#num_clientsObject

Number of clients connected to the TCP/IP server



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

def num_clients
  @tcpip_server.num_clients
end

#readPacket

Returns Latest packet read from any of the connected clients. Note this method blocks until data is available.

Returns:

  • (Packet)

    Latest packet read from any of the connected clients. Note this method blocks until data is available.



62
63
64
65
66
67
# File 'lib/cosmos/interfaces/tcpip_server_interface.rb', line 62

def read
  # Normal case will not be trying to read if not connected so don't bother checking
  packet = @tcpip_server.read
  @read_count += 1 if packet
  packet
end

#read_queue_sizeObject

Number of packets buffered in the read queue



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

def read_queue_size
  @tcpip_server.read_queue_size
end

#set_option(option_name, option_values) ⇒ Object

Supported Options LISTEN_ADDRESS - Ip address of the interface to accept connections on - Default: 0.0.0.0 (see Interface#set_option)



155
156
157
158
159
160
# File 'lib/cosmos/interfaces/tcpip_server_interface.rb', line 155

def set_option(option_name, option_values)
  super(option_name, option_values)
  if option_name.upcase == 'LISTEN_ADDRESS'
    @tcpip_server.listen_address = option_values[0]
  end
end

#start_raw_loggingObject

Start raw logging for this interface



143
144
145
# File 'lib/cosmos/interfaces/tcpip_server_interface.rb', line 143

def start_raw_logging
  @tcpip_server.start_raw_logging
end

#stop_raw_loggingObject

Stop raw logging for this interface



148
149
150
# File 'lib/cosmos/interfaces/tcpip_server_interface.rb', line 148

def stop_raw_logging
  @tcpip_server.stop_raw_logging
end

#write(packet) ⇒ Object

If the server has connections, the packet is written to all the connected clients.

Parameters:



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/cosmos/interfaces/tcpip_server_interface.rb', line 73

def write(packet)
  if connected?()
    begin
      @tcpip_server.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_queue_sizeObject

Number of packets buffered in the write queue



138
139
140
# File 'lib/cosmos/interfaces/tcpip_server_interface.rb', line 138

def write_queue_size
  @tcpip_server.write_queue_size
end

#write_raw(data) ⇒ Object

If the server has connections, the data is written to all the connected clients.

Parameters:

  • data (String)

    Raw binary data



92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/cosmos/interfaces/tcpip_server_interface.rb', line 92

def write_raw(data)
  if connected?()
    begin
      @tcpip_server.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