예제 #1
0
        /// <summary>
        /// Initiates an download of a specified APK.
        /// Inserts an entry into the upload dictionary where download fragments are kept in memory while they are being sent to devices.
        /// Returns a count of fragments to be expected for this APK when using this fragment size.
        /// </summary>
        /// <param name="packageDetails">Details of the package being downloaded.</param>
        /// <param name="fragmentSize">The size of the fragments being downloaded as requested by the device.</param>
        public static int StartOrIncrementDownload(PackageDetails packageDetails, int fragmentSize)
        {
            try
            {
                CleanUpDownloads();

                Download packageDownload = null;

                var downloadKey = GetDownloadKey(packageDetails, fragmentSize);
                var packageKey  = packageDetails.GetPackageKey();
                var package     = new byte[0];

                if (FileControl.GetPackageList().Any(x => x.GetPackageKey().Equals(packageKey, StringComparison.OrdinalIgnoreCase)))
                {
                    var path = FileControl.GetPackagePath(packageKey);

                    if (File.Exists(path))
                    {
                        package = FileControl.GetPackage(path);

                        if (activeDownloads.TryGetValue(downloadKey, out packageDownload))
                        {
                            packageDownload.ActiveDownloads++;
                            packageDownload.LastTimeStamp = DateTime.Now;
                            return(packageDownload.Fragments.Count);
                        }
                        else
                        {
                            packageDownload = new Download(packageDetails.Name, packageDetails.Version, package, fragmentSize);

                            if (activeDownloads.TryAdd(downloadKey, packageDownload))
                            {
                                return(packageDownload.Fragments.Count);
                            }
                            else
                            {
                                LoggingControl.Log("Error while starting download for " + packageDetails.Name + " " + packageDetails.Version + ". Could not add instance to download list.");
                            }
                        }
                    }
                    else
                    {
                        LoggingControl.Log("Error while starting download for " + packageDetails.Name + " " + packageDetails.Version + ". Could not find file.");
                    }
                }
                else
                {
                    LoggingControl.Log("Error while cancelling downloads for " + packageDetails.Name + " " + packageDetails.Version + ". Could not find package in list.");
                }

                return(-1);
            }
            catch (Exception e)
            {
                LoggingControl.Log("Error while cancelling downloads for " + packageDetails.Name + " " + packageDetails.Version + ".", e);
                return(-1);
            }
        }
예제 #2
0
        /// <summary>
        /// Recombines uploaded fragments into an APK file which is saved on the server before removing this entry from the upload dictionary.
        /// </summary>
        /// <param name="packageDetails">Details of the package upload that is being completed.</param>
        public static bool FinishUpload(PackageDetails packageDetails)
        {
            try
            {
                Upload upload    = null;
                var    uploadKey = GetUploadKey(packageDetails);

                if (activeUploads.TryGetValue(uploadKey, out upload))
                {
                    var package = upload.CombineFragmentsIntoPackage();

                    if (package.Length != upload.PackageSize)
                    {
                        LoggingControl.Log("Error while finishing upload for " + packageDetails.Name + " " + packageDetails.Version + ". Unexpected file size.");
                        return(false);
                    }

                    var path = FileControl.SavePackage(packageDetails.GetPackageKey(), package);

                    if (String.IsNullOrEmpty(path))
                    {
                        LoggingControl.Log("Error while finishing upload for " + packageDetails.Name + " " + packageDetails.Version + ". Could not find key save file.");
                        return(false);
                    }

                    return(activeUploads.TryRemove(uploadKey, out upload));
                }
                else
                {
                    LoggingControl.Log("Error while finishing upload for " + packageDetails.Name + " " + packageDetails.Version + ". Could not find key " + uploadKey + ".");
                    return(false);
                }
            }
            catch (Exception e)
            {
                LoggingControl.Log("Error while finishing upload for " + packageDetails.Name + " " + packageDetails.Version + ".", e);
                return(false);
            }
        }