Question: Please refactor the code below so that it follows the S . O . L . I.D principles of Java programming: package robot _ worlds
Please refactor the code below so that it follows the SOLI.D principles of Java programming:
package robotworldsserver;
import java.ioDataInputStream;
import java.ioDataOutputStream;
import java.ioIOException;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Timer;
import java.util.TimerTask;
import org.jline.reader.EndOfFileException;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import robotworldsserver.robot.Command;
import robotworldsserver.robot.Position;
import robotworldsserver.robot.Robot;
import robotworldsserver.robot.maze.SimpleMaze;
import robotworldsserver.robot.world.AbstractWorld;
import robotworldsserver.robot.world.IWorld;
import robotworldsserver.robot.world.Obstacle;
import robotworldsserver.robot.world.TextWorld;
Responsible for handling communication with a client connected to the server.
public class ClientHandler implements Runnable
This class by implementing the runnable interface, make each robot in the world
it is responsible for giving the robot properties already defined by the server's configuration
socket variables
final Socket clientSocket;
DataInputStream dis;
DataOutputStream dos;
String clientIdentifier;
Scanner commandLine;
ServerProtocol serverProtocol;
private static Gson gson new Gson;
private Timer statusCheckTimer;
Robot robot;
private int maximumShieldStrength;
private int maximumShots ;
loaded variables
public AbstractWorld world;
public Server connectedServer;
robot variables
String name;
Constructs a ClientHandler instance with the given client socket.
@param clientSocket The socket connected to the client.
public ClientHandlerSocket clientSocket
this.clientSocket clientSocket;
this.world new TextWorldnew SimpleMaze;
Constructs a ClientHandler instance with the given client socket, world, and connected server.
@param clientSocket The socket connected to the client.
@param worldChosen The world associated with the client.
@param currentConnectedServer The server to which the client is connected.
public ClientHandlerSocket clientSocket, AbstractWorld worldChosen, Server currentConnectedServer
this.clientSocket clientSocket;
this.world worldChosen;
this.connectedServer currentConnectedServer;
Handles the communication with the client.
@Override
public void run
try
Initialization
this.dis new DataInputStreamclientSocketgetInputStream;
this.dos new DataOutputStreamclientSocketgetOutputStream;
this.clientIdentifier getClientIdentifierclientSocket;
this.commandLine new ScannerSystemin;
Inform client of successful connection
Map data new HashMap;
Map state new HashMap;
luanch validation
String potentialRobotName;
maximumShieldStrength ;
maximumShots ;
while true
String launchCommand getCommand;
Map request;
try
request gson.fromJsonlaunchCommand new TypeTokengetType;
catch Exception e
data.clear;
data.putmessage "Could not parse arguments";
state.clear;
sendMessageServerProtocolbuildResponseERROR data;
continue;
Parsing command
if requestgetrobot null request.getcommand null request.getarguments null
data.clear;
data.putmessage "Could not parse arguments";
state.clear;
sendMessageServerProtocolbuildResponseERROR data;
continue;
potentialRobotName String request.getrobot;
String requestedCommand String request.getcommand;
ArrayList arguments ArrayList request.getarguments;
String make;
try
make String arguments.get;
catch Exception e
data.clear;
data.putmessage "Could not parse arguments";
state.clear;
sendMessageServerProtocolbuildResponseERROR data;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
