Beispiel #1
0
        //Response Parser. Basically all just print the received data
        //on screen except for open file, open dir
        public static void Parse(Response response)
        {
            string msg = null;

            if (!response.IsSuccess)
            {
                Console.ForegroundColor = ConsoleColor.Yellow;
                msg = $"Request {response.Request.Id} failed: {response.Message}";
            }
            else if (response.Command == Command.updateConfig)
            {
                var ips = response.Bytes.Deserialize <List <string> >();
                ConfigurationHelper.Update(State.ConfigFilePath, ips);
            }
            else if (response.Request.Command == Command.openFile)
            {
                Directory.CreateDirectory(Path.Combine(State.LocalRootDirectory,
                                                       PathHelpers.GetDirFromPath((string)response.Request.Parameters[0])));
                string path = Path.Combine(State.LocalRootDirectory, ((string)response.Request.Parameters[0]));
                File.WriteAllText(path, response.Data);

                ProcessManager.StartProcess(path);

                string data    = File.ReadAllText(path);
                var    request = new Request
                {
                    Id         = RequestHelper.GetRequestId(),
                    Command    = Command.updateFile,
                    Method     = "UpdateFile",
                    Type       = "FileService",
                    Parameters = new object[] { (string)response.Request.Parameters[0], data }
                };

                byte[] buffer = request.SerializeToByteArray();
                int    bytesSent = 0, totalBytesSent = 0;
                do
                {
                    bytesSent       = State.Server.Send(buffer);
                    totalBytesSent += bytesSent;
                }while (totalBytesSent < buffer.Length);
            }
            else if (response.Request.Command == Command.cd)
            {
                State.CurrentDirectory = response.Data;
                Console.WriteLine();
                Console.Write(State.CurrentDirectory + ">");
            }

            else
            {
                Console.ForegroundColor = ConsoleColor.Green;
                msg = $"Request {response.Request.Id} succeeded: \n{response.Message}";
            }

            if (State.WaitingForInput && msg != null)
            {
                Console.WriteLine();
                Console.WriteLine(msg);
                Console.ResetColor();
                Console.Write(State.CurrentDirectory + ">");
            }
        }
Beispiel #2
0
        public static Request GetRequestFromInput(string input)
        {
            Request request = new Request()
            {
                Id = RequestHelper.GetRequestId()
            };

            var tokens = input.Split(" ");
            //string[] tokens = new string[inputArr.Length - 1];
            //Array.Copy(inputArr, 1, tokens, 0, inputArr.Length - 1);

            Command cmd;

            try
            {
                if (Enum.TryParse(tokens[0], out cmd))
                {
                    request.Command = cmd;
                    switch (cmd)
                    {
                    case Command.ls:
                        ListDirectory(ref request);
                        break;

                    case Command.cd:
                        OpenDirectory(ref request, tokens);
                        break;

                    case Command.mkdir:
                        CreateDirectory(ref request, tokens);
                        break;

                    case Command.rmdir:
                        RemoveDirectory(ref request, tokens);
                        break;

                    case Command.rm:
                        RemoveFile(ref request, tokens);
                        break;

                    case Command.mvdir:
                        MoveDirectory(ref request, tokens);
                        break;

                    case Command.mv:
                        MoveFile(ref request, tokens);
                        break;

                    case Command.cpdir:
                        CopyDirectory(ref request, tokens);
                        break;

                    case Command.cp:
                        CopyFile(ref request, tokens);
                        break;
                    }
                    return(request);
                }
                else if (!input.IsDirectory())
                {
                    request.Command = Command.openFile;
                    OpenFile(ref request, input);
                    return(request);
                }
            }
            catch (Exception e)
            {
                //Console.WriteLine("Invalid input");
            }

            return(null);
        }