diff --git a/bramble-core/src/main/java/org/briarproject/bramble/mailbox/AbstractMailboxSession.java b/bramble-core/src/main/java/org/briarproject/bramble/mailbox/AbstractMailboxSession.java index 76dc6cae828467df4e942414226a0b032aff214b..16166e5badce36aee11064a516679f02a1c325f8 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/mailbox/AbstractMailboxSession.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/mailbox/AbstractMailboxSession.java @@ -82,20 +82,18 @@ public abstract class AbstractMailboxSession implements MailboxSession { mailboxProtocol.registerRequestHandler(new ENDHandler()); } - /** - * Must be called once at the end of a AbstractMailboxSession to signal the end of - * the session to the peer. This call blocks until the remote session - * signals that it was ended. - */ - protected void endSession() - throws InterruptedException, IOException { - - mailboxProtocol.writeRequest(new MailboxRequestEnd()); + public void endSession() + throws IOException { - synchronized (remoteSessionFinished) { - while (!remoteSessionFinished.get()) { - remoteSessionFinished.wait(); + try { + mailboxProtocol.writeRequest(new MailboxRequestEnd()); + synchronized (remoteSessionFinished) { + while (!remoteSessionFinished.get()) { + remoteSessionFinished.wait(); + } } + } catch (InterruptedException e) { + throw new IOException(e.toString()); } } @@ -239,7 +237,7 @@ public abstract class AbstractMailboxSession implements MailboxSession { boolean hasMessages; try { - txn = db.startTransaction(true); + txn = db.startTransaction(true); hasMessages = db.hasMessagesOrAcksToSend(txn, c); db.commitTransaction(txn); } finally { @@ -264,7 +262,7 @@ public abstract class AbstractMailboxSession implements MailboxSession { syncSessionFactory.createSimplexOutgoingSession(c, MailboxConstants.MAX_LATENCY, streamWriter).run(); - byte [] buf = os.toByteArray(); + byte[] buf = os.toByteArray(); if (buf.length <= 0) return null; diff --git a/bramble-core/src/main/java/org/briarproject/bramble/mailbox/ContactMailboxSession.java b/bramble-core/src/main/java/org/briarproject/bramble/mailbox/ContactMailboxSession.java index 78b5da37e781c5abcaff31036c609b3791e37e94..bf5d916ffd1eba46a461c7f0dcd09f44de81b575 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/mailbox/ContactMailboxSession.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/mailbox/ContactMailboxSession.java @@ -57,7 +57,6 @@ public class ContactMailboxSession extends AbstractMailboxSession { this.contactId = contactId; mailboxProtocol.registerRequestHandler(new TAKEHandler()); - mailboxProtocol.enableRequestHandling(); } @Override @@ -92,7 +91,7 @@ public class ContactMailboxSession extends AbstractMailboxSession { // to send END request try { endSession(); - } catch (InterruptedException | IOException e) { + } catch (IOException e) { if (LOG.isLoggable(INFO)) LOG.info(e.toString()); } diff --git a/bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxContactSession.java b/bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxContactSession.java index e40b145795d942469b46725ce38250de439a03ba..ed5ba0050617752c5f791f456f711e92b1f3952e 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxContactSession.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxContactSession.java @@ -68,7 +68,7 @@ public class MailboxContactSession extends AbstractMailboxSession { // to send END request try { endSession(); - } catch (InterruptedException | IOException e) { + } catch (IOException e) { if (LOG.isLoggable(INFO)) LOG.info(e.toString()); } diff --git a/bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxManagerImpl.java b/bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxManagerImpl.java index 77d16fbc6dc33a6ddfc66d6949f66bd0e58b716b..c6257d41841a73cb76629b0911b0cacfb9540ff3 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxManagerImpl.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxManagerImpl.java @@ -189,7 +189,7 @@ public class MailboxManagerImpl implements MailboxManager { new MailboxProtocol(ioExecutor, mailboxBdfWriter, mailboxBdfReader); - AbstractMailboxSession mailboxSession = mailboxSessionFactory + MailboxSession mailboxSession = mailboxSessionFactory .createMailboxSession(mailboxProtocol, contactId, contactType, writer.getMaxLatency(), writer.getMaxIdleTime()); diff --git a/bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxOwnerSession.java b/bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxOwnerSession.java index b3c99d3e55acb4e787a4e85f1447715186e52162..58977cffa79debeb0cfd8a91d3d3a931419c3278 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxOwnerSession.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxOwnerSession.java @@ -47,11 +47,8 @@ public class MailboxOwnerSession extends AbstractMailboxSession { this.mailboxProtocol = mailboxProtocol; this.mailboxStorage = mailboxStorage; - enableSYNCHandling(); mailboxProtocol.registerRequestHandler(new STOREHandler()); - mailboxProtocol.enableRequestHandling(); - } @Override @@ -59,6 +56,9 @@ public class MailboxOwnerSession extends AbstractMailboxSession { try { sendStoredStreams(); awaitSYNCHandlerFinished(); + // NOTE: SYNC handler only terminates if the connection is closed + // so it is not required to end the session manually by + // sending an END request } catch (InterruptedException | IOException e) { if (LOG.isLoggable(INFO)) LOG.info(e.toString()); @@ -70,6 +70,8 @@ public class MailboxOwnerSession extends AbstractMailboxSession { MailboxStorage.MailboxStorageStream nextStream = mailboxStorage.getStreamForOwner(); + // FIXME: If there are no more streams to send at this time, we should + // wait while (nextStream != null) { // Send TAKE request and delete stream if request was successfull MailboxRequestTake req = diff --git a/bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxSessionFactory.java b/bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxSessionFactory.java index 43db7c4f58bfb8692bfc1cd69792d32ac54f65f7..33fc3f44d45e89740b06428a38f026d2cbcd260a 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxSessionFactory.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/mailbox/MailboxSessionFactory.java @@ -5,7 +5,7 @@ import org.briarproject.bramble.api.contact.ContactType; import org.briarproject.bramble.mailbox.protocol.MailboxProtocol; public interface MailboxSessionFactory { - AbstractMailboxSession createMailboxSession(MailboxProtocol mailboxProtocol, + MailboxSession createMailboxSession(MailboxProtocol mailboxProtocol, ContactId contactId, ContactType contactType, int transportMaxLatency, int transportMaxIdleTime); diff --git a/bramble-core/src/main/java/org/briarproject/bramble/mailbox/PrivateMailboxSession.java b/bramble-core/src/main/java/org/briarproject/bramble/mailbox/PrivateMailboxSession.java index 731f1fea9a78c9c9bb3c60a0b241c07b7fc434cb..1d24dd3f3a51440d32c2f697bce8215b8466fa62 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/mailbox/PrivateMailboxSession.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/mailbox/PrivateMailboxSession.java @@ -35,7 +35,6 @@ import static org.briarproject.bramble.util.LogUtils.logException; public class PrivateMailboxSession extends AbstractMailboxSession { private static final Logger LOG = Logger.getLogger(PrivateMailboxSession.class.getName()); - private final KeyManager keyManager; private final SyncSessionFactory syncSessionFactory; private final StreamReaderFactory streamReaderFactory; private MailboxProtocol mailboxProtocol; @@ -51,7 +50,6 @@ public class PrivateMailboxSession extends AbstractMailboxSession { super(ioExecutor, db, keyManager, syncSessionFactory, streamWriterFactory, streamReaderFactory, mailboxProtocol, transportMaxLatency, transportMaxIdleTime, contactId); - this.keyManager = keyManager; this.syncSessionFactory = syncSessionFactory; this.streamReaderFactory = streamReaderFactory; this.mailboxProtocol = mailboxProtocol; @@ -59,7 +57,6 @@ public class PrivateMailboxSession extends AbstractMailboxSession { mailboxProtocol.registerRequestHandler(new TAKEHandler()); // Register SYNC handler and run outgoing SYNC session enableSYNCHandling(); - mailboxProtocol.enableRequestHandling(); } /** @@ -96,6 +93,9 @@ public class PrivateMailboxSession extends AbstractMailboxSession { public void run() { try { awaitSYNCHandlerFinished(); + // NOTE: SYNC handler only terminates if the connection is closed + // so it is not required to end the session manually by + // sending an END request } catch (InterruptedException e) { logException(LOG, WARNING, e); }