private void bwDataRequest_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            // Find out the Index of the bWorker that called this DoWork (could be cleaner, I know)
            int Y;
            int Index  = default;
            var data   = new _queue_data_struct();
            var loopTo = Information.UBound(bwDataRequest);

            for (Y = 0; Y <= loopTo; Y++)
            {
                if (sender.Equals(bwDataRequest[Y]))
                {
                    Index = Y;
                    break;
                }
            }

            if (ManagementNetwork.IsResponseOk(Conversions.ToString(e.Result), "statuscode"))
            {
                data        = new _queue_data_struct();
                data        = queue[queueBWorker[Index]];
                data.status = 0; // re queue the file
                lock (queueLock)
                    queue[queueBWorker[Index]] = data;
                string          errorMsg = ManagementNetwork.GetMessage(Conversions.ToString(e.Result));
                _retry_attempts retry;
                retry.counter         = retryAttempts.counter;
                retry.previousPattern = retryAttempts.previousPattern;
                retry.pattern         = retryAttempts.pattern;
                retry.errorMessage    = retryAttempts.errorMessage;
                retry.errorMessage    = retryAttempts.errorMessage.IndexOf(errorMsg) > -1 ? retryAttempts.errorMessage : retryAttempts.errorMessage + System.Environment.NewLine + errorMsg;
                retry.pattern         = QueuesMultiHash(queue);
                if (retry.previousPattern.Equals(retry.pattern))
                {
                    retry.counter += 1;
                }
                else
                {
                    retry.counter         = 1;
                    retry.previousPattern = retryAttempts.pattern;
                }

                retryAttempts = retry;
                return;
            }

            data        = new _queue_data_struct();
            data        = queue[queueBWorker[Index]];
            data.status = -1; // completed sucessfully status
            lock (queueLock)
                queue[queueBWorker[Index]] = data;
            loadingCounter      += 1;
            CompletionPercentage = (int)(loadingCounter / (double)queue.Count * 100);
            statusMessage        = "Loading data from the cloud ...";
            updateProgress?.Invoke(this, queue[queueBWorker[Index]].misc);
            dataArrived?.Invoke(this, Conversions.ToString(e.Result), queue[queueBWorker[Index]].misc);
        }
Exemplo n.º 2
0
        private void bwDataRequest_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            // Find out the Index of the bWorker that called this DoWork (could be cleaner, I know)
            int Y;
            int Index  = default;
            var loopTo = Information.UBound(bwDataRequest);

            for (Y = 0; Y <= loopTo; Y++)
            {
                if (sender.Equals(bwDataRequest[Y]))
                {
                    Index = Y;
                    break;
                }
            }

            string             responseFromServer = Encoding.UTF8.GetString((byte[])e.Result);
            string             decrypted          = "";
            var                encryption         = new AesCipher(state);
            _queue_data_struct data;

            try
            {
                if (IsBase64String(responseFromServer) & !responseFromServer.Equals(""))
                {
                    decrypted = encryption.decrypt(responseFromServer);
                }
                else
                {
                    System.Threading.Thread.CurrentThread.CurrentUICulture = System.Globalization.CultureInfo.GetCultureInfo(state.currentLang);
                    decrypted = "{'error':true,'message':'" + resources.GetString("contactingCommServer", CultureInfo.CurrentCulture) + " |" + responseFromServer + "|'}";
                }
            }
            catch (Exception ex)
            {
                decrypted = "{'error':true,'message':'" + ex.Message.ToString().Replace("'", @"\'") + "'}";
            }

            if (!ManagementNetwork.IsResponseOk(decrypted))
            {
                data        = new _queue_data_struct();
                data        = queue[queueBWorker[Index]];
                data.status = 0; // re queue the file
                lock (queueLock)
                    queue[queueBWorker[Index]] = data;
                string          errorMsg = ManagementNetwork.GetMessage(decrypted);
                _retry_attempts retry;
                retry.counter         = retryAttempts.counter;
                retry.previousPattern = retryAttempts.previousPattern;
                retry.pattern         = retryAttempts.pattern;
                retry.errorMessage    = retryAttempts.errorMessage;
                retry.errorMessage    = retryAttempts.errorMessage.IndexOf(errorMsg) > -1 ? retryAttempts.errorMessage : retryAttempts.errorMessage + System.Environment.NewLine + errorMsg;
                retry.pattern         = QueuesMultiHash(queue);
                if (retry.previousPattern.Equals(retry.pattern))
                {
                    retry.counter += 1;
                }
                else
                {
                    retry.counter         = 1;
                    retry.previousPattern = retryAttempts.pattern;
                }

                retryAttempts = retry;
                return;
            }

            data        = new _queue_data_struct();
            data        = queue[queueBWorker[Index]];
            data.status = -1; // completed sucessfully status
            lock (queueLock)
                queue[queueBWorker[Index]] = data;
            loadingCounter      += 1;
            CompletionPercentage = (int)(loadingCounter / (double)queue.Count * 100);
            statusMessage        = "Uploading data to the cloud ...";
            dataArrived?.Invoke(this, decrypted, queue[queueBWorker[Index]].misc);
        }
        private void bwDataRequest_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            // Find out the Index of the bWorker that called this DoWork (could be cleaner, I know)
            int Y;
            int Index  = default;
            var loopTo = Information.UBound(bwDataRequest);

            for (Y = 0; Y <= loopTo; Y++)
            {
                if (sender.Equals(bwDataRequest[Y]))
                {
                    Index = Y;
                    break;
                }
            }

            var data = new _queue_data_struct();

            if (ManagementNetwork.IsResponseOk(fileExtension[queueBWorker[Index]], "statuscode"))
            {
                data        = queue[queueBWorker[Index]];
                data.status = 0; // re queue the file
                lock (queueLock)
                    queue[queueBWorker[Index]] = data;
                string          errorMsg = "";
                _retry_attempts retry;
                retry.counter         = retryAttempts.counter;
                retry.previousPattern = retryAttempts.previousPattern;
                retry.pattern         = retryAttempts.pattern;
                retry.errorMessage    = retryAttempts.errorMessage;
                errorMsg           = ManagementNetwork.GetMessage(fileExtension[Index]);
                retry.errorMessage = retryAttempts.errorMessage.IndexOf(errorMsg) > -1 ? retryAttempts.errorMessage : retryAttempts.errorMessage + System.Environment.NewLine + errorMsg;
                retry.pattern      = QueuesMultiHash(queue);
                if (retry.previousPattern.Equals(retry.pattern))
                {
                    retry.counter += 1;
                }
                else
                {
                    retry.counter         = 1;
                    retry.previousPattern = retryAttempts.pattern;
                }

                retryAttempts = retry;
                return;
            }
            else if (ManagementNetwork.IsResponseOk(fileExtension[Index]))
            {
                errorMessage = ManagementNetwork.GetMessage(fileExtension[Index]);
            }

            if (!e.Result.Equals(false))
            {
                try
                {
                    fileExtension[queueBWorker[Index]] = queue[queueBWorker[Index]].filenameOrSavePath + fileExtension[queueBWorker[Index]];
                    File.WriteAllBytes(fileExtension[queueBWorker[Index]], (byte[])e.Result);
                }
                catch (Exception ex)
                {
                    _retry_attempts retry;
                    retry.counter         = retryAttempts.counter;
                    retry.previousPattern = retryAttempts.previousPattern;
                    retry.pattern         = retryAttempts.pattern;
                    retry.errorMessage    = retryAttempts.errorMessage;
                    retry.counter         = 100;
                    retry.previousPattern = retry.pattern;
                    retry.errorMessage    = "error saving file";
                    retryAttempts         = retry;
                }
                finally
                {
                }
            }

            data        = new _queue_data_struct();
            data        = queue[queueBWorker[Index]];
            data.status = -1; // completed sucessfully status
            lock (queueLock)
                queue[queueBWorker[Index]] = data;
            loadingCounter      += 1;
            CompletionPercentage = (int)(loadingCounter / (double)queue.Count * 100);
            statusMessage        = "Loading cloud files...";
            dataArrived?.Invoke(this, fileExtension[queueBWorker[Index]], queue[queueBWorker[Index]].misc);
        }