From d887c49ab342d1d48d79e46fa6632852c4ab40b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20K=C3=BCrten?= <sebastian@mobanisto.de> Date: Wed, 28 Sep 2022 14:51:23 +0200 Subject: [PATCH] Address review feedback --- .../java/org/briarproject/mailbox/cli/Main.kt | 7 +- ...letons.kt => MailboxLibEagerSingletons.kt} | 2 +- .../mailbox/lib/AbstractMailbox.kt | 135 ++++++++++++++++++ .../org/briarproject/mailbox/lib/Mailbox.kt | 135 +----------------- ...ionComponent.kt => MailboxLibComponent.kt} | 4 +- .../mailbox/lib/ProductionMailbox.kt | 15 -- .../briarproject/mailbox/lib/TestMailbox.kt | 8 +- .../mailbox/lib/MailboxLibTest.kt | 1 - 8 files changed, 151 insertions(+), 156 deletions(-) rename mailbox-lib/src/main/java/org/briarproject/mailbox/core/{JavaLibEagerSingletons.kt => MailboxLibEagerSingletons.kt} (94%) create mode 100644 mailbox-lib/src/main/java/org/briarproject/mailbox/lib/AbstractMailbox.kt rename mailbox-lib/src/main/java/org/briarproject/mailbox/lib/{MailboxLibProductionComponent.kt => MailboxLibComponent.kt} (92%) delete mode 100644 mailbox-lib/src/main/java/org/briarproject/mailbox/lib/ProductionMailbox.kt diff --git a/mailbox-cli/src/main/java/org/briarproject/mailbox/cli/Main.kt b/mailbox-cli/src/main/java/org/briarproject/mailbox/cli/Main.kt index b0d58f05..da436f36 100644 --- a/mailbox-cli/src/main/java/org/briarproject/mailbox/cli/Main.kt +++ b/mailbox-cli/src/main/java/org/briarproject/mailbox/cli/Main.kt @@ -26,7 +26,7 @@ import com.github.ajalt.clikt.parameters.options.counted import com.github.ajalt.clikt.parameters.options.flag import com.github.ajalt.clikt.parameters.options.option import org.briarproject.mailbox.core.system.InvalidIdException -import org.briarproject.mailbox.lib.ProductionMailbox +import org.briarproject.mailbox.lib.Mailbox import org.slf4j.LoggerFactory.getLogger class Main : CliktCommand( @@ -71,8 +71,7 @@ class Main : CliktCommand( getLogger(this.javaClass).debug("Hello Mailbox") println("Hello Mailbox") - val mailbox = ProductionMailbox() - mailbox.init() + val mailbox = Mailbox() if (wipe) { mailbox.wipeFilesOnly() @@ -82,7 +81,7 @@ class Main : CliktCommand( startLifecycle(mailbox) } - private fun startLifecycle(mailbox: ProductionMailbox) { + private fun startLifecycle(mailbox: Mailbox) { Runtime.getRuntime().addShutdownHook( Thread { mailbox.stopLifecycle(false) diff --git a/mailbox-lib/src/main/java/org/briarproject/mailbox/core/JavaLibEagerSingletons.kt b/mailbox-lib/src/main/java/org/briarproject/mailbox/core/MailboxLibEagerSingletons.kt similarity index 94% rename from mailbox-lib/src/main/java/org/briarproject/mailbox/core/JavaLibEagerSingletons.kt rename to mailbox-lib/src/main/java/org/briarproject/mailbox/core/MailboxLibEagerSingletons.kt index a105dc2b..079af827 100644 --- a/mailbox-lib/src/main/java/org/briarproject/mailbox/core/JavaLibEagerSingletons.kt +++ b/mailbox-lib/src/main/java/org/briarproject/mailbox/core/MailboxLibEagerSingletons.kt @@ -24,7 +24,7 @@ import org.briarproject.mailbox.core.tor.TorPlugin import javax.inject.Inject @Suppress("unused") -internal class JavaLibEagerSingletons @Inject constructor( +internal class MailboxLibEagerSingletons @Inject constructor( val taskScheduler: TaskScheduler, val javaTorPlugin: TorPlugin, ) diff --git a/mailbox-lib/src/main/java/org/briarproject/mailbox/lib/AbstractMailbox.kt b/mailbox-lib/src/main/java/org/briarproject/mailbox/lib/AbstractMailbox.kt new file mode 100644 index 00000000..c07974ee --- /dev/null +++ b/mailbox-lib/src/main/java/org/briarproject/mailbox/lib/AbstractMailbox.kt @@ -0,0 +1,135 @@ +/* + * Briar Mailbox + * Copyright (C) 2021-2022 The Briar Project + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + * + */ + +package org.briarproject.mailbox.lib + +import com.google.zxing.common.BitMatrix +import kotlinx.coroutines.flow.takeWhile +import kotlinx.coroutines.runBlocking +import org.briarproject.mailbox.core.CoreEagerSingletons +import org.briarproject.mailbox.core.MailboxLibEagerSingletons +import org.briarproject.mailbox.core.db.TransactionManager +import org.briarproject.mailbox.core.lifecycle.LifecycleManager +import org.briarproject.mailbox.core.setup.QrCodeEncoder +import org.briarproject.mailbox.core.setup.SetupManager +import org.briarproject.mailbox.core.setup.WipeManager +import org.briarproject.mailbox.core.system.System +import org.briarproject.mailbox.core.tor.TorPlugin +import org.briarproject.mailbox.core.tor.TorState +import org.briarproject.mailbox.core.util.LogUtils.info +import org.slf4j.Logger +import org.slf4j.LoggerFactory.getLogger +import java.io.File +import javax.inject.Inject + +abstract class AbstractMailbox(protected val customDataDir: File? = null) { + + companion object { + val LOG: Logger = getLogger(AbstractMailbox::class.java) + } + + @Inject + internal lateinit var coreEagerSingletons: CoreEagerSingletons + + @Inject + internal lateinit var mailboxLibEagerSingletons: MailboxLibEagerSingletons + + @Inject + internal lateinit var lifecycleManager: LifecycleManager + + @Inject + internal lateinit var db: TransactionManager + + @Inject + internal lateinit var setupManager: SetupManager + + @Inject + internal lateinit var wipeManager: WipeManager + + @Inject + internal lateinit var torPlugin: TorPlugin + + @Inject + internal lateinit var qrCodeEncoder: QrCodeEncoder + + @Inject + internal lateinit var system: System + + fun wipeFilesOnly() { + wipeManager.wipeFilesOnly() + LOG.info { "Mailbox wiped successfully \\o/" } + } + + fun startLifecycle() { + LOG.info { "Starting lifecycle" } + lifecycleManager.startServices() + LOG.info { "Waiting for startup" } + lifecycleManager.waitForStartup() + LOG.info { "Startup finished" } + } + + fun stopLifecycle(exitAfterStopping: Boolean) { + LOG.info { "Stopping lifecycle" } + lifecycleManager.stopServices(exitAfterStopping) + LOG.info { "Waiting for shutdown" } + lifecycleManager.waitForShutdown() + LOG.info { "Shutdown finished" } + } + + fun setSetupToken(token: String) { + setupManager.setToken(token, null) + } + + fun setOwnerToken(token: String) { + setupManager.setToken(null, token) + } + + fun waitForTorPublished() { + LOG.info { "Waiting for Tor to publish hidden service" } + runBlocking { + // wait until Tor becomes active and published the onion service + torPlugin.state.takeWhile { state -> + state != TorState.Published + }.collect { } + } + LOG.info { "Hidden service published" } + } + + fun waitForShutdown() { + lifecycleManager.waitForShutdown() + } + + fun getSetupToken(): String? { + return db.read { txn -> + setupManager.getSetupToken(txn) + } + } + + fun getOwnerToken(): String? { + return db.read { txn -> + setupManager.getOwnerToken(txn) + } + } + + fun getQrCode(): BitMatrix? { + return qrCodeEncoder.getQrCodeBitMatrix() + } + + fun getSystem(): System = system +} diff --git a/mailbox-lib/src/main/java/org/briarproject/mailbox/lib/Mailbox.kt b/mailbox-lib/src/main/java/org/briarproject/mailbox/lib/Mailbox.kt index 32a909e3..115bb071 100644 --- a/mailbox-lib/src/main/java/org/briarproject/mailbox/lib/Mailbox.kt +++ b/mailbox-lib/src/main/java/org/briarproject/mailbox/lib/Mailbox.kt @@ -1,137 +1,14 @@ -/* - * Briar Mailbox - * Copyright (C) 2021-2022 The Briar Project - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <https://www.gnu.org/licenses/>. - * - */ - package org.briarproject.mailbox.lib -import com.google.zxing.common.BitMatrix -import kotlinx.coroutines.flow.takeWhile -import kotlinx.coroutines.runBlocking -import org.briarproject.mailbox.core.CoreEagerSingletons -import org.briarproject.mailbox.core.JavaLibEagerSingletons -import org.briarproject.mailbox.core.db.TransactionManager -import org.briarproject.mailbox.core.lifecycle.LifecycleManager -import org.briarproject.mailbox.core.setup.QrCodeEncoder -import org.briarproject.mailbox.core.setup.SetupManager -import org.briarproject.mailbox.core.setup.WipeManager -import org.briarproject.mailbox.core.system.System -import org.briarproject.mailbox.core.tor.TorPlugin -import org.briarproject.mailbox.core.tor.TorState import org.briarproject.mailbox.core.util.LogUtils.info -import org.slf4j.Logger -import org.slf4j.LoggerFactory.getLogger import java.io.File -import javax.inject.Inject - -abstract class Mailbox(protected val customDataDir: File? = null) { - - companion object { - val LOG: Logger = getLogger(Mailbox::class.java) - } - - @Inject - internal lateinit var coreEagerSingletons: CoreEagerSingletons - - @Inject - internal lateinit var javaLibEagerSingletons: JavaLibEagerSingletons - - @Inject - internal lateinit var lifecycleManager: LifecycleManager - - @Inject - internal lateinit var db: TransactionManager - - @Inject - internal lateinit var setupManager: SetupManager - - @Inject - internal lateinit var wipeManager: WipeManager - - @Inject - internal lateinit var torPlugin: TorPlugin - - @Inject - internal lateinit var qrCodeEncoder: QrCodeEncoder - - @Inject - internal lateinit var system: System - - abstract fun init() - - fun wipeFilesOnly() { - wipeManager.wipeFilesOnly() - LOG.info { "Mailbox wiped successfully \\o/" } - } - fun startLifecycle() { - LOG.info { "Starting lifecycle" } - lifecycleManager.startServices() - LOG.info { "Waiting for startup" } - lifecycleManager.waitForStartup() - LOG.info { "Startup finished" } - } - - fun stopLifecycle(exitAfterStopping: Boolean) { - LOG.info { "Stopping lifecycle" } - lifecycleManager.stopServices(exitAfterStopping) - LOG.info { "Waiting for shutdown" } - lifecycleManager.waitForShutdown() - LOG.info { "Shutdown finished" } - } - - fun setSetupToken(token: String) { - setupManager.setToken(token, null) - } - - fun setOwnerToken(token: String) { - setupManager.setToken(null, token) - } - - fun waitForTorPublished() { - LOG.info { "Waiting for Tor to publish hidden service" } - runBlocking { - // wait until Tor becomes active and published the onion service - torPlugin.state.takeWhile { state -> - state != TorState.Published - }.collect { } - } - LOG.info { "Hidden service published" } - } - - fun waitForShutdown() { - lifecycleManager.waitForShutdown() - } - - fun getSetupToken(): String? { - return db.read { txn -> - setupManager.getSetupToken(txn) - } - } - - fun getOwnerToken(): String? { - return db.read { txn -> - setupManager.getOwnerToken(txn) - } - } +class Mailbox(mailboxDir: File? = null) : AbstractMailbox(mailboxDir) { - fun getQrCode(): BitMatrix? { - return qrCodeEncoder.getQrCodeBitMatrix() + init { + LOG.info { "Hello Mailbox" } + val mailboxLibComponent = DaggerMailboxLibComponent.builder() + .mailboxLibModule(MailboxLibModule(customDataDir)).build() + mailboxLibComponent.inject(this) } - - fun getSystem(): System = system } diff --git a/mailbox-lib/src/main/java/org/briarproject/mailbox/lib/MailboxLibProductionComponent.kt b/mailbox-lib/src/main/java/org/briarproject/mailbox/lib/MailboxLibComponent.kt similarity index 92% rename from mailbox-lib/src/main/java/org/briarproject/mailbox/lib/MailboxLibProductionComponent.kt rename to mailbox-lib/src/main/java/org/briarproject/mailbox/lib/MailboxLibComponent.kt index 7479fe68..03ef11d0 100644 --- a/mailbox-lib/src/main/java/org/briarproject/mailbox/lib/MailboxLibProductionComponent.kt +++ b/mailbox-lib/src/main/java/org/briarproject/mailbox/lib/MailboxLibComponent.kt @@ -30,6 +30,6 @@ import javax.inject.Singleton ProductionSystemModule::class, ] ) -interface MailboxLibProductionComponent { - fun inject(mailbox: ProductionMailbox) +interface MailboxLibComponent { + fun inject(mailbox: Mailbox) } diff --git a/mailbox-lib/src/main/java/org/briarproject/mailbox/lib/ProductionMailbox.kt b/mailbox-lib/src/main/java/org/briarproject/mailbox/lib/ProductionMailbox.kt deleted file mode 100644 index 7e15dc54..00000000 --- a/mailbox-lib/src/main/java/org/briarproject/mailbox/lib/ProductionMailbox.kt +++ /dev/null @@ -1,15 +0,0 @@ -package org.briarproject.mailbox.lib - -import org.briarproject.mailbox.core.util.LogUtils.info -import java.io.File - -class ProductionMailbox(mailboxDir: File? = null) : Mailbox(mailboxDir) { - - override fun init() { - LOG.info { "Hello Mailbox" } - val javaLibComponent = DaggerMailboxLibProductionComponent.builder() - .mailboxLibModule(MailboxLibModule(customDataDir)).build() - javaLibComponent.inject(this) - } - -} diff --git a/mailbox-lib/src/main/java/org/briarproject/mailbox/lib/TestMailbox.kt b/mailbox-lib/src/main/java/org/briarproject/mailbox/lib/TestMailbox.kt index fc2a1134..3d1b0784 100644 --- a/mailbox-lib/src/main/java/org/briarproject/mailbox/lib/TestMailbox.kt +++ b/mailbox-lib/src/main/java/org/briarproject/mailbox/lib/TestMailbox.kt @@ -5,13 +5,13 @@ import org.briarproject.mailbox.system.TestSystem import java.io.File import javax.inject.Inject -class TestMailbox(mailboxDir: File? = null) : Mailbox(mailboxDir) { +class TestMailbox(mailboxDir: File? = null) : AbstractMailbox(mailboxDir) { - override fun init() { + init { LOG.info { "Hello Mailbox" } - val javaLibComponent = DaggerMailboxLibTestComponent.builder() + val mailboxLibComponent = DaggerMailboxLibTestComponent.builder() .mailboxLibModule(MailboxLibModule(customDataDir)).build() - javaLibComponent.inject(this) + mailboxLibComponent.inject(this) } @Inject diff --git a/mailbox-lib/src/test/java/org/briarproject/mailbox/lib/MailboxLibTest.kt b/mailbox-lib/src/test/java/org/briarproject/mailbox/lib/MailboxLibTest.kt index dfe8d73d..f602617e 100644 --- a/mailbox-lib/src/test/java/org/briarproject/mailbox/lib/MailboxLibTest.kt +++ b/mailbox-lib/src/test/java/org/briarproject/mailbox/lib/MailboxLibTest.kt @@ -33,7 +33,6 @@ class MailboxLibTest { @Test fun testStartStopMailbox() { val mailbox = TestMailbox(mailboxDataDirectory) - mailbox.init() mailbox.startLifecycle() mailbox.waitForTorPublished() mailbox.stopLifecycle(true) -- GitLab