aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/org/pacien/tincapp/commands/Executor.kt
diff options
context:
space:
mode:
authorpacien2018-02-14 01:25:48 +0100
committerpacien2018-02-14 01:25:48 +0100
commit680fe07b6ea000ee29ac28e2f48665433e7011df (patch)
tree76b9f914a5092cbd1f96f5d749336fbd3e7a7a60 /app/src/main/java/org/pacien/tincapp/commands/Executor.kt
parent1eb53743996ffdabb6372a59f10d608f5830314e (diff)
downloadtincapp-680fe07b6ea000ee29ac28e2f48665433e7011df.tar.gz
Properly get daemon state
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.kt17
1 files changed, 14 insertions, 3 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 eace2e7..a3b5cea 100644
--- a/app/src/main/java/org/pacien/tincapp/commands/Executor.kt
+++ b/app/src/main/java/org/pacien/tincapp/commands/Executor.kt
@@ -13,6 +13,8 @@ import java.io.InputStreamReader
13 */ 13 */
14internal object Executor { 14internal object Executor {
15 15
16 private const val FAILED = -1
17
16 class CommandExecutionException(msg: String) : Exception(msg) 18 class CommandExecutionException(msg: String) : Exception(msg)
17 19
18 init { 20 init {
@@ -20,14 +22,23 @@ internal object Executor {
20 } 22 }
21 23
22 /** 24 /**
23 * @return -1 on error, forked child PID otherwise 25 * @return FAILED (-1) on error, forked child PID otherwise
24 */ 26 */
25 private external fun forkExec(argcv: Array<String>): Int 27 private external fun forkExec(argcv: Array<String>): Int
26 28
29 /**
30 * @return FAILED (-1) on error, 0 on no-op, the supplied PID otherwise
31 */
32 private external fun wait(pid: Int): Int
33
27 private fun read(stream: InputStream) = BufferedReader(InputStreamReader(stream)).readLines() 34 private fun read(stream: InputStream) = BufferedReader(InputStreamReader(stream)).readLines()
28 35
29 fun forkExec(cmd: Command) { 36 fun forkExec(cmd: Command): CompletableFuture<Void> {
30 if (forkExec(cmd.asArray()) == -1) throw CommandExecutionException("Could not fork child process.") 37 val pid = forkExec(cmd.asArray())
38 return when (pid) {
39 FAILED -> CompletableFuture.failedFuture(CommandExecutionException("Could not fork child process."))
40 else -> CompletableFuture.runAsync { wait(pid) }
41 }
31 } 42 }
32 43
33 fun call(cmd: Command): CompletableFuture<List<String>> { 44 fun call(cmd: Command): CompletableFuture<List<String>> {