private MemoryStream GetFakeDfsStream(FileTransferResponseCodeTypes fakeResponse)
        {
            var fakeStream = new MemoryStream();

            fakeStream.Write(new byte[50]);
            _dfs.ReadAsync(Arg.Any <Cid>()).Returns(fakeStream);
            _fileTransferFactory.RegisterTransfer(Arg.Any <IUploadFileInformation>()).Returns(fakeResponse);
            return(fakeStream);
        }
        /// <summary>
        /// </summary>
        /// <param name="getFileFromDfsRequest"></param>
        /// <param name="channelHandlerContext"></param>
        /// <param name="senderPeerId"></param>
        /// <param name="correlationId"></param>
        /// <returns></returns>
        protected override GetFileFromDfsResponse HandleRequest(GetFileFromDfsRequest getFileFromDfsRequest,
                                                                IChannelHandlerContext channelHandlerContext,
                                                                PeerId senderPeerId,
                                                                ICorrelationId correlationId)
        {
            Guard.Argument(getFileFromDfsRequest, nameof(getFileFromDfsRequest)).NotNull();
            Guard.Argument(channelHandlerContext, nameof(channelHandlerContext)).NotNull();
            Guard.Argument(senderPeerId, nameof(senderPeerId)).NotNull();

            long fileLen = 0;

            FileTransferResponseCodeTypes responseCodeType;

            var response = Task.Run(async() =>
            {
                try
                {
                    responseCodeType = await Task.Run(async() =>
                    {
                        var stream = await _dfsService.UnixFsApi.ReadFileAsync(Cid.Decode(getFileFromDfsRequest.DfsHash))
                                     .ConfigureAwait(false);
                        fileLen = stream.Length;
                        using (var fileTransferInformation = new UploadFileTransferInformation(
                                   stream,
                                   senderPeerId,
                                   PeerSettings.PeerId,
                                   channelHandlerContext.Channel,
                                   correlationId
                                   ))
                        {
                            return(_fileTransferFactory.RegisterTransfer(fileTransferInformation));
                        }
                    }).ConfigureAwait(false);
                }
                catch (Exception e)
                {
                    Logger.Error(e,
                                 "Failed to handle GetFileFromDfsRequestHandler after receiving message {0}",
                                 getFileFromDfsRequest);
                    responseCodeType = FileTransferResponseCodeTypes.Error;
                }

                return(ReturnResponse(responseCodeType, fileLen));
            }).ConfigureAwait(false).GetAwaiter().GetResult();

            return(response);
        }
        public async Task Can_Upload_File(uint numberOfChunks)
        {
            var uploadFileInformation = Substitute.For <IUploadFileInformation>();
            var correlationId         = CorrelationId.GenerateCorrelationId();

            uploadFileInformation.MaxChunk.Returns(numberOfChunks);
            uploadFileInformation.CorrelationId.Returns(correlationId);

            _uploadFileTransferFactory.RegisterTransfer(uploadFileInformation);

            await _uploadFileTransferFactory.FileTransferAsync(correlationId, CancellationToken.None).ConfigureAwait(false);

            for (uint i = 0; i < numberOfChunks; i++)
            {
                uploadFileInformation.Received(1).GetUploadMessageDto(i);
                uploadFileInformation.Received(1).UpdateChunkIndicator(i, true);
            }

            await uploadFileInformation.RecipientChannel.ReceivedWithAnyArgs((int)numberOfChunks).WriteAndFlushAsync(default);
        public override void SendMessage(AddFileOptions options)
        {
            var request = GetMessage(options);

            using (var fileStream = File.Open(options.File, FileMode.Open))
            {
                request.FileSize = (ulong)fileStream.Length;
            }

            var protocolMessage = request.ToProtocolMessage(SenderPeerId);
            var requestMessage  = new MessageDto(
                protocolMessage,
                RecipientPeerId
                );

            IUploadFileInformation fileTransfer = new UploadFileTransferInformation(
                File.Open(options.File, FileMode.Open),
                SenderPeerId,
                RecipientPeerId,
                Target.Channel,
                requestMessage.CorrelationId);

            _uploadFileTransferFactory.RegisterTransfer(fileTransfer);

            Target.SendMessage(requestMessage);

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

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