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

Add function to enable request handling (ensure that all handlers are

registered before reading requests) and cleanup
parent 659cacb8
No related branches found
No related tags found
No related merge requests found
...@@ -56,10 +56,6 @@ public class MailboxProtocol implements Runnable { ...@@ -56,10 +56,6 @@ public class MailboxProtocol implements Runnable {
} }
public void registerRequestHandler(MailboxRequestHandler handler) {
requestHandlers.put(handler.getType(), handler);
}
public void sendKeepAlive() throws IOException { public void sendKeepAlive() throws IOException {
// flush the writer without pending data to send a keepalive // flush the writer without pending data to send a keepalive
if (stopped.get()) if (stopped.get())
...@@ -109,12 +105,33 @@ public class MailboxProtocol implements Runnable { ...@@ -109,12 +105,33 @@ public class MailboxProtocol implements Runnable {
*/ */
@Override @Override
public void run() { public void run() {
ioExecutor.execute(() -> writeOutgoingMessages()); writeOutgoingMessages();
readIncomingMessages(); }
/**
* 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() { private void readIncomingMessages() {
readingThread = Thread.currentThread();
BdfList bdfMsg; BdfList bdfMsg;
while (!stopped.get()) { while (!stopped.get()) {
...@@ -124,7 +141,6 @@ public class MailboxProtocol implements Runnable { ...@@ -124,7 +141,6 @@ public class MailboxProtocol implements Runnable {
throw new EOFException(); throw new EOFException();
bdfMsg = mailboxBdfReader.readList(); bdfMsg = mailboxBdfReader.readList();
} catch (IOException e) { } catch (IOException e) {
readingThread.interrupt();
handleIOException(true, e); handleIOException(true, e);
return; return;
} }
...@@ -153,6 +169,10 @@ public class MailboxProtocol implements Runnable { ...@@ -153,6 +169,10 @@ public class MailboxProtocol implements Runnable {
try { try {
handler.handleRequest(req); handler.handleRequest(req);
} catch (ProtocolException e) { } catch (ProtocolException e) {
if (!req.hasResponse())
throw new RuntimeException(
"Protocol exception was thrown for request without response");
error = e.toString(); error = e.toString();
} }
} else { } else {
......
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