コード例 #1
0
ファイル: PolicyController.cs プロジェクト: theopenem/Toems
        public DtoDownloadConnectionResult CreateDownloadConnection(DtoDownloadConRequest conRequest)
        {
            var result = new DtoDownloadConnectionResult();

            var guid          = ConfigurationManager.AppSettings["ComServerUniqueId"];
            var thisComServer = new ServiceClientComServer().GetServerByGuid(guid);

            if (thisComServer == null)
            {
                Logger.Error($"Com Server With Guid {guid} Not Found");
                result.ErrorMessage = $"Com Server With Guid {guid} Not Found";
                return(result);
            }


            if (!int.TryParse(thisComServer.EmMaxClients.ToString(), out var maxClientConnections))
            {
                result.ErrorMessage = "Could Not Determine The MaxClientConnections For The Com Server: " +
                                      conRequest.ComServer;
                return(result);
            }

            if (maxClientConnections == 0) // zero is unlimited
            {
                result.Success     = true;
                result.QueueIsFull = false;
                return(result);
            }

            var client = new ServiceComputer().GetByGuid(conRequest.ComputerGuid);

            if (client == null)
            {
                result.ErrorMessage = "Could Not Find Computer With Guid: " + RequestContext.Principal.Identity.Name;
                return(result);
            }



            var serviceCurrentDownloads = new ServiceCurrentDownload();
            var currentConnections      = serviceCurrentDownloads.TotalCount(conRequest.ComServer);
            var activeClient            = serviceCurrentDownloads.GetByClientId(client.Id, conRequest.ComServer);

            if (activeClient == null && (currentConnections >= maxClientConnections))
            {
                var activeCountAfterExpire = serviceCurrentDownloads.ExpireOldConnections();
                if (activeCountAfterExpire >= maxClientConnections)
                {
                    result.Success     = true;
                    result.QueueIsFull = true;
                    return(result);
                }
            }

            //add new download connection for this client
            if (activeClient == null)
            {
                var currentDownload = new EntityCurrentDownload();
                currentDownload.ComputerId = client.Id;
                currentDownload.ComServer  = conRequest.ComServer;
                serviceCurrentDownloads.Add(currentDownload);
                result.Success     = true;
                result.QueueIsFull = false;
            }
            //update time for existing download connection
            else
            {
                activeClient.LastRequestTimeLocal = DateTime.Now;
                serviceCurrentDownloads.Update(activeClient);
                result.Success     = true;
                result.QueueIsFull = false;
            }

            return(result);
        }