Beispiel #1
0
 /// <summary>
 /// Function to receive download message
 /// </summary>
 /// <param name="downloadMessage">The message which has to be received</param>
 private void ReceivedDownloadMessage(DownloadMessage downloadMessage)
 {
     if (downloadMessage.type.Equals(TypeCode.REQUEST))
     {
         string path = _path + @".hidden\" + downloadMessage.fromUuid + @"\" +
                       downloadMessage.fullFileName + @"\" + downloadMessage.filehash;
         if (downloadMessage.statusCode == StatusCode.OK)
         {
             if (File.Exists(path))
             {
                 downloadMessage.CreateReply();
                 downloadMessage.statusCode = StatusCode.ACCEPTED;
                 downloadMessage.Send(downloadMessage.port);
                 Console.WriteLine("Response send");
             }
             else
             {
                 Console.WriteLine("File not found");
                 downloadMessage.CreateReply();
                 downloadMessage.statusCode = StatusCode.FILE_NOT_FOUND;
                 downloadMessage.Send(downloadMessage.port);
             }
         }
         else if (downloadMessage.statusCode.Equals(StatusCode.ACCEPTED))
         {
             var sender = new ChunkSender(downloadMessage.fromIp, downloadMessage.port);
             sender.Send(path);
             DiskHelper.ConsoleWrite("File send " + downloadMessage.filehash);
         }
     }
 }
Beispiel #2
0
        /// <summary>
        /// Fetching function, this fetches the given chunk from the network, returns true if the chunk is fetched.
        /// </summary>
        /// <param name="chunk">The chunk wanted to download.</param>
        /// <param name="fullFileName">The name of the full file.</param>
        /// <returns>Rather the chunk has been fetched.</returns>
        public bool Fetch(P2PChunk chunk, string fullFileName)
        {
            _port       = _ports.GetAvailablePort();
            _hash       = chunk.hash;
            _peersToAsk = chunk.peers;
            Listener listener = new Listener(this._port);

            foreach (var peer in _peersToAsk)
            {
                if (!_peers.TryGetValue(peer, out Peer currentPeer))
                {
                    break;
                }

                if (currentPeer.IsOnline())
                {
                    var download = new DownloadMessage(currentPeer)
                    {
                        port         = this._port,
                        fullFileName = chunk.originalHash,
                        filehash     = _hash
                    };

                    // Sends the download message and waits for a
                    // "response" download message to be received.
                    // Then changed 'download' to this message and
                    // returns true. If a response is not received
                    // within time, it returns false.
                    if (listener.SendAndAwaitResponse(ref download, 3000))
                    {
                        // If the download is accepted, a receiver is
                        // started and the port of the receiver is
                        // sent to the peer.
                        if (download.statusCode == StatusCode.ACCEPTED)
                        {
                            int receiverPort = _ports.GetAvailablePort();
                            download.CreateReply();
                            download.type       = Messages.TypeCode.REQUEST;
                            download.statusCode = StatusCode.ACCEPTED;
                            download.port       = receiverPort;

                            if (!Directory.Exists(_path + fullFileName + @"\"))
                            {
                                Directory.CreateDirectory(_path + fullFileName + @"\");
                            }

                            download.Send();

                            if (!currentPeer.IsOnline())
                            {
                                DiskHelper.ConsoleWrite("The peer requested went offline.");
                                continue;
                            }
                            DiskHelper.ConsoleWrite("FileReceiver opened");
                            if (!Downloader(fullFileName, receiverPort))
                            {
                                return(false);
                            }

                            _ports.Release(download.port);
                            break;
                        }
                        else if (download.statusCode == StatusCode.FILE_NOT_FOUND)
                        {
                            Console.WriteLine("File not found at peer.");
                            chunk.peers.Remove(download.fromUuid);
                        }
                    }
                }
            }

            if (File.Exists(_path + fullFileName + @"\" + _hash))
            {
                if (CheckDownloadHash(_path + fullFileName + @"\" + _hash, _hash))
                {
                    DiskHelper.ConsoleWrite(@"Chunk done downloading");
                    return(true);
                }
            }

            return(false);
        }