public IPEndPoint InitAndGetLocalEndPoint()
        {
            _listener = new TcpListener(new IPEndPoint(IPAddress.Any, 0));
            _listener.Start();
            Console.WriteLine("File receive listening:");

            int local_port = ((IPEndPoint)_listener.LocalEndpoint).Port;

            return(new IPEndPoint(FileReceive.LocalIPAddress(), local_port));
        }
 public static async Task<string> Resolve(string input_line)
 {
     string[] input_array = input_line.Split();
     string command = input_array[0];
     if (command == "retrieve")
     {
         if (input_array.Length == 3)
             return "store " + input_array[1] + " " + input_array[2];
         else
             return "error number of parameters in retrieve command.";
     }
     else if (command == "store")
     {
         if (input_array.Length == 3 && File.Exists(input_array[1]))
         {
             return "upload " + input_array[1] + " " + input_array[2]
                 + " " + FileCommon.GetSize(input_array[1]) + " " + FileCommon.ComputeHash(input_array[1]);
         }
         else
             return "error number of parameters in store command.";
     }
     else if (command == "upload")
     {
         if (input_array.Length == 5 && Convert.ToInt64(input_array[3])>0)
         {
             FileReceive fileReceive = new FileReceive(input_array[2]);
             long seek_position = fileReceive.PrepareToWrite(Convert.ToInt64(input_array[3]), input_array[4]);
             if (seek_position >= 0)
             {
                 IPEndPoint localPoint = fileReceive.InitAndGetLocalEndPoint();
                 fileReceive.Run();
                 return "move " + input_array[1] + " " + input_array[2] + " "
                     + seek_position + " " + localPoint.Address + " " + localPoint.Port;
             }
             else
             {
                 return "error file already exists";
             }
         }
         else
             return "error file size";
     }
     else if (command == "move")
     {
         FileSend.SendFile(input_array[1], Convert.ToInt64(input_array[3]), 
             input_array[4], Convert.ToInt32(input_array[5]));
         return null;
     }
     else if (command == "error")
     {
         return null;
     }
     else
         return "error invalid commandline";
 }
Exemple #3
0
        public static async Task <string> Resolve(string input_line)
        {
            string[] input_array = input_line.Split();
            string   command     = input_array[0];

            if (command == "retrieve")
            {
                if (input_array.Length == 3)
                {
                    return("store " + input_array[1] + " " + input_array[2]);
                }
                else
                {
                    return("error number of parameters in retrieve command.");
                }
            }
            else if (command == "store")
            {
                if (input_array.Length == 3 && File.Exists(input_array[1]))
                {
                    return("upload " + input_array[1] + " " + input_array[2]
                           + " " + FileCommon.GetSize(input_array[1]) + " " + FileCommon.ComputeHash(input_array[1]));
                }
                else
                {
                    return("error number of parameters in store command.");
                }
            }
            else if (command == "upload")
            {
                if (input_array.Length == 5 && Convert.ToInt64(input_array[3]) > 0)
                {
                    FileReceive fileReceive   = new FileReceive(input_array[2]);
                    long        seek_position = fileReceive.PrepareToWrite(Convert.ToInt64(input_array[3]), input_array[4]);
                    if (seek_position >= 0)
                    {
                        IPEndPoint localPoint = fileReceive.InitAndGetLocalEndPoint();
                        fileReceive.Run();
                        return("move " + input_array[1] + " " + input_array[2] + " "
                               + seek_position + " " + localPoint.Address + " " + localPoint.Port);
                    }
                    else
                    {
                        return("error file already exists");
                    }
                }
                else
                {
                    return("error file size");
                }
            }
            else if (command == "move")
            {
                FileSend.SendFile(input_array[1], Convert.ToInt64(input_array[3]),
                                  input_array[4], Convert.ToInt32(input_array[5]));
                return(null);
            }
            else if (command == "error")
            {
                return(null);
            }
            else
            {
                return("error invalid commandline");
            }
        }