diff --git a/bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxServiceImpl.java b/bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxServiceImpl.java index afe06b5903779e5b02275bab7fd9f2b48e02e5f0..d9a639539bd3fb6e8f9b2a7e32366182fce5ca7e 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxServiceImpl.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxServiceImpl.java @@ -2,6 +2,7 @@ package org.briarproject.bramble.mailbox; import org.briarproject.bramble.api.contact.Contact; import org.briarproject.bramble.api.contact.ContactId; +import org.briarproject.bramble.api.contact.ContactManager; import org.briarproject.bramble.api.contact.ContactType; import org.briarproject.bramble.api.contact.event.ContactAddedEvent; import org.briarproject.bramble.api.db.DatabaseComponent; @@ -56,12 +57,12 @@ public class MailboxServiceImpl implements MailboxService, EventListener { private final Executor ioExecutor; private final ScheduledExecutorService scheduler; private EventBus eventBus; + private ContactManager contactManager; private final DatabaseComponent db; private final ConnectionRegistry connectionRegistry; private final PluginManager pluginManager; private final MailboxManager mailboxManager; private final TransportPropertyManager transportPropertyManager; - private final SecureRandom random; private volatile Future mailboxLanFuture; @@ -71,29 +72,37 @@ public class MailboxServiceImpl implements MailboxService, EventListener { @Inject MailboxServiceImpl(@IoExecutor Executor ioExecutor, @Scheduler ScheduledExecutorService scheduler, EventBus eventBus, + ContactManager contactManager, DatabaseComponent db, ConnectionRegistry connectionRegistry, PluginManager pluginManager, MailboxManager mailboxManager, - TransportPropertyManager transportPropertyManager, - SecureRandom random) { + TransportPropertyManager transportPropertyManager) { this.ioExecutor = ioExecutor; this.scheduler = scheduler; this.eventBus = eventBus; + this.contactManager = contactManager; this.db = db; this.connectionRegistry = connectionRegistry; this.pluginManager = pluginManager; this.mailboxManager = mailboxManager; this.transportPropertyManager = transportPropertyManager; - this.random = random; } @Override - public void startService() throws ServiceException { + public void startService() { if (LOG.isLoggable(INFO)) LOG.info("Starting Mailbox Service"); - checkForPrivateMailbox(); + try { + privateMailboxId = contactManager.getPrivateMailbox().getId(); + } catch (DbException e1) { + if (LOG.isLoggable(WARNING)) + LOG.info(e1.toString()); + } + if (privateMailboxId != null) + hasPrivateMailbox.set(true); + tryToRunLanMailboxFuture(); this.eventBus.addListener(this); } @@ -116,9 +125,17 @@ public class MailboxServiceImpl implements MailboxService, EventListener { } } - if (e instanceof ContactAddedEvent) { - if (!hasPrivateMailbox.get()) - ioExecutor.execute(() -> checkForPrivateMailbox()); + if (e instanceof ContactAddedEvent){ + if (hasPrivateMailbox.get()) + return; + try { + privateMailboxId = contactManager.getPrivateMailbox().getId(); + } catch (DbException e1) { + if (LOG.isLoggable(WARNING)) + LOG.info(e1.toString()); + } + if (privateMailboxId != null) + hasPrivateMailbox.set(true); } } @@ -134,35 +151,6 @@ public class MailboxServiceImpl implements MailboxService, EventListener { POLLING_INTERVALL, plugin), POLLING_INTERVALL); } - private void checkForPrivateMailbox() { - synchronized (hasPrivateMailbox) { - if (hasPrivateMailbox.get()) - return; - - Transaction txn = null; - Collection<Contact> privateMb; - try { - txn = db.startTransaction(true); - privateMb = db.getContactsByType(txn, PRIVATE_MAILBOX); - db.commitTransaction(txn); - - if (privateMb.size() > 1) - throw new RuntimeException( - "Multiple Private Mailboxes exist!"); - - if (!privateMb.isEmpty()) { - privateMailboxId = privateMb.iterator().next().getId(); - hasPrivateMailbox.set(true); - } - } catch (DbException dbe) { - logException(LOG, WARNING, dbe); - } finally { - if (txn != null) - db.endTransaction(txn); - } - } - } - private Future schedule(Runnable task, int delay) { return scheduler .schedule(() -> ioExecutor.execute(task), delay, MILLISECONDS);