forked from j62/ctbrec
1
0
Fork 0

Make sure, the external browser lock gets released

This commit is contained in:
0xboobface 2019-01-12 17:25:13 +01:00
parent 07e0eb005b
commit 9ceec96195
4 changed files with 24 additions and 25 deletions

View File

@ -17,6 +17,7 @@ import org.slf4j.LoggerFactory;
import ctbrec.OS;
import ctbrec.io.StreamRedirectThread;
// TODO implement proxy support
public class ExternalBrowser implements AutoCloseable {
private static final transient Logger LOG = LoggerFactory.getLogger(ExternalBrowser.class);
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 {
LOG.debug("Running browser with config {}", jsonConfig);
lock.lock();
stopped = false;
this.messageListener = messageListener;
try {
stopped = false;
this.messageListener = messageListener;
p = new ProcessBuilder(OS.getBrowserCommand()).start();
new StreamRedirectThread(p.getInputStream(), System.err);
new StreamRedirectThread(p.getErrorStream(), System.err);
LOG.debug("Browser started");
p = new ProcessBuilder(OS.getBrowserCommand()).start();
new StreamRedirectThread(p.getInputStream(), System.err);
new StreamRedirectThread(p.getErrorStream(), System.err);
LOG.debug("Browser started");
connectToRemoteControlSocket();
LOG.debug("Connected to remote control server. Sending config {}", jsonConfig);
connectToRemoteControlSocket();
LOG.debug("Connected to remote control server. Sending config {}", jsonConfig);
out.write(jsonConfig.getBytes("utf-8"));
out.write('\n');
out.flush();
out.write(jsonConfig.getBytes("utf-8"));
out.write('\n');
out.flush();
LOG.debug("Waiting for browser to terminate");
p.waitFor();
int exitValue = p.exitValue();
p = null;
LOG.debug("Browser Process terminated with {}", exitValue);
LOG.debug("Waiting for browser to terminate");
p.waitFor();
int exitValue = p.exitValue();
p = null;
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++) {
try {
socket = new Socket("localhost", 3202);
@ -70,7 +76,7 @@ public class ExternalBrowser implements AutoCloseable {
} catch (IOException e) {
if(i == 19) {
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();
}
}

View File

@ -43,7 +43,6 @@ public class BongaCamsElectronLoginDialog {
throw new IOException("Couldn't wait for login dialog", e);
} finally {
browser.close();
browser.release();
}
}

View File

@ -43,7 +43,6 @@ public class Cam4ElectronLoginDialog {
throw new IOException("Couldn't wait for login dialog", e);
} finally {
browser.close();
browser.release();
}
}

View File

@ -41,7 +41,6 @@ public class LiveJasminElectronLoginDialog {
LOG.debug("Error while starting the browser or communication to it", e);
} finally {
browser.close();
browser.release();
}
}