Package net.sf.colossus.server
Class ClientHandler
- java.lang.Object
-
- net.sf.colossus.server.ClientHandlerStub
-
- net.sf.colossus.server.ClientHandler
-
- All Implemented Interfaces:
IClient
final class ClientHandler extends ClientHandlerStub 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
Fields Modifier and Type Field Description (package private) java.nio.ByteBuffer
bb
private int
cantSendMessageRepeated
private java.nio.charset.Charset
charset
private java.lang.String
CHARSET_NAME
private int
clientVersion
private int
commitPointCounter
private java.nio.charset.CharsetDecoder
decoder
private static java.lang.String
DEFAULT_CHAR_SET
private boolean
didExplicitDisconnect
(package private) java.lang.String
encodedMsg
private java.nio.charset.CharsetEncoder
encoder
boolean
fakeDisconnect
Debug stuff, only for testing/development purposesprivate java.lang.String
incompleteInput
private java.lang.String
incompleteText
private static java.util.logging.Logger
LOGGER
private static int
MAX_KEEP_LINES
private int
newCounter
private boolean
obsolete
(package private) int
previousRetries
(package private) java.util.LinkedList<java.lang.String>
queue
The queue in which messages are stored, until they were really written.private java.util.ArrayList<java.lang.String>
recentlyProcessedLines
private java.nio.channels.SelectionKey
selectorKey
(package private) int
should
private java.nio.channels.SocketChannel
socketChannel
private boolean
spectator
private boolean
temporarilyDisconnected
private long
temporarilyInTrouble
private boolean
withdrawnAlready
(package private) int
writtenTotal
-
Fields inherited from class net.sf.colossus.server.ClientHandlerStub
counter, isCommitPoint, isGone, messageCounter, playerName, redoQueue, sep, server, signonName
-
-
Constructor Summary
Constructors Constructor Description ClientHandler(Server server, java.nio.channels.SocketChannel channel, java.nio.channels.SelectionKey selKey)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private void
attemptWritingToChannel()
private void
callMethod(java.lang.String method, java.util.List<java.lang.String> args)
This is the longish if-elseif-else block which deserialized all client-to-server calls back from String to actual methodCalls.boolean
canHandleAdvancedSync()
boolean
canHandleBattleMoveNak()
protected boolean
canHandleNewVariantXML()
protected boolean
canHandlePingRequest()
void
clearDisconnectClient()
void
clearTemporarilyInTrouble()
void
cloneRedoQueue(ClientHandler oldCH)
protected void
commitPoint()
private void
confirmCommitPoint(int confirmedNr)
Remove the messages in redoQueue prior to given commit pointvoid
declareObsolete()
boolean
didExplicitDisconnect()
void
disposeClient()
Server side disposes a client (and informs it about it first) To be used only for "disposeAllClients()", otherwise setIsGone reason is misleading.private void
doCallMethodInTryBlock(java.lang.String line, java.lang.String method, java.util.List<java.lang.String> li)
java.lang.String
dumpLastProcessedLines()
protected void
enqueueToRedoQueue(int messageNr, java.lang.String message)
void
fakeDisconnectClient()
protected void
flushQueuedContent()
Selector reported that client became writable again (after a prior write attempt had not written all bytes).java.nio.channels.SelectionKey
getSelectorKey()
java.nio.channels.SocketChannel
getSocketChannel()
private void
handleEncoding(java.lang.String msg)
long
howLongAlreadyInTrouble()
void
initRedoQueueFromStub(ClientHandlerStub stub)
boolean
isGone()
boolean
isSpectator()
protected boolean
isStub()
boolean
isTemporarilyDisconnected()
boolean
isTemporarilyInTrouble()
void
processInput(java.nio.ByteBuffer byteBuffer)
private void
reEnqueue(MessageForClient mfc)
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)
protected void
sendToClient(java.lang.String message)
private void
sendViaChannel(java.lang.String msg)
private void
sendViaChannelRaw(java.lang.String msg)
void
setTemporarilyDisconnected()
private void
setTemporarilyInTrouble()
boolean
supportsReconnect()
void
syncAfterReconnect(int lastReceivedMessageNr, int syncRequestNumber)
Re-send all data after the message from which we know client got itboolean
wasFakeDisconnectFlagSet()
private void
withdrawIfNeeded(boolean explicit)
Make sure player is withdrawn from game.-
Methods inherited from class net.sf.colossus.server.ClientHandlerStub
addCreature, appendToConnectionLog, askAcquireAngel, askChooseStrikePenalty, askConcede, askFlee, askNegotiate, askPickColor, askPickFirstMarker, cleanupBattle, confirmWhenCaughtUp, createSummonAngel, didMove, didRecruit, didSplit, didSummon, doReinforce, getClientName, getPlayerName, getSignonName, initBattle, initBoard, kickPhase, log, messageFromServer, nak, nextEngagement, pingRequest, placeNewChit, removeCreature, removeDeadBattleChits, removeLegion, revealCreatures, revealEngagedCreatures, serverConfirmsConnection, setBoardActive, setColor, setIsGone, setLegionStatus, setPlayerName, setupBattleFight, setupBattleMove, setupBattleRecruit, setupBattleSummon, setupFight, setupMove, setupMuster, setupSplit, setupTurnState, syncOption, tellBattleMove, tellEngagement, tellEngagementResults, tellGameOver, tellInitialGameInfo, tellLegionLocation, tellMovementRoll, tellPlayerElim, tellProposal, tellRedo, tellReplay, tellSlowResults, tellStrikeResults, tellSyncCompleted, tellWhatsHappening, undidMove, undidRecruit, undidSplit, updateCreatureCount, updateOnePlayersInfo, updatePlayerInfo
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface net.sf.colossus.client.IClient
addCreature, appendToConnectionLog, askAcquireAngel, askChooseStrikePenalty, askConcede, askFlee, askNegotiate, askPickColor, askPickFirstMarker, cleanupBattle, confirmWhenCaughtUp, createSummonAngel, didMove, didRecruit, didSplit, didSummon, doReinforce, initBattle, initBoard, kickPhase, log, messageFromServer, nak, nextEngagement, pingRequest, placeNewChit, removeCreature, removeDeadBattleChits, removeLegion, revealCreatures, revealEngagedCreatures, serverConfirmsConnection, setBoardActive, setColor, setLegionStatus, setPlayerName, setupBattleFight, setupBattleMove, setupBattleRecruit, setupBattleSummon, setupFight, setupMove, setupMuster, setupSplit, setupTurnState, syncOption, tellBattleMove, tellEngagement, tellEngagementResults, tellGameOver, tellLegionLocation, tellMovementRoll, tellPlayerElim, tellProposal, tellRedo, tellReplay, tellSlowResults, tellStrikeResults, tellSyncCompleted, tellWhatsHappening, undidMove, undidRecruit, undidSplit, updateCreatureCount, updateOnePlayersInfo, updatePlayerInfo
-
-
-
-
Field Detail
-
LOGGER
private static final java.util.logging.Logger LOGGER
-
socketChannel
private final java.nio.channels.SocketChannel socketChannel
-
selectorKey
private final java.nio.channels.SelectionKey selectorKey
-
clientVersion
private int clientVersion
-
spectator
private boolean spectator
-
didExplicitDisconnect
private boolean didExplicitDisconnect
-
withdrawnAlready
private boolean withdrawnAlready
-
cantSendMessageRepeated
private int cantSendMessageRepeated
-
temporarilyDisconnected
private boolean temporarilyDisconnected
-
obsolete
private boolean obsolete
-
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
-
commitPointCounter
private int commitPointCounter
-
MAX_KEEP_LINES
private static final int MAX_KEEP_LINES
- See Also:
- Constant Field Values
-
recentlyProcessedLines
private final java.util.ArrayList<java.lang.String> recentlyProcessedLines
-
newCounter
private int newCounter
-
bb
java.nio.ByteBuffer bb
-
encodedMsg
java.lang.String encodedMsg
-
should
int should
-
writtenTotal
int writtenTotal
-
previousRetries
int previousRetries
-
temporarilyInTrouble
private long temporarilyInTrouble
-
queue
java.util.LinkedList<java.lang.String> queue
The queue in which messages are stored, until they were really written. Usually empty; stuff piles up only when writing to socket fails, e.g. network or client too slow.
-
fakeDisconnect
public boolean fakeDisconnect
Debug stuff, only for testing/development purposes
-
-
Constructor Detail
-
ClientHandler
ClientHandler(Server server, java.nio.channels.SocketChannel channel, java.nio.channels.SelectionKey selKey)
-
-
Method Detail
-
getSelectorKey
public java.nio.channels.SelectionKey getSelectorKey()
-
getSocketChannel
public java.nio.channels.SocketChannel getSocketChannel()
-
isGone
public boolean isGone()
-
isStub
protected boolean isStub()
- Overrides:
isStub
in classClientHandlerStub
-
isSpectator
public boolean isSpectator()
-
didExplicitDisconnect
public boolean didExplicitDisconnect()
-
setTemporarilyDisconnected
public void setTemporarilyDisconnected()
-
isTemporarilyDisconnected
public boolean isTemporarilyDisconnected()
-
canHandlePingRequest
protected boolean canHandlePingRequest()
- Overrides:
canHandlePingRequest
in classClientHandlerStub
-
canHandleNewVariantXML
protected boolean canHandleNewVariantXML()
-
disposeClient
public void disposeClient()
Server side disposes a client (and informs it about it first) To be used only for "disposeAllClients()", otherwise setIsGone reason is misleading.- Specified by:
disposeClient
in interfaceIClient
- Overrides:
disposeClient
in classClientHandlerStub
-
processInput
public void processInput(java.nio.ByteBuffer byteBuffer)
-
sendViaChannel
private void sendViaChannel(java.lang.String msg)
-
flushQueuedContent
protected void flushQueuedContent()
Description copied from class:ClientHandlerStub
Selector reported that client became writable again (after a prior write attempt had not written all bytes). Now start/try writing the message(s) which are still in the queue.- Overrides:
flushQueuedContent
in classClientHandlerStub
-
enqueueToRedoQueue
protected void enqueueToRedoQueue(int messageNr, java.lang.String message)
- Overrides:
enqueueToRedoQueue
in classClientHandlerStub
-
commitPoint
protected void commitPoint()
- Overrides:
commitPoint
in classClientHandlerStub
-
confirmCommitPoint
private void confirmCommitPoint(int confirmedNr)
Remove the messages in redoQueue prior to given commit point- Parameters:
confirmedNr
- Commit point from which we now know that client has successfully received it
-
supportsReconnect
public boolean supportsReconnect()
- Overrides:
supportsReconnect
in classClientHandlerStub
-
canHandleBattleMoveNak
public boolean canHandleBattleMoveNak()
-
canHandleAdvancedSync
public boolean canHandleAdvancedSync()
- Overrides:
canHandleAdvancedSync
in classClientHandlerStub
-
cloneRedoQueue
public void cloneRedoQueue(ClientHandler oldCH)
-
initRedoQueueFromStub
public void initRedoQueueFromStub(ClientHandlerStub stub)
-
reEnqueue
private void reEnqueue(MessageForClient mfc)
-
syncAfterReconnect
public void syncAfterReconnect(int lastReceivedMessageNr, int syncRequestNumber)
Re-send all data after the message from which we know client got it- Parameters:
lastReceivedMessageNr
- Last message which client did still receivesyncRequestNumber
- Every request has own unique id, so we don't mix them
-
isTemporarilyInTrouble
public boolean isTemporarilyInTrouble()
-
howLongAlreadyInTrouble
public long howLongAlreadyInTrouble()
-
setTemporarilyInTrouble
private void setTemporarilyInTrouble()
-
clearTemporarilyInTrouble
public void clearTemporarilyInTrouble()
-
handleEncoding
private void handleEncoding(java.lang.String msg)
-
sendViaChannelRaw
private void sendViaChannelRaw(java.lang.String msg)
-
attemptWritingToChannel
private void attemptWritingToChannel()
-
withdrawIfNeeded
private void withdrawIfNeeded(boolean explicit)
Make sure player is withdrawn from game. Explicit if via Withdraw message from client, implicit because of disconnect message or connection problems. This is just a wrapper for the both situations where for !withdrawnAlready should be checked- Parameters:
explicit
- Whether client has requested withdraw explicitly
-
declareObsolete
public void declareObsolete()
-
dumpLastProcessedLines
public java.lang.String dumpLastProcessedLines()
-
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)
This is the longish if-elseif-else block which deserialized all client-to-server calls back from String to actual methodCalls.- Parameters:
method
- The method to executeargs
- A list of argument Strings
-
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"
-
resolveMasterHex
private MasterHex resolveMasterHex(java.lang.String hexLabel)
-
resolveLegion
private Legion resolveLegion(java.lang.String markerId)
-
sendToClient
protected void sendToClient(java.lang.String message)
- Overrides:
sendToClient
in classClientHandlerStub
-
fakeDisconnectClient
public void fakeDisconnectClient()
-
clearDisconnectClient
public void clearDisconnectClient()
-
wasFakeDisconnectFlagSet
public boolean wasFakeDisconnectFlagSet()
-
-