Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added health check URL (/health-check) #616

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ sourceSets {
implementation(group="me.joshlarson", name="fast-json", version="3.0.1")
implementation(group="me.joshlarson", name="jlcommon-network", version="1.1.0")
implementation(group="me.joshlarson", name="jlcommon-argparse", version="0.9.5")
implementation(group="me.joshlarson", name="websocket", version="0.9.3")
implementation(group="me.joshlarson", name="websocket", version="0.9.4")
implementation(group="com.github.madsboddum", name="swgterrain", version="1.1.3")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import me.joshlarson.jlcommon.control.IntentChain
import me.joshlarson.jlcommon.log.Log
import me.joshlarson.websocket.common.WebSocketHandler
import me.joshlarson.websocket.common.parser.http.HttpRequest
import me.joshlarson.websocket.common.parser.http.HttpResponse
import me.joshlarson.websocket.common.parser.websocket.WebSocketCloseReason
import me.joshlarson.websocket.common.parser.websocket.WebsocketFrame
import me.joshlarson.websocket.common.parser.websocket.WebsocketFrameType
Expand All @@ -67,6 +68,7 @@ class NetworkClient(private val remoteAddress: SocketAddress, write: (ByteBuffer
private val inboundBuffer = ByteBuffer.allocate(INBOUND_BUFFER_SIZE)
private val intentChain = IntentChain()
private val connected = AtomicBoolean(true)
private val upgraded = AtomicBoolean(false)
private val status = AtomicReference(SessionStatus.DISCONNECTED)
private val wsProtocol = WebSocketServerProtocol(this, { data -> write(ByteBuffer.wrap(data)) }, closeChannel)
private val writeLock = ReentrantLock()
Expand All @@ -83,7 +85,8 @@ class NetworkClient(private val remoteAddress: SocketAddress, write: (ByteBuffer
fun close(reason: ConnectionStoppedReason = ConnectionStoppedReason.OTHER_SIDE_TERMINATED) {
if (connected.getAndSet(false)) {
serverDisconnectReason = reason
wsProtocol.sendClose(WebSocketCloseReason.NORMAL.statusCode.toInt(), reason.name)
if (upgraded.get())
wsProtocol.sendClose(WebSocketCloseReason.NORMAL.statusCode.toInt(), reason.name)
}
}

Expand All @@ -96,7 +99,15 @@ class NetworkClient(private val remoteAddress: SocketAddress, write: (ByteBuffer
inboundBuffer.position(0)
}

override fun onHttpRequest(obj: WebSocketHandler, request: HttpRequest) {
if (request.path == "/health-check") {
obj.sendHttpFrame(HttpResponse("HTTP/1.1", 200, "OK", mapOf("Content-Length" to "0"), ByteArray(0)))
StandardLog.onPlayerTrace(this, player, "requested health check")
}
}

override fun onUpgrade(obj: WebSocketHandler, request: HttpRequest) {
upgraded.set(true)
val urlParameters = request.urlParameters
val username = getUrlParameter(urlParameters, "username", true) ?: return
val password = getUrlParameter(urlParameters, "password", true) ?: return
Expand Down Expand Up @@ -133,7 +144,7 @@ class NetworkClient(private val remoteAddress: SocketAddress, write: (ByteBuffer
}

override fun onOpened() {
StandardLog.onPlayerTrace(this, player, "connecting")
StandardLog.onPlayerTrace(this, player, "connected [TCP]")
status.set(SessionStatus.CONNECTING)
intentChain.broadcastAfter(ConnectionOpenedIntent(player))
}
Expand Down