コード例 #1
0
        public async Task Cancel_File_Transfer()
        {
            var sender = PeerIdHelper.GetPeerId("sender");

            IDownloadFileInformation fileTransferInformation = new DownloadFileTransferInformation(
                sender,
                sender,
                _fakeContext.Channel,
                CorrelationId.GenerateCorrelationId(),
                string.Empty,
                555);

            var cancellationTokenSource = new CancellationTokenSource();

            _nodeFileTransferFactory.RegisterTransfer(fileTransferInformation);
            _nodeFileTransferFactory
            .FileTransferAsync(fileTransferInformation.CorrelationId, cancellationTokenSource.Token)
            .ConfigureAwait(false).GetAwaiter();
            Assert.Single(_nodeFileTransferFactory.Keys);
            cancellationTokenSource.Cancel();

            await TaskHelper.WaitForAsync(() => fileTransferInformation.IsCompleted, TimeSpan.FromSeconds(10));

            var fileCleanedUp = !File.Exists(fileTransferInformation.TempPath);

            Assert.True(fileTransferInformation.IsExpired());
            Assert.True(fileCleanedUp);
            Assert.Empty(_nodeFileTransferFactory.Keys);
        }
コード例 #2
0
        public async Task Get_File_Rpc(long byteSize)
        {
            var    addedIpfsHash = AddFileToDfs(byteSize, out var crcValue, out var stream);
            Stream fileStream    = null;

            try
            {
                var nodePeerId   = PeerIdHelper.GetPeerId("sender");
                var rpcPeerId    = PeerIdHelper.GetPeerId("recipient");
                var peerSettings = Substitute.For <IPeerSettings>();
                peerSettings.PeerId.Returns(rpcPeerId);
                var nodePeer           = nodePeerId;
                var rpcPeer            = rpcPeerId;
                var correlationId      = CorrelationId.GenerateCorrelationId();
                var fakeFileOutputPath = Path.GetTempFileName();
                IDownloadFileInformation fileDownloadInformation = new DownloadFileTransferInformation(rpcPeer,
                                                                                                       nodePeer,
                                                                                                       _fakeContext.Channel, correlationId, fakeFileOutputPath, 0);
                var getFileFromDfsResponseHandler =
                    new GetFileFromDfsResponseObserver(_logger, _fileDownloadFactory);
                var transferBytesHandler =
                    new TransferFileBytesRequestObserver(_fileDownloadFactory, peerSettings, _logger);

                _fileDownloadFactory.RegisterTransfer(fileDownloadInformation);

                var getFileResponse = new GetFileFromDfsResponse
                {
                    FileSize     = (ulong)byteSize,
                    ResponseCode = ByteString.CopyFrom((byte)FileTransferResponseCodeTypes.Successful.Id)
                }.ToProtocolMessage(nodePeer, correlationId);

                getFileResponse.SendToHandler(_fakeContext, getFileFromDfsResponseHandler);

                fileStream = await _dfsService.UnixFsApi.ReadFileAsync(addedIpfsHash.ToString());

                IUploadFileInformation fileUploadInformation = new UploadFileTransferInformation(
                    fileStream,
                    rpcPeer,
                    nodePeer,
                    _fakeContext.Channel,
                    correlationId);

                for (uint i = 0; i < fileUploadInformation.MaxChunk; i++)
                {
                    var transferMessage = fileUploadInformation
                                          .GetUploadMessageDto(i);
                    transferMessage.Content.SendToHandler(_fakeContext, transferBytesHandler);
                }

                await TaskHelper.WaitForAsync(() => fileDownloadInformation.IsCompleted, TimeSpan.FromSeconds(10));

                Assert.AreEqual(crcValue, FileHelper.GetCrcValue(fileDownloadInformation.TempPath));
            }
            finally
            {
                stream.Close();
                fileStream?.Close();
            }
        }
コード例 #3
0
        public void Handler_Uses_Correct_CorrelationId()
        {
            _nodeFileTransferFactory.RegisterTransfer(Arg.Any <IDownloadFileInformation>())
            .Returns(FileTransferResponseCodeTypes.Successful);

            var correlationId = CorrelationId.GenerateCorrelationId();

            var protocolMessage = GenerateProtocolMessage(correlationId);

            protocolMessage.SendToHandler(_fakeContext, _addFileToDfsRequestObserver);

            _nodeFileTransferFactory.RegisterTransfer(
                Arg.Is <IDownloadFileInformation>(
                    info => info.CorrelationId.Id.Equals(correlationId.Id)));
        }
コード例 #4
0
        /// <summary>
        /// </summary>
        /// <param name="addFileToDfsRequest"></param>
        /// <param name="channelHandlerContext"></param>
        /// <param name="senderPeerId"></param>
        /// <param name="correlationId"></param>
        /// <returns></returns>
        protected override AddFileToDfsResponse HandleRequest(AddFileToDfsRequest addFileToDfsRequest,
                                                              IChannelHandlerContext channelHandlerContext,
                                                              PeerId senderPeerId,
                                                              ICorrelationId correlationId)
        {
            Guard.Argument(addFileToDfsRequest, nameof(addFileToDfsRequest)).NotNull();
            Guard.Argument(channelHandlerContext, nameof(channelHandlerContext)).NotNull();
            Guard.Argument(senderPeerId, nameof(senderPeerId)).NotNull();

            var fileTransferInformation = new DownloadFileTransferInformation(PeerSettings.PeerId,
                                                                              senderPeerId, channelHandlerContext.Channel,
                                                                              correlationId, addFileToDfsRequest.FileName, addFileToDfsRequest.FileSize);

            FileTransferResponseCodeTypes responseCodeType;

            try
            {
                responseCodeType = _fileTransferFactory.RegisterTransfer(fileTransferInformation);
            }
            catch (Exception e)
            {
                Logger.Error(e,
                             "Failed to handle AddFileToDfsRequestHandler after receiving message {0}", addFileToDfsRequest);
                responseCodeType = FileTransferResponseCodeTypes.Error;
            }

            var message = GetResponse(fileTransferInformation, responseCodeType);

            if (responseCodeType != FileTransferResponseCodeTypes.Successful)
            {
                return(message);
            }

            var ctx = new CancellationTokenSource();

            _fileTransferFactory.FileTransferAsync(fileTransferInformation.CorrelationId, CancellationToken.None)
            .ContinueWith(task =>
            {
                if (fileTransferInformation.ChunkIndicatorsTrue())
                {
                    OnSuccessAsync(fileTransferInformation).ConfigureAwait(false).GetAwaiter().GetResult();
                }

                fileTransferInformation.Dispose();
            }, ctx.Token)
            .ConfigureAwait(false);

            return(message);
        }
コード例 #5
0
ファイル: GetFileCommand.cs プロジェクト: umlspec/Catalyst
        public override void SendMessage(GetFileOptions opts)
        {
            var message         = GetMessage(opts);
            var protocolMessage = message.ToProtocolMessage(SenderPeerId);
            var correlationId   = protocolMessage.CorrelationId.ToCorrelationId();

            var messageDto = new MessageDto(
                protocolMessage,
                RecipientPeerId);

            var fileTransfer = new DownloadFileTransferInformation(
                SenderPeerId,
                RecipientPeerId,
                Target.Channel,
                correlationId,
                opts.FileOutput,
                0
                );

            _downloadFileTransferFactory.RegisterTransfer(fileTransfer);
            Target.SendMessage(messageDto);

            while (!fileTransfer.ChunkIndicatorsTrue() && !fileTransfer.IsExpired())
            {
                CommandContext.UserOutput.Write($"\rDownloaded: {fileTransfer.GetPercentage().ToString()}%");
                Thread.Sleep(500);
            }

            if (fileTransfer.ChunkIndicatorsTrue())
            {
                CommandContext.UserOutput.Write($"\rDownloaded: {fileTransfer.GetPercentage().ToString()}%\n");
            }
            else
            {
                CommandContext.UserOutput.WriteLine("\nFile transfer expired.");
            }
        }