mirror of
https://github.com/ethauvin/kobalt.git
synced 2025-04-26 08:27:12 -07:00
Clean up client and server.
This commit is contained in:
parent
205f10a53d
commit
f677b06901
2 changed files with 40 additions and 115 deletions
|
@ -5,16 +5,12 @@ import com.beust.kobalt.KobaltException
|
|||
import com.beust.kobalt.api.Kobalt
|
||||
import com.beust.kobalt.app.MainModule
|
||||
import com.beust.kobalt.homeDir
|
||||
import com.beust.kobalt.internal.GraphUtil
|
||||
import com.beust.kobalt.internal.KobaltSettings
|
||||
import com.beust.kobalt.maven.aether.Exceptions
|
||||
import com.beust.kobalt.misc.KFiles
|
||||
import com.beust.kobalt.misc.kobaltLog
|
||||
import com.beust.kobalt.misc.warn
|
||||
import com.google.gson.Gson
|
||||
import com.google.gson.JsonObject
|
||||
import com.google.gson.JsonParser
|
||||
import com.google.inject.Guice
|
||||
import com.google.inject.Inject
|
||||
import okhttp3.OkHttpClient
|
||||
import okhttp3.Request
|
||||
import okhttp3.Response
|
||||
|
@ -23,39 +19,22 @@ import okhttp3.ws.WebSocket
|
|||
import okhttp3.ws.WebSocketCall
|
||||
import okhttp3.ws.WebSocketListener
|
||||
import okio.Buffer
|
||||
import retrofit2.Call
|
||||
import retrofit2.Retrofit
|
||||
import retrofit2.converter.gson.GsonConverterFactory
|
||||
import retrofit2.http.GET
|
||||
import retrofit2.http.POST
|
||||
import retrofit2.http.Query
|
||||
import java.io.*
|
||||
import java.net.Socket
|
||||
import java.nio.file.Paths
|
||||
import java.util.*
|
||||
import java.util.concurrent.Executors
|
||||
import java.io.IOException
|
||||
|
||||
fun main(argv: Array<String>) {
|
||||
Kobalt.INJECTOR = Guice.createInjector(MainModule(Args(), KobaltSettings.readSettingsXml()))
|
||||
KobaltWebSocketClient().run()
|
||||
KobaltClient().run()
|
||||
}
|
||||
|
||||
interface Api {
|
||||
@GET("/ping")
|
||||
fun ping() : Call<String>
|
||||
|
||||
@Deprecated(message = "Replaced with /v1/getDependencies")
|
||||
@POST("/v0/getDependencies")
|
||||
fun getDependencies(@Query("buildFile") buildFile: String) : Call<List<RemoteDependencyData.GetDependenciesData>>
|
||||
}
|
||||
|
||||
class KobaltWebSocketClient : Runnable {
|
||||
class KobaltClient : Runnable {
|
||||
override fun run() {
|
||||
val client = OkHttpClient()
|
||||
val port = KobaltServer.port ?: 1240
|
||||
val url = "ws://localhost:$port/v1/getDependencyGraph"
|
||||
val buildFile = homeDir("kotlin/klaxon/kobalt/src/Build.kt")
|
||||
val request = Request.Builder()
|
||||
// .url("ws://echo.websocket.org")
|
||||
.url("ws://localhost:1239/v1/getDependencyGraph?buildFile=/Users/cedricbeust/t/Kandroid/kobalt/src" +
|
||||
"/Build.kt")
|
||||
.url("$url?buildFile=$buildFile")
|
||||
.build()
|
||||
var webSocket: WebSocket? = null
|
||||
val ws = WebSocketCall.create(client, request).enqueue(object: WebSocketListener {
|
||||
|
@ -84,8 +63,14 @@ class KobaltWebSocketClient : Runnable {
|
|||
} else {
|
||||
if (wsCommand.commandName == RemoteDependencyData.GetDependenciesData.NAME) {
|
||||
val dd = Gson().fromJson(wsCommand.payload, RemoteDependencyData.GetDependenciesData::class.java)
|
||||
println("Received dependency data: " + dd.projects.size + " projects"
|
||||
+ " error: " + dd.errorMessage)
|
||||
println("Received dependency data for " + dd.projects.size + " projects")
|
||||
dd.projects.forEach {
|
||||
println(" " + it.name)
|
||||
GraphUtil.displayGraph(it.compileDependencies,
|
||||
RemoteDependencyData.DependencyData::children,
|
||||
{ d: RemoteDependencyData.DependencyData, indent: String ->
|
||||
println(" " + indent + d.id) })
|
||||
}
|
||||
} else if (wsCommand.commandName == ProgressCommand.NAME) {
|
||||
val progress = Gson().fromJson(wsCommand.payload, ProgressCommand::class.java)
|
||||
println(progress.message + (progress.progress ?: ""))
|
||||
|
@ -98,83 +83,4 @@ class KobaltWebSocketClient : Runnable {
|
|||
}
|
||||
}
|
||||
|
||||
class KobaltClient : Runnable {
|
||||
private val service = Retrofit.Builder()
|
||||
.client(OkHttpClient.Builder().build())
|
||||
.baseUrl("http://localhost:1238")
|
||||
.addConverterFactory(GsonConverterFactory.create())
|
||||
.build()
|
||||
.create(Api::class.java)
|
||||
|
||||
override fun run() {
|
||||
|
||||
// val pong = service.ping().execute()
|
||||
// println("Result from ping: " + pong)
|
||||
|
||||
val buildFile = Paths.get(com.beust.kobalt.SystemProperties.homeDir,
|
||||
"kotlin/klaxon/kobalt/src/Build.kt").toString()
|
||||
val dependencies = service.getDependencies(buildFile)
|
||||
val response = dependencies.execute()
|
||||
if (response.isSuccessful) {
|
||||
println("Dependencies: $response")
|
||||
} else {
|
||||
println("Error calling getDependencies: " + response.errorBody().string())
|
||||
}
|
||||
println("")
|
||||
}
|
||||
}
|
||||
|
||||
class ServerProcess @Inject constructor(val serverFactory: KobaltServer.IFactory) {
|
||||
val SERVER_FILE = KFiles.joinDir(homeDir(KFiles.KOBALT_DOT_DIR, "kobaltServer.properties"))
|
||||
val KEY_PORT = "port"
|
||||
val executor = Executors.newFixedThreadPool(5)
|
||||
|
||||
fun launch() : Int {
|
||||
var port = launchPrivate()
|
||||
while (port == 0) {
|
||||
executor.submit {
|
||||
serverFactory.create(force = true,
|
||||
initCallback = { buildFile -> emptyList()},
|
||||
cleanUpCallback = {})
|
||||
.call()
|
||||
}
|
||||
// launchServer(ProcessUtil.findAvailablePort())
|
||||
port = launchPrivate()
|
||||
}
|
||||
return port
|
||||
}
|
||||
|
||||
private fun launchPrivate() : Int {
|
||||
var result = 0
|
||||
File(SERVER_FILE).let { serverFile ->
|
||||
if (serverFile.exists()) {
|
||||
val properties = Properties().apply {
|
||||
load(FileReader(serverFile))
|
||||
}
|
||||
|
||||
try {
|
||||
Socket("localhost", result).use { socket ->
|
||||
val outgoing = PrintWriter(socket.outputStream, true)
|
||||
val c: String = """{ "name": "ping"}"""
|
||||
outgoing.println(c)
|
||||
val ins = BufferedReader(InputStreamReader(socket.inputStream))
|
||||
var line = ins.readLine()
|
||||
val jo = JsonParser().parse(line) as JsonObject
|
||||
val jsonData = jo["data"]?.asString
|
||||
val dataObject = JsonParser().parse(jsonData) as JsonObject
|
||||
val received = JsonParser().parse(dataObject["received"].asString) as JsonObject
|
||||
if (received["name"].asString == "ping") {
|
||||
result = properties.getProperty(KEY_PORT).toInt()
|
||||
}
|
||||
}
|
||||
} catch(ex: IOException) {
|
||||
kobaltLog(1, "Couldn't connect to current server, launching a new one")
|
||||
Thread.sleep(1000)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -9,6 +9,7 @@ import com.beust.kobalt.misc.kobaltLog
|
|||
import com.google.inject.Inject
|
||||
import com.google.inject.assistedinject.Assisted
|
||||
import java.io.File
|
||||
import java.io.FileReader
|
||||
import java.io.FileWriter
|
||||
import java.lang.management.ManagementFactory
|
||||
import java.util.*
|
||||
|
@ -40,6 +41,28 @@ class KobaltServer @Inject constructor(@Assisted val force: Boolean, @Assisted @
|
|||
* Default response sent for calls that don't return a payload.
|
||||
*/
|
||||
val OK = "ok"
|
||||
|
||||
/**
|
||||
* Properties in the server file.
|
||||
*/
|
||||
val SERVER_FILE = KFiles.joinDir(homeDir(KFiles.KOBALT_DOT_DIR, "kobaltServer.properties"))
|
||||
val KEY_PORT = "port"
|
||||
val KEY_PID = "pid"
|
||||
|
||||
val port : Int? get() {
|
||||
var result: Int? = null
|
||||
File(SERVER_FILE).let {
|
||||
if (it.exists()) {
|
||||
val properties = Properties().apply {
|
||||
load(FileReader(it))
|
||||
}
|
||||
properties.getProperty(KEY_PORT)?.let {
|
||||
result = Integer.parseInt(it)
|
||||
}
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
// var outgoing: PrintWriter? = null
|
||||
|
@ -66,10 +89,6 @@ class KobaltServer @Inject constructor(@Assisted val force: Boolean, @Assisted @
|
|||
return port
|
||||
}
|
||||
|
||||
val SERVER_FILE = KFiles.joinDir(homeDir(KFiles.KOBALT_DOT_DIR, "kobaltServer.properties"))
|
||||
val KEY_PORT = "port"
|
||||
val KEY_PID = "pid"
|
||||
|
||||
private fun createServerFile(port: Int, force: Boolean) : Boolean {
|
||||
if (File(SERVER_FILE).exists() && ! force) {
|
||||
kobaltLog(1, "Server file $SERVER_FILE already exists. Another server is probably already running.")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue