Package org.swtpra1

Class AIServer


  • public class AIServer
    extends java.lang.Object
    The player-server which creates a minimax/random player and handles messages from server.
    • Constructor Summary

      Constructors 
      Constructor Description
      AIServer​(int port, AIPlayer aiPlayer)
      Create new AIServer.
      AIServer​(int port, AIPlayer aiPlayer, boolean shouldLog)
      Create new AIServer.
      AIServer​(int port, AIPlayer aiPlayer, boolean shouldLog, boolean useHttps)
      Create new AIServer.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean checkJsonKeys​(com.google.gson.JsonObject jsonObject, java.lang.String[] keys)
      Check if passed json-object has needed keys.
      boolean checkJsonKeys​(java.lang.String jsonString, java.lang.String[] keys)
      Check if passed json-string has needed keys.
      void printBoard​(Board board)
      Prints a Array in a very pretty way.
      void printMessageWithPort​(java.lang.String message)
      Prints message of the form "[port: %d] Message".
      void run()
      Runs the AIServer.
      void sendResponse​(com.sun.net.httpserver.HttpExchange exchange, int responseCode)
      Sends a response header, either 200 or 400.
      void sendResponse​(com.sun.net.httpserver.HttpExchange exchange, java.lang.String json)
      Sends a response json.
      protected void stopServer()
      Stops the running server.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • AIServer

        public AIServer​(int port,
                        AIPlayer aiPlayer)
        Create new AIServer. Includes the player implementation used.
        Parameters:
        port - port of https server
        aiPlayer - random, negamax or minimax
      • AIServer

        public AIServer​(int port,
                        AIPlayer aiPlayer,
                        boolean shouldLog)
        Create new AIServer. Includes the player implementation used.
        Parameters:
        port - port of https server
        aiPlayer - random, negamax or minimax
        shouldLog - enables or disables console logging. Disabled by default.
      • AIServer

        public AIServer​(int port,
                        AIPlayer aiPlayer,
                        boolean shouldLog,
                        boolean useHttps)
        Create new AIServer. Includes the player implementation used.
        Parameters:
        port - port of https server
        aiPlayer - random, negamax or minimax
        shouldLog - enables or disables console logging. Disabled by default.
        useHttps - enables or disables https
    • Method Detail

      • run

        public void run()
        Runs the AIServer. Can receive start, turn and end messages.
      • stopServer

        protected void stopServer()
        Stops the running server.
      • sendResponse

        public void sendResponse​(com.sun.net.httpserver.HttpExchange exchange,
                                 java.lang.String json)
                          throws java.io.IOException
        Sends a response json. Sends 200 if json is not null, otherwise 400.
        Parameters:
        exchange - connection to answer
        json - json file to send
        Throws:
        java.io.IOException - connection lost
      • sendResponse

        public void sendResponse​(com.sun.net.httpserver.HttpExchange exchange,
                                 int responseCode)
        Sends a response header, either 200 or 400.
        Parameters:
        exchange - connection to answer
        responseCode - response code
      • checkJsonKeys

        public boolean checkJsonKeys​(java.lang.String jsonString,
                                     java.lang.String[] keys)
        Check if passed json-string has needed keys.
        Parameters:
        jsonString - JsonString passed in POST-Requests
        keys - array of keys which have to be in pJsonString
        Returns:
        true if all keys are in
      • checkJsonKeys

        public boolean checkJsonKeys​(com.google.gson.JsonObject jsonObject,
                                     java.lang.String[] keys)
        Check if passed json-object has needed keys.
        Parameters:
        jsonObject - json-object to be checked
        keys - keys which has to be in the json-object
        Returns:
        true if needed keys are in the json-object
      • printMessageWithPort

        public void printMessageWithPort​(java.lang.String message)
        Prints message of the form "[port: %d] Message". This is meant for server related prints.
        Parameters:
        message - message
      • printBoard

        public void printBoard​(Board board)
        Prints a Array in a very pretty way.
        Parameters:
        board - board that should be printed