From fccf3c0afdf4f7dd50b9912eac087d5ded1a970d Mon Sep 17 00:00:00 2001 From: bontric <benjohnwie@gmail.com> Date: Thu, 20 Sep 2018 18:27:26 +0200 Subject: [PATCH] Add function to enable request handling (ensure that all handlers are registered before reading requests) and cleanup --- .../mailbox/protocol/MailboxProtocol.java | 36 ++++++++++++++----- 1 file changed, 28 insertions(+), 8 deletions(-) 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 ad4916151..940b6db9b 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 { -- GitLab