/// <summary>
        /// Deletes an APK file from the server/service.
        /// </summary>
        /// <param name="packageDetails">Details of the APK to be deleted.</param>
        public static bool DeletePackage(PackageDetails packageDetails)
        {
            try
            {
                waitHandle.WaitOne();

                var packageKey = packageDetails.GetPackageKey();
                var path       = GetPackagePath(packageKey);

                if (File.Exists(path))
                {
                    File.Delete(path);
                }
                else
                {
                    LoggingControl.Log("Error while deleting package " + packageDetails.Name + " " + packageDetails.Version + ". Could not find file.");
                    return(false);
                }

                return(!File.Exists(path));
            }
            catch (Exception e)
            {
                LoggingControl.Log("Error while deleting package " + packageDetails.Name + " " + packageDetails.Version + ".", e);
                return(false);
            }
            finally
            {
                waitHandle.Set();
            }
        }
Beispiel #2
0
        /// <summary>
        /// Removes this download entry from the download dictionary if only one device is busy downloading or simply decremtents the count otherwise..
        /// </summary>
        /// <param name="packageDetails">Details of the package download that is being completed.</param>
        public static bool EndOrDecrementDownload(PackageDetails packageDetails, int fragmentSize)
        {
            try
            {
                var packageKey  = packageDetails.GetPackageKey();
                var downloadKey = GetDownloadKey(packageDetails, fragmentSize);

                Download download;

                if (activeDownloads.TryGetValue(downloadKey, out download))
                {
                    download.LastTimeStamp = DateTime.Now;
                    download.ActiveDownloads--;

                    if (download.ActiveDownloads <= 0)
                    {
                        return(activeDownloads.TryRemove(packageKey, out download));
                    }
                    else
                    {
                        return(true);
                    }
                }
                else
                {
                    LoggingControl.Log("Error while ending download for " + packageDetails.Name + " " + packageDetails.Version + ". Entry no longer exists in download list.");
                    return(true);
                }
            }
            catch (Exception e)
            {
                LoggingControl.Log("Error while ending download for  " + packageDetails.Name + " " + packageDetails.Version + ".", e);
                return(false);
            }
        }
Beispiel #3
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);
            }
        }
        /// <summary>
        /// Updates the filename of an APK to indicate whether it is a critical update or not.
        /// Apps updated through mobu canspecify whether they always get the latest version of an APK or only newer APKs marked as critical.
        /// This makes it possible allow testers to manually get new versions without automatically pushing it to all users.
        /// </summary>
        /// <param name="packageDetails">Details of the package that should be toggled.</param>
        /// <returns></returns>
        public static bool ToggleCriticalState(PackageDetails packageDetails)
        {
            try
            {
                waitHandle.WaitOne();

                var packageKey = packageDetails.GetPackageKey();
                var oldPath    = GetPackagePath(packageKey);
                var newPath    = oldPath.Remove(oldPath.Length - 1);

                if (File.Exists(oldPath))
                {
                    if (oldPath[oldPath.Length - 1] == '1')
                    {
                        newPath += '0';
                    }
                    else
                    {
                        newPath += '1';
                    }

                    File.Move(oldPath, newPath);
                }
                else
                {
                    LoggingControl.Log("Error while toggling critical state for " + packageDetails.Name + " " + packageDetails.Version + ". Could not find file.");;
                    return(false);
                }

                return(File.Exists(newPath) && !File.Exists(oldPath));
            }
            catch (Exception e)
            {
                LoggingControl.Log("Error while toggling critical state for " + packageDetails.Name + " " + packageDetails.Version + ".", e);
                return(false);
            }
            finally
            {
                waitHandle.Set();
            }
        }
Beispiel #5
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);
            }
        }