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

Better client/server.

This commit is contained in:
Cedric Beust 2015-10-19 23:59:03 -07:00
parent 457f190eb3
commit 246b8a90cb
2 changed files with 25 additions and 21 deletions

View file

@ -6,6 +6,7 @@ import com.beust.kobalt.mainNoExit
import com.beust.kobalt.misc.log
import com.google.inject.Inject
import java.io.BufferedReader
import java.io.BufferedWriter
import java.io.InputStreamReader
import java.io.PrintWriter
import java.net.ConnectException
@ -16,28 +17,31 @@ public class KobaltClient @Inject constructor() : Runnable {
var outgoing: PrintWriter? = null
override fun run() {
val portNumber = Args.DEFAULT_SERVER_PORT
Executors.newFixedThreadPool(1).submit {
log(1, "Lauching Kobalt main")
mainNoExit(arrayOf("--dev", "--server", "assemble"))
}
val portNumber = 1234
var done = false
var attempts = 1
while (attempts < 3 && ! done) {
try {
val socket = Socket("localhost", portNumber)
val ins = BufferedReader(InputStreamReader(socket.inputStream))
outgoing = PrintWriter(socket.outputStream, true)
val c : String = "{ \"name\":\"GetDependencies\", \"buildFile\": \"c:\\\\users\\\\cbeust\\\\java\\\\testng\\\\Build.kt\"}"
outgoing!!.println(c)
done = true
log(1, "Launching listening server")
val ins = BufferedReader(InputStreamReader(socket.inputStream))
var fromServer = ins.readLine()
while (fromServer != null) {
log(1, "From server: " + fromServer);
if (fromServer.equals("Bye."))
break;
log(1, "Response from server:\n" + fromServer)
fromServer = 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)

View file

@ -40,8 +40,7 @@ public class KobaltServer @Inject constructor(val args: Args, val executors: Kob
var inputLine = ins.readLine()
while (inputLine != null) {
log(1, "Received $inputLine")
val command = getCommand(inputLine)
command?.run()
runCommand(inputLine)
if (inputLine.equals("Bye."))
break;
inputLine = ins.readLine()
@ -49,16 +48,16 @@ public class KobaltServer @Inject constructor(val args: Args, val executors: Kob
}
interface Command {
fun run()
fun run(jo: JsonObject)
}
inner class PingCommand(val s: String) : Command {
override fun run() = sendInfo("{ \"response\" : \"$s\" }")
inner class PingCommand() : Command {
override fun run(jo: JsonObject) = sendInfo("{ \"response\" : \"${jo.toString()}\" }")
}
inner class GetDependenciesCommand() : Command {
override fun run() {
val buildFile = BuildFile(Paths.get("c:\\Users\\cbeust\\java\\jcommander\\Build.kt"), "JCommander build")
override fun run(jo: JsonObject) {
val buildFile = BuildFile(Paths.get(jo.get("buildFile").asString), "GetDependenciesCommand")
val scriptCompiler = buildFileCompilerFactory.create(listOf(buildFile))
scriptCompiler.observable.subscribe {
info -> sendInfo(toJson(info))
@ -104,14 +103,13 @@ public class KobaltServer @Inject constructor(val args: Args, val executors: Kob
Pair("GetDependencies", GetDependenciesCommand())
)
private fun getCommand(json: String): Command? {
private fun runCommand(json: String) {
val jo = JsonParser().parse(json) as JsonObject
val command = jo.get("name").asString
if (command != null) {
return COMMANDS.getOrElse(command, { PingCommand(command) })
COMMANDS.getOrElse(command, { PingCommand() }).run(jo)
} else {
error("Did not find a name in command: $json")
return null
}
}
@ -126,3 +124,5 @@ public class KobaltServer @Inject constructor(val args: Args, val executors: Kob
}
}
}