mirror of
https://github.com/ethauvin/kobalt.git
synced 2025-04-26 08:27:12 -07:00
Better client/server.
This commit is contained in:
parent
457f190eb3
commit
246b8a90cb
2 changed files with 25 additions and 21 deletions
|
@ -6,6 +6,7 @@ import com.beust.kobalt.mainNoExit
|
||||||
import com.beust.kobalt.misc.log
|
import com.beust.kobalt.misc.log
|
||||||
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.InputStreamReader
|
import java.io.InputStreamReader
|
||||||
import java.io.PrintWriter
|
import java.io.PrintWriter
|
||||||
import java.net.ConnectException
|
import java.net.ConnectException
|
||||||
|
@ -16,28 +17,31 @@ public class KobaltClient @Inject constructor() : Runnable {
|
||||||
var outgoing: PrintWriter? = null
|
var outgoing: PrintWriter? = null
|
||||||
|
|
||||||
override fun run() {
|
override fun run() {
|
||||||
val portNumber = Args.DEFAULT_SERVER_PORT
|
val portNumber = 1234
|
||||||
|
|
||||||
Executors.newFixedThreadPool(1).submit {
|
|
||||||
log(1, "Lauching Kobalt main")
|
|
||||||
mainNoExit(arrayOf("--dev", "--server", "assemble"))
|
|
||||||
}
|
|
||||||
|
|
||||||
var done = false
|
var done = false
|
||||||
var attempts = 1
|
var attempts = 1
|
||||||
while (attempts < 3 && ! done) {
|
while (attempts < 3 && ! done) {
|
||||||
try {
|
try {
|
||||||
val socket = Socket("localhost", portNumber)
|
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
|
done = true
|
||||||
log(1, "Launching listening server")
|
val ins = BufferedReader(InputStreamReader(socket.inputStream))
|
||||||
var fromServer = ins.readLine()
|
var fromServer = ins.readLine()
|
||||||
while (fromServer != null) {
|
while (fromServer != null) {
|
||||||
log(1, "From server: " + fromServer);
|
log(1, "Response from server:\n" + fromServer)
|
||||||
if (fromServer.equals("Bye."))
|
|
||||||
break;
|
|
||||||
fromServer = ins.readLine()
|
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) {
|
} 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)
|
||||||
|
|
|
@ -40,8 +40,7 @@ public class KobaltServer @Inject constructor(val args: Args, val executors: Kob
|
||||||
var inputLine = ins.readLine()
|
var inputLine = ins.readLine()
|
||||||
while (inputLine != null) {
|
while (inputLine != null) {
|
||||||
log(1, "Received $inputLine")
|
log(1, "Received $inputLine")
|
||||||
val command = getCommand(inputLine)
|
runCommand(inputLine)
|
||||||
command?.run()
|
|
||||||
if (inputLine.equals("Bye."))
|
if (inputLine.equals("Bye."))
|
||||||
break;
|
break;
|
||||||
inputLine = ins.readLine()
|
inputLine = ins.readLine()
|
||||||
|
@ -49,16 +48,16 @@ public class KobaltServer @Inject constructor(val args: Args, val executors: Kob
|
||||||
}
|
}
|
||||||
|
|
||||||
interface Command {
|
interface Command {
|
||||||
fun run()
|
fun run(jo: JsonObject)
|
||||||
}
|
}
|
||||||
|
|
||||||
inner class PingCommand(val s: String) : Command {
|
inner class PingCommand() : Command {
|
||||||
override fun run() = sendInfo("{ \"response\" : \"$s\" }")
|
override fun run(jo: JsonObject) = sendInfo("{ \"response\" : \"${jo.toString()}\" }")
|
||||||
}
|
}
|
||||||
|
|
||||||
inner class GetDependenciesCommand() : Command {
|
inner class GetDependenciesCommand() : Command {
|
||||||
override fun run() {
|
override fun run(jo: JsonObject) {
|
||||||
val buildFile = BuildFile(Paths.get("c:\\Users\\cbeust\\java\\jcommander\\Build.kt"), "JCommander build")
|
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))
|
info -> sendInfo(toJson(info))
|
||||||
|
@ -104,14 +103,13 @@ public class KobaltServer @Inject constructor(val args: Args, val executors: Kob
|
||||||
Pair("GetDependencies", GetDependenciesCommand())
|
Pair("GetDependencies", GetDependenciesCommand())
|
||||||
)
|
)
|
||||||
|
|
||||||
private fun getCommand(json: String): Command? {
|
private fun runCommand(json: String) {
|
||||||
val jo = JsonParser().parse(json) as 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) {
|
||||||
return COMMANDS.getOrElse(command, { PingCommand(command) })
|
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: $json")
|
||||||
return null
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -126,3 +124,5 @@ public class KobaltServer @Inject constructor(val args: Args, val executors: Kob
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue