예제 #1
0
        public void UploadFile(string localFilepath, string remoteFilepath)
        {
            remoteFilepath = Path.IsPathRooted(remoteFilepath) ? remoteFilepath : Path.Combine(CurrentFolder, remoteFilepath);

            FileInfo localFileInfo = new FileInfo(localFilepath);
            long     localFileSize = localFileInfo.Length;

            Stopwatch sw = Stopwatch.StartNew();

            using (ConsoleStatusWriter statusWriter = new ConsoleStatusWriter("Uploading...", localFileSize))
            {
                try
                {
                    int count  = 512 * 1024;
                    int offset = 0;

                    byte[] buffer;

                    using (FileStream fs = File.OpenRead(localFilepath))
                        using (BinaryReader br = new BinaryReader(fs))
                        {
                            do
                            {
                                buffer = br.ReadBytes(count);

                                AcknowledgementResponse response = remoteExecutor.Execute(new UploadFileRequest
                                {
                                    Path   = remoteFilepath,
                                    Data   = buffer,
                                    Offset = offset
                                });

                                offset += buffer.Length;
                                statusWriter.Update(buffer.Length);

                                if (!response.IsSuccessful)
                                {
                                    throw new CustomException("Error while uploading.");
                                }
                            }while (buffer.Length == count);
                        }
                }
                catch (IOException e)
                {
                    throw new CustomException(e.Message);
                }
            }
            sw.Stop();
            WriteStatistics(sw.ElapsedMilliseconds, localFileSize);
        }
예제 #2
0
        public void DownloadFile(string remoteFilepath, string localFilepath)
        {
            remoteFilepath = Path.IsPathRooted(remoteFilepath) ? remoteFilepath : Path.Combine(CurrentFolder, remoteFilepath);

            string remoteFilename  = Path.GetFileName(remoteFilepath);
            string remoteDirectory = remoteFilepath.Substring(0, remoteFilepath.Length - remoteFilename.Length);

            var lsResponse = remoteExecutor.Execute(new ListDirectoryClientRequest
            {
                Path = remoteDirectory,
                AuthenticationToken = authenticationToken
            });

            RemoteFile remoteFileInfo = lsResponse.RemoteFiles.FirstOrDefault(x => x.Filename == remoteFilename);

            if (remoteFileInfo == null)
            {
                throw new CustomException("File does not exist on the remote machine.");
            }

            int partLength = 512 * 1024;
            ParallelFileDownloader parallelFileDownloader = new ParallelFileDownloader(remoteFileInfo.Size, partLength);

            Stopwatch sw = Stopwatch.StartNew();

            using (ParallelFileWriter parallelFileWriter = new ParallelFileWriter(localFilepath))
                using (ConsoleStatusWriter statusWriter = new ConsoleStatusWriter("Downloading...", remoteFileInfo.Size))
                {
                    parallelFileDownloader.FilePartDownloadedEvent += (partNumber, data) => ParallelFileDownloader_FilePartDownloadedEvent(parallelFileWriter, statusWriter, partNumber, data);

                    IRemoteRequestExecutor[] executors = GetHelperRequestExecutors(4);

                    foreach (var helperExecutor in executors)
                    {
                        parallelFileDownloader.StartNew((offset, count) => helperExecutor.Execute(new DownloadFileRequest
                        {
                            AuthenticationToken = authenticationToken,
                            Filepath            = remoteFilepath,
                            Offset = offset,
                            Count  = count
                        }).Data);
                    }

                    parallelFileDownloader.Wait();
                }

            sw.Stop();
            WriteStatistics(sw.ElapsedMilliseconds, remoteFileInfo.Size);
        }
예제 #3
0
 private void ParallelFileDownloader_FilePartDownloadedEvent(ParallelFileWriter writer, ConsoleStatusWriter statusWriter, int partNumber, byte[] data)
 {
     writer.WritePart(partNumber, data);
     statusWriter.Update(data.Length);
 }