1
0
Fork 0
mirror of https://github.com/ethauvin/kobalt.git synced 2025-04-26 16:28:12 -07:00

Handling quitting.

This commit is contained in:
Cedric Beust 2015-10-20 21:52:29 -07:00
parent 1fda17b8b6
commit 53610049ff
2 changed files with 33 additions and 27 deletions

View file

@ -1,9 +1,12 @@
package com.beust.kobalt.internal package com.beust.kobalt.internal
import com.beust.kobalt.Args import com.beust.kobalt.Args
import com.beust.kobalt.SystemProperties
import com.beust.kobalt.kotlin.ScriptCompiler2 import com.beust.kobalt.kotlin.ScriptCompiler2
import com.beust.kobalt.mainNoExit import com.beust.kobalt.mainNoExit
import com.beust.kobalt.misc.log import com.beust.kobalt.misc.log
import com.google.gson.JsonObject
import com.google.gson.JsonParser
import com.google.inject.Inject import com.google.inject.Inject
import java.io.BufferedReader import java.io.BufferedReader
import java.io.BufferedWriter import java.io.BufferedWriter
@ -11,6 +14,7 @@ import java.io.InputStreamReader
import java.io.PrintWriter import java.io.PrintWriter
import java.net.ConnectException import java.net.ConnectException
import java.net.Socket import java.net.Socket
import java.nio.file.Paths
import java.util.concurrent.Executors import java.util.concurrent.Executors
public class KobaltClient @Inject constructor() : Runnable { public class KobaltClient @Inject constructor() : Runnable {
@ -25,23 +29,23 @@ public class KobaltClient @Inject constructor() : Runnable {
try { try {
val socket = Socket("localhost", portNumber) val socket = Socket("localhost", portNumber)
outgoing = PrintWriter(socket.outputStream, true) 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) outgoing!!.println(c)
done = true
val ins = BufferedReader(InputStreamReader(socket.inputStream)) val ins = BufferedReader(InputStreamReader(socket.inputStream))
var fromServer = ins.readLine() var line = ins.readLine()
while (fromServer != null) { while (! done && line != null) {
log(1, "Response from server:\n" + fromServer) log(1, "Received from server:\n" + line)
fromServer = ins.readLine() 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) { } catch(ex: ConnectException) {
log(1, "Server not up, sleeping a bit") log(1, "Server not up, sleeping a bit")
Thread.sleep(2000) Thread.sleep(2000)

View file

@ -1,5 +1,6 @@
package com.beust.kobalt.internal package com.beust.kobalt.internal
import com.beust.klaxon.json
import com.beust.kobalt.Args import com.beust.kobalt.Args
import com.beust.kobalt.kotlin.BuildFile import com.beust.kobalt.kotlin.BuildFile
import com.beust.kobalt.kotlin.ScriptCompiler2 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) { if (pending.size() > 0) {
log(1, "Emptying the queue, size $pending.size()") log(1, "Emptying the queue, size $pending.size()")
synchronized(pending) { synchronized(pending) {
pending.forEach { sendInfo(it) } pending.forEach { sendData(it) }
pending.clear() pending.clear()
} }
} }
val ins = BufferedReader(InputStreamReader(clientSocket.inputStream)) val ins = BufferedReader(InputStreamReader(clientSocket.inputStream))
try { try {
var inputLine = ins.readLine() var line = ins.readLine()
while (!quit && inputLine != null) { while (!quit && line != null) {
log(1, "Received $inputLine") log(1, "Received from client $line")
if (inputLine == "Quit") { val jo = JsonParser().parse(line) as JsonObject
if ("Quit" == jo.get("name").asString) {
log(1, "Quitting") log(1, "Quitting")
quit = true quit = true
} else { } else {
runCommand(inputLine) runCommand(jo)
inputLine = ins.readLine() line = ins.readLine()
} }
} }
} catch(ex: SocketException) { } catch(ex: SocketException) {
@ -63,7 +65,7 @@ public class KobaltServer @Inject constructor(val args: Args, val executors: Kob
} }
inner class PingCommand() : Command { 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 { 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 buildFile = BuildFile(Paths.get(jo.get("buildFile").asString), "GetDependenciesCommand")
val scriptCompiler = buildFileCompilerFactory.create(listOf(buildFile)) val scriptCompiler = buildFileCompilerFactory.create(listOf(buildFile))
scriptCompiler.observable.subscribe { scriptCompiler.observable.subscribe {
info -> sendInfo(toJson(info)) buildScriptInfo -> sendData(toJson(buildScriptInfo))
} }
scriptCompiler.compileBuildFiles(args) scriptCompiler.compileBuildFiles(args)
sendData("{ \"name\": \"Quit\" }")
} }
} }
@ -114,17 +117,16 @@ public class KobaltServer @Inject constructor(val args: Args, val executors: Kob
Pair("GetDependencies", GetDependenciesCommand()) Pair("GetDependencies", GetDependenciesCommand())
) )
private fun runCommand(json: String) { private fun runCommand(jo: JsonObject) {
val jo = JsonParser().parse(json) as JsonObject
val command = jo.get("name").asString val command = jo.get("name").asString
if (command != null) { if (command != null) {
COMMANDS.getOrElse(command, { PingCommand() }).run(jo) COMMANDS.getOrElse(command, { PingCommand() }).run(jo)
} else { } 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) { if (outgoing != null) {
outgoing!!.println(info) outgoing!!.println(info)
} else { } else {