Esempio n. 1
0
        public void Download(ClassLibrary.Entites.SearchFile fileSearchResult)
        {
            Action <object> downloadAction     = new Action <object>(StartDownload);
            Task            downloadActionTask = new Task(downloadAction, fileSearchResult);

            downloadActionTask.Start();
        }
Esempio n. 2
0
        public SearchFileDisplay(SearchFile parent) 
        {
            base.FileName = parent.FileName;
            base.FileSize = parent.FileSize;
            base.FileType = parent.FileType;
            base.FileHash = parent.FileHash;
            this.Seeders = parent.PeerNames.Count;

            this.parent = parent;
        }
Esempio n. 3
0
        public List<SearchFile> SearchAvailableFiles(string fileName)
        {
            string query = String.Format("SELECT * FROM files WHERE fileName LIKE '%{0}%'", fileName);
            SQLiteDataReader reader = dbConnection.Select(query);

            List<SearchFile> result = new List<SearchFile>();

            List<File> availableFiles = new List<File>();
            while (reader.Read())
            {
                File file = new File();

                file.FileName = (string)reader["fileName"];
                file.FileSize = long.Parse(reader["fileSize"].ToString());
                file.FileType = (string)reader["fileType"];
                file.FileHash = (string)reader["fileHash"];
                file.PeerName = (string)reader["peerName"];

                availableFiles.Add(file);
            }

            // create groups of the same files
            var sameFileGroups = availableFiles.GroupBy(x => x.FileHash); 

            foreach (var sameFiles in sameFileGroups)
            {
                // create groups of same files and with same name
                var sameNameAndFileGroups = sameFiles.GroupBy(x => x.FileName); 

                foreach (var temp in sameNameAndFileGroups) {
                    List<String> hosts = new List<String>();
                    SearchFile searchFile = new SearchFile();

                    File file = temp.First();

                    searchFile.FileName = file.FileName;
                    searchFile.FileSize = file.FileSize;
                    searchFile.FileType = file.FileType;
                    searchFile.FileHash = file.FileHash;

                    for (int i = 0; i < temp.Count(); i++)
                    {
                        hosts.Add(temp.ElementAt(i).PeerName);
                    }

                    searchFile.PeerNames = hosts;
                    result.Add(searchFile);
                }
            }

            return result;
        }
Esempio n. 4
0
        private void StartDownload(object state)
        {
            ClassLibrary.Entites.SearchFile file = state as SearchFile;
            Action <object> downloadAction       = new Action <object>(DownloadFilePart);

            ClassLibrary.Entites.File temp = new File();

            temp.FileName = file.FileName;
            temp.FileSize = file.FileSize;
            temp.FileType = file.FileType;
            temp.FileHash = file.FileHash;

            long partCount = file.FileSize / ClassLibrary.Config.FiePartsize;
            long mod       = file.FileSize % ClassLibrary.Config.FiePartsize;

            if (mod > 0)
            {
                partCount++;
            }

            int allowedThreads = partCount < (long)ClassLibrary.Config.NumberThreads ? (int)partCount:
                                 ClassLibrary.Config.NumberThreads;

            allowedThreads = allowedThreads < file.PeerNames.Count() ? allowedThreads :
                             file.PeerNames.Count();

            int  numIterations = (int)(partCount / allowedThreads);
            long part          = 0;

            while (numIterations > 0)
            {
                int i = allowedThreads;

                while (i > 0)
                {
                    temp.PeerName = file.PeerNames.ToArray()[--i];

                    Task downloadActionTask = new Task(downloadAction, new DownloadParameter {
                        File          = temp,
                        Part          = part,
                        AllPartsCount = partCount
                    });

                    downloadActionTask.Start();

                    part++;
                }

                numIterations--;
            }

            mod = partCount % allowedThreads;

            while (mod > 0)
            {
                temp.PeerName = file.PeerNames.ToArray()[--mod];

                Task downloadActionTask = new Task(downloadAction, new DownloadParameter
                {
                    File          = temp,
                    Part          = part,
                    AllPartsCount = partCount
                });

                downloadActionTask.Start();

                part++;
            }
        }