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
}
}
public GuardedProcess(final List<String> cmd) throws InterruptedException, IOException { class GuardedProcess extends Process {
this(cmd, null); private var guardThread: Thread = null
} @volatile
private var isDestroyed: Boolean = false
@volatile
private var process: Process = null
@throws(classOf[InterruptedException])
@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(GuardedProcess.TAG, "start process: " + cmd)
Log.i(TAG, "start process: " + cmd); val startTime: Long = java.lang.System.currentTimeMillis
long startTime = System.currentTimeMillis(); process = GuardedProcess.startProcess(cmd)
process = 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 (java.lang.System.currentTimeMillis - startTime < 1000) {
if (System.currentTimeMillis() - startTime < 1000) { Log.w(GuardedProcess.TAG, "process exit too fast, stop guard: " + cmd)
Log.w(TAG, "process exit too fast, stop guard: " + cmd); return
break;
}
}
} catch (InterruptedException ignored) {
Log.i(TAG, "thread interrupt, destroy process: " + cmd);
process.destroy();
} catch (IOException e) {
atomicIoException.compareAndSet(null, e);
} finally {
countDownLatch.countDown();
}
} }
}, "GuardThread-" + cmd); }
guardThread.start();
countDownLatch.await();
IOException ioException = atomicIoException.get();
if (ioException != null) {
throw ioException;
} }
catch {
case ignored: InterruptedException =>
Log.i(GuardedProcess.TAG, "thread interrupt, destroy process: " + cmd)
process.destroy()
case e: IOException =>
atomicIoException.compareAndSet(null, e)
} finally {
countDownLatch.countDown()
}
}
}, "GuardThread-" + cmd)
guardThread.start()
countDownLatch.await()
val ioException: IOException = atomicIoException.get
if (ioException != null) {
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()
try {
guardThread.join()
} }
catch {
public void destroy() { case ignored: InterruptedException =>
isDestroyed = true;
guardThread.interrupt();
process.destroy();
try {
guardThread.join();
} catch (InterruptedException ignored) {
}
} }
}
@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