/// <summary> /// Splits an apk into a list of fragments to be downloaded. /// </summary> /// <param name="package">Data of the APK</param> /// <param name="fragmentSize">Size of the fragments that the APK will be split into for downloads</param> /// <returns></returns> private List <byte[]> SplitPackageIntoFragments(Byte[] package, int fragmentSize) { try { List <byte[]> fragments = new List <byte[]>(); var index = 0; var fileSize = package.Length; while ((index + 1) * fragmentSize < fileSize) { var fragment = new byte[fragmentSize]; Array.Copy(package, index * fragmentSize, fragment, 0, fragmentSize); fragments.Add(fragment); index++; } if (index * fragmentSize < fileSize) { var lastFragmennt = new byte[(fileSize - (index * fragmentSize))]; Array.Copy(package, index * fragmentSize, lastFragmennt, 0, fileSize - (index * fragmentSize)); fragments.Add(lastFragmennt); } return(fragments); } catch (Exception e) { LoggingControl.Log("Error while recombining fragments into package upload for " + Key + ".", e); return(new List <byte[]>()); } }
/// <summary> /// Recreates the monitor from the latest settings. /// </summary> public void RestartMonitor() { try { Settings = SettingsControl.GetSettings(); if (MonitorControl != null) { if (MonitorControl.WebMonitor != null) { MonitorControl.WebMonitor.CancelAsync(); } if (MonitorControl.PackageMonitor != null) { MonitorControl.PackageMonitor.CancelAsync(); } } MonitorControl = new MonitorControl(Settings); statusIndicator.Value = 0; statusLabel.Text = "Mobu Web Service Status: Waiting..."; flowHostedPackages.Controls.Clear(); } catch (Exception e) { LoggingControl.Log("Error while refreshing/recreating manager.", e); } }
/// <summary> /// Recombines and returns the complete byte array of an apk after its upload has completed. /// </summary> public byte[] CombineFragmentsIntoPackage() { try { var package = new byte[PackageSize]; var index = 0; var fileSize = package.Length; foreach (var fragment in Fragments) { Array.Copy(fragment, 0, package, index * FragmentSize, fragment.Length); index++; } return(package); } catch (Exception e) { LoggingControl.Log("Error while recombining fragments into package upload for " + Key + ".", e); return(new byte[0]); } }
/// <summary> /// Cancels any active downloads to devices. /// The downloads will fail on the devices, giving the user the option to retry. /// </summary> private bool CancelActiveDownloads() { var activeDownloads = FrmMain.MonitorControl.WebService.CheckForActiveDownloads(PackageDetails); if (activeDownloads > 0) { var result = MessageBox.Show("Package " + PackageDetails.Name + " " + PackageDetails.Version + " is currently being downloaded by " + activeDownloads + "" + " devices. Do you want to abort these downloads? ", "Downloads in progress", MessageBoxButtons.YesNo); if (result == DialogResult.Yes) { return(FrmMain.MonitorControl.WebService.CancelActiveDownloads(PackageDetails)); } else { LoggingControl.Log("Error while cancelling active downloads for " + PackageDetails.Name + " " + PackageDetails.Version + "."); return(false); } } else { return(true); } }
/// <summary> /// Gets the latest package list from the monitor control (which is constantly updated in the background), and updates the package display based on that list. /// </summary> public void RefreshMonitor() { try { if (MonitorControl.WebServiceStatus) { statusIndicator.Value = 100; statusLabel.Text = "Mobu Web Service Status: Connected " + Settings.WebServiceUrl + "."; } else { statusIndicator.Value = 0; statusLabel.Text = "Mobu Web Service Status: Cannot connect to web service on " + Settings.WebServiceUrl + "."; } } catch (Exception e) { LoggingControl.Log("Error while refreshing component monitor.", e); } try { var webPackages = MonitorControl.CopyPackageList(); var currentPackages = new List <PackageDetails>(); foreach (var c in flowHostedPackages.Controls) { var control = (UcPackage)c; currentPackages.Add(control.PackageDetails); } var remove = new List <Control>(); foreach (var c in flowHostedPackages.Controls) { var control = (UcPackage)c; if (!webPackages.Any(x => x.Name == control.PackageDetails.Name && x.Version == control.PackageDetails.Version && x.Critical == control.PackageDetails.Critical)) { remove.Add((Control)c); } } foreach (var c in remove) { flowHostedPackages.Controls.Remove(c); } foreach (var i in webPackages) { if (!currentPackages.Any(x => x.Name == i.Name && x.Version == i.Version)) { flowHostedPackages.Controls.Add(new UcPackage(this, i)); } } } catch (Exception e) { LoggingControl.Log("Error while refreshing package monitor.", e); } }
/// <summary> /// Uploads the APK and its details as entered on the form to the web service. /// A progress bar is shown duting this progress. /// </summary> private void UploadPackage() { try { if (!webService.TestWebService()) { MessageBox.Show("Cannot reach web service at " + webService.Endpoint + "."); return; } fragmentSize = Convert.ToInt32(comboSize.SelectedItem); fragments = SplitPackageIntoFragments(package, fragmentSize); if (webService.CheckForActiveUploads(packageDetails)) { var dialogResult = MessageBox.Show("Package is already being uploaded. Do you want to cancel that upload and proceed with this one?", "Duplicate Upload", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (dialogResult == DialogResult.Yes) { var t = webService.CancelUpload(packageDetails); } else { return; } } var result = webService.StartUpload(packageDetails, package.Length, Convert.ToInt32(comboSize.SelectedItem)); Uploading = true; if (!result) { MessageBox.Show("Error while initiating upload. "); } else { MaximumSize = new System.Drawing.Size(Width, 270); MinimumSize = new System.Drawing.Size(Width, 270); Size = new System.Drawing.Size(Width, 270); barUploadProgress.Value = 0; barUploadProgress.Refresh(); lblUploadMsg.Refresh(); int retryMax = 3; int retrycounter = 0; int currentIndex = 0; while (currentIndex < fragments.Count) { barUploadProgress.Value = (int)(((currentIndex + 1) / (float)fragments.Count) * 100); barUploadProgress.Refresh(); lblUploadMsg.Refresh(); var packageFragment = new Fragment(); packageFragment.PackageDetails = packageDetails; packageFragment.Data = fragments[currentIndex]; var fragmentResult = webService.UploadNextFragment(packageFragment); while (!fragmentResult && retrycounter < retryMax) { retrycounter++; fragmentResult = webService.UploadNextFragment(packageFragment); } if (retrycounter == retryMax && !fragmentResult) { break; } else { currentIndex++; } } if (currentIndex == fragments.Count) { result = webService.FinishUpload(packageDetails); if (result) { Uploading = false; MessageBox.Show("Succesfully uploaded " + this.packageDetails.Name + " " + this.packageDetails.Version + " to server"); Close(); } } else { MessageBox.Show("Error occured while uploading fragment " + (currentIndex + 1) + " of " + fragments.Count + ". "); webService.CancelUpload(packageDetails); } } } catch (Exception e) { LoggingControl.Log("Error occured while uploading package.", e); MessageBox.Show("Error occured while uploading package."); webService.CancelUpload(packageDetails); Close(); } }