Skip to content

Commit

Permalink
Merge pull request #616 from Josh-Larson/cu_quality_assurance
Browse files Browse the repository at this point in the history
Added health check URL (/health-check)
  • Loading branch information
Josh-Larson authored Jul 6, 2022
2 parents 006eb34 + dcb7575 commit d8d107f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
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

0 comments on commit d8d107f

Please sign in to comment.