aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/org/pacien/tincapp/commands
diff options
context:
space:
mode:
authorPacien TRAN-GIRARD2017-07-09 20:20:14 +0200
committerPacien TRAN-GIRARD2017-07-09 20:21:15 +0200
commit2c5673b187233a30a0dd284bc37436fc30596c66 (patch)
tree8d6297648124a639878392c95e266ec91cfe45cd /app/src/main/java/org/pacien/tincapp/commands
parent5c52c7fbc522e7d11141291d5650bb53cd1fa509 (diff)
downloadtincapp-2c5673b187233a30a0dd284bc37436fc30596c66.tar.gz
Make tincctl calls properly async
Diffstat (limited to 'app/src/main/java/org/pacien/tincapp/commands')
-rw-r--r--app/src/main/java/org/pacien/tincapp/commands/Executor.kt26
-rw-r--r--app/src/main/java/org/pacien/tincapp/commands/Tinc.kt30
-rw-r--r--app/src/main/java/org/pacien/tincapp/commands/Tincd.kt2
3 files changed, 38 insertions, 20 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 c93de64..eccd2f9 100644
--- a/app/src/main/java/org/pacien/tincapp/commands/Executor.kt
+++ b/app/src/main/java/org/pacien/tincapp/commands/Executor.kt
@@ -1,7 +1,9 @@
1package org.pacien.tincapp.commands 1package org.pacien.tincapp.commands
2 2
3import java8.util.concurrent.CompletableFuture
3import java.io.BufferedReader 4import java.io.BufferedReader
4import java.io.IOException 5import java.io.IOException
6import java.io.InputStream
5import java.io.InputStreamReader 7import java.io.InputStreamReader
6 8
7/** 9/**
@@ -9,6 +11,8 @@ import java.io.InputStreamReader
9 */ 11 */
10internal object Executor { 12internal object Executor {
11 13
14 class CommandExecutionException(msg: String) : Exception(msg)
15
12 init { 16 init {
13 System.loadLibrary("exec") 17 System.loadLibrary("exec")
14 } 18 }
@@ -18,17 +22,23 @@ internal object Executor {
18 */ 22 */
19 private external fun forkExec(argcv: Array<String>): Int 23 private external fun forkExec(argcv: Array<String>): Int
20 24
21 @Throws(IOException::class) 25 private fun read(stream: InputStream) = BufferedReader(InputStreamReader(stream)).readLines()
26
22 fun forkExec(cmd: Command) { 27 fun forkExec(cmd: Command) {
23 if (forkExec(cmd.asArray()) == -1) 28 if (forkExec(cmd.asArray()) == -1) throw CommandExecutionException("Could not fork child process.")
24 throw IOException()
25 } 29 }
26 30
27 @Throws(IOException::class) 31 fun call(cmd: Command): CompletableFuture<List<String>> {
28 fun call(cmd: Command): List<String> { 32 val proc = try {
29 val proc = ProcessBuilder(cmd.asList()).start() 33 ProcessBuilder(cmd.asList()).start()
30 val outputReader = BufferedReader(InputStreamReader(proc.inputStream)) 34 } catch (e: IOException) {
31 return outputReader.readLines() 35 throw CommandExecutionException(e.message ?: "Could not start process.")
36 }
37
38 return CompletableFuture.supplyAsync<List<String>> {
39 if (proc.waitFor() == 0) read(proc.inputStream)
40 else throw CommandExecutionException(read(proc.errorStream).lastOrNull() ?: "Non-zero exit status.")
41 }
32 } 42 }
33 43
34} 44}
diff --git a/app/src/main/java/org/pacien/tincapp/commands/Tinc.kt b/app/src/main/java/org/pacien/tincapp/commands/Tinc.kt
index 9b57233..120525d 100644
--- a/app/src/main/java/org/pacien/tincapp/commands/Tinc.kt
+++ b/app/src/main/java/org/pacien/tincapp/commands/Tinc.kt
@@ -1,7 +1,7 @@
1package org.pacien.tincapp.commands 1package org.pacien.tincapp.commands
2 2
3import java8.util.concurrent.CompletableFuture
3import org.pacien.tincapp.context.AppPaths 4import org.pacien.tincapp.context.AppPaths
4import java.io.IOException
5 5
6/** 6/**
7 * @author pacien 7 * @author pacien
@@ -13,19 +13,29 @@ object Tinc {
13 .withOption("config", AppPaths.confDir(netName).absolutePath) 13 .withOption("config", AppPaths.confDir(netName).absolutePath)
14 .withOption("pidfile", AppPaths.pidFile(netName).absolutePath) 14 .withOption("pidfile", AppPaths.pidFile(netName).absolutePath)
15 15
16 @Throws(IOException::class) 16 fun stop(netName: String): CompletableFuture<Unit> =
17 fun stop(netName: String) { 17 Executor.call(newCommand(netName).withArguments("stop"))
18 Executor.call(newCommand(netName).withArguments("stop")) 18 .thenApply { }
19 }
20 19
21 @Throws(IOException::class) 20 fun dumpNodes(netName: String, reachable: Boolean = false): CompletableFuture<List<String>> =
22 fun dumpNodes(netName: String, reachable: Boolean = false): List<String> =
23 Executor.call( 21 Executor.call(
24 if (reachable) newCommand(netName).withArguments("dump", "reachable", "nodes") 22 if (reachable) newCommand(netName).withArguments("dump", "reachable", "nodes")
25 else newCommand(netName).withArguments("dump", "nodes")) 23 else newCommand(netName).withArguments("dump", "nodes"))
26 24
27 @Throws(IOException::class) 25 fun info(netName: String, node: String): CompletableFuture<String> =
28 fun info(netName: String, node: String): String = 26 Executor.call(newCommand(netName).withArguments("info", node))
29 Executor.call(newCommand(netName).withArguments("info", node)).joinToString("\n") 27 .thenApply<String> { it.joinToString("\n") }
28
29 fun init(netName: String): CompletableFuture<String> =
30 Executor.call(Command(AppPaths.tinc().absolutePath)
31 .withOption("config", AppPaths.confDir(netName).absolutePath)
32 .withArguments("init", netName))
33 .thenApply<String> { it.joinToString("\n") }
34
35 fun join(netName: String, invitationUrl: String): CompletableFuture<String> =
36 Executor.call(Command(AppPaths.tinc().absolutePath)
37 .withOption("config", AppPaths.confDir(netName).absolutePath)
38 .withArguments("join", invitationUrl))
39 .thenApply<String> { it.joinToString("\n") }
30 40
31} 41}
diff --git a/app/src/main/java/org/pacien/tincapp/commands/Tincd.kt b/app/src/main/java/org/pacien/tincapp/commands/Tincd.kt
index 19ebfbd..db113cc 100644
--- a/app/src/main/java/org/pacien/tincapp/commands/Tincd.kt
+++ b/app/src/main/java/org/pacien/tincapp/commands/Tincd.kt
@@ -1,14 +1,12 @@
1package org.pacien.tincapp.commands 1package org.pacien.tincapp.commands
2 2
3import org.pacien.tincapp.context.AppPaths 3import org.pacien.tincapp.context.AppPaths
4import java.io.IOException
5 4
6/** 5/**
7 * @author pacien 6 * @author pacien
8 */ 7 */
9object Tincd { 8object Tincd {
10 9
11 @Throws(IOException::class)
12 fun start(netName: String, fd: Int) { 10 fun start(netName: String, fd: Int) {
13 Executor.forkExec(Command(AppPaths.tincd().absolutePath) 11 Executor.forkExec(Command(AppPaths.tincd().absolutePath)
14 .withOption("no-detach") 12 .withOption("no-detach")