Package it.polimi.ingsw.Server
Class SocketServer
java.lang.Object
it.polimi.ingsw.Server.SocketServer
Socket Server, uses Status Messages to inform clients of game's evolution
-
Field Summary
Modifier and TypeFieldDescriptionprivate static Map<GameController,
Object> private static Map<GameController,
Object> private static Map<GameController,
Semaphore> private static final int
private static com.google.gson.Gson
private static Map<String,
GameController> private static final ExecutorService
private static int
private static final Map<PrintWriter,
GameController> private final com.google.gson.ExclusionStrategy
private static final Map<Socket,
GameController> -
Constructor Summary
ConstructorDescriptionSocketServer
(Map<String, GameController> identifierToController, Map<GameController, Semaphore> controllerToSemaphore, Map<GameController, Object> controllerToCommuter, Map<GameController, Object> controllerToLobbyCommuter) SocketServer constructor, managed by the Server Manager by default -
Method Summary
Modifier and TypeMethodDescriptionprivate static boolean
admissionResult
(GameController gc, String nickname, PrintWriter out, int numofPlayers, com.google.gson.Gson gson) this method manages admission phaseprivate static void
communication
(Socket socket) this method manages communication phases between Server and Client (Server side)static void
endGameHandler
(GameController gc, boolean invokedbyRMI, boolean invokedAtKATimeout) Implements End Game strategy, informing all clients (Socket and RMI ones) to disconnect ad deallocates Model Objects from global structuresstatic void
Main method, welcoming Server management consoleprivate static void
makeMove
(GameController gc, PrintWriter out, com.google.gson.Gson gson, AtomicBoolean clientsTurn, ProducerConsumerLock lineLockers, AtomicReference<String> l, com.google.gson.Gson gson_move) this method manages the move phaseprivate static void
readClientLine
(BufferedReader in, PrintWriter out, AtomicBoolean clientsTurn, ProducerConsumerLock lineLockers, AtomicReference<String> l, AtomicReference<SocketKeepAlive> ka) Socket thread listenerprivate static String
receiveIdentifier
(BufferedReader in, PrintWriter out, com.google.gson.Gson gson) this method receives the identifierstatic String
requestNickname
(BufferedReader in, PrintWriter out, String identifier, com.google.gson.Gson gson) this method receives the nickname chosen by the playerprivate static int
requestNumOfPlayer
(BufferedReader in, PrintWriter out, com.google.gson.Gson gson) this method receives the number of player by the first playerprivate static void
updateClientStatus
(GameController gc, String nickname, PrintWriter out, com.google.gson.Gson gson) this method sends Board, Shelf and cards at the end of every turn
-
Field Details
-
countParameters
private static final int countParameters- See Also:
-
pool
-
printWriterToGameController
-
socketToGameController
-
portNumber
private static int portNumber -
identifierToController
-
controllerToSemaphore
-
controllerToCommuter
-
controllerToLobbyCommuter
-
gson_2
private static com.google.gson.Gson gson_2 -
serializationStrategy
private final com.google.gson.ExclusionStrategy serializationStrategy
-
-
Constructor Details
-
SocketServer
public SocketServer(Map<String, GameController> identifierToController, Map<GameController, Semaphore> controllerToSemaphore, Map<GameController, Object> controllerToCommuter, Map<GameController, Object> controllerToLobbyCommuter) SocketServer constructor, managed by the Server Manager by default- Parameters:
identifierToController
- maps game's identifiers to GameControllerscontrollerToSemaphore
- maps GameControllers to synchronization semaphorescontrollerToCommuter
- maps GamesController to synchronization commuterscontrollerToLobbyCommuter
- maps GameController to synchronization lobbyCommuters
-
-
Method Details
-
main
Main method, welcoming Server management console- Parameters:
args
- default arguments array
-
communication
this method manages communication phases between Server and Client (Server side)- Parameters:
socket
- socket- Throws:
IOException
- input/output exception
-
updateClientStatus
private static void updateClientStatus(GameController gc, String nickname, PrintWriter out, com.google.gson.Gson gson) throws RemoteException this method sends Board, Shelf and cards at the end of every turn- Parameters:
gc
- game controllernickname
- player's nicknameout
- PrintWritergson
- Gson Builder- Throws:
RemoteException
- related to RMI
-
makeMove
private static void makeMove(GameController gc, PrintWriter out, com.google.gson.Gson gson, AtomicBoolean clientsTurn, ProducerConsumerLock lineLockers, AtomicReference<String> l, com.google.gson.Gson gson_move) this method manages the move phase- Parameters:
gc
- game controllerout
- print writergson
- Gson BuilderclientsTurn
- true if it's client turn, false otherwiselineLockers
- ProducerConsumerLockl
- line read by the threadgson_move
- Gson builder for move
-
admissionResult
private static boolean admissionResult(GameController gc, String nickname, PrintWriter out, int numofPlayers, com.google.gson.Gson gson) this method manages admission phase- Parameters:
gc
- game controllernickname
- player's nicknameout
- printWriternumofPlayers
- number of playersgson
- gson builder- Returns:
- true if player has been accepted, false otherwise
-
requestNumOfPlayer
private static int requestNumOfPlayer(BufferedReader in, PrintWriter out, com.google.gson.Gson gson) throws IOException this method receives the number of player by the first player- Parameters:
in
- bufferedreaderout
- printwritergson
- gson builder- Returns:
- int number of players chosen by the first player
- Throws:
IOException
- input/output exception
-
receiveIdentifier
private static String receiveIdentifier(BufferedReader in, PrintWriter out, com.google.gson.Gson gson) throws IOException this method receives the identifier- Parameters:
in
- buffered readerout
- printwritergson
- gson builder- Returns:
- game identifier
- Throws:
IOException
- input/output exception
-
readClientLine
private static void readClientLine(BufferedReader in, PrintWriter out, AtomicBoolean clientsTurn, ProducerConsumerLock lineLockers, AtomicReference<String> l, AtomicReference<SocketKeepAlive> ka) throws IOException Socket thread listener- Parameters:
in
- bufferedreaderout
- printwriterclientsTurn
- true if it's clientsTurn, false otherwiselineLockers
- ProducerConsumerLockl
- line read by the thread- Throws:
IOException
- input/output exception
-
requestNickname
public static String requestNickname(BufferedReader in, PrintWriter out, String identifier, com.google.gson.Gson gson) throws IOException this method receives the nickname chosen by the player- Parameters:
in
- bufferedReaderout
- printwriteridentifier
- identifiergson
- gsonBuilder- Returns:
- player's nickname
- Throws:
IOException
- input/output exception
-
endGameHandler
public static void endGameHandler(GameController gc, boolean invokedbyRMI, boolean invokedAtKATimeout) Implements End Game strategy, informing all clients (Socket and RMI ones) to disconnect ad deallocates Model Objects from global structures- Parameters:
gc
- Game's ControllerinvokedbyRMI
- to avoid Server callbacksinvokedAtKATimeout
- selects the correct Status Message to send to the client
-