Class: OpenC3::TcpipServerInterface
- Inherits:
-
StreamInterface
- Object
- Interface
- StreamInterface
- OpenC3::TcpipServerInterface
- Defined in:
- lib/openc3/interfaces/tcpip_server_interface.rb
Overview
TCP/IP Server which can both read and write on a single port or two independent ports. A listen thread is setup which waits for client connections. For each connection to the read port, a thread is spawned that calls the read method from the interface. This data is then available by calling the TcpipServer read method. For each connection to the write port, a thread is spawned that calls the write method from the interface when data is send to the TcpipServer via the write method.
Defined Under Namespace
Classes: InterfaceInfo
Constant Summary
Constants included from Api
Api::DELAY_METRICS, Api::DURATION_METRICS, Api::SUBSCRIPTION_DELIMITER, Api::SUM_METRICS
Constants included from ApiShared
ApiShared::DEFAULT_TLM_POLLING_RATE
Constants included from Extract
Extract::SCANNING_REGULAR_EXPRESSION
Instance Attribute Summary collapse
-
#listen_address ⇒ String
The ip address to bind to.
-
#read_connection_callback ⇒ Object
Callback method to call when a new client connects to the read port.
-
#stream_log_pair ⇒ StreamLogPair
StreamLogPair instance or nil.
-
#write_connection_callback ⇒ Object
Callback method to call when a new client connects to the write port.
Attributes inherited from StreamInterface
Attributes inherited from Interface
#auto_reconnect, #bytes_read, #bytes_written, #cmd_routers, #cmd_target_names, #config_params, #connect_on_startup, #disable_disconnect, #interfaces, #name, #options, #packet_log_writer_pairs, #protocol_info, #read_count, #read_protocols, #read_raw_data, #read_raw_data_time, #reconnect_delay, #routers, #scheduler, #secrets, #state, #stored_packet_log_writer_pairs, #target_names, #tlm_target_names, #write_count, #write_protocols, #written_raw_data, #written_raw_data_time
Instance Method Summary collapse
-
#connect ⇒ Object
Create the read and write port listen threads.
-
#connected? ⇒ Boolean
Whether the server is listening for connections.
- #connection_string ⇒ Object
-
#disconnect ⇒ Object
Shutdowns the listener threads for both the read and write ports as well as any client connections.
-
#graceful_kill ⇒ Object
Gracefully kill all the threads.
-
#initialize(write_port, read_port, write_timeout, read_timeout, protocol_type = nil, *protocol_args) ⇒ TcpipServerInterface
constructor
A new instance of TcpipServerInterface.
-
#num_clients ⇒ Integer
The number of connected clients.
-
#read ⇒ Packet
Latest packet read from any of the connected clients.
-
#read_queue_size ⇒ Integer
The number of packets waiting on the read queue.
-
#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).
-
#start_raw_logging ⇒ Object
Start raw logging for this interface.
-
#stop_raw_logging ⇒ Object
Stop raw logging for this interface.
- #write(packet) ⇒ Object
-
#write_queue_size ⇒ Integer
The number of packets waiting on the write queue.
- #write_raw(data) ⇒ Object
Methods inherited from StreamInterface
#read_interface, #write_interface
Methods inherited from Interface
#_write, #add_protocol, #as_json, #convert_data_to_packet, #convert_packet_to_data, #copy_to, #interface_cmd, #protocol_cmd, #read_allowed?, #read_interface, #read_interface_base, #write_allowed?, #write_interface, #write_interface_base, #write_raw_allowed?
Methods included from Api
#_build_cmd_output_string, #_cmd_implementation, #_extract_target_command_names, #_extract_target_command_parameter_names, #_extract_target_packet_item_names, #_extract_target_packet_names, #_get_and_set_cmd, #_get_item, #_limits_group, #_set_tlm_process_args, #_tlm_process_args, #_validate_tlm_type, #build_cmd, #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, #config_tool_names, #connect_interface, #connect_router, #delete_config, #disable_cmd, #disable_limits, #disable_limits_group, #disconnect_interface, #disconnect_router, #enable_cmd, #enable_limits, #enable_limits_group, #get_all_cmd_names, #get_all_cmds, #get_all_interface_info, #get_all_router_info, #get_all_settings, #get_all_target_info, #get_all_tlm, #get_all_tlm_names, #get_cmd, #get_cmd_buffer, #get_cmd_cnt, #get_cmd_cnts, #get_cmd_hazardous, #get_cmd_time, #get_cmd_value, #get_interface, #get_interface_names, #get_item, #get_limits, #get_limits_events, #get_limits_groups, #get_limits_set, #get_limits_sets, #get_metrics, #get_out_of_limits, #get_overall_limits_state, #get_overrides, #get_packet_derived_items, #get_packets, #get_param, #get_router, #get_router_names, #get_setting, #get_settings, #get_target, #get_target_interfaces, #get_target_names, #get_tlm, #get_tlm_buffer, #get_tlm_cnt, #get_tlm_cnts, #get_tlm_packet, #get_tlm_values, #inject_tlm, #interface_cmd, #interface_protocol_cmd, #limits_enabled?, #list_configs, #list_settings, #load_config, #map_target_to_interface, #normalize_tlm, #offline_access_needed, #override_tlm, #router_cmd, #router_protocol_cmd, #save_config, #send_raw, #set_limits, #set_limits_set, #set_offline_access, #set_setting, #set_tlm, #start_raw_logging_interface, #start_raw_logging_router, #stash_all, #stash_delete, #stash_get, #stash_keys, #stash_set, #stop_raw_logging_interface, #stop_raw_logging_router, #subscribe_packets, #tlm, #tlm_formatted, #tlm_raw, #tlm_variable, #tlm_with_units
Constructor Details
#initialize(write_port, read_port, write_timeout, read_timeout, protocol_type = nil, *protocol_args) ⇒ TcpipServerInterface
Returns a new instance of TcpipServerInterface.
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/openc3/interfaces/tcpip_server_interface.rb', line 72 def initialize(write_port, read_port, write_timeout, read_timeout, protocol_type = nil, *protocol_args) super(protocol_type, protocol_args) @write_port = ConfigParser.handle_nil(write_port) @write_port = Integer(write_port) if @write_port @read_port = ConfigParser.handle_nil(read_port) @read_port = Integer(read_port) if @read_port @write_timeout = ConfigParser.handle_nil(write_timeout) @write_timeout = @write_timeout.to_f if @write_timeout @read_timeout = ConfigParser.handle_nil(read_timeout) @read_timeout = @read_timeout.to_f if @read_timeout @listen_sockets = [] @listen_pipes = [] @listen_threads = [] @read_threads = [] @write_thread = nil @write_raw_thread = nil @write_interface_infos = [] @read_interface_infos = [] @write_queue = nil @write_queue = Queue.new if @write_port @write_raw_queue = nil @write_raw_queue = Queue.new if @write_port @read_queue = nil @read_queue = Queue.new if @read_port @write_condition_variable = nil @write_condition_variable = ConditionVariable.new if @write_port @write_raw_mutex = nil @write_raw_mutex = Mutex.new if @write_port @write_raw_condition_variable = nil @write_raw_condition_variable = ConditionVariable.new if @write_port @write_connection_callback = nil @read_connection_callback = nil @stream_log_pair = nil @raw_logging_enabled = false @connection_mutex = Mutex.new @listen_address = "0.0.0.0" @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) @connected = false end |
Instance Attribute Details
#listen_address ⇒ String
Returns The ip address to bind to. Default to ANY (0.0.0.0).
59 60 61 |
# File 'lib/openc3/interfaces/tcpip_server_interface.rb', line 59 def listen_address @listen_address end |
#read_connection_callback ⇒ Object
Callback method to call when a new client connects to the read port. This method will be called with the Interface as the only argument.
55 56 57 |
# File 'lib/openc3/interfaces/tcpip_server_interface.rb', line 55 def read_connection_callback @read_connection_callback end |
#stream_log_pair ⇒ StreamLogPair
Returns StreamLogPair instance or nil.
57 58 59 |
# File 'lib/openc3/interfaces/tcpip_server_interface.rb', line 57 def stream_log_pair @stream_log_pair end |
#write_connection_callback ⇒ Object
Callback method to call when a new client connects to the write port. This method will be called with the Interface as the only argument.
52 53 54 |
# File 'lib/openc3/interfaces/tcpip_server_interface.rb', line 52 def write_connection_callback @write_connection_callback end |
Instance Method Details
#connect ⇒ Object
Create the read and write port listen threads. Incoming connections will spawn separate threads to process the reads and writes.
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/openc3/interfaces/tcpip_server_interface.rb', line 133 def connect @cancel_threads = false @read_queue.clear if @read_queue if @write_port == @read_port # One socket start_listen_thread(@read_port, true, true) else start_listen_thread(@write_port, true, false) if @write_port start_listen_thread(@read_port, false, true) if @read_port end if @write_port @write_thread = Thread.new do loop do write_thread_body() break if @cancel_threads end rescue Exception => e shutdown_interfaces(@write_interface_infos) Logger.error("#{@name}: Tcpip server write thread unexpectedly died") Logger.error(e.formatted) end @write_raw_thread = Thread.new do loop do write_raw_thread_body() break if @cancel_threads end rescue Exception => e shutdown_interfaces(@write_interface_infos) Logger.error("#{@name}: Tcpip server write raw thread unexpectedly died") Logger.error(e.formatted) end else @write_thread = nil @write_raw_thread = nil end super() @connected = true end |
#connected? ⇒ Boolean
Returns Whether the server is listening for connections.
173 174 175 |
# File 'lib/openc3/interfaces/tcpip_server_interface.rb', line 173 def connected? @connected end |
#connection_string ⇒ Object
121 122 123 124 125 126 127 128 129 |
# File 'lib/openc3/interfaces/tcpip_server_interface.rb', line 121 def connection_string if @write_port == @read_port return "listening on #{@listen_address}:#{@write_port} (R/W)" end result = "listening on" result += " #{@listen_address}:#{@write_port} (write)" if @write_port result += " #{@listen_address}:#{@read_port} (read)" if @read_port return result end |
#disconnect ⇒ Object
Shutdowns the listener threads for both the read and write ports as well as any client connections.
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/openc3/interfaces/tcpip_server_interface.rb', line 179 def disconnect @cancel_threads = true @read_queue << nil if @read_queue @listen_pipes.each do |pipe| pipe.write('.') rescue Exception # Oh well end @listen_pipes.clear # Shutdown listen thread(s) @listen_threads.each { |listen_thread| OpenC3.kill_thread(self, listen_thread) } @listen_threads.clear # Shutdown listen socket(s) @listen_sockets.each do |listen_socket| OpenC3.close_socket(listen_socket) rescue IOError # Ok may have been closed by the thread end @listen_sockets.clear # This will unblock read threads shutdown_interfaces(@read_interface_infos) @read_threads.each { |thread| OpenC3.kill_thread(self, thread) } @read_threads.clear if @write_thread OpenC3.kill_thread(self, @write_thread) @write_thread = nil end if @write_raw_thread OpenC3.kill_thread(self, @write_raw_thread) @write_raw_thread = nil end shutdown_interfaces(@write_interface_infos) @connected = false super() end |
#graceful_kill ⇒ Object
Gracefully kill all the threads
221 222 223 |
# File 'lib/openc3/interfaces/tcpip_server_interface.rb', line 221 def graceful_kill # This method is just here to prevent warnings end |
#num_clients ⇒ Integer
Returns The number of connected clients.
271 272 273 274 275 276 |
# File 'lib/openc3/interfaces/tcpip_server_interface.rb', line 271 def num_clients interfaces = [] @write_interface_infos.each { |wii| interfaces << wii.interface } @read_interface_infos.each { |rii| interfaces << rii.interface } interfaces.uniq.length end |
#read ⇒ Packet
Returns Latest packet read from any of the connected clients. Note this method blocks until data is available.
227 228 229 230 231 232 233 234 235 236 |
# File 'lib/openc3/interfaces/tcpip_server_interface.rb', line 227 def read raise "Interface not connected for read: #{@name}" unless connected? raise "Interface not readable: #{@name}" unless read_allowed? packet = @read_queue.pop return nil unless packet @read_count += 1 packet end |
#read_queue_size ⇒ Integer
Returns The number of packets waiting on the read queue.
261 262 263 |
# File 'lib/openc3/interfaces/tcpip_server_interface.rb', line 261 def read_queue_size @read_queue ? @read_queue.size : 0 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)
293 294 295 296 297 298 299 |
# File 'lib/openc3/interfaces/tcpip_server_interface.rb', line 293 def set_option(option_name, option_values) super(option_name, option_values) case option_name.upcase when 'LISTEN_ADDRESS' @listen_address = option_values[0] end end |
#start_raw_logging ⇒ Object
Start raw logging for this interface
279 280 281 282 |
# File 'lib/openc3/interfaces/tcpip_server_interface.rb', line 279 def start_raw_logging @raw_logging_enabled = true change_raw_logging(:start) end |
#stop_raw_logging ⇒ Object
Stop raw logging for this interface
285 286 287 288 |
# File 'lib/openc3/interfaces/tcpip_server_interface.rb', line 285 def stop_raw_logging @raw_logging_enabled = false change_raw_logging(:stop) end |
#write(packet) ⇒ Object
240 241 242 243 244 245 246 247 |
# File 'lib/openc3/interfaces/tcpip_server_interface.rb', line 240 def write(packet) raise "Interface not connected for write: #{@name}" unless connected? raise "Interface not writable: #{@name}" unless write_allowed? @write_count += 1 @write_queue << packet.clone @write_condition_variable.broadcast end |
#write_queue_size ⇒ Integer
Returns The number of packets waiting on the write queue.
266 267 268 |
# File 'lib/openc3/interfaces/tcpip_server_interface.rb', line 266 def write_queue_size @write_queue ? @write_queue.size : 0 end |
#write_raw(data) ⇒ Object
251 252 253 254 255 256 257 258 |
# File 'lib/openc3/interfaces/tcpip_server_interface.rb', line 251 def write_raw(data) raise "Interface not connected for write_raw: #{@name}" unless connected? raise "Interface not write-rawable: #{@name}" unless write_raw_allowed? @write_raw_queue << data @write_raw_condition_variable.broadcast return data end |