/// <summary> /// defines the execution of the command. /// </summary> /// <param name="args"> arguments for the command</param> /// <param name="clientInfo"> info of client the sent the command</param> public override void Execute(string[] args, ClientConnectionInfo clientInfo) { string name; int rows, cols; try { name = args[0]; rows = int.Parse(args[1]); cols = int.Parse(args[2]); } catch // in case there is overflow in the index in the array { JObject errorMessage = new JObject(); errorMessage["Error"] = "missing arguments"; controller.View.SendReply(errorMessage.ToString(), clientInfo); return; } if (controller.SinglePlayerMazes.ContainsKey(name)) { controller.SinglePlayerMazes.Remove(name); if (controller.MazesSolutions.ContainsKey(name)) { controller.MazesSolutions.Remove(name); } } Maze maze = controller.Model.GenerateMaze(name, rows, cols); controller.SinglePlayerMazes.Add(name, maze); string reply = maze.ToJSON(); controller.View.SendReply(reply, clientInfo); clientInfo.ShouldKeepConnection = false; }
/// <summary> /// defines the execution of the command. /// </summary> /// <param name="args"> arguments for the command</param> /// <param name="clientInfo"> info of client the sent the command</param> public override void Execute(string[] args, ClientConnectionInfo clientInfo) { string name; int algorithm; try { name = args[0]; algorithm = int.Parse(args[1]); } catch { JObject errorMessage = new JObject(); errorMessage["Error"] = "missing arguments"; controller.View.SendReply(errorMessage.ToString(), clientInfo); return; } Solution <Position> solution = null; if (controller.MazesSolutions.ContainsKey(name)) { solution = controller.MazesSolutions[name]; } else { if (!controller.SinglePlayerMazes.ContainsKey(name)) { JObject errorMessage = new JObject(); errorMessage["Error"] = String.Format("maze with name {0} doesn't exist", name); controller.View.SendReply(errorMessage.ToString(), clientInfo); return; } Maze maze = controller.SinglePlayerMazes[name]; switch (algorithm) { case 0: solution = controller.Model.SolveMazeByBFS(maze); break; case 1: solution = controller.Model.SolveMazeByDFS(maze); break; } controller.MazesSolutions[name] = solution; } JObject entireSolution = new JObject(); entireSolution["Name"] = name; entireSolution["Solution"] = controller.Model.CreateDirectionStringOfSolution(solution); entireSolution["NodesEvaluated"] = solution.NumberOfEvaluatedNodes; controller.View.SendReply(entireSolution.ToString(), clientInfo); clientInfo.ShouldKeepConnection = false; }
public void SendReply(string reply, ClientConnectionInfo clientInfo) { new Task(() => { try { clientInfo.Writer.WriteLine(reply); } catch { } }).Start(); }
public void HandleCommand(string commandLine, ClientConnectionInfo clientInfo) { string[] arr = commandLine.Split(' '); string command = arr[0]; ICommand commandExecution; if (!commands.TryGetValue(command, out commandExecution)) { //to do } string[] args = arr.Skip(1).ToArray(); commandExecution.Execute(args, clientInfo); }
public void HandleClient(TcpClient client) { new Task(() => { try { ClientConnectionInfo connectionInfo = new ClientConnectionInfo(client); while (connectionInfo.ShouldKeepConnection) { string command = connectionInfo.Reader.ReadLine(); if (command != "" && command != null) { Controller.HandleCommand(command, connectionInfo); } } System.Threading.Thread.Sleep(1000); connectionInfo.CloseConnection(); } catch (Exception e) { Exception e1 = e.InnerException; } }).Start(); }