---
.../freecol/client/control/ClientInputHandler.java | 68 +++----
.../freecol/client/control/InGameInputHandler.java | 220 ++++++++++++++-------
.../client/control/PreGameInputHandler.java | 94 ++++++---
3 files changed, 246 insertions(+), 136 deletions(-)
diff --git a/src/net/sf/freecol/client/control/ClientInputHandler.java
b/src/net/sf/freecol/client/control/ClientInputHandler.java
index 59e650664ee..2efba5e068a 100644
--- a/src/net/sf/freecol/client/control/ClientInputHandler.java
+++ b/src/net/sf/freecol/client/control/ClientInputHandler.java
@@ -45,46 +45,45 @@ public abstract class ClientInputHandler extends
FreeColClientHolder
private static final Logger logger =
Logger.getLogger(ClientInputHandler.class.getName());
/**
- * Handle a request to a client.
+ * The constructor to use.
*
+ * @param freeColClient The {@code FreeColClient} for the game.
*/
- public interface ClientNetworkRequestHandler {
- void handle(Connection connection, Element element);
+ public ClientInputHandler(FreeColClient freeColClient) {
+ super(freeColClient);
}
-
- /**
- * The handler map provides named handlers for network
- * requests. Each handler deals with a given request type.
- */
- private final Map<String, ClientNetworkRequestHandler> handlerMap
- = Collections.synchronizedMap(new HashMap<String,
ClientNetworkRequestHandler>());
-
/**
- * The constructor to use.
+ * Do the actual message handling.
*
- * @param freeColClient The {@code FreeColClient} for the game.
+ * Common messages (applying to all game states) are handled here.
+ * Subclasses for individual game states will override it, but still call
this
+ * one (super), in order to get the common messages handled.
+ *
+ * @param connection The connection, where the messages coming in
+ * @param element The message Element
+ * @return True if the message was handled
*/
- public ClientInputHandler(FreeColClient freeColClient) {
- super(freeColClient);
+ protected boolean handleElement(Connection connection, Element element) {
+ String tag = element.getTagName();
- register(TrivialMessage.DISCONNECT_TAG,
- (Connection c, Element e) -> disconnect(e));
- register(GameStateMessage.TAG,
- (Connection c, Element e) -> gameState(e));
- register(VacantPlayersMessage.TAG,
- (Connection c, Element e) -> vacantPlayers(e));
- }
+ switch (tag) {
+ case TrivialMessage.DISCONNECT_TAG:
+ disconnect(element);
+ break;
+ case GameStateMessage.TAG:
+ gameState(element);
+ break;
- /**
- * Register a network request handler.
- *
- * @param name The handler name.
- * @param handler The {@code ClientNetworkRequestHandler} to register.
- */
- protected final void register(String name, ClientNetworkRequestHandler
handler) {
- this.handlerMap.put(name, handler);
+ case VacantPlayersMessage.TAG:
+ vacantPlayers(element);
+ break;
+
+ default:
+ return false;
+ }
+ return true;
}
// Useful handlers
@@ -134,14 +133,11 @@ public abstract class ClientInputHandler extends
FreeColClientHolder
public Element handle(Connection connection, Element element) {
if (element == null) return null;
final String tag = element.getTagName();
- ClientNetworkRequestHandler handler = handlerMap.get(tag);
try {
- if (handler == null) {
- logger.warning("Client ignored: " + tag);
- } else {
- handler.handle(connection, element);
+ if (handleElement(connection, element))
logger.log(Level.FINEST, "Client handled: " + tag);
- }
+ else
+ logger.warning("unhandled tag: "+tag);
} catch (Exception ex) {
logger.log(Level.WARNING, "Client failed: " + tag, ex);
}
diff --git a/src/net/sf/freecol/client/control/InGameInputHandler.java
b/src/net/sf/freecol/client/control/InGameInputHandler.java
index fde158bb7e2..4281fb59235 100644
--- a/src/net/sf/freecol/client/control/InGameInputHandler.java
+++ b/src/net/sf/freecol/client/control/InGameInputHandler.java
@@ -134,79 +134,157 @@ public final class InGameInputHandler extends
ClientInputHandler {
*/
public InGameInputHandler(FreeColClient freeColClient) {
super(freeColClient);
-
- register(AddPlayerMessage.TAG,
- (Connection c, Element e) -> addPlayer(e));
- register(AnimateAttackMessage.TAG,
- (Connection c, Element e) -> animateAttack(e));
- register(AnimateMoveMessage.TAG,
- (Connection c, Element e) -> animateMove(e));
- register(ChatMessage.TAG,
- (Connection c, Element e) -> chat(e));
- register(ChooseFoundingFatherMessage.TAG,
- (Connection c, Element e) -> chooseFoundingFather(e));
- register(TrivialMessage.CLOSE_MENUS_TAG,
- (Connection c, Element e) -> closeMenus());
- register(DiplomacyMessage.TAG,
- (Connection c, Element e) -> diplomacy(e));
- register(TrivialMessage.DISCONNECT_TAG,
- (Connection c, Element e) -> disconnect(e));
- register(ErrorMessage.TAG,
- (Connection c, Element e) -> error(e));
- register(FeatureChangeMessage.TAG,
- (Connection c, Element e) -> featureChange(e));
- register(FirstContactMessage.TAG,
- (Connection c, Element e) -> firstContact(e));
- register(FountainOfYouthMessage.TAG,
- (Connection c, Element e) -> fountainOfYouth(e));
- register(GameEndedMessage.TAG,
- (Connection c, Element e) -> gameEnded(e));
- register(HighScoreMessage.TAG,
- (Connection c, Element e) -> highScore(e));
- register(InciteMessage.TAG,
- (Connection c, Element e) -> incite(e));
- register(IndianDemandMessage.TAG,
- (Connection c, Element e) -> indianDemand(e));
- register(LogoutMessage.TAG,
- (Connection c, Element e) -> logout(e));
- register(LootCargoMessage.TAG,
- (Connection c, Element e) -> lootCargo(e));
- register(MonarchActionMessage.TAG,
- (Connection c, Element e) -> monarchAction(e));
- register(MultipleMessage.TAG,
- (Connection c, Element e) -> multiple(c, e));
- register(NationSummaryMessage.TAG,
- (Connection c, Element e) -> nationSummary(e));
- register(NativeTradeMessage.TAG,
- (Connection c, Element e) -> nativeTrade(e));
- register(NewLandNameMessage.TAG,
- (Connection c, Element e) -> newLandName(e));
- register(NewRegionNameMessage.TAG,
- (Connection c, Element e) -> newRegionName(e));
- register(NewTurnMessage.TAG,
- (Connection c, Element e) -> newTurn(e));
- register(NewTradeRouteMessage.TAG,
- (Connection c, Element e) -> newTradeRoute(e));
- register(TrivialMessage.RECONNECT_TAG,
- (Connection c, Element e) -> reconnect());
- register(RemoveMessage.TAG,
- (Connection c, Element e) -> remove(e));
- register(ScoutSpeakToChiefMessage.TAG,
- (Connection c, Element e) -> scoutSpeakToChief(e));
- register(SetAIMessage.TAG,
- (Connection c, Element e) -> setAI(e));
- register(SetCurrentPlayerMessage.TAG,
- (Connection c, Element e) -> setCurrentPlayer(e));
- register(SetDeadMessage.TAG,
- (Connection c, Element e) -> setDead(e));
- register(SetStanceMessage.TAG,
- (Connection c, Element e) -> setStance(e));
- register(SpySettlementMessage.TAG,
- (Connection c, Element e) -> spySettlement(e));
- register(UpdateMessage.TAG,
- (Connection c, Element e) -> update(e));
}
+ @Override
+ protected boolean handleElement(Connection connection, Element element) {
+ String tag = element.getTagName();
+ switch (tag) {
+ case AddPlayerMessage.TAG:
+ addPlayer(element);
+ break;
+
+ case AnimateAttackMessage.TAG:
+ animateAttack(element);
+ break;
+
+ case AnimateMoveMessage.TAG:
+ animateMove(element);
+ break;
+
+ case ChatMessage.TAG:
+ chat(element);
+ break;
+
+ case ChooseFoundingFatherMessage.TAG:
+ chooseFoundingFather(element);
+ break;
+
+ case TrivialMessage.CLOSE_MENUS_TAG:
+ closeMenus();
+ break;
+
+ case DiplomacyMessage.TAG:
+ diplomacy(element);
+ break;
+
+ case TrivialMessage.DISCONNECT_TAG:
+ disconnect(element);
+ break;
+
+ case ErrorMessage.TAG:
+ error(element);
+ break;
+
+ case FeatureChangeMessage.TAG:
+ featureChange(element);
+ break;
+
+ case FirstContactMessage.TAG:
+ firstContact(element);
+ break;
+
+ case FountainOfYouthMessage.TAG:
+ fountainOfYouth(element);
+ break;
+
+ case GameEndedMessage.TAG:
+ gameEnded(element);
+ break;
+
+ case HighScoreMessage.TAG:
+ highScore(element);
+ break;
+
+ case InciteMessage.TAG:
+ incite(element);
+ break;
+
+ case IndianDemandMessage.TAG:
+ indianDemand(element);
+ break;
+
+ case LogoutMessage.TAG:
+ logout(element);
+ break;
+
+ case LootCargoMessage.TAG:
+ lootCargo(element);
+ break;
+
+ case MonarchActionMessage.TAG:
+ monarchAction(element);
+ break;
+
+ case MultipleMessage.TAG:
+ multiple(connection, element);
+ break;
+
+ case NationSummaryMessage.TAG:
+ nationSummary(element);
+ break;
+
+ case NativeTradeMessage.TAG:
+ nativeTrade(element);
+ break;
+
+ case NewLandNameMessage.TAG:
+ newLandName(element);
+ break;
+
+ case NewRegionNameMessage.TAG:
+ newRegionName(element);
+ break;
+
+ case NewTurnMessage.TAG:
+ newTurn(element);
+ break;
+
+ case NewTradeRouteMessage.TAG:
+ newTradeRoute(element);
+ break;
+
+ case TrivialMessage.RECONNECT_TAG:
+ reconnect();
+ break;
+
+ case RemoveMessage.TAG:
+ remove(element);
+ break;
+
+ case ScoutSpeakToChiefMessage.TAG:
+ scoutSpeakToChief(element);
+ break;
+
+ case SetAIMessage.TAG:
+ setAI(element);
+ break;
+
+ case SetCurrentPlayerMessage.TAG:
+ setCurrentPlayer(element);
+ break;
+
+ case SetDeadMessage.TAG:
+ setDead(element);
+ break;
+
+ case SetStanceMessage.TAG:
+ setStance(element);
+ break;
+
+ case SpySettlementMessage.TAG:
+ spySettlement(element);
+ break;
+
+ case UpdateMessage.TAG:
+ update(element);
+ break;
+
+ default:
+ return super.handleElement(connection, element);
+ }
+ return true;
+ }
/**
* Shorthand to run in the EDT and wait.
diff --git a/src/net/sf/freecol/client/control/PreGameInputHandler.java
b/src/net/sf/freecol/client/control/PreGameInputHandler.java
index 02be39791ec..52fd9412fdc 100644
--- a/src/net/sf/freecol/client/control/PreGameInputHandler.java
+++ b/src/net/sf/freecol/client/control/PreGameInputHandler.java
@@ -74,37 +74,73 @@ public final class PreGameInputHandler extends
ClientInputHandler {
*/
public PreGameInputHandler(FreeColClient freeColClient) {
super(freeColClient);
-
- register(AddPlayerMessage.TAG,
- (Connection c, Element e) -> addPlayer(e));
- register(ChatMessage.TAG,
- (Connection c, Element e) -> chat(e));
- register(ErrorMessage.TAG,
- (Connection c, Element e) -> error(e));
- register(LoginMessage.TAG,
- (Connection c, Element e) -> login(e));
- register(LogoutMessage.TAG,
- (Connection c, Element e) -> logout(e));
- register(MultipleMessage.TAG,
- (Connection c, Element e) -> multiple(c, e));
- register(ReadyMessage.TAG,
- (Connection c, Element e) -> ready(e));
- register(SetAvailableMessage.TAG,
- (Connection c, Element e) -> setAvailable(e));
- register(SetColorMessage.TAG,
- (Connection c, Element e) -> setColor(e));
- register(SetNationMessage.TAG,
- (Connection c, Element e) -> setNation(e));
- register(TrivialMessage.START_GAME_TAG,
- (Connection c, Element e) -> startGame(e));
- register(UpdateMessage.TAG,
- (Connection c, Element e) -> update(e));
- register(UpdateGameOptionsMessage.TAG,
- (Connection c, Element e) -> updateGameOptions(e));
- register(UpdateMapGeneratorOptionsMessage.TAG,
- (Connection c, Element e) -> updateMapGeneratorOptions(e));
}
+ @Override
+ protected boolean handleElement(Connection connection, Element element) {
+ String tag = element.getTagName();
+ switch (tag) {
+ case AddPlayerMessage.TAG:
+ addPlayer(element);
+ break;
+
+ case ChatMessage.TAG:
+ chat(element);
+ break;
+
+ case ErrorMessage.TAG:
+ error(element);
+ break;
+
+ case LoginMessage.TAG:
+ login(element);
+ break;
+
+ case LogoutMessage.TAG:
+ logout(element);
+ break;
+
+ case MultipleMessage.TAG:
+ multiple(connection, element);
+ break;
+
+ case ReadyMessage.TAG:
+ ready(element);
+ break;
+
+ case SetAvailableMessage.TAG:
+ setAvailable(element);
+ break;
+
+ case SetColorMessage.TAG:
+ setColor(element);
+ break;
+
+ case SetNationMessage.TAG:
+ setNation(element);
+ break;
+
+ case TrivialMessage.START_GAME_TAG:
+ startGame(element);
+ break;
+
+ case UpdateMessage.TAG:
+ update(element);
+ break;
+
+ case UpdateGameOptionsMessage.TAG:
+ updateGameOptions(element);
+ break;
+
+ case UpdateMapGeneratorOptionsMessage.TAG:
+ updateMapGeneratorOptions(element);
+ break;
+
+ default:
+ return super.handleElement(connection, element);
+ }
+ return true;
+ }
// Individual handlers
--
2.11.0.rc0.7.gbe5a750
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
Freecol-developers mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/freecol-developers