|
Color Belot Server Code, Part I Today we will look at all of the supporting code for the server and part of the server code itself. The supporting code is the code necessary to carry out a game of Color Belot. Most of this supporting code is the same from the previous version. Major changes will be commented on where appropriate. It might be helpful to look at the code directly to make sure that you understand the functionality that it provides. No significant changes were made to this file. No significant changes were made to this file. Notice that the "imageNumber" instance variable that was added for the interface, continues to play a critical role since the client only manipulates images. No significant changes were made to this file. No significant changes were made to this file. A method called "findCard" was added whose purpose is to identify the "Card" that has been clicked on to be played, based on the "imageNumber" of the card in the client. Otherwise, no significant changes were made. No significant changes were made to this file. Finally, we reach the server! Today we will examine the "ClientHandler" class (it appears at the bottom of the file) and tomorrow we will examine the "Server" and "ServerThread" classes. The "Server" will spawn a separate "ClientHandler" for each of the two clients that it handles. The purpose of a "ClientHandler" is to listen for messages to be received from a client and then to take appropriate action. The constructor is passed a "Socket", a "number" (that identifies which client is being served), and a "ServerThread" (a pointer to an instance of the class that spawns a "ClientHandler"). The constructor records the "number" and "server" and then uses the "Socket" to set up a "DataInputStream" and "DataOutputStream". The "run" method begins by using the "sendMessage" (to be described tomorrow) method to send the client its "clientNumber". It then calls the "showCards" method (to be described tomorrow) to send the client its initial five cards. Next it sends a "STARTBIDDING" message to the client to get the bidding underway. The "run" method then enters a loop that continues until the "command" equals "QUIT". Within the loop, a "readUTF" method is called to read the current message from the client to the "ClientHandler". If there is currently no message, the "ClientHandler" sleeps until there is. After a message is detected, the command is determined using the "determineCommand" method (to be described shortly) and the message is then shortened using the "shortenMessage" method (to be described shortly). The possible commands that can be received are then processed using an "if" construct. The commands are processed in alphabetical order. "AGAIN" means that the client wants to play another complete game of Color Belot. "BID1" means that the client has made its first bid. "BID2" means that the client has made its second bid. "CARD1" means that the client has led a card for the current trick. "CARD2" means that the client has played the second card of a trick. "MESSAGE" means that the client wants to pass along a message to the other client. "PASSOUT" means that both players have passed twice on the current deal. In this case, the cards are shuffled and a new hand is dealt. Finally, "QUIT" means that the client does not want to play another game of Color Belot. To carry out the desired consequences, the "ClientHandler" calls appropriate methods in the "parent". These methods will be discussed tomorrow. The "determineCommand" method determines the client's current command. The command is located between the first and second "#" marks in the string that is sent to the "ClientHandler". For example, if the message is "#QUIT#", "determineCommand" returns "QUIT". The "shortenCommand" method strips off the first part of the message to make it easier to find additional arguments. If the "message" is "#CARD1#20#" and "argument" is "CARD1", the method returns the string "#20#". Finally, there are four instance variables contained in a "ClientHandler". There is a "DataInputStream" for reading messages from the client to the server, a "DataOutputStream" for sending messages from the server to the client, a "clientNumber" that identifies which client the "ClientHandler" is serving, and a "parent" that allows the "ClientHandler" to make calls back to the "ServerThread" that launched the "ClientHandler" in the first place. |