diff options
Diffstat (limited to 'app/src/main/java/org/pacien/tincapp/commands/Executor.kt')
-rw-r--r-- | app/src/main/java/org/pacien/tincapp/commands/Executor.kt | 48 |
1 files changed, 24 insertions, 24 deletions
diff --git a/app/src/main/java/org/pacien/tincapp/commands/Executor.kt b/app/src/main/java/org/pacien/tincapp/commands/Executor.kt index 38c2cb5..eace2e7 100644 --- a/app/src/main/java/org/pacien/tincapp/commands/Executor.kt +++ b/app/src/main/java/org/pacien/tincapp/commands/Executor.kt | |||
@@ -13,37 +13,37 @@ import java.io.InputStreamReader | |||
13 | */ | 13 | */ |
14 | internal object Executor { | 14 | internal object Executor { |
15 | 15 | ||
16 | class CommandExecutionException(msg: String) : Exception(msg) | 16 | class CommandExecutionException(msg: String) : Exception(msg) |
17 | 17 | ||
18 | init { | 18 | init { |
19 | System.loadLibrary("exec") | 19 | System.loadLibrary("exec") |
20 | } | 20 | } |
21 | |||
22 | /** | ||
23 | * @return -1 on error, forked child PID otherwise | ||
24 | */ | ||
25 | private external fun forkExec(argcv: Array<String>): Int | ||
21 | 26 | ||
22 | /** | 27 | private fun read(stream: InputStream) = BufferedReader(InputStreamReader(stream)).readLines() |
23 | * @return -1 on error, forked child PID otherwise | ||
24 | */ | ||
25 | private external fun forkExec(argcv: Array<String>): Int | ||
26 | 28 | ||
27 | private fun read(stream: InputStream) = BufferedReader(InputStreamReader(stream)).readLines() | 29 | fun forkExec(cmd: Command) { |
30 | if (forkExec(cmd.asArray()) == -1) throw CommandExecutionException("Could not fork child process.") | ||
31 | } | ||
28 | 32 | ||
29 | fun forkExec(cmd: Command) { | 33 | fun call(cmd: Command): CompletableFuture<List<String>> { |
30 | if (forkExec(cmd.asArray()) == -1) throw CommandExecutionException("Could not fork child process.") | 34 | val proc = try { |
35 | ProcessBuilder(cmd.asList()).start() | ||
36 | } catch (e: IOException) { | ||
37 | throw CommandExecutionException(e.message ?: "Could not start process.") | ||
31 | } | 38 | } |
32 | 39 | ||
33 | fun call(cmd: Command): CompletableFuture<List<String>> { | 40 | return supplyAsyncTask<List<String>> { |
34 | val proc = try { | 41 | if (proc.waitFor() == 0) read(proc.inputStream) |
35 | ProcessBuilder(cmd.asList()).start() | 42 | else throw CommandExecutionException(read(proc.errorStream).lastOrNull() ?: "Non-zero exit status.") |
36 | } catch (e: IOException) { | ||
37 | throw CommandExecutionException(e.message ?: "Could not start process.") | ||
38 | } | ||
39 | |||
40 | return supplyAsyncTask<List<String>> { | ||
41 | if (proc.waitFor() == 0) read(proc.inputStream) | ||
42 | else throw CommandExecutionException(read(proc.errorStream).lastOrNull() ?: "Non-zero exit status.") | ||
43 | } | ||
44 | } | 43 | } |
44 | } | ||
45 | 45 | ||
46 | fun runAsyncTask(r: () -> Unit) = CompletableFuture.runAsync(Runnable(r), AsyncTask.THREAD_POOL_EXECUTOR)!! | 46 | fun runAsyncTask(r: () -> Unit) = CompletableFuture.runAsync(Runnable(r), AsyncTask.THREAD_POOL_EXECUTOR)!! |
47 | fun <U> supplyAsyncTask(s: () -> U) = CompletableFuture.supplyAsync(Supplier(s), AsyncTask.THREAD_POOL_EXECUTOR)!! | 47 | fun <U> supplyAsyncTask(s: () -> U) = CompletableFuture.supplyAsync(Supplier(s), AsyncTask.THREAD_POOL_EXECUTOR)!! |
48 | 48 | ||
49 | } | 49 | } |