Method: OpenC3::InterfaceMicroservice#run

Defined in:
lib/openc3/microservices/interface_microservice.rb

#runObject



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
# File 'lib/openc3/microservices/interface_microservice.rb', line 461

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 => e
          handle_connection_failed(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
    # 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), 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