Esempio n. 1
0
        private List<SearchFile> LoadFiles()
        {
            DirectoryInfo dirInfo = new DirectoryInfo(ClassLibrary.Config.SharedFolder);

            FileInfo[] filesInfo = dirInfo.GetFiles();
            List<SearchFile> files = new List<SearchFile>();

            foreach (var fileInfo in filesInfo)
            {
                ClassLibrary.Entites.File file = new ClassLibrary.Entites.File();
                file.FileName = fileInfo.Name;
                file.FileType = fileInfo.Extension;
                file.FileSize = fileInfo.Length;

                SHA512 sha512 = SHA512.Create();
                using (var stream = fileInfo.Open(FileMode.Open, FileAccess.Read))
                {
                    file.FileHash = BitConverter.ToString(sha512.ComputeHash(stream), 0);
                }

                file.PeerName = manager.Peer.PeerName;
                file.PeerID = manager.Peer.PeerID;

                files.Add(new SearchFile(file));
            }

            return files;
        }
Esempio n. 2
0
        private List <SearchFile> LoadFiles()
        {
            DirectoryInfo dirInfo = new DirectoryInfo(ClassLibrary.Config.SharedFolder);

            FileInfo[]        filesInfo = dirInfo.GetFiles();
            List <SearchFile> files     = new List <SearchFile>();

            foreach (var fileInfo in filesInfo)
            {
                ClassLibrary.Entites.File file = new ClassLibrary.Entites.File();
                file.FileName = fileInfo.Name;
                file.FileType = fileInfo.Extension;
                file.FileSize = fileInfo.Length;

                SHA512 sha512 = SHA512.Create();
                using (var stream = fileInfo.Open(FileMode.Open, FileAccess.Read))
                {
                    file.FileHash = BitConverter.ToString(sha512.ComputeHash(stream), 0);
                }

                file.PeerName = manager.Peer.PeerName;
                file.PeerID   = manager.Peer.PeerID;

                files.Add(new SearchFile(file));
            }

            return(files);
        }
Esempio n. 3
0
        private void buttonAddFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();

            DialogResult result = openFileDialog.ShowDialog(this);
            if (result == DialogResult.OK)
            {
                string destination = ClassLibrary.Config.SharedFolder + "\\" + Path.GetFileName(openFileDialog.FileName);

                try
                {
                    FileSystem.CopyFile(openFileDialog.FileName, destination, UIOption.AllDialogs);

                    ClassLibrary.Entites.File file = new ClassLibrary.Entites.File();
                    FileInfo fileInfo = new FileInfo(destination);

                    file.FileName = fileInfo.Name;
                    file.FileType = fileInfo.Extension;
                    file.FileSize = fileInfo.Length;

                    SHA512 sha512 = SHA512.Create();
                    using (var stream = fileInfo.Open(FileMode.Open, FileAccess.Read))
                    {
                        file.FileHash = BitConverter.ToString(sha512.ComputeHash(stream), 0);
                    }

                    file.PeerName = manager.Peer.PeerName;
                    file.PeerID = manager.Peer.PeerID;

                    manager.AddFile(file);
                    dataGridViewFManager.DataSource = LoadFiles();
                }
                catch (OperationCanceledException) { }
            }
        }
Esempio n. 4
0
        public SearchFile(File file)
        {
            this.FileName = file.FileName;
            this.FileSize = file.FileSize;
            this.FileType = file.FileType;
            this.FileHash = file.FileHash;

            this.file = file;
        }
Esempio n. 5
0
        public void DeleteFile(File file)
        {
            string temp = String.Format("DELETE FROM files WHERE peerId='{0}' and fileHash='{1}' and fileName='{2}'",
                file.PeerID, file.FileHash, file.FileName);
            dbConnection.Update(temp);

            // log the event
            logging.LogI(String.Format("Info: *** A file '{0}' has been deleted ***" +
                Environment.NewLine, file.FileName));
        }
Esempio n. 6
0
        public void AddFile(ClassLibrary.Entites.File file)
        {
            SuperPeerServiceClient ssc = new SuperPeerServiceClient();

            List <ClassLibrary.Entites.File> tempList = new List <ClassLibrary.Entites.File>();

            tempList.Add(file);

            ssc.AddFiles(tempList);
        }
Esempio n. 7
0
        public void DoHost(Peer peer)
        {
            PeerServiceHost.peer = peer;

            string address = string.Empty;
            address = string.Format("net.tcp://{0}:{1}/PeerToPeerClient.Services/PeerService",
                peer.PeerHostName, Config.LocalPort);

            Uri uri = new Uri(address);
            NetTcpBinding tcpBinding = new NetTcpBinding(SecurityMode.None);

            ServiceHost serviceHost = new ServiceHost(typeof(PeerService), uri);
            serviceHost.AddServiceEndpoint(typeof(IPeerService), tcpBinding, "");

            serviceHost.Open();

            // add this peer to the server
            SuperPeerServiceClient ssc = new SuperPeerServiceClient();
            ssc.AddPeer(peer);

            // add the files that this peer wants to share to the server
            DirectoryInfo dirInfo = new DirectoryInfo(Config.SharedFolder);

            if (dirInfo.Exists)
            {
                FileInfo[] filesInfo = dirInfo.GetFiles();
                List<ClassLibrary.Entites.File> files = new List<ClassLibrary.Entites.File>();

                foreach (var fileInfo in filesInfo)
                {
                    ClassLibrary.Entites.File file = new ClassLibrary.Entites.File();
                    file.FileName = fileInfo.Name;
                    file.FileType = fileInfo.Extension;
                    file.FileSize = fileInfo.Length;

                    SHA512 sha512 = SHA512.Create(); 
                    using (var stream = fileInfo.Open(FileMode.Open, FileAccess.Read))
                    {
                        file.FileHash = BitConverter.ToString(sha512.ComputeHash(stream), 0);
                    }

                    file.PeerName = peer.PeerName;
                    file.PeerID = peer.PeerID;

                    files.Add(file);
                }

                if (files.Count != 0)
                    ssc.AddFiles(files);
            }

            else 
                dirInfo.Create();
        }
Esempio n. 8
0
        private void buttonAddFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();

            DialogResult result = openFileDialog.ShowDialog(this);

            if (result == DialogResult.OK)
            {
                string destination = ClassLibrary.Config.SharedFolder + "\\" + Path.GetFileName(openFileDialog.FileName);

                try
                {
                    FileSystem.CopyFile(openFileDialog.FileName, destination, UIOption.AllDialogs);

                    ClassLibrary.Entites.File file = new ClassLibrary.Entites.File();
                    FileInfo fileInfo = new FileInfo(destination);

                    file.FileName = fileInfo.Name;
                    file.FileType = fileInfo.Extension;
                    file.FileSize = fileInfo.Length;

                    SHA512 sha512 = SHA512.Create();
                    using (var stream = fileInfo.Open(FileMode.Open, FileAccess.Read))
                    {
                        file.FileHash = BitConverter.ToString(sha512.ComputeHash(stream), 0);
                    }

                    file.PeerName = manager.Peer.PeerName;
                    file.PeerID   = manager.Peer.PeerID;

                    manager.AddFile(file);
                    dataGridViewFManager.DataSource = LoadFiles();
                }
                catch (OperationCanceledException) { }
            }
        }
Esempio n. 9
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++;
            }
        }
Esempio n. 10
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. 11
0
        public void DeleteFile(ClassLibrary.Entites.File file)
        {
            SuperPeerServiceClient ssc = new SuperPeerServiceClient();

            ssc.DeleteFile(file);
        }
Esempio n. 12
0
        public void DoHost(Peer peer)
        {
            PeerServiceHost.peer = peer;

            string address = string.Empty;

            address = string.Format("net.tcp://{0}:{1}/PeerToPeerClient.Services/PeerService",
                                    peer.PeerHostName, Config.LocalPort);

            Uri           uri        = new Uri(address);
            NetTcpBinding tcpBinding = new NetTcpBinding(SecurityMode.None);

            ServiceHost serviceHost = new ServiceHost(typeof(PeerService), uri);

            serviceHost.AddServiceEndpoint(typeof(IPeerService), tcpBinding, "");

            serviceHost.Open();

            // add this peer to the server
            SuperPeerServiceClient ssc = new SuperPeerServiceClient();

            ssc.AddPeer(peer);

            // add the files that this peer wants to share to the server
            DirectoryInfo dirInfo = new DirectoryInfo(Config.SharedFolder);

            if (dirInfo.Exists)
            {
                FileInfo[] filesInfo = dirInfo.GetFiles();
                List <ClassLibrary.Entites.File> files = new List <ClassLibrary.Entites.File>();

                foreach (var fileInfo in filesInfo)
                {
                    ClassLibrary.Entites.File file = new ClassLibrary.Entites.File();
                    file.FileName = fileInfo.Name;
                    file.FileType = fileInfo.Extension;
                    file.FileSize = fileInfo.Length;

                    SHA512 sha512 = SHA512.Create();
                    using (var stream = fileInfo.Open(FileMode.Open, FileAccess.Read))
                    {
                        file.FileHash = BitConverter.ToString(sha512.ComputeHash(stream), 0);
                    }

                    file.PeerName = peer.PeerName;
                    file.PeerID   = peer.PeerID;

                    files.Add(file);
                }

                if (files.Count != 0)
                {
                    ssc.AddFiles(files);
                }
            }

            else
            {
                dirInfo.Create();
            }
        }