Class: Cosmos::UdpInterface
- Defined in:
- lib/cosmos/interfaces/udp_interface.rb
Overview
Base class for interfaces that send and receive messages over UDP
Constant Summary
Constants included from Extract
Extract::SCANNING_REGULAR_EXPRESSION
Instance Attribute Summary
Attributes inherited from Interface
#auto_reconnect, #bytes_read, #bytes_written, #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
-
#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.
-
#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) ⇒ UdpInterface
constructor
A new instance of UdpInterface.
-
#read ⇒ Packet
If the read port was given, the read_socket is read and the data returned in a Packet.
-
#write(packet) ⇒ Object
If the write_dest_port was given, the write_socket is written with the packet data.
-
#write_raw(data) ⇒ Object
If the write_dest_port was given, the write_socket is written with the data.
Methods inherited from Interface
#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(hostname, write_dest_port, read_port, write_src_port = nil, interface_address = nil, ttl = 128, write_timeout = 10.0, read_timeout = nil) ⇒ UdpInterface
Returns a new instance of UdpInterface.
31 32 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 61 62 63 64 65 |
# File 'lib/cosmos/interfaces/udp_interface.rb', line 31 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) super() @hostname = ConfigParser.handle_nil(hostname) if @hostname @hostname = @hostname.to_s @hostname = '127.0.0.1' if @hostname.upcase == 'LOCALHOST' 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) @interface_address = '127.0.0.1' if @interface_address and @interface_address.upcase == 'LOCALHOST' @ttl = ttl.to_i @ttl = 1 if @ttl < 1 @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 @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 Cosmos::UdpWriteSocket if the the write_dest_port was given in the constructor and a new Cosmos::UdpReadSocket if the read_port was given in the constructor.
70 71 72 73 74 75 76 77 |
# File 'lib/cosmos/interfaces/udp_interface.rb', line 70 def connect @write_socket = UdpWriteSocket.new(@hostname, @write_dest_port, @write_src_port, @interface_address, @ttl) if @write_dest_port @read_socket = UdpReadSocket.new(@read_port,@hostname,@interface_address) if @read_port 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.
82 83 84 85 86 87 88 89 90 |
# File 'lib/cosmos/interfaces/udp_interface.rb', line 82 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 |
#disconnect ⇒ Object
Close the active ports (read and/or write) and set the sockets to nil.
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/cosmos/interfaces/udp_interface.rb', line 93 def disconnect begin if @write_socket @write_socket.close unless @write_socket.closed? @write_socket = nil end rescue IOError @write_socket = nil end begin if @read_socket @read_socket.close unless @read_socket.closed? @read_socket = nil end rescue IOError @read_socket = nil end end |
#read ⇒ Packet
If the read port was given, the read_socket is read and the data returned in a Packet. bytes_read and read_count are updated.
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/cosmos/interfaces/udp_interface.rb', line 116 def read if @read_port begin data = @read_socket.read(@read_timeout) @raw_logger_pair.read_logger.write(data) if @raw_logger_pair rescue IOError # Disconnected Thread.stop end @bytes_read += data.length @read_count += 1 return Packet.new(nil, nil, :BIG_ENDIAN, nil, data) else # Write only interface so stop the thread which calls read Thread.stop end end |
#write(packet) ⇒ Object
If the write_dest_port was given, the write_socket is written with the packet data. bytes_written and write_count are updated.
140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/cosmos/interfaces/udp_interface.rb', line 140 def write(packet) if @write_dest_port if connected?() write_raw(packet.buffer) else raise "Interface not connected" end else raise "Attempt to write to read only interface" end end |
#write_raw(data) ⇒ Object
If the write_dest_port was given, the write_socket is written with the data. bytes_written and write_count are updated.
156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/cosmos/interfaces/udp_interface.rb', line 156 def write_raw(data) if @write_dest_port if connected?() @write_socket.write(data, @write_timeout) @bytes_written += data.length @write_count += 1 @raw_logger_pair.write_logger.write(data) if @raw_logger_pair else raise "Interface not connected" end else raise "Attempt to write to read only interface" end end |