Пример #1
0
        /// <summary>
        /// Send a new FPR to the peer for download a file pack of a not-downloading file part.
        /// </summary>
        /// <param name="download">The download object.</param>
        /// <param name="Peer">The IP address of peer to send the FPR message.</param>
        /// <returns>Return true if the message has been sended.</returns>
        private static bool StartDownloadNextFilePart(Objects.Download download, Objects.Peer Peer)
        {
            // the index of the not-downloaded file parts
            int[] index = new int[download.RemainingFileParts];

            int n = 0;

            // get the not-downloaded file parts
            for (int i = 0; i < download.ListFileParts.Length; i++)
            {
                if (download.ListFileParts[i] == 128 || (download.ListFileParts[i] > 0 && i == (download.ListFileParts.Length - 1)) )
                {
                    index[n] = i;

                    n++;
                }
            }

            // select a random not-downloaded file part to download

            Random random = new Random();

            if (n > 0)
            {
                while(true)
                {
                    int a = 0;

                    if (download.RemainingFileParts != 0)
                    {
                        a = random.Next(n);

                        if (download.ListFileParts[index[a]] != 0)
                        {
                            // start to download the file part

                            string[] Params = new string[3];
                            Params[0] = download.Name;
                            Params[1] = download.SHA1;
                            Params[2] = (index[a] * 16384).ToString();

                            Messages.IMessage FprMess = Messages.MessagesFactory.Instance.CreateMessage(Messages.Commands.FPR, Params);

                            Peer.Send(FprMess);

                            return true;
                        }
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            else
            {
                return false;
            }
        }
Пример #2
0
        /// <summary>
        /// Send a new FPR to the peer for download the next not-downloaded file pack of a this file part.
        /// </summary>
        /// <param name="download">The download object.</param>
        /// <param name="Peer">The IP address of peer to send the FPR message.</param>
        /// <returns>Return true if the message has been sended.</returns>
        private static bool DownloadFirstNotDownloadedFilePack(Objects.Download download, Objects.Peer Peer)
        {
            for (int i = 0; i < download.ListFileParts.Length; i++)
            {
                if (download.ListFileParts[i] > 0)
                {
                    int a = i * 128;

                    for (int n = 0; n < 128; n++)
                    {
                        if (download.ListFilePacks[a + n] == false)
                        {
                            // start to download the file part

                            string[] Params = new string[3];
                            Params[0] = download.Name;
                            Params[1] = download.SHA1;
                            Params[2] = ((a + n) * 16384).ToString();

                            Messages.IMessage FprMess = Messages.MessagesFactory.Instance.CreateMessage(Messages.Commands.FPR, Params);

                            Peer.Send(FprMess);

                            return true;
                        }
                    }
                }
            }

            return false;
        }
Пример #3
0
        /// <summary>
        /// Send a new FPR to the peer for download the next not-downloaded file pack of a this file part.
        /// </summary>
        /// <param name="filePackNum">The number of the file pack.</param>
        /// <param name="filePartNum">The number of the file part.</param>
        /// <param name="download">The download object.</param>
        /// <param name="Peer">The IP address of peer to send the FPR message.</param>
        /// <returns>Return true if the message has been sended; return false if the file part is completed.</returns>
        private static bool DownloadNextFilePackOfAFilePart(int filePackNum, int filePartNum, Objects.Download download, Objects.Peer Peer)
        {
            int nPack = filePackNum;
            int partEnd = (filePartNum * 128) + 128;

            for(int i = 0; i < 128; i++)
            {
                if (nPack < download.ListFilePacks.Length)
                {
                    if (download.ListFilePacks[nPack] == false)
                    {
                        // download this file pack

                        string[] Params = new string[3];
                        Params[0] = download.Name;
                        Params[1] = download.SHA1;
                        Params[2] = (nPack * 16384).ToString();

                        Messages.IMessage FprMess = Messages.MessagesFactory.Instance.CreateMessage(Messages.Commands.FPR, Params);

                        Peer.Send(FprMess);

                        return true;
                    }
                }
                else
                {
                    return false;
                }

                if (nPack < partEnd)
                {
                    nPack++;
                }
                else
                {
                    nPack = filePartNum * 128;
                }
            }

            return false;
        }
Пример #4
0
        /// <summary>
        /// Logs a downloaded file pack and send a new FPR message if is necessary.
        /// </summary>
        /// <param name="Download">The download object.</param>
        /// <param name="StartPoint">The star point of the file pack.</param>
        /// <param name="SenderPeer">The sender peer object.</param>
        /// <param name="Status">If the file-pack has been written or not ( other ).</param>
        public static void LogFilePack(Objects.Download Download, int StartPoint, Objects.Peer SenderPeer, Status_FilePack Status)
        {
            // indicate the downloading of a file pack
            SenderPeer.FilePackDownloaded = true;

            if (Status == Status_FilePack.Written)
            {
                #region ...

                int filePackNum = StartPoint / 16384;
                int filePartNum = StartPoint / 2097152;

                // control if the file pack is already been downloaded
                if (Download.ListFilePacks[filePackNum] == false)
                {
                    // update the list of file packs
                    Download.ListFilePacks[filePackNum] = true;

                    // update the number of remaining file packs
                    Download.RemainingFilePacks--;

                    // update the list of file parts
                    Download.ListFileParts[filePartNum]--;

                    // control if the file part is completed
                    if (Download.ListFileParts[filePartNum] == 0)
                    {
                        // update the number of remaining file parts
                        Download.RemainingFileParts--;

                        // control if the download is finished
                        if (Download.RemainingFilePacks == 0)
                        {
                            // move the completed download from temp-directory to shared-directory
                            File.Move(Global.TempDirectory + Download.Name, Global.SharedDirectory + Download.Name);

                            // update download's informations
                            Download.Progress = 100;
                            Download.Active = false;
                        }
                        else
                        {
                            // send a new FPR to the peer for the next not downloaded file pack of a new file part
                            StartDownloadNextFilePart(Download, SenderPeer);
                        }
                    }
                    else
                    {
                        // send a new FPR to the peer for the next not downloaded file pack of a this file part
                        if (DownloadNextFilePackOfAFilePart(filePackNum, filePartNum, Download, SenderPeer) == false)
                        {
                            StartDownloadNextFilePart(Download, SenderPeer);
                        }
                    }

                    // log the file pack
                    Download.LogFilePack(SenderPeer.IP, filePackNum);

                    NewOrUpdatedDownload = true;
                }

                #endregion
            }

            // if the FilePack is damaged, will send a new FPR-mess to the peer;
            else if (Status == Status_FilePack.Damaged)
            {
                #region ...

                string[] Params = new string[3];
                Params[0] = Download.Name;
                Params[1] = Download.SHA1;
                Params[2] = StartPoint.ToString();

                Messages.IMessage FprMess = Messages.MessagesFactory.Instance.CreateMessage(Messages.Commands.FPR, Params);

                SenderPeer.Send(FprMess);

                #endregion
            }
        }