Commit c19eb4e9 authored by ayanamist's avatar ayanamist

Convert GuardedProcess to Scala

parent cad6d7b6
...@@ -37,109 +37,111 @@ ...@@ -37,109 +37,111 @@
* *
*/ */
package com.github.shadowsocks; package com.github.shadowsocks
import android.util.Log; import android.util.Log
import java.io.IOException
import java.io.InputStream
import java.io.OutputStream
import java.util.concurrent.CountDownLatch
import java.util.concurrent.atomic.AtomicReference
import java.io.IOException; import collection.JavaConversions._
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.System;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicReference;
public class GuardedProcess extends Process { object GuardedProcess {
private static final String TAG = GuardedProcess.class.getSimpleName(); private val TAG: String = classOf[GuardedProcess].getSimpleName
private final Thread guardThread;
private volatile boolean isDestroyed = false;
private volatile Process process = null;
public GuardedProcess(String... cmd) throws InterruptedException, IOException { @throws(classOf[IOException])
this(Arrays.asList(cmd)); private def startProcess(cmd: Seq[String]): Process = {
new ProcessBuilder(cmd).redirectErrorStream(true).start
} }
}
class GuardedProcess extends Process {
private var guardThread: Thread = null
@volatile
private var isDestroyed: Boolean = false
@volatile
private var process: Process = null
public GuardedProcess(final List<String> cmd) throws InterruptedException, IOException { @throws(classOf[InterruptedException])
this(cmd, null); @throws(classOf[IOException])
def this(cmd: Seq[String], onRestartCallback: Runnable = null) {
this()
initThread(cmd, onRestartCallback)
} }
public GuardedProcess(final List<String> cmd, final Runnable onRestartCallback) throws InterruptedException, IOException { def initThread(cmd: Seq[String], onRestartCallback: Runnable): Unit = {
final CountDownLatch countDownLatch = new CountDownLatch(1); val atomicIoException: AtomicReference[IOException] = new AtomicReference[IOException](null)
final AtomicReference<IOException> atomicIoException = new AtomicReference<IOException>(null); val countDownLatch: CountDownLatch = new CountDownLatch(1)
guardThread = new Thread(new Runnable() { guardThread = new Thread(new Runnable() {
@Override override def run(): Unit = {
public void run() {
try { try {
while (!isDestroyed) { while (!isDestroyed) {
Log.i(TAG, "start process: " + cmd); Log.i(GuardedProcess.TAG, "start process: " + cmd)
long startTime = System.currentTimeMillis(); val startTime: Long = java.lang.System.currentTimeMillis
process = startProcess(cmd); process = GuardedProcess.startProcess(cmd)
if (onRestartCallback != null && countDownLatch.getCount() <= 0) { if (onRestartCallback != null && countDownLatch.getCount <= 0) {
onRestartCallback.run(); onRestartCallback.run()
} }
countDownLatch.countDown(); countDownLatch.countDown()
process.waitFor(); process.waitFor
if (System.currentTimeMillis() - startTime < 1000) { if (java.lang.System.currentTimeMillis - startTime < 1000) {
Log.w(TAG, "process exit too fast, stop guard: " + cmd); Log.w(GuardedProcess.TAG, "process exit too fast, stop guard: " + cmd)
break; return
} }
} }
} catch (InterruptedException ignored) { }
Log.i(TAG, "thread interrupt, destroy process: " + cmd); catch {
process.destroy(); case ignored: InterruptedException =>
} catch (IOException e) { Log.i(GuardedProcess.TAG, "thread interrupt, destroy process: " + cmd)
atomicIoException.compareAndSet(null, e); process.destroy()
case e: IOException =>
atomicIoException.compareAndSet(null, e)
} finally { } finally {
countDownLatch.countDown(); countDownLatch.countDown()
} }
} }
}, "GuardThread-" + cmd); }, "GuardThread-" + cmd)
guardThread.start(); guardThread.start()
countDownLatch.await(); countDownLatch.await()
IOException ioException = atomicIoException.get(); val ioException: IOException = atomicIoException.get
if (ioException != null) { if (ioException != null) {
throw ioException; throw ioException
} }
} }
private static Process startProcess(List<String> cmd) throws IOException { def destroy() {
return new ProcessBuilder(cmd).redirectErrorStream(true).start(); isDestroyed = true
} guardThread.interrupt()
process.destroy()
public void destroy() {
isDestroyed = true;
guardThread.interrupt();
process.destroy();
try { try {
guardThread.join(); guardThread.join()
} catch (InterruptedException ignored) { }
catch {
case ignored: InterruptedException =>
} }
} }
@Override def exitValue: Int = {
public int exitValue() { throw new UnsupportedOperationException
throw new UnsupportedOperationException();
} }
@Override def getErrorStream: InputStream = {
public InputStream getErrorStream() { throw new UnsupportedOperationException
throw new UnsupportedOperationException();
} }
@Override def getInputStream: InputStream = {
public InputStream getInputStream() { throw new UnsupportedOperationException
throw new UnsupportedOperationException();
} }
@Override def getOutputStream: OutputStream = {
public OutputStream getOutputStream() { throw new UnsupportedOperationException
throw new UnsupportedOperationException();
} }
@Override @throws(classOf[InterruptedException])
public int waitFor() throws InterruptedException { def waitFor: Int = {
guardThread.join(); guardThread.join()
return 0; 0
} }
} }
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment