Class: Cosmos::InterfaceThread
- Defined in:
- lib/cosmos/tools/cmd_tlm_server/interface_thread.rb
Overview
Direct Known Subclasses
Constant Summary collapse
- UNKNOWN_BYTES_TO_PRINT =
The number of bytes to print when an UNKNOWN packet is received
36
Instance Attribute Summary collapse
-
#connection_failed_callback ⇒ #call(Exception)
Callback which is called if the Cosmos::Interface#connect method throws an exception.
-
#connection_lost_callback ⇒ #call(Exception|nil)
Callback which is called if the interface connection is lost.
-
#connection_success_callback ⇒ #call()
Callback which is called if the #Cosmos::Interface#connect method succeeds.
-
#fatal_exception_callback ⇒ #call(Exception)
Callback which is called if the InterfaceThread dies for any reason.
-
#identified_packet_callback ⇒ #call(Packet)
Callback which is called when a packet has been received from the interface and identified.
Instance Method Summary collapse
- #graceful_kill ⇒ Object
-
#initialize(interface) ⇒ InterfaceThread
constructor
A new instance of InterfaceThread.
-
#start ⇒ Object
Create and start the Ruby thread that will encapsulate the interface.
-
#stop ⇒ Object
Disconnect from the interface and stop the thread.
Constructor Details
#initialize(interface) ⇒ InterfaceThread
Returns a new instance of InterfaceThread.
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/cosmos/tools/cmd_tlm_server/interface_thread.rb', line 37 def initialize(interface) @interface = interface @interface.thread = self @connection_success_callback = nil @connection_failed_callback = nil @connection_lost_callback = nil @identified_packet_callback = nil @fatal_exception_callback = nil @thread = nil @thread_sleeper = Sleeper.new end |
Instance Attribute Details
#connection_failed_callback ⇒ #call(Exception)
Returns Callback which is called if the Cosmos::Interface#connect method throws an exception.
25 26 27 |
# File 'lib/cosmos/tools/cmd_tlm_server/interface_thread.rb', line 25 def connection_failed_callback @connection_failed_callback end |
#connection_lost_callback ⇒ #call(Exception|nil)
Returns Callback which is called if the interface connection is lost.
28 29 30 |
# File 'lib/cosmos/tools/cmd_tlm_server/interface_thread.rb', line 28 def connection_lost_callback @connection_lost_callback end |
#connection_success_callback ⇒ #call()
Returns Callback which is called if the #Cosmos::Interface#connect method succeeds.
22 23 24 |
# File 'lib/cosmos/tools/cmd_tlm_server/interface_thread.rb', line 22 def connection_success_callback @connection_success_callback end |
#fatal_exception_callback ⇒ #call(Exception)
Returns Callback which is called if the InterfaceThread dies for any reason.
34 35 36 |
# File 'lib/cosmos/tools/cmd_tlm_server/interface_thread.rb', line 34 def fatal_exception_callback @fatal_exception_callback end |
#identified_packet_callback ⇒ #call(Packet)
Returns Callback which is called when a packet has been received from the interface and identified.
31 32 33 |
# File 'lib/cosmos/tools/cmd_tlm_server/interface_thread.rb', line 31 def identified_packet_callback @identified_packet_callback end |
Instance Method Details
#graceful_kill ⇒ Object
114 115 116 |
# File 'lib/cosmos/tools/cmd_tlm_server/interface_thread.rb', line 114 def graceful_kill # Just to avoid warning end |
#start ⇒ Object
Create and start the Ruby thread that will encapsulate the interface. Creates a while loop that waits for Cosmos::Interface#connect to succeed. Then calls Cosmos::Interface#read and handles all the incoming packets.
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 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 103 104 |
# File 'lib/cosmos/tools/cmd_tlm_server/interface_thread.rb', line 52 def start @thread_sleeper = Sleeper.new @thread = Thread.new do @cancel_thread = false begin Logger.info "Starting packet reading for #{@interface.name}" while true unless @interface.connected? begin connect() rescue Exception => connect_error handle_connection_failed(connect_error) if @cancel_thread break else next end end end begin packet = @interface.read unless packet Logger.info "Clean disconnect from #{@interface.name} (returned nil)" handle_connection_lost(nil) if @cancel_thread break else next end end packet.received_time = Time.now unless packet.received_time rescue Exception => err handle_connection_lost(err) if @cancel_thread break else next end end handle_packet(packet) end # loop rescue Exception => error if @fatal_exception_callback @fatal_exception_callback.call(error) else Logger.error "Packet reading thread unexpectedly died for #{@interface.name}" Cosmos.handle_fatal_exception(error) end end end # Thread.new end |
#stop ⇒ Object
Disconnect from the interface and stop the thread
107 108 109 110 111 112 |
# File 'lib/cosmos/tools/cmd_tlm_server/interface_thread.rb', line 107 def stop @cancel_thread = true @thread_sleeper.cancel @interface.disconnect Cosmos.kill_thread(self, @thread) if @thread != Thread.current end |