Class ServerSingleThread
java.lang.Object
za.co.wethinkcode.robots.server.networking.ServerSingleThread
This is a single thread execution of server logic. Its functionality is extended in
Server Thread Runner Main
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionServerSingleThread(Socket socket, ClientCommandFactory commandFactory, WorldStateIdentity sharedIdentity) Necessary Constructor for shared Mutable State identityServerSingleThread(Socket socket, WorldStateIdentity 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 TypeMethodDescriptioncall()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(); } }
-
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 oncommandFactory- the clientCommandFactory that is being created so that more commandFactories are not needlessly madesharedIdentity- 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 oncurrentWorldState- state of the world that this thread will evaluatecommandFactory- the clientCommandFactory that is being created so that more commandFactories are not needlessly made- Throws:
IOException
-
ServerSingleThread
This is a single thread execution of server logic. Its functionality is extended in Server Thread Runner Main- Throws:
IOException
-
-
Method Details
-
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
-