Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 47 additions & 38 deletions FileCloner/Models/NetworkService/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using System.Text;
using System.Diagnostics.CodeAnalysis;
using FileCloner.FileClonerLogging;
using GrpcClient;

namespace FileCloner.Models.NetworkService;

Expand Down Expand Up @@ -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");
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
);
}
Expand All @@ -264,32 +265,40 @@ public void StopCloning()
[ExcludeFromCodeCoverage]
public void OnDataReceived(string serializedData)
{
Message data = _serializer.Deserialize<Message>(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<Message>(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);
}
}

/// <summary>
Expand Down
23 changes: 9 additions & 14 deletions FileCloner/ViewModels/MainPageViewModel.FileWatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,39 +16,34 @@ namespace FileCloner.ViewModels;

partial class MainPageViewModel : ViewModelBase
{
private readonly FileSystemWatcher _watcher = new();
/// <summary>
/// Watches the file present at the given path.
/// </summary>
/// <param name="path">Since that is the file we are viewing at UI. Path will be the root directory path</param>
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 |
NotifyFilters.Size;

//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.
Expand Down