diff --git a/bramble-core/src/main/java/org/briarproject/bramble/mailbox/protocol/MailboxProtocol.java b/bramble-core/src/main/java/org/briarproject/bramble/mailbox/protocol/MailboxProtocol.java index ad4916151a5961dcdb22e1f30c97acef19e81d08..940b6db9bd596bbf5070266451ea627feea4adc5 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/mailbox/protocol/MailboxProtocol.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/mailbox/protocol/MailboxProtocol.java @@ -56,10 +56,6 @@ public class MailboxProtocol implements Runnable { } - public void registerRequestHandler(MailboxRequestHandler handler) { - requestHandlers.put(handler.getType(), handler); - } - public void sendKeepAlive() throws IOException { // flush the writer without pending data to send a keepalive if (stopped.get()) @@ -109,12 +105,33 @@ public class MailboxProtocol implements Runnable { */ @Override public void run() { - ioExecutor.execute(() -> writeOutgoingMessages()); - readIncomingMessages(); + writeOutgoingMessages(); + } + + /** + * Register a Handler for a specific request type. + * <p> + * NOTE: {@link this#enableRequestHandling()} MUST be called after all handlers + * have been registered! + * + * @param handler + */ + public void registerRequestHandler(MailboxRequestHandler handler) { + if (requestHandlers.containsKey(handler.getType())) + throw new RuntimeException( + "Handler for " + handler.getType().toString() + + " has been registered twice!"); + requestHandlers.put(handler.getType(), handler); + } + + /** + * Request handling once all request handlers are registered + */ + public void enableRequestHandling() { + ioExecutor.execute(() -> readIncomingMessages()); } private void readIncomingMessages() { - readingThread = Thread.currentThread(); BdfList bdfMsg; while (!stopped.get()) { @@ -124,7 +141,6 @@ public class MailboxProtocol implements Runnable { throw new EOFException(); bdfMsg = mailboxBdfReader.readList(); } catch (IOException e) { - readingThread.interrupt(); handleIOException(true, e); return; } @@ -153,6 +169,10 @@ public class MailboxProtocol implements Runnable { try { handler.handleRequest(req); } catch (ProtocolException e) { + if (!req.hasResponse()) + throw new RuntimeException( + "Protocol exception was thrown for request without response"); + error = e.toString(); } } else {