aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/PPC/Tango.PPC.Common/FileSystem/DefaultFileSystemService.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Software/Visual_Studio/PPC/Tango.PPC.Common/FileSystem/DefaultFileSystemService.cs')
-rw-r--r--Software/Visual_Studio/PPC/Tango.PPC.Common/FileSystem/DefaultFileSystemService.cs270
1 files changed, 260 insertions, 10 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 ffbba2e7a..a7f77855a 100644
--- a/Software/Visual_Studio/PPC/Tango.PPC.Common/FileSystem/DefaultFileSystemService.cs
+++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/FileSystem/DefaultFileSystemService.cs
@@ -7,11 +7,14 @@ using System.Text;
using System.Threading.Tasks;
using Tango.Core;
using Tango.Core.DI;
+using Tango.Core.IO;
using Tango.FileSystem;
using Tango.FileSystem.Network;
using Tango.Integration.ExternalBridge;
using Tango.PPC.Common.ExternalBridge;
using Tango.Transport;
+using Tango.Transport.Transporters;
+using Tango.WebRTC;
namespace Tango.PPC.Common.FileSystem
{
@@ -29,6 +32,8 @@ namespace Tango.PPC.Common.FileSystem
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)
{
@@ -40,16 +45,67 @@ namespace Tango.PPC.Common.FileSystem
private FileSystemManager _manager;
private Dictionary<String, FileSystemOperation> _operations;
+ private Dictionary<ExternalBridgeReceiver, BasicTransporter> _webRtcClients;
public bool Enabled { get; set; } = true;
+ public bool EnableWebRTC { get; set; } = true;
public DefaultFileSystemService(IPPCExternalBridgeService externalBridge)
{
+ _webRtcClients = new Dictionary<ExternalBridgeReceiver, BasicTransporter>();
_manager = new FileSystemManager();
_operations = new Dictionary<string, FileSystemOperation>();
externalBridge.RegisterRequestHandler(this);
}
+ [ExternalBridgeRequestHandlerMethod(typeof(InitWebRtcRequest))]
+ public async void OnInitWebRtcRequest(InitWebRtcRequest request, String token, ExternalBridgeReceiver receiver)
+ {
+ try
+ {
+ if (!EnableWebRTC)
+ {
+ await receiver.SendErrorResponse(new InvalidOperationException("The file system service WebRTC channel is disabled on this machine."), token);
+ return;
+ }
+
+ if (_webRtcClients.ContainsKey(receiver))
+ {
+ _webRtcClients[receiver].Dispose();
+ }
+
+ var webRtcAdapter = new WebRtcTransportAdapter(receiver, WebRtcTransportAdapterMode.Passive, request.DataChannelName);
+ webRtcAdapter.Ready += (x, e) =>
+ {
+ LogManager.Log("File System via WebRTC is ready.");
+ };
+
+ BasicTransporter webRtcTransporter = new BasicTransporter(webRtcAdapter);
+ webRtcTransporter = new BasicTransporter(webRtcAdapter);
+ webRtcTransporter.ComponentName = "File System Passive WebRTC Transporter";
+ webRtcTransporter.UseKeepAlive = false;
+ webRtcTransporter.RegisterRequestHandler<ChunkDownloadRequest>(WebRtcChunkDownloadRequestReceived);
+ webRtcTransporter.RegisterRequestHandler<ChunkUploadRequest>(WebRtcChunkUploadRequestReceived);
+ await webRtcTransporter.Connect();
+ await receiver.SendGenericResponse(new InitWebRtcResponse(), token);
+ _webRtcClients[receiver] = webRtcTransporter;
+ }
+ catch (Exception ex)
+ {
+ await receiver.SendErrorResponse(ex, token);
+ }
+ }
+
+ private void WebRtcChunkDownloadRequestReceived(ITransporter transporter, ChunkDownloadRequest request, string token)
+ {
+ OnChunkDownloadRequest(request, token, transporter);
+ }
+
+ private void WebRtcChunkUploadRequestReceived(ITransporter transporter, ChunkUploadRequest request, string token)
+ {
+ OnChunkUploadRequest(request, token, transporter);
+ }
+
[ExternalBridgeRequestHandlerMethod(typeof(GetFileSystemItemRequest))]
public async void OnGetFileSystemItemRequest(GetFileSystemItemRequest request, String token, ExternalBridgeReceiver receiver)
{
@@ -69,9 +125,10 @@ namespace Tango.PPC.Common.FileSystem
{
try
{
- using (var stream = new FileStream(request.Path, FileMode.Create)) { }
+ var tempFile = TemporaryManager.CreateFile();
+ using (var stream = new FileStream(tempFile, FileMode.Create)) { }
- FileSystemOperation operation = new FileSystemOperation(FileSystemOperationMode.Upload, request.Path);
+ FileSystemOperation operation = new FileSystemOperation(FileSystemOperationMode.Upload, tempFile) { UploadPostPath = request.Path };
_operations.Add(operation.Id, operation);
await receiver.SendGenericResponse(new FileUploadResponse() { OperationId = operation.Id }, token);
@@ -82,6 +139,25 @@ namespace Tango.PPC.Common.FileSystem
}
}
+ [ExternalBridgeRequestHandlerMethod(typeof(FolderUploadRequest))]
+ public async void OnFolderUploadRequest(FolderUploadRequest request, String token, ExternalBridgeReceiver receiver)
+ {
+ try
+ {
+ var tempFile = TemporaryManager.CreateFile();
+ using (var stream = new FileStream(tempFile, FileMode.Create)) { }
+
+ 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);
+ }
+ catch (Exception ex)
+ {
+ await receiver.SendErrorResponse(ex, token);
+ }
+ }
+
[ExternalBridgeRequestHandlerMethod(typeof(FileDownloadRequest))]
public async void OnFileDownloadRequest(FileDownloadRequest request, String token, ExternalBridgeReceiver receiver)
{
@@ -109,8 +185,40 @@ namespace Tango.PPC.Common.FileSystem
}
}
+ [ExternalBridgeRequestHandlerMethod(typeof(FolderDownloadRequest))]
+ public async void OnFolderDownloadRequest(FolderDownloadRequest request, String token, ExternalBridgeReceiver receiver)
+ {
+ try
+ {
+ if (!Directory.Exists(request.Path))
+ {
+ await receiver.SendErrorResponse(new FileNotFoundException("Could not find the specified directory."), token);
+ return;
+ }
+
+ var tempFile = TemporaryManager.CreateImaginaryFile();
+
+ ZipFile.CreateFromDirectory(request.Path, tempFile);
+
+ FileSystemOperation operation = new FileSystemOperation(FileSystemOperationMode.Download, tempFile);
+ operation.IsPathTempZip = true;
+
+ _operations.Add(operation.Id, operation);
+
+ await receiver.SendGenericResponse(new FolderDownloadResponse()
+ {
+ OperationId = operation.Id,
+ Length = new FileInfo(tempFile).Length
+ }, token);
+ }
+ catch (Exception ex)
+ {
+ await receiver.SendErrorResponse(ex, token);
+ }
+ }
+
[ExternalBridgeRequestHandlerMethod(typeof(ChunkUploadRequest))]
- public async void OnChunkUploadRequest(ChunkUploadRequest request, String token, ExternalBridgeReceiver receiver)
+ public async void OnChunkUploadRequest(ChunkUploadRequest request, String token, ITransporter receiver)
{
try
{
@@ -128,7 +236,33 @@ namespace Tango.PPC.Common.FileSystem
stream.Write(request.Data, 0, request.Data.Length);
}
- await receiver.SendGenericResponse(new ChunkUploadResponse(), token);
+ if (request.IsCompleted)
+ {
+ if (!operation.IsPathTempZip)
+ {
+ File.Copy(operation.Path, operation.UploadPostPath, true);
+ try
+ {
+ File.Delete(operation.Path);
+ }
+ catch { }
+ }
+ 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 { }
+ }
+ }
+
+ await receiver.SendGenericResponse(new ChunkUploadResponse(), token, new TransportResponseConfig() { Priority = QueuePriority.Low });
}
catch (Exception ex)
{
@@ -137,7 +271,7 @@ namespace Tango.PPC.Common.FileSystem
}
[ExternalBridgeRequestHandlerMethod(typeof(ChunkDownloadRequest))]
- public async void OnChunkDownloadRequest(ChunkDownloadRequest request, String token, ExternalBridgeReceiver receiver)
+ public async void OnChunkDownloadRequest(ChunkDownloadRequest request, String token, ITransporter receiver)
{
FileSystemOperation operation;
_operations.TryGetValue(request.OperationId, out operation);
@@ -148,15 +282,46 @@ namespace Tango.PPC.Common.FileSystem
return;
}
- using (FileStream stream = new FileStream(operation.Path, FileMode.Open))
+ FileStream stream = null;
+ bool removeTempZipFile = false;
+
+ try
{
+ stream = new FileStream(operation.Path, FileMode.Open);
stream.Position = request.Position;
byte[] data = new byte[Math.Min(request.MaxChunkSize, stream.Length - stream.Position)];
+
+ if (stream.Position + data.Length == stream.Length)
+ {
+ removeTempZipFile = true;
+ }
+
await stream.ReadAsync(data, 0, data.Length);
+ stream.Dispose();
+ stream = null;
await receiver.SendGenericResponse(new ChunkDownloadResponse()
{
Data = data
- }, token);
+ }, token, new TransportResponseConfig() { Priority = QueuePriority.Low });
+ }
+ catch (Exception ex)
+ {
+ stream?.Dispose();
+ await receiver.SendErrorResponse(ex, token);
+ }
+ finally
+ {
+ if (operation.IsPathTempZip && removeTempZipFile)
+ {
+ try
+ {
+ if (File.Exists(operation.Path))
+ {
+ File.Delete(operation.Path);
+ }
+ }
+ catch { }
+ }
}
}
@@ -180,9 +345,12 @@ namespace Tango.PPC.Common.FileSystem
{
File.Delete(operation.Path);
}
- else if (Directory.Exists(operation.Path))
+ }
+ else if (operation.IsPathTempZip)
+ {
+ if (File.Exists(operation.Path))
{
- Directory.Delete(operation.Path, true);
+ File.Delete(operation.Path);
}
}
@@ -194,9 +362,91 @@ namespace Tango.PPC.Common.FileSystem
}
}
- public void OnReceiverDisconnected(ExternalBridgeReceiver receiver)
+ [ExternalBridgeRequestHandlerMethod(typeof(MoveRequest))]
+ public async void 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);
+ }
+ }
+
+ [ExternalBridgeRequestHandlerMethod(typeof(CopyRequest))]
+ public async void 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);
+ }
+ }
+ [ExternalBridgeRequestHandlerMethod(typeof(DeleteRequest))]
+ public async void 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);
+ }
+ }
+
+ [ExternalBridgeRequestHandlerMethod(typeof(CreateFolderRequest))]
+ public async void 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);
+ }
+ }
+
+ [ExternalBridgeRequestHandlerMethod(typeof(PerformDiskSpaceOptimizationRequest))]
+ public async void 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);
+ }
+ }
+
+ public void OnReceiverDisconnected(ExternalBridgeReceiver receiver)
+ {
+ if (_webRtcClients.ContainsKey(receiver))
+ {
+ try
+ {
+ var webRtcTransporter = _webRtcClients[receiver];
+ _webRtcClients.Remove(receiver);
+ webRtcTransporter.Dispose();
+ }
+ catch (Exception ex)
+ {
+ LogManager.Log(ex, "Error disposing the WebRTC transporter.");
+ }
+ }
}
}
}