Class: Cosmos::UdpInterface

Inherits:
Interface show all
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 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_routers, #connect_on_startup, #disable_disconnect, #interfaces, #name, #num_clients, #options, #override_tlm, #packet_log_writer_pairs, #protocol_info, #raw_logger_pair, #read_count, #read_protocols, #read_queue_size, #read_raw_data, #read_raw_data_time, #reconnect_delay, #routers, #stored_packet_log_writer_pairs, #target_names, #thread, #write_count, #write_protocols, #write_queue_size, #written_raw_data, #written_raw_data_time

Instance Method Summary collapse

Methods inherited from Interface

#_normalize_tlm, #_override, #_override_tlm, #_override_tlm_raw, #_write, #add_protocol, #convert_data_to_packet, #convert_packet_to_data, #copy_to, #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

#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, #cmd_tlm_clear_counters, #cmd_tlm_reload, #connect_interface, #connect_router, #disable_limits, #disable_limits_group, #disconnect_interface, #disconnect_router, #enable_limits, #enable_limits_group, #get_all_cmd_info, #get_all_interface_info, #get_all_packet_logger_info, #get_all_router_info, #get_all_target_info, #get_all_tlm_info, #get_background_tasks, #get_cmd_buffer, #get_cmd_cnt, #get_cmd_hazardous, #get_cmd_list, #get_cmd_log_filename, #get_cmd_param_list, #get_cmd_time, #get_cmd_value, #get_interface_info, #get_interface_names, #get_interface_targets, #get_limits, #get_limits_event, #get_limits_groups, #get_limits_set, #get_limits_sets, #get_out_of_limits, #get_output_logs_filenames, #get_overall_limits_state, #get_packet, #get_packet_data, #get_packet_logger_info, #get_packet_loggers, #get_router_info, #get_router_names, #get_saved_config, #get_screen_definition, #get_screen_list, #get_server_message, #get_server_message_log_filename, #get_server_status, #get_stale, #get_target_ignored_items, #get_target_ignored_parameters, #get_target_info, #get_target_list, #get_tlm_buffer, #get_tlm_cnt, #get_tlm_details, #get_tlm_item_list, #get_tlm_list, #get_tlm_log_filename, #get_tlm_packet, #get_tlm_values, #inject_tlm, #interface_state, #limits_enabled?, #map_target_to_interface, #normalize_tlm, #override_tlm, #override_tlm_raw, #replay_move_end, #replay_move_index, #replay_move_start, #replay_play, #replay_reverse_play, #replay_select_file, #replay_set_playback_delay, #replay_status, #replay_step_back, #replay_step_forward, #replay_stop, #router_state, #send_raw, #set_limits, #set_limits_set, #set_tlm, #set_tlm_raw, #start_background_task, #start_cmd_log, #start_logging, #start_new_server_message_log, #start_raw_logging_interface, #start_raw_logging_router, #start_tlm_log, #stop_background_task, #stop_cmd_log, #stop_logging, #stop_raw_logging_interface, #stop_raw_logging_router, #stop_tlm_log, #subscribe_limits_events, #subscribe_packet_data, #subscribe_server_messages, #tlm, #tlm_formatted, #tlm_raw, #tlm_variable, #tlm_with_units, #unsubscribe_limits_events, #unsubscribe_packet_data, #unsubscribe_server_messages

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.

Parameters:

  • hostname (String)

    Machine to connect to

  • write_dest_port (Integer)

    Port to write commands to

  • read_port (Integer)

    Port to read telemetry from

  • write_src_port (Integer) (defaults to: nil)

    Port to allow replies if needed

  • interface_address (String) (defaults to: nil)

    If the destination machine represented by hostname supports multicast, then interface_address is used to configure the outgoing multicast address.

  • ttl (Integer) (defaults to: 128)

    Time To Live value. The number of intermediate routers allowed before dropping the packet.

  • write_timeout (Integer) (defaults to: 10.0)

    Seconds to wait before aborting writes

  • read_timeout (Integer) (defaults to: nil)

    Seconds to wait before aborting reads

  • bind_address (String) (defaults to: '0.0.0.0')

    Address to bind UDP ports to



30
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
66
67
68
69
70
71
72
# File 'lib/cosmos/interfaces/udp_interface.rb', line 30

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 = '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 = '127.0.0.1'
  end
  @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
  @bind_address = ConfigParser.handle_nil(bind_address)
  if @bind_address && @bind_address.casecmp('LOCALHOST').zero?
    @bind_address = '127.0.0.1'
  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

#connectObject

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.



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
# File 'lib/cosmos/interfaces/udp_interface.rb', line 77

def connect
  if @read_port and @write_dest_port and @write_src_port and (@read_port == @write_src_port)
    @read_socket = UdpReadWriteSocket.new(
      @read_port,
      @bind_address,
      @write_dest_port,
      @hostname,
      @interface_address,
      @ttl)
    @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
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.

Returns:

  • (Boolean)

    Whether the active ports (read and/or write) have created sockets. Since UDP is connectionless, creation of the sockets is used to determine connection.



107
108
109
110
111
112
113
114
115
# File 'lib/cosmos/interfaces/udp_interface.rb', line 107

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

#disconnectObject

Close the active ports (read and/or write) and set the sockets to nil.



118
119
120
121
122
123
124
125
126
127
# File 'lib/cosmos/interfaces/udp_interface.rb', line 118

def disconnect
  if @write_socket != @read_socket
    Cosmos.close_socket(@write_socket)
  end
  Cosmos.close_socket(@read_socket)
  @write_socket = nil
  @read_socket = nil
  @thread_sleeper.cancel if @thread_sleeper
  @thread_sleeper = nil
end

#readObject



129
130
131
132
133
134
135
# File 'lib/cosmos/interfaces/udp_interface.rb', line 129

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_interfaceObject

Reads from the socket if the read_port is defined



138
139
140
141
142
143
144
# File 'lib/cosmos/interfaces/udp_interface.rb', line 138

def read_interface
  data = @read_socket.read(@read_timeout)
  read_interface_base(data)
  return data
rescue IOError # Disconnected
  return nil
end

#write_interface(data) ⇒ Object

Writes to the socket

Parameters:

  • data (String)

    Raw packet data



148
149
150
151
152
# File 'lib/cosmos/interfaces/udp_interface.rb', line 148

def write_interface(data)
  write_interface_base(data)
  @write_socket.write(data, @write_timeout)
  data
end