Exemple #1
0
        /// <summary>
        /// Handles the deletion of a chunk of the file
        /// </summary>
        /// <param name="currentFileChunk">The P2PChunk to be deleted</param>
        /// <param name="currentFile">The P2PFile to wich the chunk belongs</param>
        /// <returns></returns>
        public bool ChunkDeleter(P2PChunk currentFileChunk, P2PFile currentFile){
            Listener listener = new Listener(this._port);

            int lastIndex = currentFileChunk.peers.Count - 1;
            //Sends a delete message to every peer with the chunk
            for (int i = lastIndex; i >= 0; i--) {
                if (_peers.TryGetValue(currentFileChunk.peers[i], out Peer currentReceiver)) {
                    if (!currentReceiver.IsOnline()){
                        return false;
                    }
                    var deletionMessage = new FileDeletionMessage(currentReceiver) {
                        type = TypeCode.REQUEST,
                        statusCode = StatusCode.OK,
                        port = _port,
                        fileHash = currentFileChunk.hash,
                        fullFileHash = currentFile.hash
                    };

                    //Sends the message and waits for a response,
                    //which will then overwrite the original sent message
                    if (listener.SendAndAwaitResponse(ref deletionMessage, 2000)) {
                        if (deletionMessage.type.Equals(TypeCode.RESPONSE)) {
                            currentFileChunk.RemovePeer(deletionMessage.fromUuid);
                            if (currentFileChunk.peers.Count == 0) {
                                currentFile.RemoveChunk(currentFileChunk.hash);
                            }
                            if (deletionMessage.statusCode.Equals(StatusCode.FILE_NOT_FOUND)) {
                                DiskHelper.ConsoleWrite("File not found at peer");
                            }
                        }
                    }
                }
            }
            return true;
        }
Exemple #2
0
        /// <summary>
        /// Function to receive a deletion request of files on the receiver's computer
        /// </summary>
        /// <param name="message">The message with the chunks which has to be deleted</param>
        private void ReceivedDeletionRequest(FileDeletionMessage message)
        {
            DiskHelper.ConsoleWrite("Deletion Message Received.");
            if (!message.type.Equals(TypeCode.REQUEST))
            {
                return;
            }
            if (!message.statusCode.Equals(StatusCode.OK))
            {
                return;
            }
            string path = _path + @".hidden\" + message.fromUuid + @"\" + message.fullFileHash + @"\" +
                          message.fileHash;

            DiskHelper.ConsoleWrite(path);
            if (File.Exists(path))
            {
                File.Delete(path);
                message.statusCode = StatusCode.ACCEPTED;
                message.CreateReply();
                message.Send(message.port);
            }
            else
            {
                message.statusCode = StatusCode.FILE_NOT_FOUND;
                message.CreateReply();
                message.Send(message.port);
            }
        }