try to: fix transmit crash and polish noping

This commit is contained in:
Nikky Ai 2018-01-23 17:50:11 +01:00
parent f05de5188c
commit c4754dd8c7
5 changed files with 49 additions and 30 deletions

View File

@ -1,13 +1,13 @@
package civilengineering
class Util {
companion object {
object Util {
const val ZWSP: Char = '\u200b'
//Inserts a zero-width space at index 1 in the string'
@JvmStatic
fun antiping(str: String): String {
return str[0].toString()+ZWSP+str.substring(1)
}
fun String.antiping(): String {
return this[0].toString()+ZWSP+this.substring(1)
}
}

View File

@ -13,7 +13,8 @@ import java.util.concurrent.ConcurrentLinkedQueue
object MessageHandler {
private var connected = false
private var sendErrors = 0
fun HttpRequestBase.authorize() {
if (cfg!!.connect.authToken.isNotEmpty() && getHeaders("Authorization").isEmpty())
setHeader("Authorization", "Bearer " + cfg!!.connect.authToken)
@ -41,9 +42,11 @@ object MessageHandler {
var rcvQueue = ConcurrentLinkedQueue<ApiMessage>()
fun transmit(msg: ApiMessage) {
if (connected) {
CivilEngineering.logger.debug("Transmitting: " + msg)
transmitMessage(msg)
}
}
fun stop() {
CivilEngineering.logger.info("Closing bridge connection...")
@ -51,6 +54,7 @@ object MessageHandler {
streamConnection.close()
CivilEngineering.logger.info("Bridge connection closed!")
connected = false
}
fun start(): Boolean {
@ -60,13 +64,14 @@ object MessageHandler {
if (!streamConnection.isAlive) {
streamConnection.start()
// MessageHandler.transmit(ApiMessage(text="bridge connected", username="Server"))
return true
connected = true
return connected
}
return false
return connected
}
@Throws(IOException::class)
private fun transmitMessage(message: ApiMessage) {
try {
//open a connection
val client = HttpClients.createDefault()
val post = HttpPost(cfg!!.connect.url + "/api/message")
@ -79,6 +84,15 @@ object MessageHandler {
if (code != 200) {
CivilEngineering.logger.error("Server returned $code for $post")
}
sendErrors = 0
} catch (e: IOException) {
CivilEngineering.logger.error("sending message caused $e")
sendErrors++
if(sendErrors > 5) {
CivilEngineering.logger.error("caught too many errors, closing bridge")
stop()
}
}
}
}

View File

@ -1,5 +1,6 @@
package civilengineering.eventhandlers
import civilengineering.Util.antiping
import civilengineering.CivilEngineering
import civilengineering.CivilEngineeringConfig
import civilengineering.Util
@ -13,10 +14,11 @@ class AdvancementEventHandler {
@SubscribeEvent
fun handleAdvancements(event: AdvancementEvent) {
if(cfg!!.relay.advancements && event.advancement.display != null) {
val zwsp: Char = '\u200B'
var player: String = Util.antiping(event.entityPlayer.name)
MessageHandler.transmit(ApiMessage("Server",player+" has earned the advancement "+event.advancement.displayText.unformattedText))
val player = event.entityPlayer.name.antiping()
val content = event.advancement.displayText.unformattedText
MessageHandler.transmit(ApiMessage(username = "Server",
text = "$player has earned the advancement $content"
))
}
}
}

View File

@ -31,7 +31,10 @@ class CommandEventHandler {
}
message = message.trim { it <= ' ' }
MessageHandler.transmit(ApiMessage(event.sender.name,message))
MessageHandler.transmit(ApiMessage(
username = event.sender.name,
text = message
))
}
}
}

View File

@ -1,6 +1,6 @@
package civilengineering.eventhandlers
import civilengineering.Util
import civilengineering.Util.antiping
import civilengineering.bridge.ApiMessage
import civilengineering.bridge.MessageHandler
import civilengineering.cfg
@ -15,7 +15,7 @@ class DeathEventHandler {
val entity = event.entityLiving
if (entity is EntityPlayer) {
var message: String = entity.getCombatTracker().deathMessage.unformattedText
MessageHandler.transmit(ApiMessage(username = "Server", text = Util.antiping(message)))
MessageHandler.transmit(ApiMessage(username = "Server", text = message.antiping()))
}
}
}