diff --git a/FileCloner/Models/NetworkService/Client.cs b/FileCloner/Models/NetworkService/Client.cs index 6da0006e..8ca6b02c 100644 --- a/FileCloner/Models/NetworkService/Client.cs +++ b/FileCloner/Models/NetworkService/Client.cs @@ -18,6 +18,7 @@ using System.Text; using System.Diagnostics.CodeAnalysis; using FileCloner.FileClonerLogging; +using GrpcClient; namespace FileCloner.Models.NetworkService; @@ -100,9 +101,9 @@ public void SendRequest() [ExcludeFromCodeCoverage] public void SendSummary() { - foreach (string responder in _responders) + try { - try + foreach (string responder in _responders) { // Generate path for the summary file specific to each responder string filePath = Path.Combine(Constants.SenderFilesFolderPath, $"{responder}.txt"); @@ -128,11 +129,11 @@ public void SendSummary() _logAction?.Invoke($"[Client] Summary Sent to {responder}"); _logger.Log($"Summary sent to {responder}"); } - catch (Exception ex) - { - _logAction?.Invoke($"[Client] Summary not sent to {responder} : {ex.Message}"); - _logger.Log($"[Client] Failed to send summary to {responder} : {ex.Message}", isErrorMessage: true); - } + } + catch (Exception ex) + { + _logAction?.Invoke($"[Client] Summary not sent: {ex.Message}"); + _logger.Log($"[Client] Failed to send summary to: {ex.Message}", isErrorMessage: true); } } @@ -197,16 +198,16 @@ public void SendFileForCloning(string from, string path, string requesterPath) [ExcludeFromCodeCoverage] public void SendFilesInChunks(string from, string path, string requesterPath) { - using FileStream fileStream = new(path, FileMode.Open, FileAccess.Read); - FileInfo fileInfo = new(path); - long fileSizeInBytes = fileInfo.Length; - int bufferSize = fileSizeInBytes < Constants.FileChunkSize ? (int)fileSizeInBytes : Constants.FileChunkSize; - byte[] buffer = new byte[bufferSize]; - int bytesRead = 0; - int indexOfChunkBeingSent = Constants.ChunkStartIndex; - try { + using FileStream fileStream = new(path, FileMode.Open, FileAccess.Read); + FileInfo fileInfo = new(path); + long fileSizeInBytes = fileInfo.Length; + int bufferSize = fileSizeInBytes < Constants.FileChunkSize ? (int)fileSizeInBytes : Constants.FileChunkSize; + byte[] buffer = new byte[bufferSize]; + int bytesRead = 0; + int indexOfChunkBeingSent = Constants.ChunkStartIndex; + while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) > 0) { if (bytesRead < buffer.Length) @@ -236,10 +237,10 @@ public void SendFilesInChunks(string from, string path, string requesterPath) catch (Exception ex) { _logAction?.Invoke( - $"[Client] Exception occured while sending {indexOfChunkBeingSent} : {ex.Message}" + $"[Client] Exception occured while sending chunk : {ex.Message}" ); _logger.Log( - $"[Client] Exception occured while sending {indexOfChunkBeingSent} : {ex.Message}", + $"[Client] Exception occured while sending chunk : {ex.Message}", isErrorMessage: true ); } @@ -264,32 +265,40 @@ public void StopCloning() [ExcludeFromCodeCoverage] public void OnDataReceived(string serializedData) { - Message data = _serializer.Deserialize(serializedData); - string subject = data.Subject; - string from = data.From; - _logger.Log($"Data received from {data.From} with subject {data.Subject}"); - - // Prevent processing self-sent messages - if (from != Constants.IPAddress || s_requestID != data.RequestID) + try { - //logAction?.Invoke($"[Client] Received {subject} from {from}"); + Message data = _serializer.Deserialize(serializedData); + string subject = data.Subject; + string from = data.From; + _logger.Log($"Data received from {data.From} with subject {data.Subject}"); - switch (subject) + // Prevent processing self-sent messages + if (from != Constants.IPAddress || s_requestID != data.RequestID) { - case Constants.Request: - SendResponse(data); - break; - case Constants.Response: - OnResponseReceived(data); - break; - case Constants.Summary: - OnSummaryReceived(data); - break; - case Constants.Cloning: - OnFileForCloningReceived(data); - break; + //logAction?.Invoke($"[Client] Received {subject} from {from}"); + + switch (subject) + { + case Constants.Request: + SendResponse(data); + break; + case Constants.Response: + OnResponseReceived(data); + break; + case Constants.Summary: + OnSummaryReceived(data); + break; + case Constants.Cloning: + OnFileForCloningReceived(data); + break; + } } } + catch (Exception ex) + { + _logAction?.Invoke($"[Client] Exception occured while receiving data: {ex.Message}"); + _logger.Log($"[Client] Exception occured while receiving data: {ex.Message}", isErrorMessage: true); + } } /// diff --git a/FileCloner/ViewModels/MainPageViewModel.FileWatcher.cs b/FileCloner/ViewModels/MainPageViewModel.FileWatcher.cs index e367586e..e1bc31a6 100644 --- a/FileCloner/ViewModels/MainPageViewModel.FileWatcher.cs +++ b/FileCloner/ViewModels/MainPageViewModel.FileWatcher.cs @@ -16,6 +16,7 @@ namespace FileCloner.ViewModels; partial class MainPageViewModel : ViewModelBase { + private readonly FileSystemWatcher _watcher = new(); /// /// Watches the file present at the given path. /// @@ -23,11 +24,10 @@ partial class MainPageViewModel : ViewModelBase public void WatchFile(string path) { Trace.WriteLine($"Started watching at {path}"); - using FileSystemWatcher watcher = new(); - watcher.Path = path; + _watcher.Path = path; //The following changes are notified to the watcher - watcher.NotifyFilter = NotifyFilters.Attributes | + _watcher.NotifyFilter = NotifyFilters.Attributes | NotifyFilters.DirectoryName | NotifyFilters.FileName | NotifyFilters.LastWrite | @@ -35,20 +35,15 @@ public void WatchFile(string path) //Watching all kinds of files. * is a wildcard which means changes in all files //with all extensions are watched upon. - watcher.Filter = "*.*"; + _watcher.Filter = "*.*"; //Setting event handlers for the changes - watcher.Created += new FileSystemEventHandler(OnChanged); - watcher.Deleted += new FileSystemEventHandler(OnChanged); - watcher.Changed += new FileSystemEventHandler(OnChanged); - watcher.Renamed += new RenamedEventHandler(OnRenamed); + _watcher.Created += new FileSystemEventHandler(OnChanged); + _watcher.Deleted += new FileSystemEventHandler(OnChanged); + _watcher.Changed += new FileSystemEventHandler(OnChanged); + _watcher.Renamed += new RenamedEventHandler(OnRenamed); - watcher.EnableRaisingEvents = true; - //Watch for as long as the UI is kept running - while (true) - { - ; - } + _watcher.EnableRaisingEvents = true; } //Update the UI as and when the name of an object is changed.