diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/Amethyst.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/Amethyst.kt index 966c8d39e..47da7cfc6 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/Amethyst.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/Amethyst.kt @@ -41,6 +41,7 @@ import com.vitorpamplona.amethyst.service.ots.OtsBlockHeightCache import com.vitorpamplona.amethyst.service.playback.diskCache.VideoCache import com.vitorpamplona.amethyst.service.playback.diskCache.VideoCacheFactory import com.vitorpamplona.amethyst.service.relayClient.CacheClientConnector +import com.vitorpamplona.amethyst.service.relayClient.RelayLogger import com.vitorpamplona.amethyst.service.relayClient.authCommand.model.AuthCoordinator import com.vitorpamplona.amethyst.service.relayClient.notifyCommand.model.NotifyCoordinator import com.vitorpamplona.amethyst.service.relayClient.reqCommand.RelaySubscriptionsCoordinator @@ -109,6 +110,8 @@ class Amethyst : Application() { // Authenticates with relays. val authCoordinator = AuthCoordinator(client) + val logger = if (isDebug) RelayLogger(client) else null + // Organizes cache clearing val trimmingService = MemoryTrimmingService(cache) diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/relayClient/RelayLogger.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/relayClient/RelayLogger.kt new file mode 100644 index 000000000..b25d09c80 --- /dev/null +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/relayClient/RelayLogger.kt @@ -0,0 +1,69 @@ +/** + * Copyright (c) 2024 Vitor Pamplona + * + * 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.vitorpamplona.amethyst.service.relayClient + +import android.util.Log +import com.vitorpamplona.ammolite.relays.NostrClient +import com.vitorpamplona.ammolite.relays.Relay +import com.vitorpamplona.quartz.nip01Core.core.Event + +/** + * Listens to NostrClient's onNotify messages from the relay + */ +class RelayLogger( + val client: NostrClient, +) { + companion object { + val TAG = RelayLogger::class.java.simpleName + } + + private val clientListener = + object : NostrClient.Listener { + /** A new message was received */ + override fun onEvent( + event: Event, + subscriptionId: String, + relay: Relay, + afterEOSE: Boolean, + ) { + Log.d(TAG, "Relay onEVENT ${relay.url} ($subscriptionId - $afterEOSE) ${event.toJson()}") + } + + override fun onSend( + relay: Relay, + msg: String, + success: Boolean, + ) { + Log.d(TAG, "Relay send ${relay.url} (${msg.length} chars) $msg") + } + } + + init { + Log.d(TAG, "Init, Subscribe") + client.subscribe(clientListener) + } + + fun destroy() { + // makes sure to run + Log.d(TAG, "Destroy, Unsubscribe") + client.unsubscribe(clientListener) + } +} diff --git a/ammolite/src/main/java/com/vitorpamplona/ammolite/relays/datasources/RelayLogger.kt b/ammolite/src/main/java/com/vitorpamplona/ammolite/relays/datasources/RelayLogger.kt new file mode 100644 index 000000000..71f9c7c93 --- /dev/null +++ b/ammolite/src/main/java/com/vitorpamplona/ammolite/relays/datasources/RelayLogger.kt @@ -0,0 +1,66 @@ +/** + * Copyright (c) 2024 Vitor Pamplona + * + * 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.vitorpamplona.ammolite.relays.datasources + +import android.util.Log +import com.vitorpamplona.ammolite.relays.NostrClient +import com.vitorpamplona.ammolite.relays.Relay +import com.vitorpamplona.quartz.nip01Core.core.Event + +/** + * Listens to NostrClient's onNotify messages from the relay + */ +class RelayLogger( + val client: NostrClient, + val notify: (message: String, relay: Relay) -> Unit, +) { + private val clientListener = + object : NostrClient.Listener { + /** A new message was received */ + override fun onEvent( + event: Event, + subscriptionId: String, + relay: Relay, + afterEOSE: Boolean, + ) { + Log.d("Relay", "Relay onEVENT ${relay.url} ($subscriptionId - $afterEOSE) ${event.toJson()}") + } + + override fun onSend( + relay: Relay, + msg: String, + success: Boolean, + ) { + Log.d("Relay", "Relay send ${relay.url} (${msg.length} chars) $msg") + } + } + + init { + Log.d("${this.javaClass.simpleName}", "Init, Subscribe") + client.subscribe(clientListener) + } + + fun destroy() { + // makes sure to run + Log.d("${this.javaClass.simpleName}", "Destroy, Unsubscribe") + client.unsubscribe(clientListener) + } +} diff --git a/ammolite/src/main/java/com/vitorpamplona/ammolite/relays/datasources/RelayNotifier.kt b/ammolite/src/main/java/com/vitorpamplona/ammolite/relays/datasources/RelayNotifier.kt index dcf4ec5d4..8a8dd9e0e 100644 --- a/ammolite/src/main/java/com/vitorpamplona/ammolite/relays/datasources/RelayNotifier.kt +++ b/ammolite/src/main/java/com/vitorpamplona/ammolite/relays/datasources/RelayNotifier.kt @@ -31,6 +31,10 @@ class RelayNotifier( val client: NostrClient, val notify: (message: String, relay: Relay) -> Unit, ) { + companion object { + val TAG = RelayNotifier::class.java.simpleName + } + private val clientListener = object : NostrClient.Listener { override fun onNotify( @@ -42,13 +46,13 @@ class RelayNotifier( } init { - Log.d("${this.javaClass.simpleName}", "Init, Subscribe") + Log.d(TAG, "Init, Subscribe") client.subscribe(clientListener) } fun destroy() { // makes sure to run - Log.d("${this.javaClass.simpleName}", "Destroy, Unsubscribe") + Log.d(TAG, "Destroy, Unsubscribe") client.unsubscribe(clientListener) } } diff --git a/quartz/src/main/java/com/vitorpamplona/quartz/nip01Core/relay/SimpleClientRelay.kt b/quartz/src/main/java/com/vitorpamplona/quartz/nip01Core/relay/SimpleClientRelay.kt index 5adde10bf..ff1e6bff3 100644 --- a/quartz/src/main/java/com/vitorpamplona/quartz/nip01Core/relay/SimpleClientRelay.kt +++ b/quartz/src/main/java/com/vitorpamplona/quartz/nip01Core/relay/SimpleClientRelay.kt @@ -418,8 +418,6 @@ class SimpleClientRelay( val result = it.send(str) listener.onSend(this@SimpleClientRelay, str, result) stats.addBytesSent(str.bytesUsedInMemory()) - - Log.d("Relay", "Relay send $url (${str.length} chars) $str") } }