/// <summary> /// Finds the peers that have the file and add them in the list of peers. /// </summary> public void FindPeers() { // search the file in net Utilities.FileSearcher.SearchFile(this.Name); // get the file from the list of found files Lists.FilesFoundList.File file = Lists.FilesFoundList.GetFile(this.Name, this.SHA1); if (file != null) { for (int i = 0; i < file.ListPeers.Count; i++) { try { Objects.Peer peer = Lists.PeersList.GetPeerByIP(file.ListPeers[i].IP); if (peer != null) { AddPeer(peer); } } catch { } } } }
/// <summary> /// Creates a new download from a exist file in the list of the found files. /// </summary> /// <param name="FileName">The name of the file.</param> /// <param name="FileID">The ID of the file.</param> public static void AddDownload(string FileName, string FileID) { Lists.FilesFoundList.File file = Lists.FilesFoundList.GetFile(FileName, FileID); Objects.Download download = new Objects.Download(file.Name, file.Size, file.SHA1); download.Active = true; download.FindPeers(); if (List.Count > 0) { int n = 0; int max = List.Count - 1; int min = 0; int compare = 0; while (max >= min) { n = (max + min) / 2; compare = FileID.CompareTo(List[n].SHA1); if (compare > 0) { min = n + 1; } else if (compare < 0) { max = n - 1; } else { return; } } if (compare < 0) { n--; } List.Add(List[List.Count - 1]); for (int i = List.Count - 1; i > n + 1; i--) { List[i] = List[i - 1]; } List[n + 1] = download; } else { List.Add(download); } NewOrUpdatedDownload = true; }
/// <summary> /// Updates a old file with a new file. /// </summary> /// <param name="OldFile">The old file.</param> /// <param name="NewFile">The new file.</param> public static Lists.FilesFoundList.File UpdateFile(Lists.FilesFoundList.File OldFile, Lists.FilesFoundList.File NewFile) { List <int> peerToAdd = new List <int>(); for (int i = 0; i < NewFile.ListPeers.Count; i++) { if (OldFile.GetPeerByIP(NewFile.ListPeers[i].IP) == null) { peerToAdd.Add(i); } } for (int n = 0; n < peerToAdd.Count; n++) { OldFile.AddPeer(NewFile.ListPeers[peerToAdd[n]]); } return(OldFile); }