diff --git a/src/main/kotlin/com/beust/kobalt/internal/KobaltClient.kt b/src/main/kotlin/com/beust/kobalt/internal/KobaltClient.kt index c957a504..2ff10169 100644 --- a/src/main/kotlin/com/beust/kobalt/internal/KobaltClient.kt +++ b/src/main/kotlin/com/beust/kobalt/internal/KobaltClient.kt @@ -1,9 +1,12 @@ package com.beust.kobalt.internal import com.beust.kobalt.Args +import com.beust.kobalt.SystemProperties import com.beust.kobalt.kotlin.ScriptCompiler2 import com.beust.kobalt.mainNoExit import com.beust.kobalt.misc.log +import com.google.gson.JsonObject +import com.google.gson.JsonParser import com.google.inject.Inject import java.io.BufferedReader import java.io.BufferedWriter @@ -11,6 +14,7 @@ import java.io.InputStreamReader import java.io.PrintWriter import java.net.ConnectException import java.net.Socket +import java.nio.file.Paths import java.util.concurrent.Executors public class KobaltClient @Inject constructor() : Runnable { @@ -25,23 +29,23 @@ public class KobaltClient @Inject constructor() : Runnable { try { val socket = Socket("localhost", portNumber) outgoing = PrintWriter(socket.outputStream, true) - val c : String = "{ \"name\":\"GetDependencies\", \"buildFile\": \"c:\\\\users\\\\cbeust\\\\java\\\\testng\\\\Build.kt\"}" + val testBuildfile = Paths.get(SystemProperties.homeDir, "java", "testng", "Build.kt") + .toFile().absolutePath + val c : String = "{ \"name\":\"GetDependencies\", \"buildFile\": \"$testBuildfile\"}" outgoing!!.println(c) - done = true val ins = BufferedReader(InputStreamReader(socket.inputStream)) - var fromServer = ins.readLine() - while (fromServer != null) { - log(1, "Response from server:\n" + fromServer) - fromServer = ins.readLine() + var line = ins.readLine() + while (! done && line != null) { + log(1, "Received from server:\n" + line) + val jo = JsonParser().parse(line) as JsonObject + if (jo.has("name") && "Quit" == jo.get("name").asString) { + log(1, "Quitting") + outgoing!!.println("{ \"name\": \"Quit\" }") + done = true + } else { + line = ins.readLine() + } } -// done = true -// log(1, "Launching listening server") -// while (fromServer != null) { -// log(1, "From server: " + fromServer); -// if (fromServer.equals("Bye.")) -// break; -// fromServer = ins.readLine() -// } } catch(ex: ConnectException) { log(1, "Server not up, sleeping a bit") Thread.sleep(2000) diff --git a/src/main/kotlin/com/beust/kobalt/internal/KobaltServer.kt b/src/main/kotlin/com/beust/kobalt/internal/KobaltServer.kt index 9851e08b..1f77f390 100644 --- a/src/main/kotlin/com/beust/kobalt/internal/KobaltServer.kt +++ b/src/main/kotlin/com/beust/kobalt/internal/KobaltServer.kt @@ -1,5 +1,6 @@ package com.beust.kobalt.internal +import com.beust.klaxon.json import com.beust.kobalt.Args import com.beust.kobalt.kotlin.BuildFile import com.beust.kobalt.kotlin.ScriptCompiler2 @@ -35,21 +36,22 @@ public class KobaltServer @Inject constructor(val args: Args, val executors: Kob if (pending.size() > 0) { log(1, "Emptying the queue, size $pending.size()") synchronized(pending) { - pending.forEach { sendInfo(it) } + pending.forEach { sendData(it) } pending.clear() } } val ins = BufferedReader(InputStreamReader(clientSocket.inputStream)) try { - var inputLine = ins.readLine() - while (!quit && inputLine != null) { - log(1, "Received $inputLine") - if (inputLine == "Quit") { + var line = ins.readLine() + while (!quit && line != null) { + log(1, "Received from client $line") + val jo = JsonParser().parse(line) as JsonObject + if ("Quit" == jo.get("name").asString) { log(1, "Quitting") quit = true } else { - runCommand(inputLine) - inputLine = ins.readLine() + runCommand(jo) + line = ins.readLine() } } } catch(ex: SocketException) { @@ -63,7 +65,7 @@ public class KobaltServer @Inject constructor(val args: Args, val executors: Kob } inner class PingCommand() : Command { - override fun run(jo: JsonObject) = sendInfo("{ \"response\" : \"${jo.toString()}\" }") + override fun run(jo: JsonObject) = sendData("{ \"response\" : \"${jo.toString()}\" }") } inner class GetDependenciesCommand() : Command { @@ -71,9 +73,10 @@ public class KobaltServer @Inject constructor(val args: Args, val executors: Kob val buildFile = BuildFile(Paths.get(jo.get("buildFile").asString), "GetDependenciesCommand") val scriptCompiler = buildFileCompilerFactory.create(listOf(buildFile)) scriptCompiler.observable.subscribe { - info -> sendInfo(toJson(info)) + buildScriptInfo -> sendData(toJson(buildScriptInfo)) } scriptCompiler.compileBuildFiles(args) + sendData("{ \"name\": \"Quit\" }") } } @@ -114,17 +117,16 @@ public class KobaltServer @Inject constructor(val args: Args, val executors: Kob Pair("GetDependencies", GetDependenciesCommand()) ) - private fun runCommand(json: String) { - val jo = JsonParser().parse(json) as JsonObject + private fun runCommand(jo: JsonObject) { val command = jo.get("name").asString if (command != null) { COMMANDS.getOrElse(command, { PingCommand() }).run(jo) } else { - error("Did not find a name in command: $json") + error("Did not find a name in command: $jo") } } - fun sendInfo(info: String) { + fun sendData(info: String) { if (outgoing != null) { outgoing!!.println(info) } else {