From c355f2dcdd6b66e13150773328607cf697cbcae9 Mon Sep 17 00:00:00 2001 From: Cedric Beust Date: Tue, 20 Oct 2015 23:06:11 -0700 Subject: [PATCH] New format for command data. --- .../com/beust/kobalt/internal/KobaltClient.kt | 10 ++++++++-- .../com/beust/kobalt/internal/KobaltServer.kt | 17 +++++++++++++---- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/main/kotlin/com/beust/kobalt/internal/KobaltClient.kt b/src/main/kotlin/com/beust/kobalt/internal/KobaltClient.kt index b5a9fb4a..2c938dc4 100644 --- a/src/main/kotlin/com/beust/kobalt/internal/KobaltClient.kt +++ b/src/main/kotlin/com/beust/kobalt/internal/KobaltClient.kt @@ -5,6 +5,7 @@ import com.beust.kobalt.SystemProperties import com.beust.kobalt.kotlin.BuildFileCompiler import com.beust.kobalt.mainNoExit import com.beust.kobalt.misc.log +import com.google.gson.Gson import com.google.gson.JsonObject import com.google.gson.JsonParser import com.google.inject.Inject @@ -25,12 +26,14 @@ public class KobaltClient @Inject constructor() : Runnable { var done = false var attempts = 1 - while (attempts < 3 && ! done) { + while (attempts < 10 && ! done) { try { val socket = Socket("localhost", portNumber) outgoing = PrintWriter(socket.outputStream, true) val testBuildfile = Paths.get(SystemProperties.homeDir, "java", "testng", "Build.kt") .toFile().absolutePath + val testBuildfile2 = Paths.get(SystemProperties.homeDir, "java", "jcommander", "Build.kt") + .toFile().absolutePath val c : String = "{ \"name\":\"GetDependencies\", \"buildFile\": \"$testBuildfile\"}" outgoing!!.println(c) val ins = BufferedReader(InputStreamReader(socket.inputStream)) @@ -40,9 +43,12 @@ public class KobaltClient @Inject constructor() : Runnable { val jo = JsonParser().parse(line) as JsonObject if (jo.has("name") && "Quit" == jo.get("name").asString) { log(1, "Quitting") - outgoing!!.println("{ \"name\": \"Quit\" }") +// outgoing!!.println("{ \"name\": \"Quit\" }") done = true } else { + val data = jo.get("data").asString + val dd = Gson().fromJson(data, KobaltServer.GetDependenciesData::class.java) + println("Read GetDependencyData, project count: ${dd.projects.size()}") line = ins.readLine() } } diff --git a/src/main/kotlin/com/beust/kobalt/internal/KobaltServer.kt b/src/main/kotlin/com/beust/kobalt/internal/KobaltServer.kt index 64ef13e6..0da6091d 100644 --- a/src/main/kotlin/com/beust/kobalt/internal/KobaltServer.kt +++ b/src/main/kotlin/com/beust/kobalt/internal/KobaltServer.kt @@ -26,7 +26,7 @@ public class KobaltServer @Inject constructor(val args: Args, val executors: Kob override fun run() { val portNumber = args.port - log(1, "Starting on port $portNumber") + log(1, "Listening to port $portNumber") var quit = false val serverSocket = ServerSocket(portNumber) while (! quit) { @@ -63,6 +63,8 @@ public class KobaltServer @Inject constructor(val args: Args, val executors: Kob fun run(jo: JsonObject) } + class CommandData(val commandName: String, val data: String) + inner class PingCommand() : Command { override fun run(jo: JsonObject) = sendData("{ \"response\" : \"${jo.toString()}\" }") } @@ -72,7 +74,9 @@ 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 { - buildScriptInfo -> sendData(toJson(buildScriptInfo)) + buildScriptInfo -> if (buildScriptInfo.projects.size() > 0) { + sendData(toJson(buildScriptInfo)) + } } scriptCompiler.compileBuildFiles(args) sendData("{ \"name\": \"Quit\" }") @@ -87,7 +91,12 @@ public class KobaltServer @Inject constructor(val args: Args, val executors: Kob val testDependencies: List, val testProvidedDependencies: List) - class GetDependenciesData(val projects: List) + class GetDependenciesData(val projects: List) { + fun toData() : CommandData { + val data = Gson().toJson(this) + return CommandData("GetDependencies", data) + } + } private fun toJson(info: BuildFileCompiler.BuildScriptInfo) : String { val executor = executors.miscExecutor @@ -107,7 +116,7 @@ public class KobaltServer @Inject constructor(val args: Args, val executors: Kob project.testProvidedDependencies.map { toDependencyData(it) })) } log(1, "Returning BuildScriptInfo") - val result = Gson().toJson(GetDependenciesData(projects)) + val result = Gson().toJson(GetDependenciesData(projects).toData()) log(2, " $result") return result }