Example #1
0
        public override void ProcessCmd(string[] args)
        {
            if (args.Length < 3 || args.Length == 4)
            {
                UnknowCommand();
                return;
            }
            //2.1 将文件切分成块
            List <string> workers  = new List <string>(StateHelper.GetChildren("ISE://system/state/worker"));
            string        filePath = args[1];

            if (File.Exists(filePath))
            {
                //普通切分模式
                if (args.Length == 3)
                {
                    FileBlock.Split(filePath, workers.Count);
                }
                //用户自定义切分
                else
                {
                    string     splitStr = args[3];
                    List <int> keys     = new List <int>();
                    for (int i = 4; i < args.Length; i++)
                    {
                        int index;
                        if (!int.TryParse(args[i], out index))
                        {
                            Logger.Error("index should be an int.");
                            break;
                        }
                        keys.Add(index);
                    }
                    FileBlock.Split(filePath, workers.Count, splitStr, keys.ToArray());
                }
            }
            else
            {
                Logger.Error(filePath + " is not found.");
                return;
            }

            //2.2 告诉worker即将发送文件块
            string remotePath = args[2];

            string[] childFiles = Directory.GetFiles(filePath + ".part");
            for (int i = 0; i < workers.Count; i++)
            {
                string[] ip =
                    workers[i].Substring(workers[i].LastIndexOf('/') + 1, workers[i].Length - workers[i].LastIndexOf('/') - 1)
                    .Split(',');
                Framework.Network.Synchronous.Client transfer = new Framework.Network.Synchronous.Client(ip[0],
                                                                                                         int.Parse(ip[1]));
                ExcutePacket codePacket = new ExcutePacket(Encoding.UTF8.GetBytes(remotePath), string.Empty, string.Empty, string.Empty,
                                                           ExcutePacket.Type.FileFragment);
                codePacket.SetReturnAddress(Dns.GetHostName(), 8800);
                //1207
                codePacket.WaiteCallBack = false;
                transfer.Send <bool>(codePacket);
                Logger.Info(ip[0] + "," + ip[1] + " has been noticed to receive the file.");

                int maxRetryCount = 5;
                while (maxRetryCount > 0)
                {
                    try
                    {
                        FileTransfer fileTransfer = new FileTransfer();
                        fileTransfer.Send(childFiles[i], ip[0], 7001);
                        maxRetryCount = -1;
                    }
                    catch (Exception exception)
                    {
                        Logger.Error(exception.Message);
                        maxRetryCount--;
                        if (maxRetryCount > 0)
                        {
                            Console.WriteLine("Now retry...");
                            Thread.Sleep(1000);
                        }
                    }
                }
                if (maxRetryCount == 0)
                {
                    Logger.Info(ip[0] + "," + ip[1] + " do not get the file.");
                }
            }
            //2.4 删掉切分的文件
            Directory.Delete(filePath + ".part", true);
            StateHelper.Put("ISE://File/" + remotePath, new FileInfo(filePath).Length / 1024);
        }