Class: OpenC3::InterfaceMicroservice
- Inherits:
-
Microservice
- Object
- Microservice
- OpenC3::InterfaceMicroservice
- Defined in:
- lib/openc3/microservices/interface_microservice.rb
Direct Known Subclasses
Constant Summary collapse
- UNKNOWN_BYTES_TO_PRINT =
16
Instance Attribute Summary
Attributes inherited from Microservice
#count, #custom, #error, #logger, #microservice_status_thread, #name, #scope, #secrets, #state
Instance Method Summary collapse
-
#attempting(*params) ⇒ Object
Called to connect the interface/router.
- #connect ⇒ Object
- #disconnect(allow_reconnect = true) ⇒ Object
- #graceful_kill ⇒ Object
- #handle_connection_failed(connect_error) ⇒ Object
- #handle_connection_lost(err = nil, reconnect: true) ⇒ Object
- #handle_packet(packet) ⇒ Object
-
#initialize(name) ⇒ InterfaceMicroservice
constructor
A new instance of InterfaceMicroservice.
- #run ⇒ Object
- #shutdown(sig = nil) ⇒ Object
-
#stop ⇒ Object
Disconnect from the interface and stop the thread.
Methods inherited from Microservice
Constructor Details
#initialize(name) ⇒ InterfaceMicroservice
Returns a new instance of InterfaceMicroservice.
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 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 |
# File 'lib/openc3/microservices/interface_microservice.rb', line 348 def initialize(name) @mutex = Mutex.new super(name) if @interface_or_router == 'INTERFACE' @metric.set(name: 'interface_tlm_total', value: @count, type: 'counter') else @metric.set(name: 'router_cmd_total', value: @count, type: 'counter') end @interface_or_router = self.class.name.to_s.split("Microservice")[0].upcase.split("::")[-1] @scope = name.split("__")[0] interface_name = name.split("__")[2] if @interface_or_router == 'INTERFACE' @interface = InterfaceModel.get_model(name: interface_name, scope: @scope).build else @interface = RouterModel.get_model(name: interface_name, scope: @scope).build end @interface.name = interface_name # Map the interface to the interface's targets @interface.target_names.each do |target_name| target = System.targets[target_name] target.interface = @interface end @interface.tlm_target_names.each do |target_name| # Initialize the target's packet counters based on the Topic stream # Prevents packet count resetting to 0 when interface restarts begin System.telemetry.packets(target_name).each do |packet_name, packet| topic = "#{@scope}__TELEMETRY__{#{target_name}}__#{packet_name}" msg_id, msg_hash = Topic.(topic) if msg_id packet.received_count = msg_hash['received_count'].to_i else packet.received_count = 0 end end rescue # Handle targets without telemetry end end if @interface.connect_on_startup @interface.state = 'ATTEMPTING' else @interface.state = 'DISCONNECTED' end if @interface_or_router == 'INTERFACE' InterfaceStatusModel.set(@interface.as_json(:allow_nan => true), scope: @scope) else RouterStatusModel.set(@interface.as_json(:allow_nan => true), scope: @scope) end @interface_thread_sleeper = Sleeper.new @cancel_thread = false @connection_failed_messages = [] @connection_lost_messages = [] if @interface_or_router == 'INTERFACE' @handler_thread = InterfaceCmdHandlerThread.new(@interface, self, logger: @logger, metric: @metric, scope: @scope) else @handler_thread = RouterTlmHandlerThread.new(@interface, self, logger: @logger, metric: @metric, scope: @scope) end @handler_thread.start end |
Instance Method Details
#attempting(*params) ⇒ Object
Called to connect the interface/router. It takes optional parameters to rebuilt the interface/router. Once we set the state to ‘ATTEMPTING’ the run method handles the actual connection.
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 |
# File 'lib/openc3/microservices/interface_microservice.rb', line 414 def attempting(*params) unless params.empty? @interface.disconnect() # Build New Interface, this can fail if passed bad parameters new_interface = @interface.class.new(*params) @interface.copy_to(new_interface) # Replace interface for targets @interface.target_names.each do |target_name| target = System.targets[target_name] target.interface = new_interface end @interface = new_interface end @interface.state = 'ATTEMPTING' if @interface_or_router == 'INTERFACE' InterfaceStatusModel.set(@interface.as_json(:allow_nan => true), scope: @scope) else RouterStatusModel.set(@interface.as_json(:allow_nan => true), scope: @scope) end @interface # Return the interface/router since we may have recreated it # Need to rescue Exception so we cover LoadError rescue Exception => error @logger.error("Attempting connection failed with params #{params} due to #{error.}") if SignalException === error @logger.info "#{@interface.name}: Closing from signal" @cancel_thread = true end @interface # Return the original interface/router in case of error end |
#connect ⇒ Object
614 615 616 617 618 619 620 621 622 623 624 |
# File 'lib/openc3/microservices/interface_microservice.rb', line 614 def connect @logger.info "#{@interface.name}: Connecting ..." @interface.connect @interface.state = 'CONNECTED' if @interface_or_router == 'INTERFACE' InterfaceStatusModel.set(@interface.as_json(:allow_nan => true), scope: @scope) else RouterStatusModel.set(@interface.as_json(:allow_nan => true), scope: @scope) end @logger.info "#{@interface.name}: Connection Success" end |
#disconnect(allow_reconnect = true) ⇒ Object
626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 |
# File 'lib/openc3/microservices/interface_microservice.rb', line 626 def disconnect(allow_reconnect = true) return if @interface.state == 'DISCONNECTED' && !@interface.connected? # Synchronize the calls to @interface.disconnect since it takes an unknown # amount of time. If two calls to disconnect stack up, the if statement # should avoid multiple calls to disconnect. @mutex.synchronize do begin @interface.disconnect if @interface.connected? rescue => e @logger.error "Disconnect: #{@interface.name}: #{e.formatted}" end end # If the interface is set to auto_reconnect then delay so the thread # can come back around and allow the interface a chance to reconnect. if allow_reconnect and @interface.auto_reconnect and @interface.state != 'DISCONNECTED' attempting() if !@cancel_thread # @logger.debug "reconnect delay: #{@interface.reconnect_delay}" @interface_thread_sleeper.sleep(@interface.reconnect_delay) end else @interface.state = 'DISCONNECTED' if @interface_or_router == 'INTERFACE' InterfaceStatusModel.set(@interface.as_json(:allow_nan => true), scope: @scope) else RouterStatusModel.set(@interface.as_json(:allow_nan => true), scope: @scope) end end end |
#graceful_kill ⇒ Object
685 686 687 |
# File 'lib/openc3/microservices/interface_microservice.rb', line 685 def graceful_kill # Just to avoid warning end |
#handle_connection_failed(connect_error) ⇒ Object
568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 |
# File 'lib/openc3/microservices/interface_microservice.rb', line 568 def handle_connection_failed(connect_error) @error = connect_error @logger.error "#{@interface.name}: Connection Failed: #{connect_error.formatted(false, false)}" case connect_error when SignalException @logger.info "#{@interface.name}: Closing from signal" @cancel_thread = true when Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::ETIMEDOUT, Errno::ENOTSOCK, Errno::EHOSTUNREACH, IOError # Do not write an exception file for these extremely common cases else if RuntimeError === connect_error and (connect_error. =~ /canceled/ or connect_error. =~ /timeout/) # Do not write an exception file for these extremely common cases else @logger.error "#{@interface.name}: #{connect_error.formatted}" unless @connection_failed_messages.include?(connect_error.) OpenC3.write_exception_file(connect_error) @connection_failed_messages << connect_error. end end end disconnect() # Ensure we do a clean disconnect end |
#handle_connection_lost(err = nil, reconnect: true) ⇒ Object
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 |
# File 'lib/openc3/microservices/interface_microservice.rb', line 591 def handle_connection_lost(err = nil, reconnect: true) if err @error = err @logger.info "#{@interface.name}: Connection Lost: #{err.formatted(false, false)}" case err when SignalException @logger.info "#{@interface.name}: Closing from signal" @cancel_thread = true when Errno::ECONNABORTED, Errno::ECONNRESET, Errno::ETIMEDOUT, Errno::EBADF, Errno::ENOTSOCK, IOError # Do not write an exception file for these extremely common cases else @logger.error "#{@interface.name}: #{err.formatted}" unless @connection_lost_messages.include?(err.) OpenC3.write_exception_file(err) @connection_lost_messages << err. end end else @logger.info "#{@interface.name}: Connection Lost" end disconnect(reconnect) # Ensure we do a clean disconnect end |
#handle_packet(packet) ⇒ Object
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 |
# File 'lib/openc3/microservices/interface_microservice.rb', line 513 def handle_packet(packet) InterfaceStatusModel.set(@interface.as_json(:allow_nan => true), scope: @scope) packet.received_time = Time.now.sys unless packet.received_time if packet.stored # Stored telemetry does not update the current value table identified_packet = System.telemetry.identify_and_define_packet(packet, @interface.tlm_target_names) else # Identify and update packet if packet.identified? begin # Preidentifed packet - place it into the current value table identified_packet = System.telemetry.update!(packet.target_name, packet.packet_name, packet.buffer) rescue RuntimeError # Packet identified but we don't know about it # Clear packet_name and target_name and try to identify @logger.warn "#{@interface.name}: Received unknown identified telemetry: #{packet.target_name} #{packet.packet_name}" packet.target_name = nil packet.packet_name = nil identified_packet = System.telemetry.identify!(packet.buffer, @interface.tlm_target_names) end else # Packet needs to be identified identified_packet = System.telemetry.identify!(packet.buffer, @interface.tlm_target_names) end end if identified_packet identified_packet.received_time = packet.received_time identified_packet.stored = packet.stored identified_packet.extra = packet.extra packet = identified_packet else unknown_packet = System.telemetry.update!('UNKNOWN', 'UNKNOWN', packet.buffer) unknown_packet.received_time = packet.received_time unknown_packet.stored = packet.stored unknown_packet.extra = packet.extra packet = unknown_packet json_hash = CvtModel.build_json_from_packet(packet) CvtModel.set(json_hash, target_name: packet.target_name, packet_name: packet.packet_name, scope: scope) num_bytes_to_print = [UNKNOWN_BYTES_TO_PRINT, packet.length].min data = packet.buffer(false)[0..(num_bytes_to_print - 1)] prefix = data.each_byte.map { | byte | sprintf("%02X", byte) }.join() @logger.warn "#{@interface.name} #{packet.target_name} packet length: #{packet.length} starting with: #{prefix}" end # Write to stream packet.received_count += 1 TelemetryTopic.write_packet(packet, scope: @scope) end |
#run ⇒ Object
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 |
# File 'lib/openc3/microservices/interface_microservice.rb', line 446 def run begin if @interface.read_allowed? @logger.info "#{@interface.name}: Starting packet reading" else @logger.info "#{@interface.name}: Starting connection maintenance" end while true break if @cancel_thread case @interface.state when 'DISCONNECTED' # Just wait to see if we should connect later @interface_thread_sleeper.sleep(1) when 'ATTEMPTING' begin @mutex.synchronize do # We need to make sure connect is not called after stop() has been called connect() unless @cancel_thread end rescue Exception => connect_error handle_connection_failed(connect_error) break if @cancel_thread end when 'CONNECTED' if @interface.read_allowed? begin packet = @interface.read if packet handle_packet(packet) @count += 1 if @interface_or_router == 'INTERFACE' @metric.set(name: 'interface_tlm_total', value: @count, type: 'counter') else @metric.set(name: 'router_cmd_total', value: @count, type: 'counter') end else @logger.info "#{@interface.name}: Internal disconnect requested (returned nil)" handle_connection_lost() break if @cancel_thread end rescue Exception => err handle_connection_lost(err) break if @cancel_thread end else @interface_thread_sleeper.sleep(1) handle_connection_lost() if !@interface.connected? end end end rescue Exception => error unless SystemExit === error or SignalException === error @logger.error "#{@interface.name}: Packet reading thread died: #{error.formatted}" OpenC3.handle_fatal_exception(error) end # Try to do clean disconnect because we're going down disconnect(false) end if @interface_or_router == 'INTERFACE' InterfaceStatusModel.set(@interface.as_json(:allow_nan => true), scope: @scope) else RouterStatusModel.set(@interface.as_json(:allow_nan => true), scope: @scope) end @logger.info "#{@interface.name}: Stopped packet reading" end |
#shutdown(sig = nil) ⇒ Object
679 680 681 682 683 |
# File 'lib/openc3/microservices/interface_microservice.rb', line 679 def shutdown(sig = nil) @logger.info "#{@interface ? @interface.name : @name}: shutdown requested" stop() super() end |
#stop ⇒ Object
Disconnect from the interface and stop the thread
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 |
# File 'lib/openc3/microservices/interface_microservice.rb', line 659 def stop @logger.info "#{@interface ? @interface.name : @name}: stop requested" @mutex.synchronize do # Need to make sure that @cancel_thread is set and the interface disconnected within # mutex to ensure that connect() is not called when we want to stop() @cancel_thread = true @handler_thread.stop if @handler_thread @interface_thread_sleeper.cancel if @interface_thread_sleeper if @interface @interface.disconnect if @interface_or_router == 'INTERFACE' valid_interface = InterfaceStatusModel.get_model(name: @interface.name, scope: @scope) else valid_interface = RouterStatusModel.get_model(name: @interface.name, scope: @scope) end valid_interface.destroy if valid_interface end end end |