diff options
author | Pacien TRAN-GIRARD | 2017-07-11 14:49:08 +0200 |
---|---|---|
committer | Pacien TRAN-GIRARD | 2017-07-11 14:49:08 +0200 |
commit | a2bfd75dee9b4952771a66c555ba1d920a072ae9 (patch) | |
tree | 3fcf598fe4e8fe858b296089f651a198b4e5f47c /app/src/main/java/org/pacien/tincapp/commands | |
parent | 336a8f9cc02b2f78796fd10ea6244e5d0bc725c7 (diff) | |
download | tincapp-a2bfd75dee9b4952771a66c555ba1d920a072ae9.tar.gz |
Implement create and join network dialogs + refactoring
Diffstat (limited to 'app/src/main/java/org/pacien/tincapp/commands')
-rw-r--r-- | app/src/main/java/org/pacien/tincapp/commands/Executor.kt | 7 | ||||
-rw-r--r-- | app/src/main/java/org/pacien/tincapp/commands/TincApp.kt | 29 |
2 files changed, 35 insertions, 1 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 eccd2f9..38c2cb5 100644 --- a/app/src/main/java/org/pacien/tincapp/commands/Executor.kt +++ b/app/src/main/java/org/pacien/tincapp/commands/Executor.kt | |||
@@ -1,6 +1,8 @@ | |||
1 | package org.pacien.tincapp.commands | 1 | package org.pacien.tincapp.commands |
2 | 2 | ||
3 | import android.os.AsyncTask | ||
3 | import java8.util.concurrent.CompletableFuture | 4 | import java8.util.concurrent.CompletableFuture |
5 | import java8.util.function.Supplier | ||
4 | import java.io.BufferedReader | 6 | import java.io.BufferedReader |
5 | import java.io.IOException | 7 | import java.io.IOException |
6 | import java.io.InputStream | 8 | import java.io.InputStream |
@@ -35,10 +37,13 @@ internal object Executor { | |||
35 | throw CommandExecutionException(e.message ?: "Could not start process.") | 37 | throw CommandExecutionException(e.message ?: "Could not start process.") |
36 | } | 38 | } |
37 | 39 | ||
38 | return CompletableFuture.supplyAsync<List<String>> { | 40 | return supplyAsyncTask<List<String>> { |
39 | if (proc.waitFor() == 0) read(proc.inputStream) | 41 | if (proc.waitFor() == 0) read(proc.inputStream) |
40 | else throw CommandExecutionException(read(proc.errorStream).lastOrNull() ?: "Non-zero exit status.") | 42 | else throw CommandExecutionException(read(proc.errorStream).lastOrNull() ?: "Non-zero exit status.") |
41 | } | 43 | } |
42 | } | 44 | } |
43 | 45 | ||
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)!! | ||
48 | |||
44 | } | 49 | } |
diff --git a/app/src/main/java/org/pacien/tincapp/commands/TincApp.kt b/app/src/main/java/org/pacien/tincapp/commands/TincApp.kt new file mode 100644 index 0000000..108b27d --- /dev/null +++ b/app/src/main/java/org/pacien/tincapp/commands/TincApp.kt | |||
@@ -0,0 +1,29 @@ | |||
1 | package org.pacien.tincapp.commands | ||
2 | |||
3 | import java8.util.concurrent.CompletableFuture | ||
4 | import org.pacien.tincapp.commands.Executor.runAsyncTask | ||
5 | import org.pacien.tincapp.context.AppPaths | ||
6 | import org.pacien.tincapp.data.VpnInterfaceConfiguration | ||
7 | |||
8 | /** | ||
9 | * @author pacien | ||
10 | */ | ||
11 | object TincApp { | ||
12 | |||
13 | private val SCRIPT_SUFFIXES = listOf("-up", "-down", "-created", "-accepted") | ||
14 | private val STATIC_SCRIPTS = listOf("tinc", "host", "subnet", "invitation").flatMap { s -> SCRIPT_SUFFIXES.map { s + it } } | ||
15 | |||
16 | private fun listScripts(netName: String) = AppPaths.confDir(netName).listFiles { f -> f.name in STATIC_SCRIPTS } + | ||
17 | AppPaths.hostsDir(netName).listFiles { f -> SCRIPT_SUFFIXES.none { f.name.endsWith(it) } } | ||
18 | |||
19 | fun removeScripts(netName: String): CompletableFuture<Void> = runAsyncTask { | ||
20 | listScripts(netName).forEach { it.delete() } | ||
21 | } | ||
22 | |||
23 | fun generateIfaceCfg(netName: String): CompletableFuture<Void> = runAsyncTask { | ||
24 | VpnInterfaceConfiguration | ||
25 | .fromInvitation(AppPaths.invitationFile(netName)) | ||
26 | .write(AppPaths.netConfFile(netName)) | ||
27 | } | ||
28 | |||
29 | } | ||