Skip to content
Snippets Groups Projects
Commit 85e91c2e authored by bontric's avatar bontric
Browse files

Use getPrivateMailbox function in mailbox service

parent 33b5d754
No related branches found
No related tags found
No related merge requests found
Pipeline #
......@@ -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);
......
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