Class: OpenC3::UdpInterface
- Defined in:
- lib/openc3/interfaces/udp_interface.rb
Overview
Base class for interfaces that send and receive messages over UDP
Constant Summary collapse
- HOST_127_0_0_1 =
'127.0.0.1'- HOST_0_0_0_0 =
'0.0.0.0'
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
Attributes inherited from Interface
#auto_reconnect, #bytes_read, #bytes_written, #cmd_target_enabled, #cmd_target_names, #config_params, #connect_on_startup, #disable_disconnect, #interfaces, #name, #num_clients, #options, #protocol_info, #read_count, #read_protocols, #read_queue_size, #read_raw_data, #read_raw_data_time, #reconnect_delay, #routers, #save_raw_data, #scheduler, #secrets, #state, #stream_log_pair, #target_names, #tlm_target_enabled, #tlm_target_names, #write_count, #write_protocols, #write_queue_size, #written_raw_data, #written_raw_data_time
Instance Method Summary collapse
-
#connect ⇒ Object
Creates a new UdpWriteSocket if the the write_dest_port was given in the constructor and a new UdpReadSocket if the read_port was given in the constructor.
-
#connected? ⇒ Boolean
Whether the active ports (read and/or write) have created sockets.
- #connection_string ⇒ Object
- #details ⇒ Object
-
#disconnect ⇒ Object
Close the active ports (read and/or write) and set the sockets to nil.
-
#initialize(hostname, write_dest_port, read_port, write_src_port = nil, interface_address = nil, ttl = 128, write_timeout = 10.0, read_timeout = nil, bind_address = '0.0.0.0') ⇒ UdpInterface
constructor
A new instance of UdpInterface.
- #read ⇒ Object
-
#read_interface ⇒ Object
Reads from the socket if the read_port is defined.
-
#write_interface(data, extra = nil) ⇒ Object
Writes to the socket.
Methods inherited from Interface
#_write, #add_protocol, #as_json, #convert_data_to_packet, #convert_packet_to_data, #copy_to, #interface_cmd, #post_connect, #protocol_cmd, #read_allowed?, #read_interface_base, #set_option, #start_raw_logging, #stop_raw_logging, #write, #write_allowed?, #write_interface_base, #write_raw, #write_raw_allowed?
Methods included from Api
#_cal_to_epoch, #_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, #commit_timeline_activity, #config_tool_names, #connect_interface, #connect_router, #count_timeline_activities, #create_timeline, #create_timeline_activity, #delete_config, #delete_limits_set, #delete_timeline, #delete_timeline_activity, #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_tlm, #get_all_tlm_item_names, #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_timeline, #get_timeline_activities, #get_timeline_activity, #get_tlm, #get_tlm_available, #get_tlm_buffer, #get_tlm_cnt, #get_tlm_cnts, #get_tlm_packet, #get_tlm_values, #inject_tlm, #interface_cmd, #interface_details, #interface_protocol_cmd, #interface_target_disable, #interface_target_enable, #limits_enabled?, #list_configs, #list_settings, #list_timelines, #load_config, #map_target_to_interface, #map_target_to_router, #normalize_tlm, #offline_access_needed, #override_tlm, #router_cmd, #router_details, #router_protocol_cmd, #router_target_disable, #router_target_enable, #save_config, #send_raw, #set_limits, #set_limits_set, #set_offline_access, #set_setting, #set_state_color, #set_timeline_color, #set_timeline_execute, #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_with_units, #unmap_target_from_interface, #unmap_target_from_router, #update_news, #update_plugin_store, #update_timeline_activity
Methods included from CmdLog
Constructor Details
#initialize(hostname, write_dest_port, read_port, write_src_port = nil, interface_address = nil, ttl = 128, write_timeout = 10.0, read_timeout = nil, bind_address = '0.0.0.0') ⇒ UdpInterface
Returns a new instance of UdpInterface.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/openc3/interfaces/udp_interface.rb', line 41 def initialize( hostname, write_dest_port, read_port, write_src_port = nil, interface_address = nil, ttl = 128, # default for Windows write_timeout = 10.0, read_timeout = nil, bind_address = '0.0.0.0' ) super() @hostname = ConfigParser.handle_nil(hostname) if @hostname @hostname = @hostname.to_s @hostname = HOST_127_0_0_1 if @hostname.casecmp('LOCALHOST').zero? end @write_dest_port = ConfigParser.handle_nil(write_dest_port) @write_dest_port = write_dest_port.to_i if @write_dest_port @read_port = ConfigParser.handle_nil(read_port) @read_port = read_port.to_i if @read_port @write_src_port = ConfigParser.handle_nil(write_src_port) @write_src_port = @write_src_port.to_i if @write_src_port @interface_address = ConfigParser.handle_nil(interface_address) if @interface_address && @interface_address.casecmp('LOCALHOST').zero? @interface_address = HOST_127_0_0_1 end @ttl = ttl.to_i @ttl = 1 if @ttl < 1 @write_timeout = ConfigParser.handle_nil(write_timeout) if @write_timeout @write_timeout = @write_timeout.to_f else Logger.instance.warn("Warning: To avoid interface lock, write_timeout can not be nil. Setting to 10 seconds.") @write_timeout = 10.0 end @read_timeout = ConfigParser.handle_nil(read_timeout) @read_timeout = @read_timeout.to_f if @read_timeout @bind_address = ConfigParser.handle_nil(bind_address) if @bind_address @bind_address = HOST_127_0_0_1 if @bind_address.casecmp('LOCALHOST').zero? else # nil means all local addresses which is what 0.0.0.0 means @bind_address = HOST_0_0_0_0 end @write_socket = nil @read_socket = nil @read_allowed = false unless @read_port @write_allowed = false unless @write_dest_port @write_raw_allowed = false unless @write_dest_port end |
Instance Method Details
#connect ⇒ Object
Creates a new OpenC3::UdpWriteSocket if the the write_dest_port was given in the constructor and a new OpenC3::UdpReadSocket if the read_port was given in the constructor.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/openc3/interfaces/udp_interface.rb', line 107 def connect if @read_port and @write_dest_port and @write_src_port and (@read_port == @write_src_port) # read_multicast is false because this socket is not connected and thus # doesn't filter by peer address. Joining the multicast group we write to # would deliver our own commands back to us as telemetry (multicast # loopback is enabled by default). @read_socket = UdpReadWriteSocket.new( @read_port, @bind_address, @write_dest_port, @hostname, @interface_address, @ttl, false, # read_multicast true, # write_multicast false # connect_socket ) @write_socket = @read_socket else @read_socket = UdpReadSocket.new( @read_port, @hostname, @interface_address, @bind_address ) if @read_port @write_socket = UdpWriteSocket.new( @hostname, @write_dest_port, @write_src_port, @interface_address, @ttl, @bind_address ) if @write_dest_port end @thread_sleeper = nil super() end |
#connected? ⇒ Boolean
Returns Whether the active ports (read and/or write) have created sockets. Since UDP is connectionless, creation of the sockets is used to determine connection.
148 149 150 151 152 153 154 155 156 |
# File 'lib/openc3/interfaces/udp_interface.rb', line 148 def connected? if @write_dest_port && @read_port (@write_socket && @read_socket) ? true : false elsif @write_dest_port @write_socket ? true : false else @read_socket ? true : false end end |
#connection_string ⇒ Object
94 95 96 97 98 99 100 101 102 |
# File 'lib/openc3/interfaces/udp_interface.rb', line 94 def connection_string result = '' result += " #{@hostname}:#{@write_dest_port} (write dest port)" if @write_dest_port result += " #{@write_src_port} (write src port)" if @write_src_port result += " #{@bind_address}:#{@read_port} (read)" if @read_port result += " #{@interface_address} (interface addr)" if @interface_address result += " #{@bind_address} (bind addr)" if @bind_address != HOST_0_0_0_0 return result.strip end |
#details ⇒ Object
199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/openc3/interfaces/udp_interface.rb', line 199 def details result = super() result['hostname'] = @hostname result['write_dest_port'] = @write_dest_port result['read_port'] = @read_port result['write_src_port'] = @write_src_port result['interface_address'] = @interface_address result['ttl'] = @ttl result['write_timeout'] = @write_timeout result['read_timeout'] = @read_timeout result['bind_address'] = @bind_address return result end |
#disconnect ⇒ Object
Close the active ports (read and/or write) and set the sockets to nil.
159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/openc3/interfaces/udp_interface.rb', line 159 def disconnect if @write_socket != @read_socket OpenC3.close_socket(@write_socket) end OpenC3.close_socket(@read_socket) @write_socket = nil @read_socket = nil @thread_sleeper.cancel if @thread_sleeper @thread_sleeper = nil super() end |
#read ⇒ Object
171 172 173 174 175 176 177 178 |
# File 'lib/openc3/interfaces/udp_interface.rb', line 171 def read return super() if @read_port # Write only interface so stop the thread which calls read @thread_sleeper = Sleeper.new @thread_sleeper.sleep(1_000_000_000) while connected? return nil end |
#read_interface ⇒ Object
Reads from the socket if the read_port is defined
181 182 183 184 185 186 187 188 189 |
# File 'lib/openc3/interfaces/udp_interface.rb', line 181 def read_interface data = @read_socket.read(@read_timeout) Logger.info "#{@name}: Udp read returned 0 bytes (stream closed)" if data.length <= 0 extra = nil read_interface_base(data, extra) return data, extra rescue IOError # Disconnected return nil end |
#write_interface(data, extra = nil) ⇒ Object
Writes to the socket
193 194 195 196 197 |
# File 'lib/openc3/interfaces/udp_interface.rb', line 193 def write_interface(data, extra = nil) write_interface_base(data, extra) @write_socket.write(data, @write_timeout) return data, extra end |