|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcz.cuni.pogamut.server.UTServerConnection
public class UTServerConnection
This class is used by UTServer for managing the server. It contains thread that is continuously processing the requests that can be made through UTServer class. It also alters the information in UTServer (protected ones). Remember ... on this.requests / this.messagesFromGB is synchronized the this.requestOrMessageLatch ... on that we're waiting if no request of message is in queue ... IF YOU ADD ANYTHING TO this.requests || this.messagesFromGB always do it in SYNC with respective object (this.requests || this.messagesFromGB) and in the same SYNC do this.requestOrMessageLatch.countDown() Note that if you in thread finds out that communication died out, just throw new UTServerConnectionReinitialize("reason") exception, the try-catch block in the run() will catch it and process it accordinaly.
Field Summary | |
---|---|
private static int |
AFTER_SAME_MAP_WAIT_MILLIS
|
private java.util.concurrent.BlockingQueue<java.lang.String> |
commandsToGB
Commands that should be delivered to gamebots, are taken by Mediator's CLIENT->PARSER thread. |
private static int |
COMMUNICATION_FLAG_LISTENER
|
private boolean |
communicationShouldBeRunning
Here we store information whether the communication should be OK. |
private Flag<UTServerRequest> |
currentRequestFlag
Request the thread is currently processing. |
private java.net.URI |
currentUri
|
private GameBotConnection |
gameBotConnection
|
private java.util.logging.Logger |
gbRawDataLog
Logger for GameBotConnection for raw data (not parsed). |
private static long |
INFINITE_WAIT
|
private long |
lastPingSentMillis
|
private long |
lastReconnectTriedMillis
|
private java.util.logging.Logger |
log
Log of the server, logging the requests and it's results. |
private int |
mapChangeReconnectAttempt
How many times we have tried to connect to GB before connect to the same map again. |
private boolean |
mapChanging
Flag that tells us whether the map is changing. |
private static int |
MAX_MAP_RECONNECT_ATTEMPTS
|
private Mediator |
mediator
|
protected java.util.Set<RcvMsgListener> |
messageListeners
|
private static UTServerRequest |
messagePoll
|
protected java.util.concurrent.BlockingQueue<MessageObject> |
messagesFromGB
Messages from GameBots... |
private static long |
NO_WAIT
|
private static UTServerRequest |
nothing
|
private UTServerConnectionInterface |
owner
Owner of the UTServerConnection, who spawned it. |
private Parser |
parser
|
private boolean |
pingSent
|
private boolean |
pongReceived
|
private java.lang.String |
previousMap
Previous map on the server, this information is needed, to check whether the server has changed the map or not. |
private static UTServerRequest |
requestAutoReconnect
|
private java.util.concurrent.CountDownLatch |
requestOrMessageLatch
|
private java.lang.Object |
requestOrMessageLatchAccess
|
private static UTServerRequest |
requestPingPong
|
private java.util.concurrent.BlockingQueue<UTServerRequest> |
requests
Queue with requests to the server. |
private boolean |
shouldRun
Config flag ... when dropped, the thread will die and the object will be no longer usable! |
private java.lang.Thread |
thread
Here we store the instance of the Thread. |
private Flag<java.lang.Boolean> |
threadAlive
Flag which tells us wether threadIsAlive, it is changed to TRUE when the thread is started and to FALSE as the last command of the thread. |
private static int |
threadCounter
Serves for numbering the threads of all ServerControls, just for naming of the threads. |
private static UTServerRequest |
waitingBounded
|
private static UTServerRequest |
waitingInfinite
|
Constructor Summary | |
---|---|
UTServerConnection(UTServerConnectionInterface owner)
|
Method Summary | |
---|---|
void |
addRcvMsgListener(RcvMsgListener listener)
Add listener that will be notified when a new message from Server Control will arrive. |
private void |
clearServerInfo()
Clears informations in this.owner (like navpoints, items, available maps, etc.) |
void |
closeClient()
Close the connection (if any exists). |
private void |
communicationDied()
Called everytime ... communicationShouldBeRunning and it isn't |
private long |
computeMaxWaiting()
This should return millis how long (max) we should wait for GB message / request to arrive before checking the PING/PONG and AUTO-RECONNECT |
void |
connect()
Can be called when the server state is TERMINATED or NONE, start up the thread. |
private void |
connectToGB()
Connects to the GameBot, URI is taken from this.owner.getServerURI() Doesn't do anything if communication is alive. |
void |
disconnect()
|
private void |
doAutomaticReconnect()
Called only IFF communication is down... |
private void |
doPingPong()
|
void |
flagChanged(java.lang.Object changedValue,
int listenerParam)
When registering the flag you may additionally specify listener parameter, if you do so - your listener will be called with that parameter. |
private MessageObject |
getBlockingMessageFromGB()
Called ONLY from server connection thread! |
UTServerRequest |
getCurrentRequest()
Returns the request the object is currently processing, may be null! |
java.net.URI |
getCurrentURI()
|
java.lang.Thread |
getThread()
Get thread of the instance, if not isThreadAlive() then it returns null. |
Flag<java.lang.Boolean> |
getThreadAliveFlag()
Returns the flag that tells you whether the thread is alive, you may register listener on it. |
boolean |
changeMap(java.lang.String mapName)
Will send command to change the map IFF it's not the one that is currently running. |
boolean |
changeMapAndWait(java.lang.String mapName)
Will send command to change the map IFF it's not the one that is currently running. |
protected void |
interruptThread()
|
private boolean |
isCommunicationAlive()
|
boolean |
isThreadAlive()
Returns state of the thread. |
private void |
loadAvailableMaps(boolean forced)
Called only from connectToGB(), to get list of available maps on server. |
private java.util.List<UTMap> |
loadAvailableMapsInner()
Sends GETITEMS to GB and waits for items, returns list of them. |
private void |
loadInfoInner(java.lang.String command,
MessageType begin,
MessageType info,
MessageType end,
java.util.Collection result)
Sends @command to GB and waits for message of type @begin, then filling result array with messages of type @info until message of type @end comes. |
private void |
loadInventory(boolean forced)
Called only from processRequest(), it will load inventory, which is map 'Human readable string' -> Item with filled property. |
private void |
loadItems(boolean forced)
Called only from processRequest(), it will load navpoints IFF not previously loaded... it will store it to the this.owner.items It will also raise the latch in owner ... ... this.owner.itemsLoadedLatch |
private java.util.List<Item> |
loadItemsInner()
Sends GETITEMS to GB and waits for items, returns list of them. |
private void |
loadNavPoints(boolean forced)
Called only from processRequest(), it will load navpoints IFF not previously loaded... it will store it to the this.owner.navPoints It will also raise the latch in owner ... ... this.owner.navPointsLoadedLatch |
private java.util.List<NavPoint> |
loadNavPointsInner()
Sends GETNAVS to GB and waits for navpoints, returns list of them. |
private void |
loadPlayers()
Called from processRequest() , don't call from anywhere else (out of sync). |
private java.util.Set<Player> |
loadPlayersInner()
Sends GETPLRS to GB and waits for players, returns list of them. |
private void |
logException(java.lang.Exception e)
|
void |
mapFinishedMessageCame()
|
private void |
preprocessGBMessage(MessageObject msg)
Every message from GB goes through HERE ... doing some miscellaneous work. |
private void |
processGBMessage()
Process one message from the GB queue ... currently it's just removing it from the queue (all of them). |
private void |
processRequest()
Process one request from the requests queue ... currently it's just removing it from the queue. |
private void |
processRequestOrMessage()
Processing one MESSAGE and one REQUEST if there is any... |
java.lang.String |
receiveMessageFromClient()
Get one message from client which is needed to deliver to the GameBots. |
private void |
reinitialize(boolean reconnect)
Must be called only from withing the run() Reinitilize the communication ... e.g. will close it, clean the queues (commands, messages, requests). |
private void |
reinitInnerVariables()
|
void |
removeRcvMsgListener(RcvMsgListener listener)
Remove listener that listens for messages from server control. |
boolean |
request(UTServerRequest request)
This method is synchronized! |
void |
run()
Is called internaly from runThread() ... this class shouldn't be run via Thread.run(object). |
private java.lang.Thread |
runThread()
Called from constructor to start the thread. |
void |
sendCommandToGB(java.lang.String message)
|
void |
sendMessageToClient(MessageObject msg)
Sends message object to the client. |
private void |
setCurrentRequest(UTServerRequest request)
|
private void |
setServerState(UTServerState serverState)
|
private void |
stopCommunication()
Will stop communication if working. |
protected void |
terminate()
|
private void |
threadIteration()
"Body" of the thread ... here we're processing the requests to the connection. |
private MessageObject |
waitForResponse(MessageType type)
Wait until message of type @type arrives, then return it. |
void |
wakeUpWaiting()
Wakes up the thread is waiting for request / message. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
private static final UTServerRequest requestPingPong
private static final UTServerRequest requestAutoReconnect
private static final UTServerRequest messagePoll
private static final UTServerRequest waitingInfinite
private static final UTServerRequest waitingBounded
private static final UTServerRequest nothing
private static final long INFINITE_WAIT
private static final long NO_WAIT
private static final int COMMUNICATION_FLAG_LISTENER
private static final int MAX_MAP_RECONNECT_ATTEMPTS
private static final int AFTER_SAME_MAP_WAIT_MILLIS
private UTServerConnectionInterface owner
private static int threadCounter
private Flag<java.lang.Boolean> threadAlive
private java.lang.Thread thread
private java.util.logging.Logger gbRawDataLog
private java.util.logging.Logger log
private boolean shouldRun
private boolean mapChanging
private int mapChangeReconnectAttempt
private java.lang.String previousMap
private Flag<UTServerRequest> currentRequestFlag
private java.lang.Object requestOrMessageLatchAccess
private java.util.concurrent.CountDownLatch requestOrMessageLatch
private java.net.URI currentUri
protected java.util.Set<RcvMsgListener> messageListeners
private java.util.concurrent.BlockingQueue<UTServerRequest> requests
private java.util.concurrent.BlockingQueue<java.lang.String> commandsToGB
protected java.util.concurrent.BlockingQueue<MessageObject> messagesFromGB
private GameBotConnection gameBotConnection
private Parser parser
private Mediator mediator
private boolean communicationShouldBeRunning
private boolean pingSent
private long lastPingSentMillis
private boolean pongReceived
private long lastReconnectTriedMillis
Constructor Detail |
---|
public UTServerConnection(UTServerConnectionInterface owner)
Method Detail |
---|
public void addRcvMsgListener(RcvMsgListener listener)
public void removeRcvMsgListener(RcvMsgListener listener)
private void setCurrentRequest(UTServerRequest request)
public boolean request(UTServerRequest request)
request
-
public UTServerRequest getCurrentRequest()
public Flag<java.lang.Boolean> getThreadAliveFlag()
public boolean isThreadAlive()
private java.lang.Thread runThread()
public java.lang.Thread getThread()
public void run()
run
in interface java.lang.Runnable
private void logException(java.lang.Exception e)
private void setServerState(UTServerState serverState)
private void communicationDied()
private void reinitInnerVariables()
private void clearServerInfo()
public java.net.URI getCurrentURI()
private void stopCommunication()
private void reinitialize(boolean reconnect)
public java.lang.String receiveMessageFromClient() throws CantReadException
receiveMessageFromClient
in interface MediatorClientInterface
CantReadException
java.io.IOException
public void sendMessageToClient(MessageObject msg) throws CantWriteException
sendMessageToClient
in interface MediatorClientInterface
msg
-
java.io.IOException
CantWriteException
public void closeClient() throws CantCloseConnectionException
MediatorClientInterface
closeClient
in interface MediatorClientInterface
CantCloseConnectionException
private boolean isCommunicationAlive()
public void sendCommandToGB(java.lang.String message)
private MessageObject getBlockingMessageFromGB() throws CantReadException
CantReadException
private MessageObject waitForResponse(MessageType type) throws CantReadException
CantReadException
private void threadIteration() throws java.lang.Exception
java.lang.Exception
private void processRequestOrMessage() throws java.net.UnknownHostException, ConnectException, CantReadException, UTServerConnectionTerminationRequested, UTServerConnectionReinitialization
CantReadException
ConnectException
java.net.UnknownHostException
UTServerConnectionTerminationRequested
UTServerConnectionReinitialization
private void preprocessGBMessage(MessageObject msg)
msg
- private long computeMaxWaiting()
private void doPingPong() throws java.lang.Exception
java.lang.Exception
private void doAutomaticReconnect() throws java.net.UnknownHostException, ConnectException, CantReadException, UTServerConnectionReinitialization
ConnectException
java.net.UnknownHostException
CantReadException
UTServerConnectionReinitialization
private void connectToGB() throws java.net.UnknownHostException, ConnectException, CantReadException, UTServerConnectionReinitialization
java.net.UnknownHostException
ConnectException
CantReadException
UTServerConnectionReinitialization
private void processGBMessage()
private void processRequest() throws UTServerConnectionTerminationRequested, java.net.UnknownHostException, ConnectException, CantReadException, UTServerConnectionReinitialization
CantReadException
ConnectException
java.net.UnknownHostException
UTServerConnectionReinitialization
UTServerConnectionException
UTServerConnectionTerminationRequested
protected void terminate()
public void connect()
public void disconnect()
private void loadInfoInner(java.lang.String command, MessageType begin, MessageType info, MessageType end, java.util.Collection result) throws CantReadException
CantReadException
private java.util.Set<Player> loadPlayersInner() throws CantReadException
forced
- whether to load even if list exists
CantReadException
private java.util.List<NavPoint> loadNavPointsInner() throws CantReadException
CantReadException
private java.util.List<Item> loadItemsInner() throws CantReadException
CantReadException
private java.util.List<UTMap> loadAvailableMapsInner() throws CantReadException
CantReadException
private void loadPlayers() throws CantReadException
CantReadException
private void loadNavPoints(boolean forced) throws CantReadException
CantReadException
private void loadItems(boolean forced) throws CantReadException
CantReadException
private void loadAvailableMaps(boolean forced) throws CantReadException
CantReadException
private void loadInventory(boolean forced) throws CantReadException
CantReadException
public void flagChanged(java.lang.Object changedValue, int listenerParam)
FlagListener
flagChanged
in interface FlagListener
public void wakeUpWaiting()
public boolean changeMap(java.lang.String mapName)
mapName
- public boolean changeMapAndWait(java.lang.String mapName)
mapName
- public void mapFinishedMessageCame() throws UTServerConnectionReinitialization
UTServerConnectionReinitialization
protected void interruptThread()
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |