Example #1
0
        private static byte[] ProcessTask(byte[] bytes)
        {
            var packet = Serializer.DeserializeFromBytes<ExcutePacket>(bytes);
            Logger.Info("Get process task.");
            //如果是执行代码
            if (packet.ExcuteType == ExcutePacket.Type.Code)
            {
                string appName = packet.AppName;
                if (_statusCenter.ContainsKey(appName))
                {
                    _statusCenter.Remove(appName);
                    _runner.Remove(appName);
                }
                var status = new RunningStatus(packet, "Running");
                var runner = new Runner(ref status);
                runner.StartRun(_machineName, _servicePort);
                _statusCenter.Add(appName, status);
                _runner.Add(appName, runner);
                Backup();
                return Serializer.SerializeToBytes("Submit Success.");
            }

            if (packet.ExcuteType == ExcutePacket.Type.Kill)
            {
                string appName = packet.AppName;
                string flag = "[" + _machineName + "," + _servicePort + "] :";
                if (_statusCenter.ContainsKey(appName))
                {
                    if (_runner.ContainsKey(appName))
                    {
                        var runner = (Runner)_runner[appName];
                        _runner.Remove(appName);
                        runner.Kill();
                    }
                    _statusCenter.Remove(appName);
                    Backup();
                    return Serializer.SerializeToBytes(flag + "Kill Success.");
                }
                return Serializer.SerializeToBytes(flag + "Not found your application");
            }

            if (packet.ExcuteType == ExcutePacket.Type.Task)
            {
                var runningApps = new List<string>();
                if (_statusCenter.Count > 0)
                {
                    runningApps.AddRange(from DictionaryEntry dictionaryEntry in _statusCenter
                                         let key = dictionaryEntry.Key.ToString()
                                         let status = ((RunningStatus)dictionaryEntry.Value).Description
                                         select "[" + _machineName + "," + _servicePort + "] :" + key + " -> " + status);
                }
                return Serializer.SerializeToBytes(runningApps);
            }

            //如果是文件片
            if (packet.ExcuteType == ExcutePacket.Type.FileFragment)
            {
                byte[] fileNameBytes = packet.Data;
                string fileName = Encoding.UTF8.GetString(fileNameBytes);
                if (fileName.Contains("/"))
                {
                    string[] folder = fileName.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
                    string tempPath = _servicePort + "/";
                    for (int i = 0; i < folder.Length - 1; i++)
                    {
                        tempPath += folder[i] + "/";
                        if (!Directory.Exists(tempPath))
                        {
                            Directory.CreateDirectory(tempPath);
                        }
                    }
                }
                Logger.Info("Get command to save file fragment by name " + fileName);
                var fileTransfer = new FileTransfer();
                fileTransfer.Receive(7001, _servicePort + "/" + fileName);
                return fileNameBytes;
            }

            //如果是下载文件
            if (packet.ExcuteType == ExcutePacket.Type.Download)
            {
                Logger.Info("Get command download.");
                byte[] fileNameBytes = packet.Data;
                string fileName = Encoding.UTF8.GetString(fileNameBytes);

                Logger.Info("Start send file:" + fileName);
                var fileTransfer = new FileTransfer();
                fileTransfer.Send(_servicePort + "/" + fileName, packet.ReturnIp, 7002);
                Logger.Info("Send finished.");

                return fileNameBytes;
            }

            //如果是删除文件
            if (packet.ExcuteType == ExcutePacket.Type.Delete)
            {
                Logger.Info("Get command delete.");
                byte[] fileNameBytes = packet.Data;
                string fileName = Encoding.UTF8.GetString(fileNameBytes);
                if (File.Exists(_servicePort + "/" + fileName))
                {
                    Logger.Info("Start delete file:" + fileName);
                    File.Delete(_servicePort + "/" + fileName);
                    Logger.Info("Deleted.");
                }
                else
                {
                    Logger.Warn("Delete file:" + fileName + " not found.");
                }
                return fileNameBytes;
            }

            //如果是重命名文件
            if (packet.ExcuteType == ExcutePacket.Type.Rename)
            {
                Logger.Info("Get command rename.");
                var fileTuple = Serializer.DeserializeFromBytes<Tuple<string, string
                    >>(packet.Data);
                string fileName = _servicePort + "/" + fileTuple.Item1;
                string fileNewName = _servicePort + "/" + fileTuple.Item2;
                if (File.Exists(fileName) && !File.Exists(fileNewName) && fileName != fileNewName)
                {
                    File.Move(fileName, fileNewName);
                }
            }

            //如果是查看硬盘空间
            if (packet.ExcuteType == ExcutePacket.Type.Disk)
            {
                Logger.Info("Get command Disk Storage Information");
                DriveInfo[] dis = DriveInfo.GetDrives();

                long DiskStorage = 0;
                foreach (DriveInfo di in dis)
                {
                        DiskStorage += di.TotalFreeSpace / (1024 * 1024);
                }

                int WorkerCount = Serializer.DeserializeFromBytes<int>(packet.Data);

                return Serializer.SerializeToBytes(DiskStorage / WorkerCount);
            }

            //如果是查看内存
            if (packet.ExcuteType == ExcutePacket.Type.Memory)
            {
                Logger.Info("Get command Memory Information");

                MEMINFO MemInfo = new MEMINFO();//实例化结构
                GlobalMemoryStatus(ref MemInfo);//给此结构赋值
                string availinfo = Convert.ToString(MemInfo.dwAvailPhys / 1024 / 1024) + "MB";//获得已用内存量
                string allinfo = Convert.ToString(MemInfo.dwTotalPhys / 1024 / 1024) + "MB";//获得内存总量
                string result = Dns.GetHostName() + " Total: " + allinfo + " || Available: " + availinfo;
                Console.WriteLine(result);

                return Serializer.SerializeToBytes(result);
            }
            return null;
        }
Example #2
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);
        }
Example #3
0
        private static byte[] ProcessTask(byte[] bytes)
        {
            var packet = Serializer.DeserializeFromBytes<ExcutePacket>(bytes);
            Logger.Info("Get process task.");
            //如果是执行代码
            if (packet.ExcuteType == ExcutePacket.Type.Code)
            {
                string appName = packet.AppName;
                if (_statusCenter.ContainsKey(appName))
                {
                    _statusCenter.Remove(appName);
                    _runner.Remove(appName);
                }
                var status = new RunningStatus(packet, "Running");
                var runner = new Runner(ref status);
                runner.StartRun(_machineName, _servicePort);
                _statusCenter.Add(appName, status);
                _runner.Add(appName, runner);
                Backup();
                return Serializer.SerializeToBytes("Submit Success.");
            }

            if (packet.ExcuteType == ExcutePacket.Type.Kill)
            {
                string appName = packet.AppName;
                string flag = "[" + _machineName + "," + _servicePort + "] :";
                if (_statusCenter.ContainsKey(appName))
                {
                    if (_runner.ContainsKey(appName))
                    {
                        var runner = (Runner)_runner[appName];
                        _runner.Remove(appName);
                        runner.Kill();
                    }
                    _statusCenter.Remove(appName);
                    Backup();
                    return Serializer.SerializeToBytes(flag + "Kill Success.");
                }
                return Serializer.SerializeToBytes(flag + "Not found your application");
            }

            if (packet.ExcuteType == ExcutePacket.Type.Task)
            {
                var runningApps = new List<string>();
                if (_statusCenter.Count > 0)
                {
                    runningApps.AddRange(from DictionaryEntry dictionaryEntry in _statusCenter
                                         let key = dictionaryEntry.Key.ToString()
                                         let status = ((RunningStatus)dictionaryEntry.Value).Description
                                         select "[" + _machineName + "," + _servicePort + "] :" + key + " -> " + status);
                }
                return Serializer.SerializeToBytes(runningApps);
            }

            //如果是文件片
            if (packet.ExcuteType == ExcutePacket.Type.FileFragment)
            {
                byte[] fileNameBytes = packet.Data;
                string fileName = Encoding.UTF8.GetString(fileNameBytes);
                if (fileName.Contains("/"))
                {
                    string[] folder = fileName.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
                    string tempPath = _servicePort + "/";
                    for (int i = 0; i < folder.Length - 1; i++)
                    {
                        tempPath += folder[i] + "/";
                        if (!Directory.Exists(tempPath))
                        {
                            Directory.CreateDirectory(tempPath);
                        }
                    }
                }
                Logger.Info("Get command to save file fragment by name " + fileName);
                var fileTransfer = new FileTransfer();
                fileTransfer.Receive(7001, _servicePort + "/" + fileName);
                return fileNameBytes;
            }

            //如果是下载文件
            if (packet.ExcuteType == ExcutePacket.Type.Download)
            {
                Logger.Info("Get command download.");
                byte[] fileNameBytes = packet.Data;
                string fileName = Encoding.UTF8.GetString(fileNameBytes);

                Logger.Info("Start send file:" + fileName);
                var fileTransfer = new FileTransfer();
                fileTransfer.Send(_servicePort + "/" + fileName, packet.ReturnIp, 7002);
                Logger.Info("Send finished.");

                return fileNameBytes;
            }

            //如果是删除文件
            if (packet.ExcuteType == ExcutePacket.Type.Delete)
            {
                Logger.Info("Get command delete.");
                byte[] fileNameBytes = packet.Data;
                string fileName = Encoding.UTF8.GetString(fileNameBytes);
                if (File.Exists(_servicePort + "/" + fileName))
                {
                    Logger.Info("Start delete file:" + fileName);
                    File.Delete(_servicePort + "/" + fileName);
                    Logger.Info("Deleted.");
                }
                else
                {
                    Logger.Warn("Delete file:" + fileName + " not found.");
                }
                return fileNameBytes;
            }

            //如果是重命名文件
            if (packet.ExcuteType == ExcutePacket.Type.Rename)
            {
                Logger.Info("Get command rename.");
                var fileTuple = Serializer.DeserializeFromBytes<Tuple<string, string
                    >>(packet.Data);
                string fileName = _servicePort + "/" + fileTuple.Item1;
                string fileNewName = _servicePort + "/" + fileTuple.Item2;
                if (File.Exists(fileName) && !File.Exists(fileNewName) && fileName != fileNewName)
                {
                    File.Move(fileName, fileNewName);
                }
            }
            return null;
        }
Example #4
0
        public override void ProcessCmd(string[] args)
        {
            if (args.Length != 3)
            {
                UnknowCommand();
                return;
            }
            //3.1 创建子文件存放的文件夹
            string fileName = args[2];
            string remoteFilePath = args[1];
            if (remoteFilePath.EndsWith(".part"))
            {
                string partFileFolder = fileName + ".part";
                if (Directory.Exists(partFileFolder))
                {
                    Directory.Delete(partFileFolder, true);
                }
                Directory.CreateDirectory(partFileFolder);

                List<string> workers = new List<string>(StateHelper.GetChildren("ISE://system/state/worker"));
                for (int i = 0; i < workers.Count; i++)
                {
                    //3.2 通知下载文件
                    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(remoteFilePath), string.Empty,
                        string.Empty, string.Empty,
                        ExcutePacket.Type.Download);
                    codePacket.SetReturnAddress(Dns.GetHostName(), 8800);
                    //1207
                    codePacket.WaiteCallBack = false;
                    transfer.Send<bool>(codePacket);
                    Logger.Info(ip[0] + "," + ip[1] + " has been noticed to send file " + fileName);

                    //3.3 准备接收文件块
                    FileTransfer fileTransfer = new FileTransfer();
                    fileTransfer.Receive(7002, partFileFolder + "/" + i);
                }

                //3.4 合并文件
                FileBlock.Merge(partFileFolder, fileName);
                Directory.Delete(partFileFolder, true);
            }
            else
            {
                List<string> workers = new List<string>(StateHelper.GetChildren("ISE://system/state/worker"));
                string[] ip =
                    workers[0].Substring(workers[0].LastIndexOf('/') + 1,
                        workers[0].Length - workers[0].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(remoteFilePath), string.Empty,
                    string.Empty, string.Empty,
                    ExcutePacket.Type.Download);
                codePacket.SetReturnAddress(Dns.GetHostName(), 8800);
                //1207
                codePacket.WaiteCallBack = false;
                transfer.Send<bool>(codePacket);
                Logger.Info(ip[0] + "," + ip[1] + " has been noticed to send file " + fileName);
                FileTransfer fileTransfer = new FileTransfer();
                fileTransfer.Receive(7002, fileName);
            }
        }