messaging/broker : update handlers

This commit is contained in:
Thibaud Gasser 2020-03-15 14:35:18 +01:00
parent e407698e84
commit c5ea09d09d
8 changed files with 55 additions and 26 deletions

View File

@ -1,8 +1,12 @@
package messagebroker; package messagebroker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.PrintWriter; import java.io.PrintWriter;
public abstract class BaseMessageHandler implements MessageHandler { public abstract class BaseMessageHandler implements MessageHandler {
private static final Logger LOGGER = LoggerFactory.getLogger(BaseMessageHandler.class);
private MessageHandler nextHandler; private MessageHandler nextHandler;
private final MessageHandlerContext context; private final MessageHandlerContext context;
@ -14,9 +18,11 @@ public abstract class BaseMessageHandler implements MessageHandler {
public void handleMessage(String message) throws UnknownMessageFormatException { public void handleMessage(String message) throws UnknownMessageFormatException {
if (nextHandler != null) { if (nextHandler != null) {
LOGGER.debug("Trying next handler in chain: {}", nextHandler);
nextHandler.handleMessage(message); nextHandler.handleMessage(message);
} else { } else {
String msg = "Unable to handle message " + message; String msg = "Unable to handle message " + message;
LOGGER.warn(msg);
throw new UnknownMessageFormatException(msg); throw new UnknownMessageFormatException(msg);
} }
} }
@ -24,6 +30,11 @@ public abstract class BaseMessageHandler implements MessageHandler {
@Override @Override
public void setNextHandler(MessageHandler nextHandler) { public void setNextHandler(MessageHandler nextHandler) {
this.nextHandler = nextHandler; this.nextHandler = nextHandler;
LOGGER.debug("Next handler of {} set to {}", getClass().getCanonicalName(), nextHandler.getClass().getCanonicalName());
}
public MessageHandler getNextHandler() {
return nextHandler;
} }
protected MessageHandlerContext getContext() { protected MessageHandlerContext getContext() {

View File

@ -1,12 +1,13 @@
package messagebroker; package messagebroker;
import messagebroker.handlers.AddHandlerMessageHandler;
import messagebroker.handlers.ListHandlersMessageHandler;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;
import java.net.ServerSocket; import java.net.ServerSocket;
import java.net.Socket; import java.net.Socket;
@ -44,6 +45,10 @@ public class MessageBroker {
messageHandlerFactory.addHandler(handlerName); messageHandlerFactory.addHandler(handlerName);
} }
public MessageHandler getHandler() {
return messageHandlerFactory.getHandler();
}
private class ClientHandler implements Runnable { private class ClientHandler implements Runnable {
private final Socket socket; private final Socket socket;
@ -82,7 +87,8 @@ public class MessageBroker {
int port = Integer.parseInt(args[0]); int port = Integer.parseInt(args[0]);
MessageBroker broker = new MessageBroker(port); MessageBroker broker = new MessageBroker(port);
broker.addHandler("messagebroker.handlers.AddHandler"); broker.addHandler(AddHandlerMessageHandler.class.getCanonicalName());
broker.addHandler(ListHandlersMessageHandler.class.getCanonicalName());
broker.run(); broker.run();
} }
} }

View File

@ -2,5 +2,6 @@ package messagebroker;
public interface MessageHandler { public interface MessageHandler {
void handleMessage(String message) throws UnknownMessageFormatException; void handleMessage(String message) throws UnknownMessageFormatException;
MessageHandler getNextHandler();
void setNextHandler(MessageHandler nextHandler); void setNextHandler(MessageHandler nextHandler);
} }

View File

@ -3,9 +3,6 @@ package messagebroker;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
class MessageHandlerFactory { class MessageHandlerFactory {
private static final Logger LOGGER = LoggerFactory.getLogger(MessageHandlerFactory.class); private static final Logger LOGGER = LoggerFactory.getLogger(MessageHandlerFactory.class);
@ -23,7 +20,7 @@ class MessageHandlerFactory {
Class<? extends MessageHandler> handlerClass = Class.forName(handlerName) Class<? extends MessageHandler> handlerClass = Class.forName(handlerName)
.asSubclass(MessageHandler.class); .asSubclass(MessageHandler.class);
LOGGER.debug("Class {} found for handler name {}", handlerClass, handlerName); LOGGER.debug("Class {} found for handler name {}", handlerClass.getCanonicalName(), handlerName);
MessageHandler next = newMessageHandler(handlerClass); MessageHandler next = newMessageHandler(handlerClass);
if (firstHandler == null) { if (firstHandler == null) {
firstHandler = next; firstHandler = next;

View File

@ -33,6 +33,7 @@ public class AddHandlerMessageHandler extends BaseMessageHandler {
sendResponse(msg); sendResponse(msg);
} }
} else { } else {
LOGGER.debug("Pattern \"{}\" does not match. Cannot handle message {}", msgPattern, message);
super.handleMessage(message); super.handleMessage(message);
} }
} }

View File

@ -6,7 +6,6 @@ import messagebroker.UnknownMessageFormatException;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.io.PrintWriter;
import java.util.regex.Pattern; import java.util.regex.Pattern;
public class EchoMessageHandler extends BaseMessageHandler { public class EchoMessageHandler extends BaseMessageHandler {
@ -25,6 +24,7 @@ public class EchoMessageHandler extends BaseMessageHandler {
LOGGER.info("Parsed message {}", parsed); LOGGER.info("Parsed message {}", parsed);
sendResponse(parsed); sendResponse(parsed);
} else { } else {
LOGGER.debug("Pattern \"{}\" does not match. Cannot handle message {}", PATTERN, message);
super.handleMessage(message); super.handleMessage(message);
} }
} }

View File

@ -0,0 +1,32 @@
package messagebroker.handlers;
import messagebroker.BaseMessageHandler;
import messagebroker.MessageHandlerContext;
import messagebroker.UnknownMessageFormatException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ListHandlersMessageHandler extends BaseMessageHandler {
private static final Logger LOGGER = LoggerFactory.getLogger(EchoMessageHandler.class);
private static final String PATTERN = "CONTROL.LIST_HANDLERS";
public ListHandlersMessageHandler(MessageHandlerContext context) {
super(context);
}
@Override
public void handleMessage(String message) throws UnknownMessageFormatException {
if (message.equals(PATTERN)) {
LOGGER.info("Received message {}", message);
var response = new StringBuilder();
for (var nextHandler = getContext().getBroker().getHandler(); nextHandler != null; nextHandler = nextHandler.getNextHandler()){
response.append(nextHandler.getClass().getCanonicalName())
.append("\n");
}
sendResponse(response.toString());
} else {
LOGGER.debug("Pattern \"{}\" does not match. Cannot handle message {}", PATTERN, message);
super.handleMessage(message);
}
}
}

View File

@ -1,19 +0,0 @@
package messagebroker.handlers;
import messagebroker.BaseMessageHandler;
import messagebroker.MessageHandlerContext;
import messagebroker.UnknownMessageFormatException;
public class NewHandler extends BaseMessageHandler {
private final MessageHandlerContext context;
public NewHandler(MessageHandlerContext context) {
super(context);
this.context = context;
}
@Override
public void handleMessage(String message) throws UnknownMessageFormatException {
super.handleMessage(message);
}
}