Skip to content

Commit

Permalink
Finished up delay copy operation logic... Its alive!
Browse files Browse the repository at this point in the history
  • Loading branch information
tarikguney committed Dec 21, 2018
1 parent c48ffef commit 464c86f
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 38 deletions.
2 changes: 1 addition & 1 deletion Client/CommandOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class CommandOptions
[Option('e', "debug", Default = false, Required = false, HelpText = "Shows debug information.")]
public bool Debug { get; set; }

[Option('t', "delay", Default = 0, Required = false, HelpText = "Delays copy operation for a given time in seconds.")]
[Option('t', "delay", Default = 0, Required = false, HelpText = "Delays copy operation for a given time in milliseconds.")]
public int Delay { get; set; }

[Usage]
Expand Down
21 changes: 11 additions & 10 deletions Client/FileCopier.cs
Original file line number Diff line number Diff line change
@@ -1,41 +1,42 @@
using System;
using System.IO;
using CopierPluginBase;

namespace Copier.Client
{
class FileCopier : IFileCopier, IPreCopyEventBroadcaster, IPostCopyEventBroadcaster
{
public event Action<string> PreCopyEvent = delegate { };
public event Action<string> PostCopy = delegate { };
public event Action<string> PostCopyEvent = delegate { };

private readonly ILogger _logger;
private readonly CommandOptions _options;

public FileCopier(ILogger logger)
public FileCopier(ILogger logger, CommandOptions options)
{
_logger = logger;
_options = options;
}

public void CopyFile(CommandOptions options, string fileName)
public void CopyFile(string fileName)
{
var absoluteSourceFilePath = Path.Combine(options.SourceDirectoryPath, fileName);
var absoluteTargetFilePath = Path.Combine(options.DestinationDirectoryPath, fileName);
var absoluteSourceFilePath = Path.Combine(_options.SourceDirectoryPath, fileName);
var absoluteTargetFilePath = Path.Combine(_options.DestinationDirectoryPath, fileName);

if (File.Exists(absoluteTargetFilePath) && !options.OverwriteTargetFile)
if (File.Exists(absoluteTargetFilePath) && !_options.OverwriteTargetFile)
{
_logger.LogInfo($"{fileName} exists. Skipped the copy operation because OverwriteTargetFile is set to false.");
return;
}

PreCopyEvent(absoluteSourceFilePath);
File.Copy(absoluteSourceFilePath, absoluteTargetFilePath, options.OverwriteTargetFile);
PostCopy(absoluteSourceFilePath);
File.Copy(absoluteSourceFilePath, absoluteTargetFilePath, _options.OverwriteTargetFile);
PostCopyEvent(absoluteSourceFilePath);
}
}

public interface IPostCopyEventBroadcaster
{
event Action<string> PostCopy;
event Action<string> PostCopyEvent;
}

public interface IPreCopyEventBroadcaster
Expand Down
4 changes: 2 additions & 2 deletions Client/FileWatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void Watch(CommandOptions options)
_logger.LogInfo($"{args.Name} file has changed.");
}

_fileCopier.CopyFile(options, args.Name);
_fileCopier.CopyFile(args.Name);
};

watcher.Renamed += (sender, args) =>
Expand All @@ -41,7 +41,7 @@ public void Watch(CommandOptions options)
_logger.LogInfo($"{args.OldName} has been renamed to {args.Name}.");
}

_fileCopier.CopyFile(options, args.Name);
_fileCopier.CopyFile(args.Name);
};

watcher.EnableRaisingEvents = true;
Expand Down
2 changes: 1 addition & 1 deletion Client/IFileCopier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ namespace Copier.Client
{
public interface IFileCopier
{
void CopyFile(CommandOptions options, string fileName);
void CopyFile(string fileName);
}
}
2 changes: 1 addition & 1 deletion Client/PluginLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public void Subscribe(IPreCopyEventBroadcaster pre, IPostCopyEventBroadcaster po
_postCopyListeners.ForEach(a =>
{
var listenerObject = (IPostCopyEventListener) Activator.CreateInstance(a);
post.PostCopy += listenerObject.OnPostCopy;
post.PostCopyEvent += listenerObject.OnPostCopy;
});
}
}
Expand Down
11 changes: 8 additions & 3 deletions Client/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,15 @@ private static void StartWatching(CommandOptions options)
: options.SourceDirectoryPath;

IPluginLoader loader = new PluginLoader(logger, options.Debug);
IFileCopier copier = options.Delay > 0
? (IFileCopier) new QueuedFileCopier(logger)
: new FileCopier(logger);

var fileCopier = new FileCopier(logger, options);
IFileCopier copier = fileCopier;

if (options.Delay > 0)
{
copier = new QueuedFileCopier(fileCopier, logger, options);
}

IFileWatcher fileWatcher = new FileWatcher(copier, logger);

loader.Subscribe((IPreCopyEventBroadcaster) copier, (IPostCopyEventBroadcaster) copier);
Expand Down
69 changes: 49 additions & 20 deletions Client/QueuedFileCopier.cs
Original file line number Diff line number Diff line change
@@ -1,49 +1,78 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Timers;

namespace Copier.Client
{
public class QueuedFileCopier: IFileCopier
public class QueuedFileCopier : IFileCopier, IPreCopyEventBroadcaster, IPostCopyEventBroadcaster
{
public event Action<string> PreCopyEvent = delegate {};
public event Action<string> PostCopyEvent = delegate {};

private readonly IFileCopier _fileCopier;
private readonly ILogger _logger;
private CommandOptions _commandOptions;
private readonly CommandOptions _options;

private readonly HashSet<string> _fileNameQueue = new HashSet<string>();
private Timer _copyTimer;
private Task _copyTask;

public QueuedFileCopier(IFileCopier fileCopier, ILogger logger)
public QueuedFileCopier(IFileCopier fileCopier, ILogger logger, CommandOptions options)
{
_fileCopier = fileCopier;
_logger = logger;
_options = options;

if (_options.Debug)
{
logger.LogInfo("Delay option has been specified. QueuedFileCopier is chosen as the copier strategy.");
}
}

public void CopyFile(CommandOptions options, string fileName)
public void CopyFile(string fileName)
{
if (_copyTimer == null)
if (_copyTask == null)
{
_commandOptions = options;
_copyTimer = new Timer();
_copyTimer.Elapsed += TimerOnElapsed;
_copyTask = Task.Run(async () =>
{
await Task.Delay(TimeSpan.FromMilliseconds(_options.Delay));
if (_options.Verbose || _options.Debug)
{
_logger.LogInfo($"{_options.Delay} milliseconds have passed. The copy operation has started...");
}

PreCopyEvent("");

foreach (var item in _fileNameQueue)
{
_fileCopier.CopyFile(item);
}

PostCopyEvent("");

_copyTask = null;

if (_options.Verbose || _options.Debug)
{
_logger.LogInfo($"The copy operation has finished...");
_logger.LogInfo("The file queue has been emptied.");
}
});
}

if (!_fileNameQueue.Contains(fileName))
{
if (_options.Verbose || _options.Debug)
{
_logger.LogInfo(
$"{fileName} has been added to the file queue and will be copied over in {_options.Delay} milliseconds.");
}

_fileNameQueue.Add(fileName);
}
}

private void TimerOnElapsed(object sender, ElapsedEventArgs e)
{
foreach (var fileName in _fileNameQueue)
else if (_options.Debug)
{
_fileCopier.CopyFile(_commandOptions, fileName);
_logger.LogInfo($"{fileName} exists in the file queue, thereby skipped.");
}

_fileNameQueue.Clear();
}
}
}

0 comments on commit 464c86f

Please sign in to comment.