Class: Cosmos::TcpipServerInterface
- Inherits:
-
StreamInterface
- Object
- Interface
- StreamInterface
- Cosmos::TcpipServerInterface
- Defined in:
- lib/cosmos/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::SETTINGS_KEY, Api::SUBSCRIPTION_DELIMITER
Constants included from ApiShared
ApiShared::DEFAULT_TLM_POLLING_RATE
Constants included from Extract
Extract::SCANNING_REGULAR_EXPRESSION
Instance Attribute Summary collapse
-
#auto_system_meta ⇒ boolean
Automatically send SYSTEM META on connect - Default false - Can be CMD/TLM.
-
#listen_address ⇒ String
The ip address to bind to.
-
#raw_logger_pair ⇒ RawLoggerPair
RawLoggerPair instance or nil.
-
#read_connection_callback ⇒ Object
Callback method to call when a new client connects to the read port.
-
#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, #config_params, #connect_on_startup, #disable_disconnect, #interfaces, #name, #options, #override_tlm, #packet_log_writer_pairs, #protocol_info, #read_count, #read_protocols, #read_raw_data, #read_raw_data_time, #reconnect_delay, #routers, #state, #stored_packet_log_writer_pairs, #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.
-
#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 AUTO_SYSTEM_META - Automatically send SYSTEM META on connect - Default false (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
#_normalize_tlm, #_override, #_override_tlm, #_override_tlm_raw, #_write, #add_protocol, #as_json, #convert_data_to_packet, #convert_packet_to_data, #copy_to, #read_allowed?, #read_interface, #read_interface_base, #write_allowed?, #write_interface, #write_interface_base, #write_raw_allowed?
Methods included from Api
#_get_cnt, #_limits_group, #_validate_tlm_type, #build_cmd_output_string, #cmd, #cmd_implementation, #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, #delete_config, #disable_limits, #disable_limits_group, #disconnect_interface, #disconnect_router, #enable_limits, #enable_limits_group, #get_all_cmd_info, #get_all_cmd_tlm_info, #get_all_commands, #get_all_commands_list, #get_all_interface_info, #get_all_router_info, #get_all_settings, #get_all_target_info, #get_all_telemetry, #get_all_telemetry_list, #get_all_tlm_info, #get_cmd_buffer, #get_cmd_cnt, #get_cmd_hazardous, #get_cmd_time, #get_cmd_value, #get_command, #get_interface, #get_interface_names, #get_item, #get_limits, #get_limits_events, #get_limits_groups, #get_limits_set, #get_limits_sets, #get_oldest_logfile, #get_out_of_limits, #get_overall_limits_state, #get_packet_derived_items, #get_packets, #get_parameter, #get_router, #get_router_names, #get_saved_config, #get_setting, #get_settings, #get_stale, #get_target, #get_target_list, #get_telemetry, #get_tlm_buffer, #get_tlm_cnt, #get_tlm_packet, #get_tlm_values, #inject_tlm, #limits_enabled?, #list_configs, #list_settings, #load_config, #normalize_tlm, #override_tlm, #save_config, #save_setting, #send_raw, #set_limits, #set_limits_set, #set_tlm, #set_tlm_process_args, #start_raw_logging_interface, #start_raw_logging_router, #stop_raw_logging_interface, #stop_raw_logging_router, #subscribe_packets, #tlm, #tlm_formatted, #tlm_process_args, #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.
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 120 121 |
# File 'lib/cosmos/interfaces/tcpip_server_interface.rb', line 73 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 @raw_logger_pair = nil @raw_logging_enabled = false @connection_mutex = Mutex.new @listen_address = "0.0.0.0" @auto_system_meta = false @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
#auto_system_meta ⇒ boolean
Returns Automatically send SYSTEM META on connect - Default false - Can be CMD/TLM.
59 60 61 |
# File 'lib/cosmos/interfaces/tcpip_server_interface.rb', line 59 def @auto_system_meta end |
#listen_address ⇒ String
Returns The ip address to bind to. Default to ANY (0.0.0.0).
57 58 59 |
# File 'lib/cosmos/interfaces/tcpip_server_interface.rb', line 57 def listen_address @listen_address end |
#raw_logger_pair ⇒ RawLoggerPair
Returns RawLoggerPair instance or nil.
55 56 57 |
# File 'lib/cosmos/interfaces/tcpip_server_interface.rb', line 55 def raw_logger_pair @raw_logger_pair 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.
53 54 55 |
# File 'lib/cosmos/interfaces/tcpip_server_interface.rb', line 53 def read_connection_callback @read_connection_callback 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.
50 51 52 |
# File 'lib/cosmos/interfaces/tcpip_server_interface.rb', line 50 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.
125 126 127 128 129 130 131 132 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 |
# File 'lib/cosmos/interfaces/tcpip_server_interface.rb', line 125 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 => err shutdown_interfaces(@write_interface_infos) Logger.error("#{@name}: Tcpip server write thread unexpectedly died") Logger.error(err.formatted) end @write_raw_thread = Thread.new do loop do write_raw_thread_body() break if @cancel_threads end rescue Exception => err shutdown_interfaces(@write_interface_infos) Logger.error("#{@name}: Tcpip server write raw thread unexpectedly died") Logger.error(err.formatted) end else @write_thread = nil @write_raw_thread = nil end @connected = true end |
#connected? ⇒ Boolean
Returns Whether the server is listening for connections.
164 165 166 |
# File 'lib/cosmos/interfaces/tcpip_server_interface.rb', line 164 def connected? @connected end |
#disconnect ⇒ Object
Shutdowns the listener threads for both the read and write ports as well as any client connections.
170 171 172 173 174 175 176 177 178 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 |
# File 'lib/cosmos/interfaces/tcpip_server_interface.rb', line 170 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| Cosmos.kill_thread(self, listen_thread) } @listen_threads.clear # Shutdown listen socket(s) @listen_sockets.each do |listen_socket| Cosmos.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| Cosmos.kill_thread(self, thread) } @read_threads.clear if @write_thread Cosmos.kill_thread(self, @write_thread) @write_thread = nil end if @write_raw_thread Cosmos.kill_thread(self, @write_raw_thread) @write_raw_thread = nil end shutdown_interfaces(@write_interface_infos) @connected = false end |
#graceful_kill ⇒ Object
Gracefully kill all the threads
211 212 213 |
# File 'lib/cosmos/interfaces/tcpip_server_interface.rb', line 211 def graceful_kill # This method is just here to prevent warnings end |
#num_clients ⇒ Integer
Returns The number of connected clients.
261 262 263 264 265 266 |
# File 'lib/cosmos/interfaces/tcpip_server_interface.rb', line 261 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.
217 218 219 220 221 222 223 224 225 226 |
# File 'lib/cosmos/interfaces/tcpip_server_interface.rb', line 217 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.
251 252 253 |
# File 'lib/cosmos/interfaces/tcpip_server_interface.rb', line 251 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 AUTO_SYSTEM_META - Automatically send SYSTEM META on connect - Default false (see Interface#set_option)
284 285 286 287 288 289 290 291 292 |
# File 'lib/cosmos/interfaces/tcpip_server_interface.rb', line 284 def set_option(option_name, option_values) super(option_name, option_values) case option_name.upcase when 'LISTEN_ADDRESS' @listen_address = option_values[0] when 'AUTO_SYSTEM_META' @auto_system_meta = ConfigParser.handle_true_false(option_values[0]) end end |
#start_raw_logging ⇒ Object
Start raw logging for this interface
269 270 271 272 |
# File 'lib/cosmos/interfaces/tcpip_server_interface.rb', line 269 def start_raw_logging @raw_logging_enabled = true change_raw_logging(:start) end |
#stop_raw_logging ⇒ Object
Stop raw logging for this interface
275 276 277 278 |
# File 'lib/cosmos/interfaces/tcpip_server_interface.rb', line 275 def stop_raw_logging @raw_logging_enabled = false change_raw_logging(:stop) end |
#write(packet) ⇒ Object
230 231 232 233 234 235 236 237 |
# File 'lib/cosmos/interfaces/tcpip_server_interface.rb', line 230 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.
256 257 258 |
# File 'lib/cosmos/interfaces/tcpip_server_interface.rb', line 256 def write_queue_size @write_queue ? @write_queue.size : 0 end |
#write_raw(data) ⇒ Object
241 242 243 244 245 246 247 248 |
# File 'lib/cosmos/interfaces/tcpip_server_interface.rb', line 241 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 |