Exemple #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;
        }
Exemple #2
0
        //private string _status;

        public Runner(ref RunningStatus status)
        {
            Status = status;
        }
Exemple #3
0
        //private string _status;

        public Runner(ref RunningStatus status)
        {
            Status = status;
        }
Exemple #4
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;
        }
Exemple #5
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);
        }