Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Network;
- import GameV1.Mark;
- import Network.ClientHandler;
- import java.util.*;
- import java.io.IOException;
- import java.net.ServerSocket;
- import java.net.Socket;
- import Network.ProtocolConstants;
- import Network.ProtocolControl;
- import Network.Board;
- /**
- * Server.
- * @author Theo Ruys
- * @version 2005.02.21
- */
- public class Server {
- private static final String USAGE = "usage: " + Server.class.getName() + " <port>";
- /** Start een Server-applicatie op. */
- public static void main(String[] args) {
- if (args.length != 1) {
- System.out.println(USAGE);
- System.exit(0);
- }
- Scanner in = new Scanner(System.in);
- System.out.println("Enter portnumber: ");
- String portInput = in.nextLine();
- port = Integer.parseInt(portInput);
- Server server = new Server(port);
- server.run();
- }
- private static int port;
- private List<ClientHandler> inactiveThreads;
- private List<ClientHandler> waitingThreads;
- private List<ClientHandler> matchedPairs;
- // private List<Game> activeGames;
- /** Constructs a new Server object */
- public Server(int port) {
- this.inactiveThreads = new ArrayList<ClientHandler>();
- this.waitingThreads = new ArrayList<ClientHandler>();
- this.matchedPairs = new ArrayList<ClientHandler>();
- //this.activeGames = new ArrayList<Game>();
- //this.start();
- }
- /**
- * Listens to a port of this Server if there are any Clients that
- * would like to connect. For every new socket connection a new
- * ClientHandler thread is started that takes care of the further
- * communication with the Client.
- * @param array
- */
- public void run() {
- int i = 0;
- try {
- ServerSocket ssocket = new ServerSocket(this.port);
- while(true){
- Socket socket = ssocket.accept();
- ClientHandler handler = new ClientHandler(this, socket);
- System.out.println("[Client no. " + (++i) + "]" + "connected.");
- handler.announce();
- handler.start();
- addInactiveHandler(handler);
- }} catch (IOException e){}}
- public void acceptRequest(ClientHandler c){
- c.sendMessage(ProtocolControl.acceptRequest + ProtocolConstants.msgSeperator + c.getClientName());
- if(waitingThreads.get(0).getClientName().equals(c.getClientName())){
- usernameInUse(c);
- } else { inactiveThreads.remove(c);
- waitingThreads.add(c);
- if(waitingThreads.size() == 2){
- moveToPairs(c);
- }
- }
- }
- public void sendBoard(ClientHandler c) {
- String strBoard = "";
- Board copy = new Board();
- for(int i = 0; i < (copy.ROW * copy.COL)-1; i++){
- strBoard += ProtocolConstants.msgSeperator + copy.getField(i);
- }
- c.sendMessage(ProtocolControl.sendBoard + strBoard);
- }
- public int aantalZetten(){
- int zetten = 0;
- Board copy = new Board();
- for(int i = 0; i < (copy.ROW * copy.COL)-1; i++){
- if(copy.getField(i) != Mark.EMPTY){
- zetten++;
- }
- }
- return zetten;
- }
- public String turn(){
- return waitingThreads.get(this.aantalZetten()%2).getClientName();
- }
- /* if(aantalZetten()%2==0){
- waitingThreads.get(0).getClientName();
- } else {
- waitingThreads.get(1).getClientName();
- }
- }*/
- public void moveToPairs(ClientHandler c){
- if(waitingThreads.size() == 2);
- waitingThreads.remove(c);
- matchedPairs.add(c);
- }
- public void colors(ClientHandler c){
- }
- //Errors m.b.t. verkeerde command
- /*public void invalidUserTurn(ClientHandler c){
- c.sendMessage(ProtocolConstants.invalidCommand + ProtocolConstants.msgSeperator + ProtocolConstants.invalidUserTurn + ProtocolConstants.msgSeperator + c.getClientName());
- }
- public void invalidCommand(ClientHandler c){
- c.sendMessage(ProtocolConstants.invalidCommand + ProtocolConstants.msgSeperator + ProtocolConstants.invalidCommand);
- }*/
- public void usernameInUse(ClientHandler c){
- c.sendMessage(ProtocolConstants.invalidCommand + ProtocolConstants.msgSeperator + ProtocolConstants.usernameInUse);
- }
- /*
- public void invalidMove(ClientHandler c){
- c.sendMessage(ProtocolConstants.invalidCommand + ProtocolConstants.msgSeperator + ProtocolConstants.invalidMove);
- }
- public void invalidUserName(ClientHandler c){
- c.sendMessage(ProtocolConstants.invalidCommand + ProtocolConstants.msgSeperator + ProtocolConstants.invalidUsername);
- }
- /*Endgame constanten
- public void endGameWinner(){
- c.sendMessage("endGame" + ProtocolConstants.msgSeperator + resultString + ProtocolConstants.msgSeperator + "winner");
- }
- public void endGameConnection(){
- c.sendMessage("endGame" + ProtocolConstants.msgSeperator + resultString + ProtocolConstants.msgSeperator + "connectionlost");
- }
- public void endGameDraw(){
- c.sendMessage("endGame" + ProtocolConstants.msgSeperator + resultString + ProtocolConstants.msgSeperator + "draw");
- }
- public void endGameUnknown(){
- c.sendMessage("endGame" + ProtocolConstants.msgSeperator + resultString + ProtocolConstants.msgSeperator + "unknownerror");
- }*/
- /**
- * Sends a message using the collection of connected ClientHandlers
- * to all connected Clients.
- * @param msg message that is send
- */
- public void broadcast(String msg, ClientHandler c) {
- String[] splitArray = msg.split(ProtocolConstants.msgSeperator);
- if (splitArray[0].equals(ProtocolControl.joinRequest)){
- acceptRequest(c);}
- else { if(splitArray[0].equals(ProtocolControl.getBoard)){
- sendBoard(c);}
- else{ if(splitArray[0].equals(ProtocolControl.playerTurn)){
- turn();}
- }
- }
- }
- /*else { if(splitArray[0].equals(ProtocolControl.doMove)){
- moveResult(index, c.getClientName(), valid, nextPlayer)){
- }*/
- //isValid wanneer vakje leeg is
- //
- public void endGame(){
- }
- /**
- * Add a ClientHandler to the collection of ClientHandlers.
- * @param handler ClientHandler that will be added
- */
- public void addInactiveHandler(ClientHandler handler) {
- inactiveThreads.add(handler);
- }
- /**
- * Remove a ClientHandler from the collection of ClientHanlders.
- * @param handler ClientHandler that will be removed
- */
- public void removeInactiveHandler(ClientHandler handler) {
- inactiveThreads.remove(handler);
- }
- public void addWaitingHandler(ClientHandler handler) {
- waitingThreads.add(handler);
- }
- public void removeWaitingHandler(ClientHandler handler) {
- waitingThreads.remove(handler);
- }
- public void removeMatchedPairs(ClientHandler handler) {
- matchedPairs.remove(handler);
- }
- public void addMatchedPairs(ClientHandler handler) {
- matchedPairs.add(handler);
- }
- }// end of class Server
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement