Beispiel #1
0
        /// <summary>
        /// Main running function for UploadManager, needs to be called for it to run.
        /// </summary>
        public void Run()
        {
            _isStopped = false;
            this._waitHandle.Set();

            while (_isRunning)
            {
                this._waitHandle.WaitOne();

                if (!_isRunning)
                {
                    break;
                }

                while (this._queue.TryDequeue(out P2PFile file))
                {
                    bool uploaded = true;

                    if (!_peers.Any(p => p.Value.IsOnline()))
                    {
                        this._queue.Enqueue(file);
                        break;
                    }



                    if (!_isRunning)
                    {
                        this._queue.Enqueue(file);
                        break;
                    }

                    string filePath           = file.paths[0];
                    string compressedFilePath = this._path + @".hidden\" + file.hash;

                    // Compress file
                    bool compressionCompleted = Compressor.CompressFile(filePath, compressedFilePath);

                    if (!compressionCompleted)
                    {
                        this._queue.Enqueue(file);
                        continue;
                    }

                    // Encrypt file
                    var encryption = new FileEncryption(compressedFilePath, ".lzma");

                    bool encryptionCompleted = encryption.DoEncrypt(IdHandler.GetKeyMold());

                    if (!encryptionCompleted)
                    {
                        this._queue.Enqueue(file);
                        continue;
                    }

                    _hiddenFolder.Remove(compressedFilePath + ".lzma");

                    string encryptedFilePath = compressedFilePath + ".aes";

                    // Initialize splitter
                    var splitter = new SplitterLibrary();

                    List <string> chunks =
                        splitter.SplitFile(encryptedFilePath, file.hash, _path + @".hidden\splitter\");
                    file.AddChunk(chunks);

                    FileUploader uploader = new FileUploader(_ports, _peers);

                    int i = 0;
                    foreach (var chunk in file.chunks)
                    {
                        string path = _path + @".hidden\splitter\" + chunk.hash;

                        //3 is used, because the network is relatively small. 10 is the default.
                        if (!uploader.Push(chunk, path, 3, i))
                        {
                            uploaded = false;
                        }
                        i++;
                    }

                    if (!uploaded)
                    {
                        this._queue.Enqueue(file);
                    }

                    if (uploaded)
                    {
                        Console.WriteLine();
                        DiskHelper.ConsoleWrite($"The file {file.hash} was successfully sent to all \n");
                    }
                }

                this._waitHandle.Reset();
            }

            _isStopped = true;
        }