Skip to content
Snippets Groups Projects
Verified Commit d887c49a authored by Sebastian's avatar Sebastian
Browse files

Address review feedback

parent 72d3d905
No related branches found
No related tags found
1 merge request!115Add lib module for using mailbox as a library, especially in briar integration tests
Pipeline #12233 passed
...@@ -26,7 +26,7 @@ import com.github.ajalt.clikt.parameters.options.counted ...@@ -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.flag
import com.github.ajalt.clikt.parameters.options.option import com.github.ajalt.clikt.parameters.options.option
import org.briarproject.mailbox.core.system.InvalidIdException import org.briarproject.mailbox.core.system.InvalidIdException
import org.briarproject.mailbox.lib.ProductionMailbox import org.briarproject.mailbox.lib.Mailbox
import org.slf4j.LoggerFactory.getLogger import org.slf4j.LoggerFactory.getLogger
class Main : CliktCommand( class Main : CliktCommand(
...@@ -71,8 +71,7 @@ class Main : CliktCommand( ...@@ -71,8 +71,7 @@ class Main : CliktCommand(
getLogger(this.javaClass).debug("Hello Mailbox") getLogger(this.javaClass).debug("Hello Mailbox")
println("Hello Mailbox") println("Hello Mailbox")
val mailbox = ProductionMailbox() val mailbox = Mailbox()
mailbox.init()
if (wipe) { if (wipe) {
mailbox.wipeFilesOnly() mailbox.wipeFilesOnly()
...@@ -82,7 +81,7 @@ class Main : CliktCommand( ...@@ -82,7 +81,7 @@ class Main : CliktCommand(
startLifecycle(mailbox) startLifecycle(mailbox)
} }
private fun startLifecycle(mailbox: ProductionMailbox) { private fun startLifecycle(mailbox: Mailbox) {
Runtime.getRuntime().addShutdownHook( Runtime.getRuntime().addShutdownHook(
Thread { Thread {
mailbox.stopLifecycle(false) mailbox.stopLifecycle(false)
......
...@@ -24,7 +24,7 @@ import org.briarproject.mailbox.core.tor.TorPlugin ...@@ -24,7 +24,7 @@ import org.briarproject.mailbox.core.tor.TorPlugin
import javax.inject.Inject import javax.inject.Inject
@Suppress("unused") @Suppress("unused")
internal class JavaLibEagerSingletons @Inject constructor( internal class MailboxLibEagerSingletons @Inject constructor(
val taskScheduler: TaskScheduler, val taskScheduler: TaskScheduler,
val javaTorPlugin: TorPlugin, val javaTorPlugin: TorPlugin,
) )
/*
* 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
}
/*
* 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 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.briarproject.mailbox.core.util.LogUtils.info
import org.slf4j.Logger
import org.slf4j.LoggerFactory.getLogger
import java.io.File 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() { class Mailbox(mailboxDir: File? = null) : AbstractMailbox(mailboxDir) {
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? { init {
return qrCodeEncoder.getQrCodeBitMatrix() LOG.info { "Hello Mailbox" }
val mailboxLibComponent = DaggerMailboxLibComponent.builder()
.mailboxLibModule(MailboxLibModule(customDataDir)).build()
mailboxLibComponent.inject(this)
} }
fun getSystem(): System = system
} }
...@@ -30,6 +30,6 @@ import javax.inject.Singleton ...@@ -30,6 +30,6 @@ import javax.inject.Singleton
ProductionSystemModule::class, ProductionSystemModule::class,
] ]
) )
interface MailboxLibProductionComponent { interface MailboxLibComponent {
fun inject(mailbox: ProductionMailbox) fun inject(mailbox: Mailbox)
} }
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)
}
}
...@@ -5,13 +5,13 @@ import org.briarproject.mailbox.system.TestSystem ...@@ -5,13 +5,13 @@ import org.briarproject.mailbox.system.TestSystem
import java.io.File import java.io.File
import javax.inject.Inject 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" } LOG.info { "Hello Mailbox" }
val javaLibComponent = DaggerMailboxLibTestComponent.builder() val mailboxLibComponent = DaggerMailboxLibTestComponent.builder()
.mailboxLibModule(MailboxLibModule(customDataDir)).build() .mailboxLibModule(MailboxLibModule(customDataDir)).build()
javaLibComponent.inject(this) mailboxLibComponent.inject(this)
} }
@Inject @Inject
......
...@@ -33,7 +33,6 @@ class MailboxLibTest { ...@@ -33,7 +33,6 @@ class MailboxLibTest {
@Test @Test
fun testStartStopMailbox() { fun testStartStopMailbox() {
val mailbox = TestMailbox(mailboxDataDirectory) val mailbox = TestMailbox(mailboxDataDirectory)
mailbox.init()
mailbox.startLifecycle() mailbox.startLifecycle()
mailbox.waitForTorPublished() mailbox.waitForTorPublished()
mailbox.stopLifecycle(true) mailbox.stopLifecycle(true)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment