Commit c19eb4e9 authored by ayanamist's avatar ayanamist

Convert GuardedProcess to Scala

parent cad6d7b6
......@@ -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 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;
import collection.JavaConversions._
public class GuardedProcess extends Process {
private static final String TAG = GuardedProcess.class.getSimpleName();
private final Thread guardThread;
private volatile boolean isDestroyed = false;
private volatile Process process = null;
object GuardedProcess {
private val TAG: String = classOf[GuardedProcess].getSimpleName
public GuardedProcess(String... cmd) throws InterruptedException, IOException {
this(Arrays.asList(cmd));
@throws(classOf[IOException])
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 {
this(cmd, 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 {
final CountDownLatch countDownLatch = new CountDownLatch(1);
final AtomicReference<IOException> atomicIoException = new AtomicReference<IOException>(null);
def initThread(cmd: Seq[String], onRestartCallback: Runnable): Unit = {
val atomicIoException: AtomicReference[IOException] = new AtomicReference[IOException](null)
val countDownLatch: CountDownLatch = new CountDownLatch(1)
guardThread = new Thread(new Runnable() {
@Override
public void run() {
override def run(): Unit = {
try {
while (!isDestroyed) {
Log.i(TAG, "start process: " + cmd);
long startTime = System.currentTimeMillis();
process = startProcess(cmd);
if (onRestartCallback != null && countDownLatch.getCount() <= 0) {
onRestartCallback.run();
}
countDownLatch.countDown();
process.waitFor();
if (System.currentTimeMillis() - startTime < 1000) {
Log.w(TAG, "process exit too fast, stop guard: " + cmd);
break;
}
}
} catch (InterruptedException ignored) {
Log.i(TAG, "thread interrupt, destroy process: " + cmd);
process.destroy();
} catch (IOException e) {
atomicIoException.compareAndSet(null, e);
Log.i(GuardedProcess.TAG, "start process: " + cmd)
val startTime: Long = java.lang.System.currentTimeMillis
process = GuardedProcess.startProcess(cmd)
if (onRestartCallback != null && countDownLatch.getCount <= 0) {
onRestartCallback.run()
}
countDownLatch.countDown()
process.waitFor
if (java.lang.System.currentTimeMillis - startTime < 1000) {
Log.w(GuardedProcess.TAG, "process exit too fast, stop guard: " + cmd)
return
}
}
}
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();
countDownLatch.countDown()
}
}
}, "GuardThread-" + cmd);
guardThread.start();
countDownLatch.await();
IOException ioException = atomicIoException.get();
}, "GuardThread-" + cmd)
guardThread.start()
countDownLatch.await()
val ioException: IOException = atomicIoException.get
if (ioException != null) {
throw ioException;
throw ioException
}
}
private static Process startProcess(List<String> cmd) throws IOException {
return new ProcessBuilder(cmd).redirectErrorStream(true).start();
}
public void destroy() {
isDestroyed = true;
guardThread.interrupt();
process.destroy();
def destroy() {
isDestroyed = true
guardThread.interrupt()
process.destroy()
try {
guardThread.join();
} catch (InterruptedException ignored) {
guardThread.join()
}
catch {
case ignored: InterruptedException =>
}
}
@Override
public int exitValue() {
throw new UnsupportedOperationException();
def exitValue: Int = {
throw new UnsupportedOperationException
}
@Override
public InputStream getErrorStream() {
throw new UnsupportedOperationException();
def getErrorStream: InputStream = {
throw new UnsupportedOperationException
}
@Override
public InputStream getInputStream() {
throw new UnsupportedOperationException();
def getInputStream: InputStream = {
throw new UnsupportedOperationException
}
@Override
public OutputStream getOutputStream() {
throw new UnsupportedOperationException();
def getOutputStream: OutputStream = {
throw new UnsupportedOperationException
}
@Override
public int waitFor() throws InterruptedException {
guardThread.join();
return 0;
@throws(classOf[InterruptedException])
def waitFor: Int = {
guardThread.join()
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