Skip to content

Commit

Permalink
Merge pull request #112 from BallAerospace/protect_interface_thread_s…
Browse files Browse the repository at this point in the history
…top_from_autoconnect

closes #111. Protect InterfaceThread Stop from Auto Reconnect
  • Loading branch information
ryanmelt committed Apr 16, 2015
2 parents 114d2ef + 6d13bb5 commit ab2090f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
1 change: 0 additions & 1 deletion lib/cosmos/tools/cmd_tlm_server/connections.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ def disconnect(connection_name)
connection = @connections[connection_name.upcase]
raise "Unknown #{@keyword}: #{connection_name}" unless connection

connection.disconnect
stop_thread(connection)
Logger.info "Disconnected from #{@keyword} #{connection_name.upcase}"
end
Expand Down
24 changes: 17 additions & 7 deletions lib/cosmos/tools/cmd_tlm_server/gui/interfaces_tab.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,20 @@ def update(name)
interfaces.all.each do |interface_name, interface|
button = @interfaces_table[name].cellWidget(row,1)
state = @interfaces_table[name].item(row,2)
if interface.connected?
if interface.connected? and interface.thread
button.setText('Disconnect')
button.setDisabled(true) if interface.disable_disconnect
state.setText('true')
state.textColor = Cosmos::GREEN
elsif interface.thread
button.text = 'Cancel Connect'
button.setDisabled(false)
state.text = 'attempting'
state.setText('attempting')
state.textColor = Cosmos::RED
elsif interface.connected?
button.setText('Error')
button.setDisabled(false)
state.setText('error')
state.textColor = Cosmos::RED
else
button.setText('Connect')
Expand Down Expand Up @@ -137,12 +142,14 @@ def populate_interface_table(name, interfaces, interfaces_table)
end

def create_button(name, interface, interface_name)
if interface.connected?
if interface.connected? and interface.thread
button_text = 'Disconnect'
elsif interface.thread.nil?
button_text = 'Connect'
else
elsif interface.thread
button_text = 'Cancel Connect'
elsif interface.connected?
button_text = 'Error'
else
button_text = 'Connect'
end
button = Qt::PushButton.new(button_text)
if name == ROUTERS
Expand Down Expand Up @@ -172,12 +179,15 @@ def create_button(name, interface, interface_name)

def create_state(interface)
state = Qt::TableWidgetItem.new
if interface.connected?
if interface.connected? and interface.thread
state.setText('true')
state.textColor = Cosmos::GREEN
elsif interface.thread
state.setText('attempting')
state.textColor = Cosmos::YELLOW
elsif interface.connected?
state.setText('error')
state.textColor = Cosmos::RED
else
state.setText('false')
state.textColor = Cosmos::BLACK
Expand Down
19 changes: 15 additions & 4 deletions lib/cosmos/tools/cmd_tlm_server/interface_thread.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def initialize(interface)
@thread_sleeper = Sleeper.new
@connection_failed_messages = []
@connection_lost_messages = []
@mutex = Mutex.new
end

# Create and start the Ruby thread that will encapsulate the interface.
Expand All @@ -58,9 +59,14 @@ def start
begin
Logger.info "Starting packet reading for #{@interface.name}"
while true
break if @cancel_thread
unless @interface.connected?
begin
connect()
@mutex.synchronize do
# We need to make sure connect is not called after stop() has been called
connect() unless @cancel_thread
end
break if @cancel_thread
rescue Exception => connect_error
handle_connection_failed(connect_error)
if @cancel_thread
Expand Down Expand Up @@ -102,14 +108,19 @@ def start
Cosmos.handle_fatal_exception(error)
end
end
Logger.info "Stopped packet reading for #{@interface.name}"
end # Thread.new
end # def start

# Disconnect from the interface and stop the thread
def stop
@cancel_thread = true
@thread_sleeper.cancel
@interface.disconnect
@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
@thread_sleeper.cancel
@interface.disconnect
end
Cosmos.kill_thread(self, @thread) if @thread != Thread.current
end

Expand Down

0 comments on commit ab2090f

Please sign in to comment.