Class: OpenC3::InterfaceMicroservice
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
#as_json, #microservice_cmd, run, #setup_microservice_topic
Constructor Details
Returns a new instance of InterfaceMicroservice.
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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
|
# File 'lib/openc3/microservices/interface_microservice.rb', line 459
def initialize(name)
@mutex = Mutex.new
super(name)
@interface_or_router = self.class.name.to_s.split("Microservice")[0].upcase.split("::")[-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
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
@interface.target_names.each do |target_name|
target = System.targets[target_name]
target.interface = @interface
end
@interface.tlm_target_names.each do |target_name|
begin
System.telemetry.packets(target_name).each do |packet_name, packet|
topic = "#{@scope}__TELEMETRY__{#{target_name}}__#{packet_name}"
msg_id, msg_hash = Topic.get_newest_message(topic)
if msg_id
packet.received_count = msg_hash['received_count'].to_i
else
packet.received_count = 0
end
end
rescue
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
@queued = false
@sync_packet_count_data = {}
@sync_packet_count_time = nil
@sync_packet_count_delay_seconds = 1.0 @interface.options.each do |option_name, option_values|
if option_name.upcase == 'OPTIMIZE_THROUGHPUT'
@queued = true
update_interval = option_values[0].to_f
EphemeralStoreQueued.instance.set_update_interval(update_interval)
StoreQueued.instance.set_update_interval(update_interval)
end
if option_name.upcase == 'SYNC_PACKET_COUNT_DELAY_SECONDS'
@sync_packet_count_delay_seconds = option_values[0].to_f
end
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.
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
567
568
569
570
571
|
# File 'lib/openc3/microservices/interface_microservice.rb', line 541
def attempting(*params)
unless params.empty?
@interface.disconnect()
new_interface = @interface.class.new(*params)
@interface.copy_to(new_interface)
@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), queued: true, scope: @scope)
else
RouterStatusModel.set(@interface.as_json(:allow_nan => true), queued: true, scope: @scope)
end
@interface rescue Exception => e
@logger.error("Attempting connection #{@interface.connection_string} failed due to #{e.message}")
if SignalException === e
@logger.info "#{@interface.name}: Closing from signal"
@cancel_thread = true
end
@interface end
|
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
|
# File 'lib/openc3/microservices/interface_microservice.rb', line 741
def connect
@logger.info "#{@interface.name}: Connect #{@interface.connection_string}"
begin
@interface.connect
@interface.post_connect
rescue Exception => e
begin
@interface.disconnect rescue Exception
end
raise e
end
@interface.state = 'CONNECTED'
if @interface_or_router == 'INTERFACE'
InterfaceStatusModel.set(@interface.as_json(:allow_nan => true), queued: true, scope: @scope)
else
RouterStatusModel.set(@interface.as_json(:allow_nan => true), queued: true, scope: @scope)
end
@logger.info "#{@interface.name}: Connection Success"
end
|
#disconnect(allow_reconnect = true) ⇒ Object
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
|
# File 'lib/openc3/microservices/interface_microservice.rb', line 763
def disconnect(allow_reconnect = true)
return if @interface.state == 'DISCONNECTED' && !@interface.connected?
@mutex.synchronize do
begin
@interface.disconnect if @interface.connected?
rescue => e
@logger.error "Disconnect: #{@interface.name}: #{e.formatted}"
end
end
if allow_reconnect and @interface.auto_reconnect and @interface.state != 'DISCONNECTED'
attempting()
if !@cancel_thread
@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), queued: true, scope: @scope)
else
RouterStatusModel.set(@interface.as_json(:allow_nan => true), queued: true, scope: @scope)
end
end
end
|
#graceful_kill ⇒ Object
822
823
824
|
# File 'lib/openc3/microservices/interface_microservice.rb', line 822
def graceful_kill
end
|
#handle_connection_failed(connection, connect_error) ⇒ Object
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
|
# File 'lib/openc3/microservices/interface_microservice.rb', line 695
def handle_connection_failed(connection, connect_error)
@error = connect_error
@logger.error "#{@interface.name}: Connection #{connection} failed due to #{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
else
if RuntimeError === connect_error and (connect_error.message =~ /canceled/ or connect_error.message =~ /timeout/)
else
@logger.error "#{@interface.name}: #{connect_error.formatted}"
unless @connection_failed_messages.include?(connect_error.message)
OpenC3.write_exception_file(connect_error)
@connection_failed_messages << connect_error.message
end
end
end
disconnect() end
|
#handle_connection_lost(err = nil, reconnect: true) ⇒ Object
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
|
# File 'lib/openc3/microservices/interface_microservice.rb', line 718
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
else
@logger.error "#{@interface.name}: #{err.formatted}"
unless @connection_lost_messages.include?(err.message)
OpenC3.write_exception_file(err)
@connection_lost_messages << err.message
end
end
else
@logger.info "#{@interface.name}: Connection Lost"
end
disconnect(reconnect) end
|
#handle_packet(packet) ⇒ Object
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
|
# File 'lib/openc3/microservices/interface_microservice.rb', line 640
def handle_packet(packet)
InterfaceStatusModel.set(@interface.as_json(:allow_nan => true), queued: true, scope: @scope)
packet.received_time = Time.now.sys unless packet.received_time
if packet.stored
identified_packet = System.telemetry.identify_and_define_packet(packet, @interface.tlm_target_names)
else
if packet.identified?
begin
identified_packet = System.telemetry.update!(packet.target_name,
packet.packet_name,
packet.buffer)
rescue RuntimeError
@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
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. = packet.
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. = packet.
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, queued: @queued, 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
sync_tlm_packet_counts(packet)
TelemetryTopic.write_packet(packet, queued: @queued, scope: @scope)
end
|
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
|
# File 'lib/openc3/microservices/interface_microservice.rb', line 573
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'
@interface_thread_sleeper.sleep(1)
when 'ATTEMPTING'
begin
@mutex.synchronize do
connect() unless @cancel_thread
end
rescue Exception => e
handle_connection_failed(@interface.connection_string, e)
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 => e
handle_connection_lost(e)
break if @cancel_thread
end
else
@interface_thread_sleeper.sleep(1)
handle_connection_lost() if !@interface.connected?
end
end
end
rescue Exception => e
unless SystemExit === e or SignalException === e
@logger.error "#{@interface.name}: Packet reading thread died: #{e.formatted}"
OpenC3.handle_fatal_exception(e)
end
disconnect(false)
end
if @interface_or_router == 'INTERFACE'
InterfaceStatusModel.set(@interface.as_json(:allow_nan => true), queued: true, scope: @scope)
else
RouterStatusModel.set(@interface.as_json(:allow_nan => true), queued: true, scope: @scope)
end
@logger.info "#{@interface.name}: Stopped packet reading"
end
|
#shutdown(_sig = nil) ⇒ Object
816
817
818
819
820
|
# File 'lib/openc3/microservices/interface_microservice.rb', line 816
def shutdown(_sig = nil)
@logger.info "#{@interface ? @interface.name : @name}: shutdown requested"
stop()
super()
end
|
Disconnect from the interface and stop the thread
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
|
# File 'lib/openc3/microservices/interface_microservice.rb', line 796
def stop
@logger.info "#{@interface ? @interface.name : @name}: stop requested"
@mutex.synchronize do
@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
|
#sync_tlm_packet_counts(packet) ⇒ Object
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
|
# File 'lib/openc3/microservices/interface_microservice.rb', line 826
def sync_tlm_packet_counts(packet)
if @sync_packet_count_delay_seconds <= 0
packet.received_count = TargetModel.increment_telemetry_count(packet.target_name, packet.packet_name, 1, scope: @scope)
else
@sync_packet_count_data[packet.target_name] ||= {}
@sync_packet_count_data[packet.target_name][packet.packet_name] ||= 0
@sync_packet_count_data[packet.target_name][packet.packet_name] += 1
packet.received_count += 1
if @sync_packet_count_time.nil? or (Time.now - @sync_packet_count_time) > @sync_packet_count_delay_seconds
@sync_packet_count_time = Time.now
inc_count = 0
result = Store.redis_pool.pipelined do
@sync_packet_count_data.each do |target_name, packet_data|
packet_data.each do |packet_name, count|
TargetModel.increment_telemetry_count(target_name, packet_name, count, scope: @scope)
inc_count += 1
end
end
@sync_packet_count_data = {}
@interface.tlm_target_names.each do |target_name|
TargetModel.get_all_telemetry_counts(target_name, scope: @scope)
end
TargetModel.get_all_telemetry_counts('UNKNOWN', scope: @scope)
end
@interface.tlm_target_names.each do |target_name|
result[inc_count].each do |packet_name, count|
update_packet = System.telemetry.packet(target_name, packet_name)
update_packet.received_count = count.to_i
end
inc_count += 1
end
result[inc_count].each do |packet_name, count|
update_packet = System.telemetry.packet('UNKNOWN', packet_name)
update_packet.received_count = count.to_i
end
end
end
end
|