Revert master watcher

This commit is contained in:
Hylke Bons 2012-07-25 11:25:52 +02:00
parent 63eaf5daa5
commit afd894ac50
7 changed files with 84 additions and 128 deletions

View file

@ -16,7 +16,6 @@ SOURCES = \
SparkleRepoBase.cs \
SparkleUser.cs \
SparkleWatcher.cs \
SparkleWatcherFactory.cs \
SparkleWrappers.cs

View file

@ -114,15 +114,12 @@ namespace SparkleLib {
private string identifier;
private SparkleListenerBase listener;
private SparkleWatcher watcher;
private TimeSpan poll_interval = PollInterval.Short;
private DateTime last_poll = DateTime.Now;
private DateTime progress_last_change = DateTime.Now;
private TimeSpan progress_change_interval = new TimeSpan (0, 0, 0, 1);
private Timers.Timer remote_timer = new Timers.Timer () {
Interval = 5000
};
private Timers.Timer remote_timer = new Timers.Timer () { Interval = 5000 };
private bool is_syncing {
get {
@ -151,7 +148,7 @@ namespace SparkleLib {
Status = status;
};
SparkleWatcherFactory.CreateWatcher (this);
this.watcher = new SparkleWatcher (LocalPath);
new Thread (() => CreateListener ()).Start ();
this.remote_timer.Elapsed += delegate {
@ -178,6 +175,8 @@ namespace SparkleLib {
public void Initialize ()
{
this.watcher.ChangeEvent += OnFileActivity;
// Sync up everything that changed
// since we've been offline
if (HasLocalChanges) {
@ -212,8 +211,10 @@ namespace SparkleLib {
if (IsBuffering || !HasLocalChanges)
return;
SparkleHelpers.DebugInfo ("Local", Name + " | Activity detected, waiting for it to settle...");
IsBuffering = true;
this.watcher.Disable ();
SparkleHelpers.DebugInfo ("Local", Name + " | Activity detected, waiting for it to settle...");
List<double> size_buffer = new List<double> ();
@ -247,6 +248,8 @@ namespace SparkleLib {
}
} while (IsBuffering);
this.watcher.Enable ();
}
@ -275,6 +278,8 @@ namespace SparkleLib {
private void SyncUpBase ()
{
this.watcher.Disable ();
SparkleHelpers.DebugInfo ("SyncUp", Name + " | Initiated");
HasUnsyncedChanges = true;
@ -282,10 +287,10 @@ namespace SparkleLib {
if (SyncUp ()) {
SparkleHelpers.DebugInfo ("SyncUp", Name + " | Done");
HasUnsyncedChanges = false;
ChangeSets = GetChangeSets ();
HasUnsyncedChanges = false;
SyncStatusChanged (SyncStatus.Idle);
this.listener.Announce (new SparkleAnnouncement (Identifier, CurrentRevision));
@ -293,10 +298,12 @@ namespace SparkleLib {
SparkleHelpers.DebugInfo ("SyncUp", Name + " | Error");
SyncDownBase ();
this.watcher.Disable ();
if (ServerOnline && SyncUp ()) {
HasUnsyncedChanges = false;
SyncStatusChanged (SyncStatus.Idle);
SyncStatusChanged (SyncStatus.Idle);
this.listener.Announce (new SparkleAnnouncement (Identifier, CurrentRevision));
} else {
@ -307,11 +314,15 @@ namespace SparkleLib {
ProgressPercentage = 0.0;
ProgressSpeed = "";
this.watcher.Enable ();
}
private void SyncDownBase ()
{
this.watcher.Disable ();
SparkleHelpers.DebugInfo ("SyncDown", Name + " | Initiated");
SyncStatusChanged (SyncStatus.SyncDown);
@ -361,6 +372,8 @@ namespace SparkleLib {
ProgressPercentage = 0.0;
ProgressSpeed = "";
this.watcher.Enable ();
SyncStatusChanged (SyncStatus.Idle);
}
@ -475,7 +488,7 @@ namespace SparkleLib {
this.listener.Disconnected -= ListenerDisconnectedDelegate;
this.listener.AnnouncementReceived -= ListenerAnnouncementReceivedDelegate;
SparkleWatcherFactory.DisposeWatcher (this);
this.watcher.Dispose ();
}
}
}

View file

@ -16,54 +16,57 @@
using System;
using System.Collections.Generic;
using System.Threading;
using IO = System.IO;
using System.IO;
namespace SparkleLib {
public class SparkleWatcher : IO.FileSystemWatcher {
public class SparkleWatcher : FileSystemWatcher {
public List<SparkleRepoBase> ReposToNotify = new List<SparkleRepoBase> ();
public delegate void ChangeEventEventHandler (FileSystemEventArgs args);
public event ChangeEventEventHandler ChangeEvent;
private Object thread_lock = new Object ();
public SparkleWatcher (SparkleRepoBase repo)
public SparkleWatcher (string path) : base (path)
{
ReposToNotify.Add (repo);
Changed += Notify;
Created += Notify;
Deleted += Notify;
Renamed += Notify;
Filter = "*";
Path = IO.Path.GetDirectoryName (repo.LocalPath);
IncludeSubdirectories = true;
EnableRaisingEvents = true;
Filter = "*";
Changed += delegate (object o, FileSystemEventArgs args) {
if (ChangeEvent != null)
ChangeEvent (args);
};
Created += delegate (object o, FileSystemEventArgs args) {
if (ChangeEvent != null)
ChangeEvent (args);
};
Deleted += delegate (object o, FileSystemEventArgs args) {
if (ChangeEvent != null)
ChangeEvent (args);
};
Renamed += delegate (object o, RenamedEventArgs args) {
if (ChangeEvent != null)
ChangeEvent (args);
};
}
public void Notify (object sender, IO.FileSystemEventArgs args)
public void Enable ()
{
char separator = IO.Path.DirectorySeparatorChar;
string relative_path = args.FullPath.Substring (Path.Length);
relative_path = relative_path.Trim (new char [] {' ', separator});
lock (this.thread_lock)
EnableRaisingEvents = true;
}
// Ignore changes that happened in the parent path
if (!relative_path.Contains (separator.ToString ()))
return;
string repo_name = relative_path.Substring (0, relative_path.IndexOf (separator));
foreach (SparkleRepoBase repo in ReposToNotify) {
if (repo.Name.Equals (repo_name) && !repo.IsBuffering &&
(repo.Status != SyncStatus.SyncUp && repo.Status != SyncStatus.SyncDown)) {
new Thread (() => repo.OnFileActivity (args)).Start ();
}
}
public void Disable ()
{
lock (this.thread_lock)
EnableRaisingEvents = false;
}
}
}

View file

@ -1,74 +0,0 @@
// SparkleShare, a collaboration and sharing tool.
// Copyright (C) 2010 Hylke Bons <hylkebons@gmail.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using System;
using System.Collections.Generic;
using System.IO;
namespace SparkleLib {
public static class SparkleWatcherFactory {
private static List<SparkleWatcher> watchers = new List<SparkleWatcher> ();
public static SparkleWatcher CreateWatcher (SparkleRepoBase repo_to_watch)
{
foreach (SparkleWatcher watcher in watchers) {
foreach (SparkleRepoBase repo in watcher.ReposToNotify) {
string path_to_watch = Path.GetDirectoryName (repo_to_watch.LocalPath);
if (watcher.Path.Equals (path_to_watch)) {
watcher.ReposToNotify.Add (repo_to_watch);
SparkleHelpers.DebugInfo ("WatcherFactory", "Refered to existing watcher for " + path_to_watch);
return watcher;
}
}
}
SparkleWatcher new_watcher = new SparkleWatcher (repo_to_watch);
watchers.Add (new_watcher);
SparkleHelpers.DebugInfo ("WatcherFactory", "Issued new watcher for " + repo_to_watch.Name);
return watchers [watchers.Count - 1];
}
public static void TriggerWatcherManually (FileSystemEventArgs args)
{
foreach (SparkleWatcher watcher in watchers) {
if (args.FullPath.StartsWith (watcher.Path)) {
watcher.Notify (null, args);
return;
}
}
}
public static void DisposeWatcher (SparkleRepoBase repo)
{
foreach (SparkleWatcher watcher in watchers) {
if (watcher.ReposToNotify.Contains (repo)) {
watcher.ReposToNotify.Remove (repo);
return;
}
}
}
}
}

View file

@ -82,9 +82,6 @@
<Link>SparkleWatcher.cs</Link>
<SubType>Component</SubType>
</Compile>
<Compile Include="..\SparkleWatcherFactory.cs">
<Link>SparkleWatcherFactory.cs</Link>
</Compile>
<Compile Include="AssemblyInfo.cs" />
<Compile Include="Defines.cs">
<DependentUpon>Defines.tt</DependentUpon>
@ -168,4 +165,4 @@
<PropertyGroup>
<PreBuildEvent>"$(ProjectDir)transform_tt.cmd"</PreBuildEvent>
</PropertyGroup>
</Project>
</Project>

View file

@ -68,12 +68,30 @@ namespace SparkleShare {
base.Initialize ();
this.watcher.Changed += delegate (string path) {
string full_path = Path.Combine (SparkleConfig.DefaultConfig.FoldersPath, path + "/something");
// Don't even bother with paths in .git/
if (path.Contains (".git"))
return;
FileSystemEventArgs event_args = new FileSystemEventArgs (WatcherChangeTypes.Changed,
Path.GetDirectoryName (full_path), Path.GetFileName (full_path));
string repo_name;
SparkleWatcherFactory.TriggerWatcherManually (event_args);
if (path.Contains ("/"))
repo_name = path.Substring (0, path.IndexOf ("/"));
else
repo_name = path;
// Ignore changes in the root of each subfolder, these
// are already handled by the repository
if (Path.GetFileNameWithoutExtension (path).Equals (repo_name))
return;
repo_name = repo_name.Trim ("/".ToCharArray ());
FileSystemEventArgs fse_args = new FileSystemEventArgs (WatcherChangeTypes.Changed,
Path.Combine (SparkleConfig.DefaultConfig.FoldersPath, path), Path.GetFileName (path));
foreach (SparkleRepoBase repo in Repositories) {
if (repo.Name.Equals (repo_name))
repo.OnFileActivity (fse_args);
}
};
}

View file

@ -252,7 +252,7 @@ namespace SparkleShare {
this.animation_frame_number = 0;
this.animation = new Timer () {
Interval = 100
Interval = 50
};
this.animation.Elapsed += delegate {