public PackageStatusResponse GetPackageStatusResponse(Hash[] packageIds)
        {
            var packages = new PackageStatusDetail[packageIds.Length];

            for (int i = 0; i < packageIds.Length; i++)
            {
                var  detail = new PackageStatusDetail();
                Hash id     = packageIds[i];
                packages[i] = detail;
                if (!packageRegistry.TryGetPackage(id, out LocalPackageInfo info) || info.LockProvider.IsMarkedToDelete)
                {
                    detail.IsFound = false;
                    continue;
                }

                // found
                detail.IsFound         = true;
                detail.BytesDownloaded = info.DownloadStatus.Data.DownloadedBytes;
                detail.SegmentsBitmap  = info.DownloadStatus.Data.SegmentsBitmap;
            }

            var result = new PackageStatusResponse()
            {
                Packages = packages
            };

            return(result);
        }
Esempio n. 2
0
        /// <summary>
        /// Throws exception if given status detail is invalid for package represented by this instance. This can happen only if data has been manipulated.
        /// </summary>
        public void ValidateStatusUpdateFromPeer(PackageStatusDetail detail)
        {
            if (detail == null)
            {
                throw new InvalidOperationException("Detail is NULL.");
            }

            if (!detail.IsFound)
            {
                throw new InvalidOperationException("Detail is invalid. It is marked as not found.");
            }

            if (detail.BytesDownloaded < 0 || detail.BytesDownloaded > sequenceInfo.PackageSize)
            {
                throw new InvalidOperationException("Invalid bytes downloaded counter. Value is out of range.");
            }

            if (detail.BytesDownloaded == sequenceInfo.PackageSize && detail.SegmentsBitmap != null)
            {
                throw new InvalidOperationException("Invalid bitmap. Bitmap should be NULL if package is already downloaded.");
            }

            if (detail.BytesDownloaded < sequenceInfo.PackageSize && detail.SegmentsBitmap == null)
            {
                throw new InvalidOperationException("Invalid bitmap. Bitmap should NOT be NULL if package is not yet downloaded.");
            }

            if (detail.SegmentsBitmap != null && detail.SegmentsBitmap.Length != dto.SegmentsBitmap.Length)
            {
                throw new InvalidOperationException("Invalid bitmap. Invalid bitmap length.");
            }

            if (detail.SegmentsBitmap != null && detail.SegmentsBitmap.Length > 0 && (byte)(detail.SegmentsBitmap[detail.SegmentsBitmap.Length - 1] & ~lastByteMask) != 0)
            {
                throw new InvalidOperationException("Invalid bitmap. Last bitmap byte is out of range.");
            }
        }