From 3a5295efe8f96929c08f16632aee00f9cceb86e7 Mon Sep 17 00:00:00 2001 From: Robinson Date: Tue, 28 Nov 2023 20:53:37 +0100 Subject: [PATCH] Cleaned logging --- src/dorkbox/network/Client.kt | 38 ++++++++++++------- src/dorkbox/network/handshake/PubSub.kt | 10 ++--- .../network/handshake/ServerHandshake.kt | 33 ++++++++++------ 3 files changed, 48 insertions(+), 33 deletions(-) diff --git a/src/dorkbox/network/Client.kt b/src/dorkbox/network/Client.kt index 18403f7c..103fc85a 100644 --- a/src/dorkbox/network/Client.kt +++ b/src/dorkbox/network/Client.kt @@ -42,6 +42,7 @@ import org.slf4j.LoggerFactory import java.net.Inet4Address import java.net.Inet6Address import java.net.InetAddress +import java.util.* import java.util.concurrent.* /** @@ -797,11 +798,13 @@ open class Client(config: ClientConfiguration = ClientC val pubSub = clientConnection.connectionInfo - val connectionType = if (connectionInfo.bufferedMessages) { + val bufferedMessages = connectionInfo.bufferedMessages + val connectionType = if (bufferedMessages) { "buffered connection" } else { "connection" } + val connectionTypeCaps = connectionType.replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() } val logInfo = pubSub.getLogInfo(logger.isDebugEnabled) if (logger.isDebugEnabled) { @@ -815,7 +818,7 @@ open class Client(config: ClientConfiguration = ClientC endPoint = this, connectionInfo = clientConnection.connectionInfo, publicKeyValidation = validateRemoteAddress, - enableBufferedMessages = connectionInfo.bufferedMessages, + enableBufferedMessages = bufferedMessages, cryptoKey = connectionInfo.secretKey )) @@ -824,11 +827,11 @@ open class Client(config: ClientConfiguration = ClientC if (!handshakeConnection.pubSub.isIpc) { // NOTE: Client can ALWAYS connect to the server. The server makes the decision if the client can connect or not. if (logger.isTraceEnabled) { - logger.trace("[${handshakeConnection.details}] (${handshake.connectKey}) Buffered connection (${newConnection.id}) adding new signature for [$addressString -> ${connectionInfo.publicKey.toHexString()}]") + logger.trace("[${handshakeConnection.details}] (${handshake.connectKey}) $connectionTypeCaps (${newConnection.id}) adding new signature for [$addressString -> ${connectionInfo.publicKey.toHexString()}]") } else if (logger.isDebugEnabled) { - logger.debug("[${handshakeConnection.details}] Buffered connection (${newConnection.id}) adding new signature for [$addressString -> ${connectionInfo.publicKey.toHexString()}]") + logger.debug("[${handshakeConnection.details}] $connectionTypeCaps (${newConnection.id}) adding new signature for [$addressString -> ${connectionInfo.publicKey.toHexString()}]") } else if (logger.isInfoEnabled) { - logger.info("[${handshakeConnection.details}] Buffered connection adding new signature for [$addressString -> ${connectionInfo.publicKey.toHexString()}]") + logger.info("[${handshakeConnection.details}] $connectionTypeCaps adding new signature for [$addressString -> ${connectionInfo.publicKey.toHexString()}]") } storage.addRegisteredServerKey(address!!, connectionInfo.publicKey) @@ -853,9 +856,9 @@ open class Client(config: ClientConfiguration = ClientC handshakeConnection.close(this) if (logger.isTraceEnabled) { - logger.debug("[${handshakeConnection.details}] (${handshake.connectKey}) Buffered connection (${newConnection.id}) done with handshake.") + logger.debug("[${handshakeConnection.details}] (${handshake.connectKey}) $connectionTypeCaps (${newConnection.id}) done with handshake.") } else if (logger.isDebugEnabled) { - logger.debug("[${handshakeConnection.details}] Buffered connection (${newConnection.id}) done with handshake.") + logger.debug("[${handshakeConnection.details}] $connectionTypeCaps (${newConnection.id}) done with handshake.") } connection0 = newConnection @@ -937,7 +940,6 @@ open class Client(config: ClientConfiguration = ClientC if (autoReconnect) { // clients can reconnect automatically ONLY if there are driver errors, otherwise it's explicit! eventDispatch.CLOSE.launch { - logger.error("MUST AUTORECONNECT STARTING ON CLOSE ***********************************") waitForEndpointShutdown() // also wait for everyone else to shutdown!! @@ -1017,8 +1019,11 @@ open class Client(config: ClientConfiguration = ClientC return if (c != null) { c.send(message) + } else if (isShutdown()) { + logger.error("Cannot send a message '${message::class.java}' when there is no connection! (We are shutdown)") + false } else { - val exception = TransmitException("Cannot send a message when there is no connection!") + val exception = TransmitException("Cannot send message '${message::class.java}' when there is no connection!") listenerManager.notifyError(exception) false } @@ -1037,8 +1042,11 @@ open class Client(config: ClientConfiguration = ClientC return if (c != null) { @Suppress("UNCHECKED_CAST") c.send(message, onSuccessCallback as Connection.() -> Unit) + } else if (isShutdown()) { + logger.error("Cannot send-sync message '${message::class.java}' when there is no connection! (We are shutdown)") + false } else { - val exception = TransmitException("Cannot send-sync a message when there is no connection!") + val exception = TransmitException("Cannot send-sync message '${message::class.java}' when there is no connection!") listenerManager.notifyError(exception) false } @@ -1054,14 +1062,16 @@ open class Client(config: ClientConfiguration = ClientC fun ping(function: Ping.() -> Unit): Boolean { val c = connection0 - if (c != null) { - return c.ping(function) + return if (c != null) { + c.ping(function) + } else if (isShutdown()) { + logger.error("Cannot send a ping when there is no connection! (We are shutdown)") + false } else { val exception = TransmitException("Cannot send a ping when there is no connection!") listenerManager.notifyError(exception) + false } - - return false } /** diff --git a/src/dorkbox/network/handshake/PubSub.kt b/src/dorkbox/network/handshake/PubSub.kt index 72222d23..4f444f15 100644 --- a/src/dorkbox/network/handshake/PubSub.kt +++ b/src/dorkbox/network/handshake/PubSub.kt @@ -41,17 +41,13 @@ data class PubSub( fun getLogInfo(extraDetails: Boolean): String { return if (isIpc) { val prefix = if (tagName.isNotEmpty()) { - EndPoint.IPC_NAME + " $tagName" + EndPoint.IPC_NAME + " ($tagName)" } else { EndPoint.IPC_NAME } if (extraDetails) { - if (tagName.isNotEmpty()) { - "$prefix sessionID: p=${sessionIdPub} s=${sessionIdSub}, streamID: p=${streamIdPub} s=${streamIdSub}, reg: p=${pub.registrationId()} s=${sub.registrationId()}" - } else { - "$prefix sessionID: p=${sessionIdPub} s=${sessionIdSub}, streamID: p=${streamIdPub} s=${streamIdSub}, reg: p=${pub.registrationId()} s=${sub.registrationId()}" - } + "$prefix sessionID: p=${sessionIdPub} s=${sessionIdSub}, streamID: p=${streamIdPub} s=${streamIdSub}, reg: p=${pub.registrationId()} s=${sub.registrationId()}" } else { prefix } @@ -63,7 +59,7 @@ data class PubSub( } if (tagName.isNotEmpty()) { - prefix += " $tagName" + prefix += " ($tagName)" } if (extraDetails) { diff --git a/src/dorkbox/network/handshake/ServerHandshake.kt b/src/dorkbox/network/handshake/ServerHandshake.kt index 280482bb..79d74d29 100644 --- a/src/dorkbox/network/handshake/ServerHandshake.kt +++ b/src/dorkbox/network/handshake/ServerHandshake.kt @@ -31,6 +31,7 @@ import net.jodah.expiringmap.ExpiringMap import org.slf4j.Logger import java.net.Inet4Address import java.net.InetAddress +import java.util.* import java.util.concurrent.* @@ -128,11 +129,17 @@ internal class ServerHandshake( return true } + val connectionType = if (newConnection.enableBufferedMessages) { + "Buffered connection" + } else { + "Connection" + } + // Server is the "source", client mirrors the server if (logger.isTraceEnabled) { - logger.trace("[${newConnection}] (${message.connectKey}) Buffered connection (${newConnection.id}) done with handshake.") + logger.trace("[${newConnection}] (${message.connectKey}) $connectionType (${newConnection.id}) done with handshake.") } else if (logger.isDebugEnabled) { - logger.debug("[${newConnection}] Buffered connection (${newConnection.id}) done with handshake.") + logger.debug("[${newConnection}] $connectionType (${newConnection.id}) done with handshake.") } newConnection.setImage() @@ -239,9 +246,8 @@ internal class ServerHandshake( ): Boolean { val serialization = config.serialization - val clientTagName = message.tag.let { if (it.isEmpty()) "" else "[$it]" } - if (clientTagName.length > 34) { - // 34 to account for [] + val clientTagName = message.tag + if (clientTagName.length > 32) { listenerManager.notifyError(ServerHandshakeException("[$logInfo] Connection not allowed! Invalid tag name.")) return false } @@ -361,6 +367,9 @@ internal class ServerHandshake( "connection" } + val connectionTypeCaps = connectionType.replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() } + + val logInfo = newConnectionDriver.pubSub.getLogInfo(logger.isDebugEnabled) if (logger.isDebugEnabled) { logger.debug("Creating new $connectionType to $logInfo") @@ -412,9 +421,9 @@ internal class ServerHandshake( pendingConnections[message.connectKey] = newConnection if (logger.isTraceEnabled) { - logger.trace("[$logInfo] (${message.connectKey}) buffered connection (${newConnection.id}) responding to handshake hello.") + logger.trace("[$logInfo] (${message.connectKey}) $connectionType (${newConnection.id}) responding to handshake hello.") } else if (logger.isDebugEnabled) { - logger.debug("[$logInfo] Buffered connection (${newConnection.id}) responding to handshake hello.") + logger.debug("[$logInfo] $connectionTypeCaps (${newConnection.id}) responding to handshake hello.") } // this tells the client all the info to connect. @@ -477,9 +486,8 @@ internal class ServerHandshake( return false } - val clientTagName = message.tag.let { if (it.isEmpty()) "" else "[$it]" } - if (clientTagName.length > 34) { - // 34 to account for [] + val clientTagName = message.tag + if (clientTagName.length > 32) { listenerManager.notifyError(ServerHandshakeException("[$logInfo] Connection not allowed! Invalid tag name.")) return false } @@ -628,6 +636,7 @@ internal class ServerHandshake( "connection" } + val connectionTypeCaps = connectionType.replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() } val logInfo = newConnectionDriver.pubSub.getLogInfo(logger.isDebugEnabled) if (logger.isDebugEnabled) { @@ -676,9 +685,9 @@ internal class ServerHandshake( pendingConnections[message.connectKey] = newConnection if (logger.isTraceEnabled) { - logger.trace("[$logInfo] Buffered connection (${newConnection.id}) responding to handshake hello.") + logger.trace("[$logInfo] $connectionTypeCaps (${newConnection.id}) responding to handshake hello.") } else if (logger.isDebugEnabled) { - logger.debug("[$logInfo] Buffered connection (${newConnection.id}) responding to handshake hello.") + logger.debug("[$logInfo] $connectionTypeCaps (${newConnection.id}) responding to handshake hello.") } // this tells the client all the info to connect.