-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
See issue #363. Handler code also is isolated from Server. The Server class is already doing too much.
- Loading branch information
Showing
3 changed files
with
101 additions
and
35 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
cardshifter-server/src/main/java/com/cardshifter/server/model/AuthorizationDecorator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.cardshifter.server.model; | ||
|
||
import com.cardshifter.api.*; | ||
import com.cardshifter.api.messages.*; | ||
import com.cardshifter.api.outgoing.*; | ||
import com.cardshifter.core.messages.*; | ||
|
||
public class AuthorizationDecorator<T extends Message> implements MessageHandler<T> { | ||
|
||
private final MessageHandler<T> handler; | ||
|
||
public AuthorizationDecorator(MessageHandler<T> handler) { | ||
this.handler = handler; | ||
} | ||
|
||
@Override | ||
public void handle(T message, ClientIO client) { | ||
if (client.isLoggedIn()) { | ||
handler.handle(message, client); | ||
} | ||
else { | ||
client.sendToClient(new ServerErrorMessage("Login required")); | ||
} | ||
} | ||
|
||
} |
69 changes: 69 additions & 0 deletions
69
cardshifter-server/src/main/java/com/cardshifter/server/model/HandlerManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package com.cardshifter.server.model; | ||
|
||
import com.cardshifter.api.*; | ||
import com.cardshifter.api.both.*; | ||
import com.cardshifter.api.incoming.*; | ||
import com.cardshifter.api.messages.*; | ||
import com.cardshifter.api.outgoing.*; | ||
import com.cardshifter.core.messages.*; | ||
import org.apache.log4j.*; | ||
|
||
import java.util.*; | ||
|
||
public class HandlerManager { | ||
|
||
private static final Logger logger = LogManager.getLogger(HandlerManager.class); | ||
|
||
private final IncomingHandler incomingHandler; | ||
private final CommandHandler commandHandler; | ||
|
||
public HandlerManager(Server server) { | ||
incomingHandler = new IncomingHandler(); | ||
commandHandler = new CommandHandler(server); | ||
|
||
Handlers handlers = new Handlers(server); | ||
|
||
addUnauthorizedHandler("login", LoginMessage.class, handlers::loginMessage); | ||
|
||
addHandler("chat", ChatMessage.class, handlers::chat); | ||
addHandler("startgame", StartGameRequest.class, handlers::play); | ||
addHandler("inviteResponse", InviteResponse.class, handlers::inviteResponse); | ||
addHandler("query", ServerQueryMessage.class, handlers::query); | ||
|
||
// Directly game-related | ||
addHandler("use", UseAbilityMessage.class, handlers::useAbility); | ||
addHandler("requestTargets", RequestTargetsMessage.class, handlers::requestTargets); | ||
addHandler("playerconfig", PlayerConfigMessage.class, handlers::incomingConfig); | ||
} | ||
|
||
public IncomingHandler getIncomingHandler() { | ||
return incomingHandler; | ||
} | ||
|
||
public CommandHandler getCommandHandler() { | ||
return commandHandler; | ||
} | ||
|
||
public <E extends Message> void addHandler(String command, Class<E> handler, MessageHandler<E> consumer) { | ||
this.addUnauthorizedHandler(command, handler, new AuthorizationDecorator<>(consumer)); | ||
} | ||
|
||
public <E extends Message> void addUnauthorizedHandler(String command, Class<E> handler, MessageHandler<E> consumer) { | ||
this.incomingHandler.addHandler(command, handler, consumer); | ||
} | ||
|
||
public void handleMessage(ClientIO client, String json) { | ||
Objects.requireNonNull(client, "Cannot handle message from a null client"); | ||
logger.info("Handle message " + client + ": " + json); | ||
|
||
Message message; | ||
try { | ||
message = getIncomingHandler().parse(json); | ||
logger.info("Parsed Message: " + message); | ||
getIncomingHandler().perform(message, client); | ||
} catch (Exception e) { | ||
logger.error("Unable to parse incoming json: " + json, e); | ||
client.sendToClient(new ServerErrorMessage(e.getMessage())); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters