Class: Cosmos::CmdTlmServer
- Includes:
- Api
- Defined in:
- lib/cosmos/tools/cmd_tlm_server/cmd_tlm_server.rb
Overview
Provides the interface for all applications to get the latest telemetry and to send commands.
Direct Known Subclasses
Constant Summary collapse
- DEFAULT_HOST =
The default host
'localhost'
- DEFAULT_CONFIG_FILE =
The default configuration file name
'cmd_tlm_server.txt'
- DEFAULT_LIMITS_EVENT_QUEUE_SIZE =
The maximum number of limits events that are queued. Used when subscribing to limits events.
1000
- DEFAULT_PACKET_DATA_QUEUE_SIZE =
The maximum number of packets that are queued. Used when subscribing to packet data.
1000
- @@instance =
nil
- @@meta_callback =
nil
Constants included from Extract
Extract::SCANNING_REGULAR_EXPRESSION
Instance Attribute Summary collapse
-
#limits_event_queue_mutex ⇒ Mutex
readonly
Synchronization object around limits events.
-
#limits_event_queues ⇒ Hash<Integer, Array<Queue, Integer>>
readonly
The limits event queues hashed by id.
-
#next_limits_event_queue_id ⇒ Integer
The next limits event queue id when subscribe_limits_event is called.
-
#next_packet_data_queue_id ⇒ Integer
The next packet data queue id when subscribe_packet_data is called.
-
#packet_data_queue_mutex ⇒ Mutex
readonly
Synchronization object around packet data events.
-
#packet_data_queues ⇒ Hash<Integer, Array<Queue, Integer>>
readonly
The packet data queues hashed by id.
Class Method Summary collapse
-
.clear_counters ⇒ Object
Calls clear_counters on the System, interfaces, routers, and sets the request_count on json_drb to 0.
-
.get_limits_event(id, non_block = false) ⇒ Object
Get a limits event from the queue created by Api#subscribe_limits_events.
-
.get_packet_data(id, non_block = false) ⇒ Object
Get packet data from the queue created by Api#subscribe_packet_data.
-
.instance ⇒ Object
Get the instance of the CmdTlmServer.
-
.meta_callback=(meta_callback) ⇒ Object
Set the meta callback.
-
.subscribe_limits_events(queue_size = DEFAULT_LIMITS_EVENT_QUEUE_SIZE) ⇒ Integer
Create a queue on the CmdTlmServer that gets populated with every limits event in the system.
-
.subscribe_packet_data(packets, queue_size = CmdTlmServer::DEFAULT_PACKET_DATA_QUEUE_SIZE) ⇒ Integer
Subscribe to one or more telemetry packets.
-
.unsubscribe_limits_events(id) ⇒ Object
Unsubscribe from being notified for every limits event in the system.
-
.unsubscribe_packet_data(id) ⇒ Object
Unsubscribe to telemetry packets.
Instance Method Summary collapse
-
#background_tasks ⇒ BackgroundTasks
Access to the background tasks.
-
#commanding ⇒ Commanding
Allows for sending commands to targets and interfaces.
-
#graceful_kill ⇒ Object
Gracefully kill the staleness monitor thread.
-
#initialize(config_file = DEFAULT_CONFIG_FILE, production = false, disconnect = false, create_message_log = true) ⇒ CmdTlmServer
constructor
Constructor for a CmdTlmServer.
-
#interfaces ⇒ Interfaces
Access to the interfaces.
-
#json_drb ⇒ JsonDRb
Provides access to the server for all tools both internal and external.
-
#limits_change_callback(packet, item, old_limits_state, value, log_change) ⇒ Object
Called when an item in any packet changes limits states.
-
#message_log ⇒ MessageLog
Message log for the CmdTlmServer.
-
#packet_logging ⇒ PacketLogging
Access to the packet loggers.
-
#post_limits_event(event_type, event_data) ⇒ Object
Post a limits event to all subscribed limits event listeners.
-
#post_packet(packet) ⇒ Object
Post packet data to all subscribed packet data listeners.
-
#routers ⇒ Routers
Access to the routers.
-
#start(production = false) ⇒ Object
Start up the system by starting the JSON-RPC server, interfaces, routers, and background tasks.
-
#stop ⇒ Object
Properly shuts down the command and telemetry server by stoping the JSON-RPC server, background tasks, routers, and interfaces.
-
#stop_callback=(stop_callback) ⇒ Object
Set a stop callback.
-
#title ⇒ String
CmdTlmServer title as set in the config file.
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_cmd_time, #get_cmd_value, #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_stale, #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(config_file = DEFAULT_CONFIG_FILE, production = false, disconnect = false, create_message_log = true) ⇒ CmdTlmServer
Constructor for a CmdTlmServer
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 144 145 146 147 |
# File 'lib/cosmos/tools/cmd_tlm_server/cmd_tlm_server.rb', line 108 def initialize(config_file = DEFAULT_CONFIG_FILE, production = false, disconnect = false, = true) @@instance = self @packet_logging = nil # Removes warnings @message_log = MessageLog.new('server') if super() # For Api Logger.info "COSMOS Version: #{COSMOS_VERSION}" @disconnect = disconnect @limits_event_queue_mutex = Mutex.new @limits_event_queues = {} @next_limits_event_queue_id = 1 @packet_data_queue_mutex = Mutex.new @packet_data_queues = {} @next_packet_data_queue_id = 1 # Process cmd_tlm_server.txt @config = CmdTlmServerConfig.new(File.join('config', 'tools', 'cmd_tlm_server', config_file)) @background_tasks = BackgroundTasks.new(@config) @commanding = Commanding.new(@config) @interfaces = Interfaces.new(@config, method(:identified_packet_callback)) @packet_logging = PacketLogging.new(@config) @routers = Routers.new(@config) @title = @config.title @stop_callback = nil # Set Threads to kill CTS if they throw an exception Thread.abort_on_exception = true # Don't start the DRb service or the telemetry monitoring thread # if we started the server in disconnect mode @json_drb = nil start(production) unless @disconnect end |
Instance Attribute Details
#limits_event_queue_mutex ⇒ Mutex (readonly)
Returns Synchronization object around limits events.
53 54 55 |
# File 'lib/cosmos/tools/cmd_tlm_server/cmd_tlm_server.rb', line 53 def limits_event_queue_mutex @limits_event_queue_mutex end |
#limits_event_queues ⇒ Hash<Integer, Array<Queue, Integer>> (readonly)
Returns The limits event queues hashed by id. Returns an array containing the queue followed by the queue size.
57 58 59 |
# File 'lib/cosmos/tools/cmd_tlm_server/cmd_tlm_server.rb', line 57 def limits_event_queues @limits_event_queues end |
#next_limits_event_queue_id ⇒ Integer
Returns The next limits event queue id when subscribe_limits_event is called. This ID must be used in the limits_event_queues hash to access the queue.
61 62 63 |
# File 'lib/cosmos/tools/cmd_tlm_server/cmd_tlm_server.rb', line 61 def next_limits_event_queue_id @next_limits_event_queue_id end |
#next_packet_data_queue_id ⇒ Integer
Returns The next packet data queue id when subscribe_packet_data is called. This ID must be used in the packet_data_queues hash to access the queue.
71 72 73 |
# File 'lib/cosmos/tools/cmd_tlm_server/cmd_tlm_server.rb', line 71 def next_packet_data_queue_id @next_packet_data_queue_id end |
#packet_data_queue_mutex ⇒ Mutex (readonly)
Returns Synchronization object around packet data events.
63 64 65 |
# File 'lib/cosmos/tools/cmd_tlm_server/cmd_tlm_server.rb', line 63 def packet_data_queue_mutex @packet_data_queue_mutex end |
#packet_data_queues ⇒ Hash<Integer, Array<Queue, Integer>> (readonly)
Returns The packet data queues hashed by id. Returns an array containing the queue followed by the queue size.
67 68 69 |
# File 'lib/cosmos/tools/cmd_tlm_server/cmd_tlm_server.rb', line 67 def packet_data_queues @packet_data_queues end |
Class Method Details
.clear_counters ⇒ Object
Calls clear_counters on the System, interfaces, routers, and sets the request_count on json_drb to 0.
494 495 496 497 498 499 |
# File 'lib/cosmos/tools/cmd_tlm_server/cmd_tlm_server.rb', line 494 def self.clear_counters System.clear_counters self.instance.interfaces.clear_counters self.instance.routers.clear_counters self.instance.json_drb.request_count = 0 end |
.get_limits_event(id, non_block = false) ⇒ Object
Get a limits event from the queue created by Api#subscribe_limits_events.
Each limits event consists of an Array with two elements:
The Symbol name of the event and an Array of data
369 370 371 372 373 374 375 376 377 378 379 |
# File 'lib/cosmos/tools/cmd_tlm_server/cmd_tlm_server.rb', line 369 def self.get_limits_event(id, non_block = false) queue = nil @@instance.limits_event_queue_mutex.synchronize do queue, _ = @@instance.limits_event_queues[id] end if queue return queue.pop(non_block) else raise "Limits event queue with id #{id} not found" end end |
.get_packet_data(id, non_block = false) ⇒ Object
Get packet data from the queue created by Api#subscribe_packet_data.
Each packet data consists of an Array with five elements: [buffer, target name, packet name, received time sec, received time us]
480 481 482 483 484 485 486 487 488 489 490 |
# File 'lib/cosmos/tools/cmd_tlm_server/cmd_tlm_server.rb', line 480 def self.get_packet_data(id, non_block = false) queue = nil @@instance.packet_data_queue_mutex.synchronize do queue, _, _ = @@instance.packet_data_queues[id] end if queue return queue.pop(non_block) else raise "Packet data queue with id #{id} not found" end end |
.instance ⇒ Object
Get the instance of the CmdTlmServer
88 89 90 |
# File 'lib/cosmos/tools/cmd_tlm_server/cmd_tlm_server.rb', line 88 def self.instance @@instance end |
.meta_callback=(meta_callback) ⇒ Object
Set the meta callback
93 94 95 |
# File 'lib/cosmos/tools/cmd_tlm_server/cmd_tlm_server.rb', line 93 def self. () @@meta_callback = end |
.subscribe_limits_events(queue_size = DEFAULT_LIMITS_EVENT_QUEUE_SIZE) ⇒ Integer
Create a queue on the CmdTlmServer that gets populated with every limits event in the system. A limits event occurs when a telemetry item with limits changes state. Thus limits events occur on negative transitions (:GREEN to :YELLOW_LOW) and positive transitions (:YELLOW_HIGH to :GREEN).
332 333 334 335 336 337 338 339 340 |
# File 'lib/cosmos/tools/cmd_tlm_server/cmd_tlm_server.rb', line 332 def self.subscribe_limits_events(queue_size = DEFAULT_LIMITS_EVENT_QUEUE_SIZE) id = nil @@instance.limits_event_queue_mutex.synchronize do id = @@instance.next_limits_event_queue_id @@instance.limits_event_queues[id] = [Queue.new, queue_size] @@instance.next_limits_event_queue_id += 1 end return id end |
.subscribe_packet_data(packets, queue_size = CmdTlmServer::DEFAULT_PACKET_DATA_QUEUE_SIZE) ⇒ Integer
Subscribe to one or more telemetry packets.
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 |
# File 'lib/cosmos/tools/cmd_tlm_server/cmd_tlm_server.rb', line 428 def self.subscribe_packet_data(packets, queue_size = CmdTlmServer::DEFAULT_PACKET_DATA_QUEUE_SIZE) if !packets.is_a?(Array) || !packets[0].is_a?(Array) raise ArgumentError, "packets must be nested array: [['TGT','PKT'],...]" end id = nil upcase_packets = [] # Upper case packet names packets.length.times do |index| upcase_packets << [] upcase_packets[index][0] = packets[index][0].upcase upcase_packets[index][1] = packets[index][1].upcase # Get the packet to ensure it exists @@instance.get_tlm_packet(upcase_packets[index][0], upcase_packets[index][1]) end @@instance.packet_data_queue_mutex.synchronize do id = @@instance.next_packet_data_queue_id @@instance.packet_data_queues[id] = [Queue.new, upcase_packets, queue_size] @@instance.next_packet_data_queue_id += 1 end return id end |
.unsubscribe_limits_events(id) ⇒ Object
Unsubscribe from being notified for every limits event in the system. This deletes the queue and further calls to Api#get_limits_event will raise an exception.
348 349 350 351 352 353 354 355 356 |
# File 'lib/cosmos/tools/cmd_tlm_server/cmd_tlm_server.rb', line 348 def self.unsubscribe_limits_events(id) queue = nil @@instance.limits_event_queue_mutex.synchronize do # Remove the queue to stop servicing it. Nil is added to unblock any client threads # that might otherwise be left blocking forever for something on the queue queue, queue_size = @@instance.limits_event_queues.delete(id) queue << nil if queue end end |
.unsubscribe_packet_data(id) ⇒ Object
Unsubscribe to telemetry packets.
459 460 461 462 463 464 465 466 467 |
# File 'lib/cosmos/tools/cmd_tlm_server/cmd_tlm_server.rb', line 459 def self.unsubscribe_packet_data(id) @@instance.packet_data_queue_mutex.synchronize do # Remove the queue to stop servicing it. Nil is added to unblock any client threads # that might otherwise be left blocking forever for something on the queue queue, packets, queue_size = @@instance.packet_data_queues.delete(id) queue << nil if queue end return nil end |
Instance Method Details
#background_tasks ⇒ BackgroundTasks
Returns Access to the background tasks.
31 |
# File 'lib/cosmos/tools/cmd_tlm_server/cmd_tlm_server.rb', line 31 instance_attr_reader :background_tasks |
#commanding ⇒ Commanding
Returns Allows for sending commands to targets and interfaces.
34 |
# File 'lib/cosmos/tools/cmd_tlm_server/cmd_tlm_server.rb', line 34 instance_attr_reader :commanding |
#graceful_kill ⇒ Object
Gracefully kill the staleness monitor thread
240 241 242 |
# File 'lib/cosmos/tools/cmd_tlm_server/cmd_tlm_server.rb', line 240 def graceful_kill @sleeper.cancel end |
#interfaces ⇒ Interfaces
Returns Access to the interfaces.
36 |
# File 'lib/cosmos/tools/cmd_tlm_server/cmd_tlm_server.rb', line 36 instance_attr_reader :interfaces |
#json_drb ⇒ JsonDRb
Returns Provides access to the server for all tools both internal and external.
45 |
# File 'lib/cosmos/tools/cmd_tlm_server/cmd_tlm_server.rb', line 45 instance_attr_accessor :json_drb |
#limits_change_callback(packet, item, old_limits_state, value, log_change) ⇒ Object
Called when an item in any packet changes limits states.
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/cosmos/tools/cmd_tlm_server/cmd_tlm_server.rb', line 252 def limits_change_callback(packet, item, old_limits_state, value, log_change) if log_change # Write to Server Messages that limits state has changed tgt_pkt_item_str = "#{packet.target_name} #{packet.packet_name} #{item.name} = #{value} is" case item.limits.state when :BLUE Logger.info "<B>#{tgt_pkt_item_str} #{item.limits.state}" when :GREEN, :GREEN_LOW, :GREEN_HIGH Logger.info "<G>#{tgt_pkt_item_str} #{item.limits.state}" when :YELLOW, :YELLOW_LOW, :YELLOW_HIGH Logger.warn "<Y>#{tgt_pkt_item_str} #{item.limits.state}" when :RED, :RED_LOW, :RED_HIGH Logger.error "<R>#{tgt_pkt_item_str} #{item.limits.state}" else Logger.error "#{tgt_pkt_item_str} UNKNOWN" end end post_limits_event(:LIMITS_CHANGE, [packet.target_name, packet.packet_name, item.name, old_limits_state, item.limits.state]) if item.limits.response begin item.limits.response.call(packet, item, old_limits_state) rescue Exception => err Logger.error "#{packet.target_name} #{packet.packet_name} #{item.name} Limits Response Exception!" Logger.error "Called with old_state = #{old_limits_state}, new_state = #{item.limits.state}" Logger.error err.formatted end end end |
#message_log ⇒ MessageLog
Returns Message log for the CmdTlmServer.
42 |
# File 'lib/cosmos/tools/cmd_tlm_server/cmd_tlm_server.rb', line 42 instance_attr_reader :message_log |
#packet_logging ⇒ PacketLogging
Returns Access to the packet loggers.
38 |
# File 'lib/cosmos/tools/cmd_tlm_server/cmd_tlm_server.rb', line 38 instance_attr_reader :packet_logging |
#post_limits_event(event_type, event_data) ⇒ Object
Post a limits event to all subscribed limits event listeners.
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 |
# File 'lib/cosmos/tools/cmd_tlm_server/cmd_tlm_server.rb', line 295 def post_limits_event(event_type, event_data) if @limits_event_queues.length > 0 queues_to_drop = [] @limits_event_queue_mutex.synchronize do # Post event to active queues @limits_event_queues.each do |id, data| queue = data[0] queue_size = data[1] queue << [event_type, event_data] if queue.length > queue_size # Drop queue queues_to_drop << id end end # Drop queues which are not being serviced queues_to_drop.each do |id| # Remove the queue to stop servicing it. Nil is added to unblock any client threads # that might otherwise be left blocking forever for something on the queue queue, queue_size = @limits_event_queues.delete(id) queue << nil if queue end end end end |
#post_packet(packet) ⇒ Object
Post packet data to all subscribed packet data listeners.
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 |
# File 'lib/cosmos/tools/cmd_tlm_server/cmd_tlm_server.rb', line 384 def post_packet(packet) if @packet_data_queues.length > 0 queues_to_drop = [] @packet_data_queue_mutex.synchronize do # Post event to active queues @packet_data_queues.each do |id, data| queue = data[0] packets = data[1] queue_size = data[2] packets.each do |target_name, packet_name| if packet.target_name == target_name and packet.packet_name == packet_name received_time = packet.received_time received_time ||= Time.now queue << [packet.buffer, target_name, packet_name, received_time.tv_sec, received_time.tv_usec, packet.received_count] if queue.length > queue_size # Drop queue queues_to_drop << id end break end end end # Drop queues which are not being serviced queues_to_drop.each do |id| # Remove the queue to stop servicing it. Nil is added to unblock any client threads # that might otherwise be left blocking forever for something on the queue queue, packets, queue_size = @packet_data_queues.delete(id) queue << nil if queue end end end end |
#routers ⇒ Routers
Returns Access to the routers.
40 |
# File 'lib/cosmos/tools/cmd_tlm_server/cmd_tlm_server.rb', line 40 instance_attr_reader :routers |
#start(production = false) ⇒ Object
Start up the system by starting the JSON-RPC server, interfaces, routers, and background tasks. Starts a thread to monitor all packets for staleness so other tools (such as Packet Viewer or Telemetry Viewer) can react accordingly.
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 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 209 210 211 212 |
# File 'lib/cosmos/tools/cmd_tlm_server/cmd_tlm_server.rb', line 155 def start(production = false) System.telemetry # Make sure definitions are loaded by starting anything return unless @json_drb.nil? @@meta_callback.call(@config., @config.) if @@meta_callback if @config. and @config. # Start DRb with access control @json_drb = JsonDRb.new @json_drb.acl = System.acl if System.acl # In production we start logging and don't allow the user to stop it # We also disallow setting telemetry and disconnecting from interfaces if production @packet_logging.start @api_whitelist.delete('stop_logging') @api_whitelist.delete('stop_cmd_log') @api_whitelist.delete('stop_tlm_log') @interfaces.all.each do |name, interface| interface.disable_disconnect = true end @routers.all.each do |name, interface| interface.disable_disconnect = true end end @json_drb.method_whitelist = @api_whitelist begin @json_drb.start_service("localhost", System.ports['CTS_API'], self) rescue Exception # Call packet_logging shutdown here to explicitly kill the logging # threads since this CTS is not going to launch @packet_logging.shutdown raise FatalError.new("Error starting JsonDRb on port #{System.ports['CTS_API']}.\nPerhaps a Command and Telemetry Server is already running?") end @routers.add_preidentified('PREIDENTIFIED_ROUTER', System.instance.ports['CTS_PREIDENTIFIED']) System.telemetry.limits_change_callback = method(:limits_change_callback) @interfaces.start @routers.start @background_tasks.start # Start staleness monitor thread @sleeper = Sleeper.new @staleness_monitor_thread = Thread.new do begin while true stale = System.telemetry.check_stale stale.each do |packet| post_limits_event(:STALE_PACKET, [packet.target_name, packet.packet_name]) end broken = @sleeper.sleep(10) break if broken end rescue Exception => err Logger.fatal "Staleness Monitor thread unexpectedly died" Cosmos.handle_fatal_exception(err) end end # end Thread.new end |
#stop ⇒ Object
Properly shuts down the command and telemetry server by stoping the JSON-RPC server, background tasks, routers, and interfaces. Also kills the packet staleness monitor thread.
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/cosmos/tools/cmd_tlm_server/cmd_tlm_server.rb', line 217 def stop # Shutdown DRb @json_drb.stop_service # Shutdown staleness monitor thread Cosmos.kill_thread(self, @staleness_monitor_thread) @background_tasks.stop @routers.stop @interfaces.stop @packet_logging.shutdown @stop_callback.call if @stop_callback @message_log.stop if @message_log @json_drb = nil end |
#stop_callback=(stop_callback) ⇒ Object
Set a stop callback
235 236 237 |
# File 'lib/cosmos/tools/cmd_tlm_server/cmd_tlm_server.rb', line 235 def stop_callback= (stop_callback) @stop_callback = stop_callback end |
#title ⇒ String
Returns CmdTlmServer title as set in the config file.
47 |
# File 'lib/cosmos/tools/cmd_tlm_server/cmd_tlm_server.rb', line 47 instance_attr_accessor :title |