EditorServer.kt
/**
* Dracon - An IntelliJ-Pijul integration.
* Copyright 2021 JonathanxD <jhrldev@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.github.jonathanxd.dracon.editor
import com.github.jonathanxd.dracon.util.findBinary
import java.net.ServerSocket
import java.net.URI
import java.net.http.HttpClient
import java.net.http.HttpRequest
import java.net.http.HttpResponse
import java.time.Duration
import java.time.Instant
class EditorServer(val port: Int, val isClosed: () -> Boolean) {
private var connected: Boolean = false
private val client = HttpClient.newHttpClient()
private lateinit var content: String
fun connectAndRetrieveContent(): Boolean {
val start = Instant.now()
while (!this.isClosed()) {
if (Duration.between(start, Instant.now()).seconds > 20) {
return false
}
try {
val response = this.client.send(
HttpRequest.newBuilder(URI.create("http://0.0.0.0:$port/read"))
.GET()
.version(HttpClient.Version.HTTP_2)
.build(),
HttpResponse.BodyHandlers.ofString()
)
if (response.statusCode() == 200) {
this.connected = true
this.content = response.body();
break;
}
} catch (t: Throwable) {
}
Thread.sleep(200)
}
return this.connected
}
fun content() = this.content
fun connected() = this.connected
fun write(text: String) {
if (this.isClosed()) {
throw IllegalStateException("Could not write: editor-server is already closed.")
}
if (!this.connected) {
throw IllegalStateException("Could not write: Not connected to editor-server!")
}
val response = this.client.send(
HttpRequest.newBuilder(URI.create("http://0.0.0.0:$port/write"))
.POST(HttpRequest.BodyPublishers.ofString(text))
.version(HttpClient.Version.HTTP_2)
.build(),
HttpResponse.BodyHandlers.ofString()
)
handleResponse(response)
}
private fun handleResponse(response: HttpResponse<*>) {
if (response.statusCode() != 200) {
throw IllegalStateException("editor-server responded with $response.")
}
}
fun close() {
if (this.isClosed()) {
throw IllegalStateException("Could not close: editor-server is already closed.")
}
if (!this.connected) {
throw IllegalStateException("Could not close: Not connected to editor-server!")
}
try {
// This will make the connection close immediately
// thus throwing an exception.
this.client.send(
HttpRequest.newBuilder(URI.create("http://0.0.0.0:$port/close"))
.GET()
.version(HttpClient.Version.HTTP_2)
.build(),
HttpResponse.BodyHandlers.ofString()
)
} catch (t: Throwable) {
}
}
}
fun freePort(): Int = ServerSocket(0).use {
it.localPort
}
fun editorServerPath(): String = findBinary("editor-server")