net.sf.colossus.server
Class ClientHandler

java.lang.Object
  extended by net.sf.colossus.server.ClientHandler
All Implemented Interfaces:
IClient

final class ClientHandler
extends java.lang.Object
implements IClient

Holds all data specific to one client connection. (Earlier this was the class ServerSocketThread, but since changing to NIO it's not an own thread any more.) The code in here is (should be) executed exclusively by the server thread as reaction to something happening on the selector - first the client connection being accepted, and then later always when data from client was received (usually from THIS client, but there might be other cases).

Author:
David Ripton

Field Summary
private  java.nio.charset.Charset charset
           
private  java.lang.String CHARSET_NAME
           
private static int counter
           
private  java.nio.charset.CharsetDecoder decoder
           
private static java.lang.String DEFAULT_CHAR_SET
           
private  java.nio.charset.CharsetEncoder encoder
           
private  java.lang.String incompleteInput
           
private  java.lang.String incompleteText
           
private  boolean isGone
           
private static java.util.logging.Logger LOGGER
           
private  java.lang.String playerName
           
private  java.nio.channels.SelectionKey selectionKey
           
private static java.lang.String sep
           
private  Server server
           
private  java.lang.String signonName
           
private  java.nio.channels.SocketChannel socketChannel
           
private  boolean withdrawnAlready
           
 
Constructor Summary
ClientHandler(Server server, java.nio.channels.SocketChannel channel, java.nio.channels.SelectionKey selKey)
           
 
Method Summary
 void addCreature(Legion legion, CreatureType creature, java.lang.String reason)
           
 void askAcquireAngel(Legion legion, java.util.List<CreatureType> recruits)
           
 void askChooseStrikePenalty(java.util.List<java.lang.String> choices)
           
 void askConcede(Legion ally, Legion enemy)
           
 void askFlee(Legion ally, Legion enemy)
           
 void askNegotiate(Legion attacker, Legion defender)
           
 void askPickColor(java.util.List<PlayerColor> colorsLeft)
           
 void askPickFirstMarker()
           
private  void callMethod(java.lang.String method, java.util.List<java.lang.String> args)
           
 void cleanupBattle()
           
 void confirmWhenCaughtUp()
           
 void createSummonAngel(Legion legion)
           
 void didMove(Legion legion, MasterHex startingHex, MasterHex currentHex, EntrySide entrySide, boolean teleport, CreatureType teleportingLord, boolean splitLegionHasForcedMove)
           
 void didRecruit(Legion legion, CreatureType recruit, CreatureType recruiter, int numRecruiters)
           
 void didSplit(MasterHex hex, Legion parent, Legion child, int childHeight, java.util.List<CreatureType> splitoffs, int turn)
           
 void didSummon(Legion summoner, Legion donor, CreatureType summon)
           
 void dispose()
           
private  void doCallMethodInTryBlock(java.lang.String line, java.lang.String method, java.util.List<java.lang.String> li)
           
 void doReinforce(Legion legion)
           
 java.nio.channels.SelectionKey getKey()
           
 java.lang.String getPlayerName()
           
 java.nio.channels.SocketChannel getSocketChannel()
           
 void initBattle(MasterHex hex, int battleTurnNumber, Player battleActivePlayer, BattlePhase battlePhase, Legion attacker, Legion defender)
           
 void initBoard()
           
 boolean isGone()
           
 void kickPhase()
           
 void log(java.lang.String message)
           
 void nak(java.lang.String reason, java.lang.String errmsg)
           
 void nextEngagement()
           
 void placeNewChit(java.lang.String imageName, boolean inverted, int tag, BattleHex hex)
           
 void processInput(java.nio.ByteBuffer byteBuffer)
           
 void removeCreature(Legion legion, CreatureType creature, java.lang.String reason)
           
 void removeDeadBattleChits()
           
 void removeLegion(Legion legion)
           
private  BattleHex resolveBattleHex(java.lang.String hexLabel)
           
private  CreatureType resolveCreatureType(java.lang.String name)
           
private  CreatureType resolveCreatureTypeNullOk(java.lang.String name)
          There are cases where "null" comes over network and is not meant to be resolved to a CreatureType, namely: teleportingLord if no teleport; null recruiter; decline Acquire.
private  Legion resolveLegion(java.lang.String markerId)
           
private  MasterHex resolveMasterHex(java.lang.String hexLabel)
           
 void revealCreatures(Legion legion, java.util.List<CreatureType> creatures, java.lang.String reason)
           
 void revealEngagedCreatures(Legion legion, java.util.List<CreatureType> creatures, boolean isAttacker, java.lang.String reason)
          print the 'revealEngagagedCreature'-message, args: markerId, isAttacker, list of creature names
 void sendToClient(java.lang.String message)
           
private  void sendViaChannel(java.lang.String msg)
           
private  void sendViaChannelRaw(java.lang.String msg)
           
 void serverConfirmsConnection()
           
 void setBoardActive(boolean val)
           
 void setColor(PlayerColor color)
           
 void setIsGone(boolean val)
           
 void setLegionStatus(Legion legion, boolean moved, boolean teleported, EntrySide entrySide, CreatureType lastRecruit)
           
 void setPlayerName(java.lang.String playerName)
           
 void setupBattleFight(BattlePhase battlePhase, Player battleActivePlayer)
           
 void setupBattleMove(Player battleActivePlayer, int battleTurnNumber)
           
 void setupBattleRecruit(Player battleActivePlayer, int battleTurnNumber)
           
 void setupBattleSummon(Player battleActivePlayer, int battleTurnNumber)
           
 void setupFight()
           
 void setupMove()
           
 void setupMuster()
           
 void setupSplit(Player activePlayer, int turnNumber)
           
 void setupTurnState(Player activePlayer, int turnNumber)
           
private  void sleepFor(long millis)
           
 void syncOption(java.lang.String optname, java.lang.String value)
           
 void tellBattleMove(int tag, BattleHex startingHex, BattleHex endingHex, boolean undo)
           
 void tellEngagement(MasterHex hex, Legion attacker, Legion defender)
           
 void tellEngagementResults(Legion winner, java.lang.String method, int points, int turns)
           
 void tellGameOver(java.lang.String message, boolean disposeFollows)
           
 void tellInitialGameInfo(java.lang.String variantName, java.util.Collection<java.lang.String> playerNames)
           
 void tellLegionLocation(Legion legion, MasterHex hex)
           
 void tellMovementRoll(int roll)
           
 void tellPlayerElim(Player player, Player slayer)
           
 void tellProposal(java.lang.String proposalString)
           
 void tellRedo(boolean val)
           
 void tellReplay(boolean val, int maxTurn)
           
 void tellStrikeResults(int strikerTag, int targetTag, int strikeNumber, java.util.List<java.lang.String> rolls, int damage, boolean killed, boolean wasCarry, int carryDamageLeft, java.util.Set<java.lang.String> carryTargetDescriptions)
           
 void tellWhatsHappening(java.lang.String message)
           
 void undidMove(Legion legion, MasterHex formerHex, MasterHex currentHex, boolean splitLegionHasForcedMove)
           
 void undidRecruit(Legion legion, CreatureType recruit)
           
 void undidSplit(Legion splitoff, Legion survivor, int turn)
           
 void updateCreatureCount(CreatureType type, int count, int deadCount)
           
 void updatePlayerInfo(java.util.List<java.lang.String> infoStrings)
           
private  void withdrawIfNeeded()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LOGGER

private static final java.util.logging.Logger LOGGER

server

private final Server server

socketChannel

private final java.nio.channels.SocketChannel socketChannel

selectionKey

private final java.nio.channels.SelectionKey selectionKey

playerName

private java.lang.String playerName

signonName

private java.lang.String signonName

isGone

private boolean isGone

withdrawnAlready

private boolean withdrawnAlready

sep

private static final java.lang.String sep
See Also:
Constant Field Values

counter

private static int counter

incompleteInput

private java.lang.String incompleteInput

incompleteText

private java.lang.String incompleteText

DEFAULT_CHAR_SET

private static final java.lang.String DEFAULT_CHAR_SET

CHARSET_NAME

private final java.lang.String CHARSET_NAME

charset

private final java.nio.charset.Charset charset

encoder

private final java.nio.charset.CharsetEncoder encoder

decoder

private final java.nio.charset.CharsetDecoder decoder
Constructor Detail

ClientHandler

ClientHandler(Server server,
              java.nio.channels.SocketChannel channel,
              java.nio.channels.SelectionKey selKey)
Method Detail

getKey

public java.nio.channels.SelectionKey getKey()

getSocketChannel

public java.nio.channels.SocketChannel getSocketChannel()

isGone

public boolean isGone()

setIsGone

public void setIsGone(boolean val)

processInput

public void processInput(java.nio.ByteBuffer byteBuffer)

sleepFor

private void sleepFor(long millis)

sendViaChannel

private void sendViaChannel(java.lang.String msg)

sendViaChannelRaw

private void sendViaChannelRaw(java.lang.String msg)

doCallMethodInTryBlock

private void doCallMethodInTryBlock(java.lang.String line,
                                    java.lang.String method,
                                    java.util.List<java.lang.String> li)

callMethod

private void callMethod(java.lang.String method,
                        java.util.List<java.lang.String> args)

resolveBattleHex

private BattleHex resolveBattleHex(java.lang.String hexLabel)

resolveCreatureType

private CreatureType resolveCreatureType(java.lang.String name)

resolveCreatureTypeNullOk

private CreatureType resolveCreatureTypeNullOk(java.lang.String name)
There are cases where "null" comes over network and is not meant to be resolved to a CreatureType, namely: teleportingLord if no teleport; null recruiter; decline Acquire. TODO What to do with the "Anything"?

Parameters:
name - Name of the creatureType to find, might be "null"
Returns:
CreatureType for that name, or null if name is "null"

withdrawIfNeeded

private void withdrawIfNeeded()

resolveMasterHex

private MasterHex resolveMasterHex(java.lang.String hexLabel)

resolveLegion

private Legion resolveLegion(java.lang.String markerId)

sendToClient

public void sendToClient(java.lang.String message)

dispose

public void dispose()
Specified by:
dispose in interface IClient

tellEngagement

public void tellEngagement(MasterHex hex,
                           Legion attacker,
                           Legion defender)
Specified by:
tellEngagement in interface IClient

tellEngagementResults

public void tellEngagementResults(Legion winner,
                                  java.lang.String method,
                                  int points,
                                  int turns)
Specified by:
tellEngagementResults in interface IClient

tellWhatsHappening

public void tellWhatsHappening(java.lang.String message)
Specified by:
tellWhatsHappening in interface IClient

tellMovementRoll

public void tellMovementRoll(int roll)
Specified by:
tellMovementRoll in interface IClient

syncOption

public void syncOption(java.lang.String optname,
                       java.lang.String value)
Specified by:
syncOption in interface IClient

updatePlayerInfo

public void updatePlayerInfo(java.util.List<java.lang.String> infoStrings)
Specified by:
updatePlayerInfo in interface IClient

setColor

public void setColor(PlayerColor color)
Specified by:
setColor in interface IClient

updateCreatureCount

public void updateCreatureCount(CreatureType type,
                                int count,
                                int deadCount)
Specified by:
updateCreatureCount in interface IClient

removeLegion

public void removeLegion(Legion legion)
Specified by:
removeLegion in interface IClient

setLegionStatus

public void setLegionStatus(Legion legion,
                            boolean moved,
                            boolean teleported,
                            EntrySide entrySide,
                            CreatureType lastRecruit)
Specified by:
setLegionStatus in interface IClient

addCreature

public void addCreature(Legion legion,
                        CreatureType creature,
                        java.lang.String reason)
Specified by:
addCreature in interface IClient

removeCreature

public void removeCreature(Legion legion,
                           CreatureType creature,
                           java.lang.String reason)
Specified by:
removeCreature in interface IClient

revealCreatures

public void revealCreatures(Legion legion,
                            java.util.List<CreatureType> creatures,
                            java.lang.String reason)
Specified by:
revealCreatures in interface IClient

revealEngagedCreatures

public void revealEngagedCreatures(Legion legion,
                                   java.util.List<CreatureType> creatures,
                                   boolean isAttacker,
                                   java.lang.String reason)
print the 'revealEngagagedCreature'-message, args: markerId, isAttacker, list of creature names

Specified by:
revealEngagedCreatures in interface IClient
Parameters:
markerId - legion marker name that is currently in battle
creatures - List of creatures in this legion
isAttacker - true for attacker, false for defender
reason - why this was revealed

removeDeadBattleChits

public void removeDeadBattleChits()
Specified by:
removeDeadBattleChits in interface IClient

placeNewChit

public void placeNewChit(java.lang.String imageName,
                         boolean inverted,
                         int tag,
                         BattleHex hex)
Specified by:
placeNewChit in interface IClient

tellReplay

public void tellReplay(boolean val,
                       int maxTurn)
Specified by:
tellReplay in interface IClient

tellRedo

public void tellRedo(boolean val)
Specified by:
tellRedo in interface IClient

initBoard

public void initBoard()
Specified by:
initBoard in interface IClient

setPlayerName

public void setPlayerName(java.lang.String playerName)
Specified by:
setPlayerName in interface IClient

getPlayerName

public java.lang.String getPlayerName()

createSummonAngel

public void createSummonAngel(Legion legion)
Specified by:
createSummonAngel in interface IClient

askAcquireAngel

public void askAcquireAngel(Legion legion,
                            java.util.List<CreatureType> recruits)
Specified by:
askAcquireAngel in interface IClient

askChooseStrikePenalty

public void askChooseStrikePenalty(java.util.List<java.lang.String> choices)
Specified by:
askChooseStrikePenalty in interface IClient

tellGameOver

public void tellGameOver(java.lang.String message,
                         boolean disposeFollows)
Specified by:
tellGameOver in interface IClient

tellPlayerElim

public void tellPlayerElim(Player player,
                           Player slayer)
Specified by:
tellPlayerElim in interface IClient

askConcede

public void askConcede(Legion ally,
                       Legion enemy)
Specified by:
askConcede in interface IClient

askFlee

public void askFlee(Legion ally,
                    Legion enemy)
Specified by:
askFlee in interface IClient

askNegotiate

public void askNegotiate(Legion attacker,
                         Legion defender)
Specified by:
askNegotiate in interface IClient

tellProposal

public void tellProposal(java.lang.String proposalString)
Specified by:
tellProposal in interface IClient

tellStrikeResults

public void tellStrikeResults(int strikerTag,
                              int targetTag,
                              int strikeNumber,
                              java.util.List<java.lang.String> rolls,
                              int damage,
                              boolean killed,
                              boolean wasCarry,
                              int carryDamageLeft,
                              java.util.Set<java.lang.String> carryTargetDescriptions)
Specified by:
tellStrikeResults in interface IClient

initBattle

public void initBattle(MasterHex hex,
                       int battleTurnNumber,
                       Player battleActivePlayer,
                       BattlePhase battlePhase,
                       Legion attacker,
                       Legion defender)
Specified by:
initBattle in interface IClient

cleanupBattle

public void cleanupBattle()
Specified by:
cleanupBattle in interface IClient

nextEngagement

public void nextEngagement()
Specified by:
nextEngagement in interface IClient

doReinforce

public void doReinforce(Legion legion)
Specified by:
doReinforce in interface IClient

didRecruit

public void didRecruit(Legion legion,
                       CreatureType recruit,
                       CreatureType recruiter,
                       int numRecruiters)
Specified by:
didRecruit in interface IClient

undidRecruit

public void undidRecruit(Legion legion,
                         CreatureType recruit)
Specified by:
undidRecruit in interface IClient

setupTurnState

public void setupTurnState(Player activePlayer,
                           int turnNumber)
Specified by:
setupTurnState in interface IClient

setupSplit

public void setupSplit(Player activePlayer,
                       int turnNumber)
Specified by:
setupSplit in interface IClient

setupMove

public void setupMove()
Specified by:
setupMove in interface IClient

setupFight

public void setupFight()
Specified by:
setupFight in interface IClient

setupMuster

public void setupMuster()
Specified by:
setupMuster in interface IClient

kickPhase

public void kickPhase()
Specified by:
kickPhase in interface IClient

setupBattleSummon

public void setupBattleSummon(Player battleActivePlayer,
                              int battleTurnNumber)
Specified by:
setupBattleSummon in interface IClient

setupBattleRecruit

public void setupBattleRecruit(Player battleActivePlayer,
                               int battleTurnNumber)
Specified by:
setupBattleRecruit in interface IClient

setupBattleMove

public void setupBattleMove(Player battleActivePlayer,
                            int battleTurnNumber)
Specified by:
setupBattleMove in interface IClient

setupBattleFight

public void setupBattleFight(BattlePhase battlePhase,
                             Player battleActivePlayer)
Specified by:
setupBattleFight in interface IClient

tellLegionLocation

public void tellLegionLocation(Legion legion,
                               MasterHex hex)
Specified by:
tellLegionLocation in interface IClient

tellBattleMove

public void tellBattleMove(int tag,
                           BattleHex startingHex,
                           BattleHex endingHex,
                           boolean undo)
Specified by:
tellBattleMove in interface IClient

didMove

public void didMove(Legion legion,
                    MasterHex startingHex,
                    MasterHex currentHex,
                    EntrySide entrySide,
                    boolean teleport,
                    CreatureType teleportingLord,
                    boolean splitLegionHasForcedMove)
Specified by:
didMove in interface IClient

undidMove

public void undidMove(Legion legion,
                      MasterHex formerHex,
                      MasterHex currentHex,
                      boolean splitLegionHasForcedMove)
Specified by:
undidMove in interface IClient

didSummon

public void didSummon(Legion summoner,
                      Legion donor,
                      CreatureType summon)
Specified by:
didSummon in interface IClient

undidSplit

public void undidSplit(Legion splitoff,
                       Legion survivor,
                       int turn)
Specified by:
undidSplit in interface IClient

didSplit

public void didSplit(MasterHex hex,
                     Legion parent,
                     Legion child,
                     int childHeight,
                     java.util.List<CreatureType> splitoffs,
                     int turn)
Specified by:
didSplit in interface IClient

askPickColor

public void askPickColor(java.util.List<PlayerColor> colorsLeft)
Specified by:
askPickColor in interface IClient

askPickFirstMarker

public void askPickFirstMarker()
Specified by:
askPickFirstMarker in interface IClient

log

public void log(java.lang.String message)
Specified by:
log in interface IClient

nak

public void nak(java.lang.String reason,
                java.lang.String errmsg)
Specified by:
nak in interface IClient

setBoardActive

public void setBoardActive(boolean val)
Specified by:
setBoardActive in interface IClient

tellInitialGameInfo

public void tellInitialGameInfo(java.lang.String variantName,
                                java.util.Collection<java.lang.String> playerNames)

confirmWhenCaughtUp

public void confirmWhenCaughtUp()
Specified by:
confirmWhenCaughtUp in interface IClient

serverConfirmsConnection

public void serverConfirmsConnection()
Specified by:
serverConfirmsConnection in interface IClient