Terminate the controller listener thread when the connection is interrupted. Start the controller listener only after the connection is established. Call dispose() on a different thread since Swing doesn't like it when that's called on an interrupted thread.

This commit is contained in:
Cameron Gutman
2013-12-12 17:48:00 -05:00
parent c51659f50c
commit a800913cfa

View File

@@ -26,6 +26,7 @@ public class Limelight implements NvConnectionListener {
private NvConnection conn; private NvConnection conn;
private boolean connectionFailed; private boolean connectionFailed;
private static JFrame limeFrame; private static JFrame limeFrame;
private Thread controllerListenerThread;
public Limelight(String host) { public Limelight(String host) {
this.host = host; this.host = host;
@@ -39,12 +40,10 @@ public class Limelight implements NvConnectionListener {
PlatformBinding.getAudioRenderer(), PlatformBinding.getAudioRenderer(),
PlatformBinding.getVideoDecoderRenderer()); PlatformBinding.getVideoDecoderRenderer());
streamFrame.build(conn, fullscreen); streamFrame.build(conn, fullscreen);
startControllerListener();
} }
private void startControllerListener() { private void startControllerListener() {
new Thread(new Runnable() { controllerListenerThread = new Thread() {
@Override @Override
public void run() { public void run() {
@@ -63,7 +62,7 @@ public class Limelight implements NvConnectionListener {
construct = defEnv.getDeclaredConstructor(); construct = defEnv.getDeclaredConstructor();
construct.setAccessible(true); construct.setAccessible(true);
while(true) { while(!isInterrupted()) {
ControllerEnvironment defaultEnv = null; ControllerEnvironment defaultEnv = null;
@@ -90,13 +89,16 @@ public class Limelight implements NvConnectionListener {
try { try {
Thread.sleep(1000); Thread.sleep(1000);
} catch (InterruptedException e) {} } catch (InterruptedException e) {
return;
}
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
}).start(); };
controllerListenerThread.start();
} }
private static void createFrame() { private static void createFrame() {
@@ -151,6 +153,7 @@ public class Limelight implements NvConnectionListener {
@Override @Override
public void connectionStarted() { public void connectionStarted() {
streamFrame.hideSpinner(); streamFrame.hideSpinner();
startControllerListener();
} }
@Override @Override
@@ -158,9 +161,28 @@ public class Limelight implements NvConnectionListener {
e.printStackTrace(); e.printStackTrace();
if (!connectionFailed) { if (!connectionFailed) {
connectionFailed = true; connectionFailed = true;
streamFrame.dispose();
displayError("Connection Terminated", "The connection failed unexpectedly"); // Kill the connection to the target
conn.stop(); conn.stop();
// Kill the controller rescanning thread
if (controllerListenerThread != null) {
controllerListenerThread.interrupt();
try {
controllerListenerThread.join();
} catch (InterruptedException e1) {}
}
// Spin off a new thread to update the UI since
// this thread has been interrupted and will terminate
// shortly
new Thread(new Runnable() {
@Override
public void run() {
streamFrame.dispose();
displayError("Connection Terminated", "The connection failed unexpectedly");
}
}).start();
} }
} }