예제 #1
0
        public async Task <StreamUploadResponse> UpdateWorkers(StreamUploadRequest request)
        {
            var memoryStream = new MemoryStream();

            try
            {
                await request.Stream.CopyToAsync(memoryStream);

                var workers = NetHelper.DeserializeFromStream <Workers>(memoryStream);
                ViewModel.Instance.WorkersPropertyEventsRemove();
                ViewModel.Instance.Workers = workers.Clone();
                ViewModel.Instance.WorkersPropertyEventsAdd();
                Workers.SaveWorkers(workers);
                var date = Workers.GetWorkersLastUpdateTime();
                return(new StreamUploadResponse {
                    ResponseFlag = true, Date = date
                });
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }
            finally
            {
                memoryStream.Dispose();
            }
        }
예제 #2
0
        public async Task <StreamUploadResponse> UpdateWtmSettings(StreamUploadRequest request)
        {
            var memoryStream = new MemoryStream();

            try
            {
                await request.Stream.CopyToAsync(memoryStream);

                var wtmSettings = NetHelper.DeserializeFromStream <WtmSettingsObject>(memoryStream);
                if (request.ProxyPassword.Length != 0)
                {
                    wtmSettings.ProxyPassword          = request.ProxyPassword.ToSecureString();
                    wtmSettings.ProxyPasswordEncrypted = request.ProxyPassword.Encrypt();
                    Array.Clear(request.ProxyPassword, 0, request.ProxyPassword.Length);
                }
                ViewModel.Instance.WtmSettings = wtmSettings.Clone();
                ViewModel.Instance.SaveWtmSettings();
                var date = WtmSettingsObject.GetWtmSettingsLastUpdateTime();
                return(new StreamUploadResponse {
                    ResponseFlag = true, Date = date
                });
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }
            finally
            {
                memoryStream.Dispose();
            }
        }
예제 #3
0
        private async Task <int> UpdateWtmSettings(Computer pc, CancellationToken token, int errorCount, int iteration, int jobCount, DateTime wtmSettingsDate)
        {
            pc.SwitchStatus = Computer.OperationStatus.OperationInProgress;

            var address = ViewModel.BuildServerAddress(pc.Name, Constants.StreamServer);
            var channel = Service.NewStreamChannel(address, TimeSpan.FromSeconds(60));

            MemoryStream stream        = NetHelper.SerializeToStream <WtmSettingsObject>(ViewModel.Instance.WtmSettings);
            var          uploadRequest = new StreamUploadRequest {
                Stream = stream
            };

            try
            {
                if (ViewModel.Instance.WtmSettings.ProxyPassword != null && ViewModel.Instance.WtmSettings.ProxyPassword.Length != 0)
                {
                    uploadRequest.ProxyPassword = ViewModel.Instance.WtmSettings.ProxyPassword.ToCharArray();
                }
                var response = await channel.UpdateWtmSettings(uploadRequest).WithCancellation(token).ConfigureAwait(false);

                if (response.ResponseFlag)
                {
                    pc.SwitchStatus    = Computer.OperationStatus.Success;
                    pc.WtmSettingsDate = response.Date;
                    if (pc.WtmSettingsDate >= wtmSettingsDate)
                    {
                        pc.UpdateStatus = Computer.OperationStatus.NotNecessary;
                    }
                }
            }
            catch (Exception ex)
            {
                errorCount++;
                pc.SwitchStatus = Computer.OperationStatus.Failure;
                Application.Current.Dispatcher.Invoke(() =>
                {
                    var p = new Paragraph();
                    p.Inlines.Add(new Run($"{pc.Name}: ").FontWeight(FontWeights.Bold).Color(Colors.Salmon));
                    p.Inlines.Add(new Run($"Failed to update.\r\n"));
                    p.Inlines.Add(new Run(ex.Message + "\r\n"));
                    NewParagraph = p;
                });
            }
            finally
            {
                if (uploadRequest.ProxyPassword.Length != 0)
                {
                    Array.Clear(uploadRequest.ProxyPassword, 0, uploadRequest.ProxyPassword.Length);
                }
                NetHelper.CloseChannel(channel);
                stream.Dispose();
                ReportTitle = $"Progress: {iteration + 1} of {jobCount}.";
            }
            return(errorCount);
        }