forked from j62/ctbrec
Make sure, the external browser lock gets released
This commit is contained in:
parent
07e0eb005b
commit
9ceec96195
|
@ -17,6 +17,7 @@ import org.slf4j.LoggerFactory;
|
||||||
import ctbrec.OS;
|
import ctbrec.OS;
|
||||||
import ctbrec.io.StreamRedirectThread;
|
import ctbrec.io.StreamRedirectThread;
|
||||||
|
|
||||||
|
// TODO implement proxy support
|
||||||
public class ExternalBrowser implements AutoCloseable {
|
public class ExternalBrowser implements AutoCloseable {
|
||||||
private static final transient Logger LOG = LoggerFactory.getLogger(ExternalBrowser.class);
|
private static final transient Logger LOG = LoggerFactory.getLogger(ExternalBrowser.class);
|
||||||
private static final ExternalBrowser INSTANCE = new ExternalBrowser();
|
private static final ExternalBrowser INSTANCE = new ExternalBrowser();
|
||||||
|
@ -35,30 +36,35 @@ public class ExternalBrowser implements AutoCloseable {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void run(String jsonConfig, Consumer<String> messageListener) throws InterruptedException, IOException {
|
public void run(String jsonConfig, Consumer<String> messageListener) throws InterruptedException, IOException {
|
||||||
|
LOG.debug("Running browser with config {}", jsonConfig);
|
||||||
lock.lock();
|
lock.lock();
|
||||||
stopped = false;
|
try {
|
||||||
this.messageListener = messageListener;
|
stopped = false;
|
||||||
|
this.messageListener = messageListener;
|
||||||
|
|
||||||
p = new ProcessBuilder(OS.getBrowserCommand()).start();
|
p = new ProcessBuilder(OS.getBrowserCommand()).start();
|
||||||
new StreamRedirectThread(p.getInputStream(), System.err);
|
new StreamRedirectThread(p.getInputStream(), System.err);
|
||||||
new StreamRedirectThread(p.getErrorStream(), System.err);
|
new StreamRedirectThread(p.getErrorStream(), System.err);
|
||||||
LOG.debug("Browser started");
|
LOG.debug("Browser started");
|
||||||
|
|
||||||
connectToRemoteControlSocket();
|
connectToRemoteControlSocket();
|
||||||
LOG.debug("Connected to remote control server. Sending config {}", jsonConfig);
|
LOG.debug("Connected to remote control server. Sending config {}", jsonConfig);
|
||||||
|
|
||||||
out.write(jsonConfig.getBytes("utf-8"));
|
out.write(jsonConfig.getBytes("utf-8"));
|
||||||
out.write('\n');
|
out.write('\n');
|
||||||
out.flush();
|
out.flush();
|
||||||
|
|
||||||
LOG.debug("Waiting for browser to terminate");
|
LOG.debug("Waiting for browser to terminate");
|
||||||
p.waitFor();
|
p.waitFor();
|
||||||
int exitValue = p.exitValue();
|
int exitValue = p.exitValue();
|
||||||
p = null;
|
p = null;
|
||||||
LOG.debug("Browser Process terminated with {}", exitValue);
|
LOG.debug("Browser Process terminated with {}", exitValue);
|
||||||
|
} finally {
|
||||||
|
lock.unlock();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void connectToRemoteControlSocket() {
|
private void connectToRemoteControlSocket() throws IOException {
|
||||||
for (int i = 0; i < 20; i++) {
|
for (int i = 0; i < 20; i++) {
|
||||||
try {
|
try {
|
||||||
socket = new Socket("localhost", 3202);
|
socket = new Socket("localhost", 3202);
|
||||||
|
@ -70,7 +76,7 @@ public class ExternalBrowser implements AutoCloseable {
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
if(i == 19) {
|
if(i == 19) {
|
||||||
LOG.error("Connection to remote control socket failed", e);
|
LOG.error("Connection to remote control socket failed", e);
|
||||||
return;
|
throw e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -134,8 +140,4 @@ public class ExternalBrowser implements AutoCloseable {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void release() {
|
|
||||||
lock.unlock();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -43,7 +43,6 @@ public class BongaCamsElectronLoginDialog {
|
||||||
throw new IOException("Couldn't wait for login dialog", e);
|
throw new IOException("Couldn't wait for login dialog", e);
|
||||||
} finally {
|
} finally {
|
||||||
browser.close();
|
browser.close();
|
||||||
browser.release();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -43,7 +43,6 @@ public class Cam4ElectronLoginDialog {
|
||||||
throw new IOException("Couldn't wait for login dialog", e);
|
throw new IOException("Couldn't wait for login dialog", e);
|
||||||
} finally {
|
} finally {
|
||||||
browser.close();
|
browser.close();
|
||||||
browser.release();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -41,7 +41,6 @@ public class LiveJasminElectronLoginDialog {
|
||||||
LOG.debug("Error while starting the browser or communication to it", e);
|
LOG.debug("Error while starting the browser or communication to it", e);
|
||||||
} finally {
|
} finally {
|
||||||
browser.close();
|
browser.close();
|
||||||
browser.release();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue