Class ServerSingleThread

java.lang.Object
za.co.wethinkcode.robots.server.networking.ServerSingleThread
All Implemented Interfaces:
Callable<Void>

public class ServerSingleThread extends Object implements Callable<Void>
This is a single thread execution of server logic. Its functionality is extended in Server Thread Runner Main
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    com.fasterxml.jackson.databind.ObjectMapper
     
    static final int
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    ServerSingleThread(Socket socket, ClientCommandFactory commandFactory, WorldStateIdentity sharedIdentity)
    Necessary Constructor for shared Mutable State identity
    This is a single thread execution of server logic.
    ServerSingleThread(Socket socket, WorldState currentWorldState, ClientCommandFactory commandFactory, WorldStateIdentity sharedIdentity)
    Constructor OverLoad for the single Thread execution of server Logic.
  • Method Summary

    Modifier and Type
    Method
    Description
     
    public void run() { try { String messageFromClient; while((messageFromClient = in.readLine()) != null) { try { //Takes in a request from a client ClientCommandRequest request = mapper.readValue(messageFromClient, ClientCommandRequest.class); //Creates a new command from it ClientCommand command = ClientCommandFactory.createCommand(request.getCommand(), request, this.currentWorldState, this.mapper); //Evaluates that command into a result ClientCommandResult commandResult = (ClientCommandResult) command.execute(request.getArgumentsMap(),this.currentWorldState); //Creates a response based on that result ServerResponse serverResponse = ServerResponseFactory.create(commandResult); //Sends that response back to the client mapper.writeValue(out,serverResponse); String tempO = request.getRobotName(); System.out.println("Message \"" + messageFromClient + "\" from " + clientMachine); out.println(tempO); out.println("Thanks for this message: "+messageFromClient); } catch (Exception e) { // throw new RuntimeException(e); out.println(e.getMessage()); } } } catch(IOException ex) { System.out.println("Shutting down single client server"); } finally { closeQuietly(); } }

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • mapper

      public com.fasterxml.jackson.databind.ObjectMapper mapper
    • PORT

      public static final int PORT
      See Also:
  • Constructor Details

    • ServerSingleThread

      public ServerSingleThread(Socket socket, ClientCommandFactory commandFactory, WorldStateIdentity sharedIdentity) throws IOException
      Necessary Constructor for shared Mutable State identity
      Parameters:
      socket - what socket this server is running on
      commandFactory - the clientCommandFactory that is being created so that more commandFactories are not needlessly made
      sharedIdentity - the global shared identity of the worldState
      Throws:
      IOException
    • ServerSingleThread

      public ServerSingleThread(Socket socket, WorldState currentWorldState, ClientCommandFactory commandFactory, WorldStateIdentity sharedIdentity) throws IOException
      Constructor OverLoad for the single Thread execution of server Logic. this one accepts
      Parameters:
      socket - what socket this server is running on
      currentWorldState - state of the world that this thread will evaluate
      commandFactory - the clientCommandFactory that is being created so that more commandFactories are not needlessly made
      Throws:
      IOException
    • ServerSingleThread

      public ServerSingleThread(Socket socket, WorldStateIdentity identity) throws IOException
      This is a single thread execution of server logic. Its functionality is extended in Server Thread Runner Main
      Throws:
      IOException
  • Method Details

    • getNextWorldState

      public WorldState getNextWorldState()
      public void run() { try { String messageFromClient; while((messageFromClient = in.readLine()) != null) { try { //Takes in a request from a client ClientCommandRequest request = mapper.readValue(messageFromClient, ClientCommandRequest.class); //Creates a new command from it ClientCommand command = ClientCommandFactory.createCommand(request.getCommand(), request, this.currentWorldState, this.mapper); //Evaluates that command into a result ClientCommandResult commandResult = (ClientCommandResult) command.execute(request.getArgumentsMap(),this.currentWorldState); //Creates a response based on that result ServerResponse serverResponse = ServerResponseFactory.create(commandResult); //Sends that response back to the client mapper.writeValue(out,serverResponse); String tempO = request.getRobotName(); System.out.println("Message \"" + messageFromClient + "\" from " + clientMachine); out.println(tempO); out.println("Thanks for this message: "+messageFromClient); } catch (Exception e) { // throw new RuntimeException(e); out.println(e.getMessage()); } } } catch(IOException ex) { System.out.println("Shutting down single client server"); } finally { closeQuietly(); } }
    • call

      public Void call() throws Exception
      Specified by:
      call in interface Callable<Void>
      Returns:
      the commandResult to be evaluated against and mutate the global state
      Throws:
      Exception - any exception possible