Skip to content

Commit

Permalink
Update multiple path args support within 1 worker thread (#223)
Browse files Browse the repository at this point in the history
  • Loading branch information
gyk4j authored Mar 7, 2024
1 parent ce94f56 commit ddb9e1a
Show file tree
Hide file tree
Showing 12 changed files with 144 additions and 94 deletions.
22 changes: 7 additions & 15 deletions Wreck/Controller/AbstractController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,35 +205,27 @@ public virtual void Verify()
private void Run(CorrectionMode mode)
{
string[] args = Environment.GetCommandLineArgs();
string[] dirs;
string[] paths;

if(args.Length > 1)
{
dirs = new string[args.Length-1];
Array.Copy(args, 1, dirs, 0, args.Length-1);
paths = new string[args.Length-1];
Array.Copy(args, 1, paths, 0, args.Length-1);
}
else
{
dirs = new string[0];
paths = new string[0];
Error();
}

foreach(string d in dirs)
{
if(Directory.Exists(d))
Run(mode, new DirectoryInfo(d));
else if(File.Exists(d))
Run(mode, new FileInfo(d));
else
LOG.ErrorFormat("Unknown path type: {0}", d);
}
Run(mode, paths);
}

public virtual void Run(CorrectionMode mode, FileSystemInfo fsi)
public virtual void Run(CorrectionMode mode, string[] paths)
{
LOG.InfoFormat("Mode: {0}, File: {1}",
Enum.GetName(typeof(CorrectionMode), mode),
fsi.FullName);
string.Join(", ", paths));
throw new NotImplementedException();
}

Expand Down
2 changes: 1 addition & 1 deletion Wreck/Controller/IController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Wreck.Controller
public interface IController
{
void Error();
void Run(CorrectionMode mode, FileSystemInfo fsi);
void Run(CorrectionMode mode, string[] paths);
void Analyze();
void Backup();
void Repair();
Expand Down
63 changes: 40 additions & 23 deletions Wreck/Service/WreckService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.IO;

using log4net;
using Wreck.IO.Task;
using Wreck.Resources;
using Wreck.UI;
Expand All @@ -14,40 +15,55 @@ namespace Wreck.Service
/// </summary>
public class WreckService
{
private static readonly ILog LOG = LogManager.GetLogger(typeof(WreckService));
/*
public bool IsRestorable(FileSystemInfo startPath)
{
CsvLogRepository r = CsvLogRepository.Instance;
return r.Exists(startPath);
}
*/
public ITask Run(
FileSystemInfo startPath,
*/
public List<ITask> Run(
string[] startPaths,
CorrectionMode mode,
Dictionary<SourceEnum, bool> sources,
Dictionary<CorrectionEnum, bool> corrections,
DateTime custom)
// PropertyChangeListener pcl) // Belongs to Java Beans
{
List<ITask> tasks = new List<ITask>();

ITask task = null;

switch(mode)
FileSystemInfo startPath;
foreach(string p in startPaths)
{
case CorrectionMode.Analyze:
task = new AnalyzeTask(
startPath,
sources,
custom,
corrections);
break;
case CorrectionMode.SaveAttributes:
task = new CorrectTask(
startPath,
sources,
custom,
corrections);
break;
if(Directory.Exists(p))
startPath = new DirectoryInfo(p);
else if(File.Exists(p))
startPath = new FileInfo(p);
else
{
LOG.WarnFormat("Skipped unsupported type: {0}", p);
continue;
}

switch(mode)
{
case CorrectionMode.Analyze:
tasks.Add(
new AnalyzeTask(
startPath,
sources,
custom,
corrections));
break;
case CorrectionMode.SaveAttributes:
tasks.Add(
new CorrectTask(
startPath,
sources,
custom,
corrections));
break;
// case CorrectionMode.BackupAttributes:
// task = new BackupTask(startPath);
// break;
Expand All @@ -57,11 +73,12 @@ public ITask Run(
// case CorrectionMode.VerifyAttributes:
// task = new VerifyTask(startPath);
// break;
default:
throw new ArgumentException("Unknown correction mode");
default:
throw new ArgumentException("Unknown correction mode");
}
}

return task;
return tasks;
}
}
}
6 changes: 3 additions & 3 deletions WreckCli/Controller/CliController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public override void Error()
Environment.Exit(-1);
}

public override void Run(CorrectionMode mode, FileSystemInfo fsi)
public override void Run(CorrectionMode mode, string[] paths)
{
Dictionary<SourceEnum, bool> sources = new Dictionary<SourceEnum, bool>();
foreach(SourceEnum s in SourceEnum.Values)
Expand All @@ -73,11 +73,11 @@ public override void Run(CorrectionMode mode, FileSystemInfo fsi)
// FIXME: To check command line parameters
DateTime customDateTime = DateTime.Now;

ITask task = Service.Run(fsi, mode, sources, corrections, customDateTime);
List<ITask> tasks = Service.Run(paths, mode, sources, corrections, customDateTime);

PropertyChangeListener propertyChangeListener = new ProgressPropertyChangeListener(this);

CliWorker pw = new CliWorker(task, fsi);
CliWorker pw = new CliWorker(tasks, paths);
pw.AddPropertyChangeListener(propertyChangeListener);

// HACK: Worker must save the reference before Execute as this is single-threaded.
Expand Down
13 changes: 8 additions & 5 deletions WreckCli/IO/CliVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using log4net;
using Wreck.Entity;
using Wreck.IO;
using Wreck.IO.Task;
using Wreck.Resources;
using Wreck.Util.Logging;

Expand All @@ -21,10 +22,12 @@ public class CliVisitor : AbstractFileVisitor
private static readonly StatisticsCollector STATS = StatisticsCollector.Instance;

CliWorker progressWorker = null;
ITask task = null;

public CliVisitor(CliWorker progressWorker) : base()
public CliVisitor(CliWorker progressWorker, ITask task) : base()
{
this.progressWorker = progressWorker;
this.task = task;
}

public override FileVisitResult PreVisitDirectory(DirectoryInfo dir)
Expand All @@ -33,15 +36,15 @@ public override FileVisitResult PreVisitDirectory(DirectoryInfo dir)
progressWorker.Publish(visit);

base.PreVisitDirectory(dir);
progressWorker.Task.PreVisitDirectory(Suggestions, dir);
task.PreVisitDirectory(Suggestions, dir);

return FileVisitResult.Continue;
}

public override FileVisitResult PostVisitDirectory(DirectoryInfo dir, IOException exc)
{
base.PostVisitDirectory(dir, exc);
progressWorker.Task.PostVisitDirectory(Suggestions, dir);
task.PostVisitDirectory(Suggestions, dir);

return FileVisitResult.Continue;
}
Expand All @@ -57,15 +60,15 @@ public override FileVisitResult VisitFile(FileInfo file)
FileVisit visit = new FileVisit(file);
progressWorker.Publish(visit);

progressWorker.Task.VisitFile(Suggestions, file);
task.VisitFile(Suggestions, file);

return FileVisitResult.Continue;
}

public override FileVisitResult VisitFileFailed(FileSystemInfo file, IOException exc)
{
base.VisitFileFailed(file, exc);
progressWorker.Task.VisitFileFailed(Suggestions, file, exc);
task.VisitFileFailed(Suggestions, file, exc);

return FileVisitResult.Continue;
}
Expand Down
46 changes: 31 additions & 15 deletions WreckCli/IO/CliWorker.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;

using Java.Beans;
Expand Down Expand Up @@ -28,27 +29,23 @@ public class CliWorker : RunnableFuture<string>

private readonly PropertyChangeSupport propertyChangeSupport;

private readonly ITask task;
private readonly FileSystemInfo startPath;

private readonly CliVisitor visitor;
private readonly List<ITask> tasks;
private readonly string[] startPaths;

private FileVisit visit;
// private FileBean fileBean;

public CliWorker(ITask task, FileSystemInfo startPath)
public CliWorker(List<ITask> tasks, string[] startPaths)
{
state = StateValue.Pending;
propertyChangeSupport = new PropertyChangeSupport(this);

this.task = task;
this.startPath = startPath;
this.visitor = new CliVisitor(this);
this.tasks = tasks;
this.startPaths = startPaths;
}

public ITask Task { get { return task; } }
private FileSystemInfo StartPath { get { return startPath; } }
private AbstractFileVisitor Visitor { get { return visitor; } }
public List<ITask> Tasks { get { return tasks; } }
private string[] StartPaths { get { return startPaths; } }

public void Execute()
{
Expand All @@ -67,9 +64,28 @@ public void Run()

protected string DoInBackground()
{
Files.WalkFileTree(
StartPath,
Visitor);
Debug.Assert(StartPaths.Length == Tasks.Count);

int len = Math.Min(StartPaths.Length, Tasks.Count);

for(int i = 0; i < len; i++)
{
string p = StartPaths[i];
FileSystemInfo startPath;
if(Directory.Exists(p))
startPath = new DirectoryInfo(p);
else if(File.Exists(p))
startPath = new FileInfo(p);
else
throw new IOException("Unsupported file type: " + p);

ITask task = Tasks[i];
CliVisitor visitor = new CliVisitor(this, task);

Files.WalkFileTree(
startPath,
visitor);
}

// Return if background worker is cancelled by user.
return IsCancelled()? "Cancelled" : "Done";
Expand Down Expand Up @@ -192,7 +208,7 @@ protected void Process(List<FileVisit> chunks)
}
);
}
*/
*/

public void Publish(params FileVisit[] chunks)
{
Expand Down
2 changes: 1 addition & 1 deletion WreckCli/WreckCli.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<TargetFrameworkVersion>v2.0</TargetFrameworkVersion>
<AppDesignerFolder>Properties</AppDesignerFolder>
<SourceAnalysisOverrideSettingsFile>C:\Users\USER\AppData\Roaming\ICSharpCode/SharpDevelop3.0\Settings.SourceAnalysis</SourceAnalysisOverrideSettingsFile>
<StartArguments>"C:\temp\Public"</StartArguments>
<StartArguments>"C:\temp\Public\Documents" "C:\temp\Public\Downloads" "C:\temp\Public\Music" "C:\temp\Public\Pictures" "C:\temp\Public\Videos"</StartArguments>
<StartupObject>Wreck.Program</StartupObject>
<AllowUnsafeBlocks>False</AllowUnsafeBlocks>
<NoStdLib>False</NoStdLib>
Expand Down
6 changes: 3 additions & 3 deletions WreckGui/Controller/GuiController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ private void PrefillPaths()
}
}

public override void Run(CorrectionMode mode, FileSystemInfo fsi)
public override void Run(CorrectionMode mode, string[] paths)
{
Dictionary<SourceEnum, bool> sources = new Dictionary<SourceEnum, bool>();
foreach(SourceEnum s in SourceEnum.Values)
Expand All @@ -163,11 +163,11 @@ public override void Run(CorrectionMode mode, FileSystemInfo fsi)
// FIXME: To check GUI control checkbox and textbox
DateTime customDateTime = DateTime.Now;

ITask task = Service.Run(fsi, mode, sources, corrections, customDateTime);
List<ITask> tasks = Service.Run(paths, mode, sources, corrections, customDateTime);

PropertyChangeListener propertyChangeListener = new ProgressPropertyChangeListener(this);

GuiWorker pw = new GuiWorker(task, fsi);
GuiWorker pw = new GuiWorker(tasks, paths);
pw.AddPropertyChangeListener(propertyChangeListener);
pw.Execute();
Worker = pw; // Need to save to Worker for cleanup later.
Expand Down
File renamed without changes.
Loading

0 comments on commit ddb9e1a

Please sign in to comment.