diff options
Diffstat (limited to 'Software/Visual_Studio/PPC/Tango.PPC.Common/FileSystem')
4 files changed, 229 insertions, 229 deletions
diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/FileSystem/DefaultFileSystemService.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/FileSystem/DefaultFileSystemService.cs index a7f77855a..02975a2b3 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.Common/FileSystem/DefaultFileSystemService.cs +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/FileSystem/DefaultFileSystemService.cs @@ -18,31 +18,15 @@ using Tango.WebRTC; namespace Tango.PPC.Common.FileSystem { + /// <summary> + /// Represents the <see cref="IFileSystemService"/> default implementation. + /// </summary> + /// <seealso cref="Tango.Core.ExtendedObject" /> + /// <seealso cref="Tango.PPC.Common.FileSystem.IFileSystemService" /> + /// <seealso cref="Tango.Integration.ExternalBridge.IExternalBridgeRequestHandler" /> [TangoCreateWhenRegistered] public class DefaultFileSystemService : ExtendedObject, IFileSystemService, IExternalBridgeRequestHandler { - private enum FileSystemOperationMode - { - Upload, - Download - } - - private class FileSystemOperation - { - public FileSystemOperationMode Mode { get; set; } - public String Id { get; set; } - public String Path { get; set; } - public bool IsPathTempZip { get; set; } - public String UploadPostPath { get; set; } - - public FileSystemOperation(FileSystemOperationMode mode, String path) - { - Mode = mode; - Id = Guid.NewGuid().ToString(); - Path = path; - } - } - private FileSystemManager _manager; private Dictionary<String, FileSystemOperation> _operations; private Dictionary<ExternalBridgeReceiver, BasicTransporter> _webRtcClients; @@ -58,9 +42,11 @@ namespace Tango.PPC.Common.FileSystem externalBridge.RegisterRequestHandler(this); } - [ExternalBridgeRequestHandlerMethod(typeof(InitWebRtcRequest))] - public async void OnInitWebRtcRequest(InitWebRtcRequest request, String token, ExternalBridgeReceiver receiver) + [ExternalBridgeRequestHandlerMethod(typeof(InitWebRtcRequest), RequestHandlerLoggingMode.LogRequestNameAndContent)] + public async Task OnInitWebRtcRequest(InitWebRtcRequest request, String token, ExternalBridgeReceiver receiver) { + this.ThrowIfDisabled(); + try { if (!EnableWebRTC) @@ -69,15 +55,18 @@ namespace Tango.PPC.Common.FileSystem return; } + LogManager.Log("Initializing WebRTC channel for file system service."); + if (_webRtcClients.ContainsKey(receiver)) { _webRtcClients[receiver].Dispose(); } + LogManager.Log("Initializing WebRTC transport adapter on 'Passive' mode."); var webRtcAdapter = new WebRtcTransportAdapter(receiver, WebRtcTransportAdapterMode.Passive, request.DataChannelName); webRtcAdapter.Ready += (x, e) => { - LogManager.Log("File System via WebRTC is ready."); + LogManager.Log("The file system service WebRTC channel is ready."); }; BasicTransporter webRtcTransporter = new BasicTransporter(webRtcAdapter); @@ -87,199 +76,171 @@ namespace Tango.PPC.Common.FileSystem webRtcTransporter.RegisterRequestHandler<ChunkDownloadRequest>(WebRtcChunkDownloadRequestReceived); webRtcTransporter.RegisterRequestHandler<ChunkUploadRequest>(WebRtcChunkUploadRequestReceived); await webRtcTransporter.Connect(); + + LogManager.Log("Sending WebRTC initialization response..."); + await receiver.SendGenericResponse(new InitWebRtcResponse(), token); _webRtcClients[receiver] = webRtcTransporter; } catch (Exception ex) { + LogManager.Log(ex, "Error initializing WebRTC channel for file system service."); await receiver.SendErrorResponse(ex, token); } } - private void WebRtcChunkDownloadRequestReceived(ITransporter transporter, ChunkDownloadRequest request, string token) + private async void WebRtcChunkDownloadRequestReceived(ITransporter transporter, ChunkDownloadRequest request, string token) { - OnChunkDownloadRequest(request, token, transporter); + await OnChunkDownloadRequest(request, token, transporter); } - private void WebRtcChunkUploadRequestReceived(ITransporter transporter, ChunkUploadRequest request, string token) + private async void WebRtcChunkUploadRequestReceived(ITransporter transporter, ChunkUploadRequest request, string token) { - OnChunkUploadRequest(request, token, transporter); + await OnChunkUploadRequest(request, token, transporter); } - [ExternalBridgeRequestHandlerMethod(typeof(GetFileSystemItemRequest))] - public async void OnGetFileSystemItemRequest(GetFileSystemItemRequest request, String token, ExternalBridgeReceiver receiver) + [ExternalBridgeRequestHandlerMethod(typeof(GetFileSystemItemRequest), RequestHandlerLoggingMode.LogRequestNameAndContent)] + public async Task OnGetFileSystemItemRequest(GetFileSystemItemRequest request, String token, ExternalBridgeReceiver receiver) { - try - { - FileSystemItemDTO dto = _manager.GetFolder(request); - await receiver.SendGenericResponse(new GetFileSystemItemResponse() { FileSystemItem = dto }, token); - } - catch (Exception ex) - { - await receiver.SendErrorResponse(ex, token); - } + this.ThrowIfDisabled(); + + FileSystemItemDTO dto = _manager.GetFolder(request); + await receiver.SendGenericResponse(new GetFileSystemItemResponse() { FileSystemItem = dto }, token); } - [ExternalBridgeRequestHandlerMethod(typeof(FileUploadRequest))] - public async void OnFileUploadRequest(FileUploadRequest request, String token, ExternalBridgeReceiver receiver) + [ExternalBridgeRequestHandlerMethod(typeof(FileUploadRequest), RequestHandlerLoggingMode.LogRequestNameAndContent)] + public async Task OnFileUploadRequest(FileUploadRequest request, String token, ExternalBridgeReceiver receiver) { - try - { - var tempFile = TemporaryManager.CreateFile(); - using (var stream = new FileStream(tempFile, FileMode.Create)) { } + this.ThrowIfDisabled(); - FileSystemOperation operation = new FileSystemOperation(FileSystemOperationMode.Upload, tempFile) { UploadPostPath = request.Path }; - _operations.Add(operation.Id, operation); + var tempFile = TemporaryManager.CreateFile(); + using (var stream = new FileStream(tempFile, FileMode.Create)) { } - await receiver.SendGenericResponse(new FileUploadResponse() { OperationId = operation.Id }, token); - } - catch (Exception ex) - { - await receiver.SendErrorResponse(ex, token); - } + FileSystemOperation operation = new FileSystemOperation(FileSystemOperationMode.Upload, tempFile) { UploadPostPath = request.Path }; + _operations.Add(operation.Id, operation); + + await receiver.SendGenericResponse(new FileUploadResponse() { OperationId = operation.Id }, token); } - [ExternalBridgeRequestHandlerMethod(typeof(FolderUploadRequest))] - public async void OnFolderUploadRequest(FolderUploadRequest request, String token, ExternalBridgeReceiver receiver) + [ExternalBridgeRequestHandlerMethod(typeof(FolderUploadRequest), RequestHandlerLoggingMode.LogRequestNameAndContent)] + public async Task OnFolderUploadRequest(FolderUploadRequest request, String token, ExternalBridgeReceiver receiver) { - try - { - var tempFile = TemporaryManager.CreateFile(); - using (var stream = new FileStream(tempFile, FileMode.Create)) { } + this.ThrowIfDisabled(); - FileSystemOperation operation = new FileSystemOperation(FileSystemOperationMode.Upload, tempFile) { UploadPostPath = request.Path, IsPathTempZip = true }; - _operations.Add(operation.Id, operation); + var tempFile = TemporaryManager.CreateFile(); + using (var stream = new FileStream(tempFile, FileMode.Create)) { } - await receiver.SendGenericResponse(new FolderUploadResponse() { OperationId = operation.Id }, token); - } - catch (Exception ex) - { - await receiver.SendErrorResponse(ex, token); - } + FileSystemOperation operation = new FileSystemOperation(FileSystemOperationMode.Upload, tempFile) { UploadPostPath = request.Path, IsPathTempZip = true }; + _operations.Add(operation.Id, operation); + + await receiver.SendGenericResponse(new FolderUploadResponse() { OperationId = operation.Id }, token); } - [ExternalBridgeRequestHandlerMethod(typeof(FileDownloadRequest))] - public async void OnFileDownloadRequest(FileDownloadRequest request, String token, ExternalBridgeReceiver receiver) + [ExternalBridgeRequestHandlerMethod(typeof(FileDownloadRequest), RequestHandlerLoggingMode.LogRequestNameAndContent)] + public async Task OnFileDownloadRequest(FileDownloadRequest request, String token, ExternalBridgeReceiver receiver) { - try + this.ThrowIfDisabled(); + + if (!File.Exists(request.Path)) { - if (!File.Exists(request.Path)) - { - await receiver.SendErrorResponse(new FileNotFoundException("Could not find the specified file."), token); - return; - } + throw new FileNotFoundException("Could not find the specified file."); + } - FileSystemOperation operation = new FileSystemOperation(FileSystemOperationMode.Download, request.Path); + FileSystemOperation operation = new FileSystemOperation(FileSystemOperationMode.Download, request.Path); - _operations.Add(operation.Id, operation); + _operations.Add(operation.Id, operation); - await receiver.SendGenericResponse(new FileDownloadResponse() - { - OperationId = operation.Id, - Length = new FileInfo(request.Path).Length - }, token); - } - catch (Exception ex) + await receiver.SendGenericResponse(new FileDownloadResponse() { - await receiver.SendErrorResponse(ex, token); - } + OperationId = operation.Id, + Length = new FileInfo(request.Path).Length + }, token); } - [ExternalBridgeRequestHandlerMethod(typeof(FolderDownloadRequest))] - public async void OnFolderDownloadRequest(FolderDownloadRequest request, String token, ExternalBridgeReceiver receiver) + [ExternalBridgeRequestHandlerMethod(typeof(FolderDownloadRequest), RequestHandlerLoggingMode.LogRequestNameAndContent)] + public async Task OnFolderDownloadRequest(FolderDownloadRequest request, String token, ExternalBridgeReceiver receiver) { - try + this.ThrowIfDisabled(); + + if (!Directory.Exists(request.Path)) { - if (!Directory.Exists(request.Path)) - { - await receiver.SendErrorResponse(new FileNotFoundException("Could not find the specified directory."), token); - return; - } + throw new FileNotFoundException("Could not find the specified directory."); + } - var tempFile = TemporaryManager.CreateImaginaryFile(); + var tempFile = TemporaryManager.CreateImaginaryFile(); - ZipFile.CreateFromDirectory(request.Path, tempFile); + ZipFile.CreateFromDirectory(request.Path, tempFile); - FileSystemOperation operation = new FileSystemOperation(FileSystemOperationMode.Download, tempFile); - operation.IsPathTempZip = true; + FileSystemOperation operation = new FileSystemOperation(FileSystemOperationMode.Download, tempFile); + operation.IsPathTempZip = true; - _operations.Add(operation.Id, operation); + _operations.Add(operation.Id, operation); - await receiver.SendGenericResponse(new FolderDownloadResponse() - { - OperationId = operation.Id, - Length = new FileInfo(tempFile).Length - }, token); - } - catch (Exception ex) + await receiver.SendGenericResponse(new FolderDownloadResponse() { - await receiver.SendErrorResponse(ex, token); - } + OperationId = operation.Id, + Length = new FileInfo(tempFile).Length + }, token); } [ExternalBridgeRequestHandlerMethod(typeof(ChunkUploadRequest))] - public async void OnChunkUploadRequest(ChunkUploadRequest request, String token, ITransporter receiver) + public async Task OnChunkUploadRequest(ChunkUploadRequest request, String token, ITransporter receiver) { - try + this.ThrowIfDisabled(); + + FileSystemOperation operation; + _operations.TryGetValue(request.OperationId, out operation); + + if (operation == null) { - FileSystemOperation operation; - _operations.TryGetValue(request.OperationId, out operation); + throw new ArgumentException("Invalid operation id."); + } - if (operation == null) - { - await receiver.SendErrorResponse(new ArgumentException("Invalid operation id."), token); - return; - } + using (var stream = new FileStream(operation.Path, FileMode.Append)) + { + stream.Write(request.Data, 0, request.Data.Length); + } - using (var stream = new FileStream(operation.Path, FileMode.Append)) + if (request.IsCompleted) + { + if (!operation.IsPathTempZip) { - stream.Write(request.Data, 0, request.Data.Length); + File.Copy(operation.Path, operation.UploadPostPath, true); + try + { + File.Delete(operation.Path); + } + catch { } } - - if (request.IsCompleted) + else { - if (!operation.IsPathTempZip) + using (Ionic.Zip.ZipFile zip = new Ionic.Zip.ZipFile(operation.Path)) { - File.Copy(operation.Path, operation.UploadPostPath, true); - try - { - File.Delete(operation.Path); - } - catch { } + zip.ExtractAll(operation.UploadPostPath, Ionic.Zip.ExtractExistingFileAction.OverwriteSilently); } - else - { - using (Ionic.Zip.ZipFile zip = new Ionic.Zip.ZipFile(operation.Path)) - { - zip.ExtractAll(operation.UploadPostPath, Ionic.Zip.ExtractExistingFileAction.OverwriteSilently); - } - try - { - File.Delete(operation.Path); - } - catch { } + try + { + File.Delete(operation.Path); } + catch { } } - - await receiver.SendGenericResponse(new ChunkUploadResponse(), token, new TransportResponseConfig() { Priority = QueuePriority.Low }); - } - catch (Exception ex) - { - await receiver.SendErrorResponse(ex, token); } + + await receiver.SendGenericResponse(new ChunkUploadResponse(), token, new TransportResponseConfig() { Priority = QueuePriority.Low }); } [ExternalBridgeRequestHandlerMethod(typeof(ChunkDownloadRequest))] - public async void OnChunkDownloadRequest(ChunkDownloadRequest request, String token, ITransporter receiver) + public async Task OnChunkDownloadRequest(ChunkDownloadRequest request, String token, ITransporter receiver) { + this.ThrowIfDisabled(); + FileSystemOperation operation; _operations.TryGetValue(request.OperationId, out operation); if (operation == null) { - await receiver.SendErrorResponse(new ArgumentException("Invalid operation id."), token); - return; + throw new ArgumentException("Invalid operation id."); } FileStream stream = null; @@ -307,7 +268,7 @@ namespace Tango.PPC.Common.FileSystem catch (Exception ex) { stream?.Dispose(); - await receiver.SendErrorResponse(ex, token); + throw ex; } finally { @@ -325,111 +286,78 @@ namespace Tango.PPC.Common.FileSystem } } - [ExternalBridgeRequestHandlerMethod(typeof(AbortOperationRequest))] - public async void OnAbortOperationRequest(AbortOperationRequest request, String token, ExternalBridgeReceiver receiver) + [ExternalBridgeRequestHandlerMethod(typeof(AbortOperationRequest), RequestHandlerLoggingMode.LogRequestNameAndContent)] + public async Task OnAbortOperationRequest(AbortOperationRequest request, String token, ExternalBridgeReceiver receiver) { + this.ThrowIfDisabled(); + FileSystemOperation operation; _operations.TryGetValue(request.OperationId, out operation); if (operation == null) { - await receiver.SendErrorResponse(new ArgumentException("Invalid operation id."), token); - return; + throw new ArgumentException("Invalid operation id."); } - try + if (operation.Mode == FileSystemOperationMode.Upload) { - if (operation.Mode == FileSystemOperationMode.Upload) + if (File.Exists(operation.Path)) { - if (File.Exists(operation.Path)) - { - File.Delete(operation.Path); - } + File.Delete(operation.Path); } - else if (operation.IsPathTempZip) - { - if (File.Exists(operation.Path)) - { - File.Delete(operation.Path); - } - } - - await receiver.SendGenericResponse(new AbortOperationResponse(), token); } - catch (Exception ex) + else if (operation.IsPathTempZip) { - await receiver.SendErrorResponse(ex, token); + if (File.Exists(operation.Path)) + { + File.Delete(operation.Path); + } } + + await receiver.SendGenericResponse(new AbortOperationResponse(), token); } - [ExternalBridgeRequestHandlerMethod(typeof(MoveRequest))] - public async void OnMoveRequest(MoveRequest request, String token, ExternalBridgeReceiver receiver) + [ExternalBridgeRequestHandlerMethod(typeof(MoveRequest), RequestHandlerLoggingMode.LogRequestNameAndContent)] + public async Task OnMoveRequest(MoveRequest request, String token, ExternalBridgeReceiver receiver) { - try - { - _manager.Move(request); - await receiver.SendGenericResponse(new MoveResponse(), token); - } - catch (Exception ex) - { - await receiver.SendErrorResponse(ex, token); - } + this.ThrowIfDisabled(); + + _manager.Move(request); + await receiver.SendGenericResponse(new MoveResponse(), token); } - [ExternalBridgeRequestHandlerMethod(typeof(CopyRequest))] - public async void OnCopyRequest(CopyRequest request, String token, ExternalBridgeReceiver receiver) + [ExternalBridgeRequestHandlerMethod(typeof(CopyRequest), RequestHandlerLoggingMode.LogRequestNameAndContent)] + public async Task OnCopyRequest(CopyRequest request, String token, ExternalBridgeReceiver receiver) { - try - { - _manager.Copy(request); - await receiver.SendGenericResponse(new CopyResponse(), token); - } - catch (Exception ex) - { - await receiver.SendErrorResponse(ex, token); - } + this.ThrowIfDisabled(); + + _manager.Copy(request); + await receiver.SendGenericResponse(new CopyResponse(), token); } - [ExternalBridgeRequestHandlerMethod(typeof(DeleteRequest))] - public async void OnDeleteRequest(DeleteRequest request, String token, ExternalBridgeReceiver receiver) + [ExternalBridgeRequestHandlerMethod(typeof(DeleteRequest), RequestHandlerLoggingMode.LogRequestNameAndContent)] + public async Task OnDeleteRequest(DeleteRequest request, String token, ExternalBridgeReceiver receiver) { - try - { - _manager.Delete(request.Path); - await receiver.SendGenericResponse(new DeleteResponse(), token); - } - catch (Exception ex) - { - await receiver.SendErrorResponse(ex, token); - } + this.ThrowIfDisabled(); + + _manager.Delete(request.Path); + await receiver.SendGenericResponse(new DeleteResponse(), token); } - [ExternalBridgeRequestHandlerMethod(typeof(CreateFolderRequest))] - public async void OnCreateFolderRequest(CreateFolderRequest request, String token, ExternalBridgeReceiver receiver) + [ExternalBridgeRequestHandlerMethod(typeof(CreateFolderRequest), RequestHandlerLoggingMode.LogRequestNameAndContent)] + public async Task OnCreateFolderRequest(CreateFolderRequest request, String token, ExternalBridgeReceiver receiver) { - try - { - var dto = _manager.CreateFolder(request.Path, request.FolderName); - await receiver.SendGenericResponse(new CreateFolderResponse() { FolderItem = dto }, token); - } - catch (Exception ex) - { - await receiver.SendErrorResponse(ex, token); - } + this.ThrowIfDisabled(); + + var dto = _manager.CreateFolder(request.Path, request.FolderName); + await receiver.SendGenericResponse(new CreateFolderResponse() { FolderItem = dto }, token); } - [ExternalBridgeRequestHandlerMethod(typeof(PerformDiskSpaceOptimizationRequest))] - public async void OnPerformDiskSpaceOptimizationRequest(PerformDiskSpaceOptimizationRequest request, String token, ExternalBridgeReceiver receiver) + [ExternalBridgeRequestHandlerMethod(typeof(PerformDiskSpaceOptimizationRequest), RequestHandlerLoggingMode.LogRequestNameAndContent)] + public async Task OnPerformDiskSpaceOptimizationRequest(PerformDiskSpaceOptimizationRequest request, String token, ExternalBridgeReceiver receiver) { - try - { - var deletedBytes = _manager.PerformDiskSpaceOptimization(); - await receiver.SendGenericResponse(new PerformDiskSpaceOptimizationResponse() { DeletedBytes = deletedBytes }, token); - } - catch (Exception ex) - { - await receiver.SendErrorResponse(ex, token); - } + var deletedBytes = _manager.PerformDiskSpaceOptimization(); + await receiver.SendGenericResponse(new PerformDiskSpaceOptimizationResponse() { DeletedBytes = deletedBytes }, token); } public void OnReceiverDisconnected(ExternalBridgeReceiver receiver) @@ -438,13 +366,14 @@ namespace Tango.PPC.Common.FileSystem { try { + LogManager.Log("External bridge receiver disconnected. Disposing file system service WebRTC channel..."); var webRtcTransporter = _webRtcClients[receiver]; _webRtcClients.Remove(receiver); webRtcTransporter.Dispose(); } catch (Exception ex) { - LogManager.Log(ex, "Error disposing the WebRTC transporter."); + LogManager.Log(ex, "Error disposing the WebRTC channel."); } } } diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/FileSystem/FileSystemOperation.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/FileSystem/FileSystemOperation.cs new file mode 100644 index 000000000..9fba7a874 --- /dev/null +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/FileSystem/FileSystemOperation.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Tango.PPC.Common.FileSystem +{ + /// <summary> + /// Represents an active file system file/folder download/upload operation + /// </summary> + public class FileSystemOperation + { + /// <summary> + /// Gets or sets the operation mode. + /// </summary> + public FileSystemOperationMode Mode { get; set; } + + /// <summary> + /// Gets or sets the operation identifier. + /// </summary> + public String Id { get; set; } + + /// <summary> + /// Gets or sets the path for the operation. + /// </summary> + public String Path { get; set; } + + /// <summary> + /// Should be set to true when the <see cref="Path"/> is a temporary zip file when performing download of a folder. + /// </summary> + public bool IsPathTempZip { get; set; } + + /// <summary> + /// Gets or sets the actual path to extract the zip file when uploading folder. + /// </summary> + public String UploadPostPath { get; set; } + + /// <summary> + /// Initializes a new instance of the <see cref="FileSystemOperation"/> class. + /// </summary> + /// <param name="mode">The mode.</param> + /// <param name="path">The path.</param> + public FileSystemOperation(FileSystemOperationMode mode, String path) + { + Mode = mode; + Id = Guid.NewGuid().ToString(); + Path = path; + } + } +} diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/FileSystem/FileSystemOperationMode.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/FileSystem/FileSystemOperationMode.cs new file mode 100644 index 000000000..e28843bce --- /dev/null +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/FileSystem/FileSystemOperationMode.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Tango.PPC.Common.FileSystem +{ + public enum FileSystemOperationMode + { + Upload, + Download + } +} diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/FileSystem/IFileSystemService.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/FileSystem/IFileSystemService.cs index 6cf3321a3..7a80db9c7 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.Common/FileSystem/IFileSystemService.cs +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/FileSystem/IFileSystemService.cs @@ -6,9 +6,15 @@ using System.Threading.Tasks; namespace Tango.PPC.Common.FileSystem { - public interface IFileSystemService + /// <summary> + /// Represents a PPC file system remote service. + /// </summary> + /// <seealso cref="Tango.PPC.Common.IPPCService" /> + public interface IFileSystemService : IPPCService { - bool Enabled { get; set; } + /// <summary> + /// Gets or sets a value indicating whether to enable the WebRTC transport channel. + /// </summary> bool EnableWebRTC { get; set; } } } |
