Calculate IDs in the fetcher and add the to the config

This commit is contained in:
Hylke Bons 2012-07-05 13:33:32 +02:00
parent 201a753c41
commit d244f12459
7 changed files with 138 additions and 94 deletions

View file

@ -186,11 +186,13 @@ namespace SparkleLib.Git {
public override bool IsFetchedRepoEmpty {
get {
SparkleGit git = new SparkleGit (TargetFolder, "rev-parse HEAD");
git.StartInfo.RedirectStandardError = true;
git.Start ();
// Reading the standard output HAS to go before
// WaitForExit, or it will hang forever on output > 4096 bytes
git.StandardOutput.ReadToEnd ();
git.StandardError.ReadToEnd ();
git.WaitForExit ();
return (git.ExitCode != 0);
@ -292,12 +294,13 @@ namespace SparkleLib.Git {
public override void Complete ()
{
if (IsFetchedRepoEmpty)
return;
if (!IsFetchedRepoEmpty) {
SparkleGit git = new SparkleGit (TargetFolder, "checkout --quiet HEAD");
git.Start ();
git.WaitForExit ();
}
SparkleGit git = new SparkleGit (TargetFolder, "checkout --quiet HEAD");
git.Start ();
git.WaitForExit ();
base.Complete ();
}

View file

@ -91,14 +91,7 @@ namespace SparkleLib.Git {
File.WriteAllText (size_file_path, size.ToString ());
File.WriteAllText (history_size_file_path, history_size.ToString ());
}
public override void CreateInitialChangeSet ()
{
base.CreateInitialChangeSet ();
SyncUp (); // FIXME: Weird freeze happens when base class handles this
}
public override string [] UnsyncedFilePaths {
get {

View file

@ -210,11 +210,14 @@ namespace SparkleLib {
}
public void AddFolder (string name, string url, string backend)
public void AddFolder (string name, string identifier, string url, string backend)
{
XmlNode node_name = CreateElement ("name");
node_name.InnerText = name;
XmlNode node_identifier = CreateElement ("identifier");
node_identifier.InnerText = identifier;
XmlNode node_url = CreateElement ("url");
node_url.InnerText = url;
@ -223,6 +226,7 @@ namespace SparkleLib {
XmlNode node_folder = CreateNode (XmlNodeType.Element, "folder", null);
node_folder.AppendChild (node_name);
node_folder.AppendChild (node_identifier);
node_folder.AppendChild (node_url);
node_folder.AppendChild (node_backend);
@ -250,6 +254,12 @@ namespace SparkleLib {
}
public string GetIdentifierForFolder (string name)
{
return GetFolderValue (name, "identifier");
}
public string GetUrlForFolder (string name)
{
return GetFolderValue (name, "url");
@ -304,11 +314,10 @@ namespace SparkleLib {
{
XmlNode folder = GetFolder(name);
if ((folder != null) && (folder [key] != null)) {
if ((folder != null) && (folder [key] != null))
return folder [key].InnerText;
}
return null;
else
return null;
}

View file

@ -41,7 +41,6 @@ namespace SparkleLib {
public abstract bool Fetch ();
public abstract void Stop ();
public abstract void Complete ();
public abstract bool IsFetchedRepoEmpty { get; }
public abstract bool IsFetchedRepoPasswordCorrect (string password);
public abstract void EnableFetchedRepoCrypto (string password);
@ -51,6 +50,7 @@ namespace SparkleLib {
public readonly bool FetchPriorHistory = false;
public string TargetFolder { get; protected set; }
public bool IsActive { get; private set; }
public string Identifier = CreateIdentifier ();
public string [] Warnings {
get {
@ -170,6 +170,40 @@ namespace SparkleLib {
}
public virtual void Complete ()
{
string identifier_path = Path.Combine (TargetFolder, ".sparkleshare");
if (!File.Exists (identifier_path))
File.WriteAllText (identifier_path, Identifier);
if (IsFetchedRepoEmpty)
CreateInitialChangeSet ();
}
// Create an initial change set when the
// user has fetched an empty remote folder
public void CreateInitialChangeSet ()
{
string file_path = Path.Combine (TargetFolder, "SparkleShare.txt");
string n = Environment.NewLine;
string text = "Congratulations, you've successfully created a SparkleShare repository!" + n +
n +
"Any files you add or change in this folder will be automatically synced to " + n +
RemoteUrl + " and everyone connected to it." + n +
n +
"SparkleShare is an Open Source software program that helps people " + n +
"collaborate and share files. If you like what we do, please consider a small " + n +
"donation to support the project: http://sparkleshare.org/support-us/" + n +
n +
"Have fun! :)" + n;
File.WriteAllText (file_path, text);
}
public void Dispose ()
{
if (this.thread != null) {
@ -178,6 +212,15 @@ namespace SparkleLib {
}
}
public static string CreateIdentifier ()
{
Random random = new Random ();
string number = "" + random.Next () + "" + random.Next () + "" + random.Next ();
return SparkleHelpers.SHA1 (number);
}
protected void OnProgressChanged (double percentage) {
if (ProgressChanged != null)

View file

@ -31,8 +31,8 @@ namespace SparkleLib {
public event DisconnectedEventHandler Disconnected;
public delegate void DisconnectedEventHandler ();
public event ReceivedEventHandler Received;
public delegate void ReceivedEventHandler (SparkleAnnouncement announcement);
public event AnnouncementReceivedEventHandler AnnouncementReceived;
public delegate void AnnouncementReceivedEventHandler (SparkleAnnouncement announcement);
public readonly Uri Server;
@ -175,8 +175,8 @@ namespace SparkleLib {
AddRecentAnnouncement (announcement);
this.queue_down [announcement.FolderIdentifier] = announcement;
if (Received != null)
Received (announcement);
if (AnnouncementReceived != null)
AnnouncementReceived (announcement);
}

View file

@ -83,13 +83,16 @@ namespace SparkleLib {
if (File.Exists (id_path))
this.identifier = File.ReadAllText (id_path).Trim ();
if (this.identifier != null && this.identifier.Length > 0) {
if (!string.IsNullOrEmpty (this.identifier)) {
return this.identifier;
} else {
Random random = new Random ();
string number = "" + random.Next () + "" + random.Next () + "" + random.Next ();
this.identifier = SparkleHelpers.SHA1 (number);
string config_identifier = SparkleConfig.DefaultConfig.GetIdentifierForFolder (Name);
if (!string.IsNullOrEmpty (config_identifier))
this.identifier = config_identifier;
else
this.identifier = SparkleFetcherBase.CreateIdentifier ();
File.WriteAllText (id_path, this.identifier);
File.SetAttributes (id_path, FileAttributes.Hidden);
@ -148,9 +151,6 @@ namespace SparkleLib {
this.identifier = Identifier;
if (CurrentRevision == null)
CreateInitialChangeSet ();
ChangeSets = GetChangeSets ();
this.watcher = CreateWatcher ();
@ -194,28 +194,6 @@ namespace SparkleLib {
}
// Create an initial change set when the
// user has fetched an empty remote folder
public virtual void CreateInitialChangeSet ()
{
string file_path = Path.Combine (LocalPath, "SparkleShare.txt");
string n = Environment.NewLine;
File.WriteAllText (file_path,
"Congratulations, you've successfully created a SparkleShare repository!" + n +
"" + n +
"Any files you add or change in this folder will be automatically synced to " + n +
RemoteUrl + " and everyone connected to it." + n +
"" + n +
"SparkleShare is an Open Source software program that helps people " + n +
"collaborate and share files. If you like what we do, please consider a small " + n +
"donation to support the project: http://sparkleshare.org/support-us/" + n +
"" + n +
"Have fun! :)" + n
);
}
public List<SparkleChangeSet> GetChangeSets () {
return GetChangeSets (30);
}
@ -427,52 +405,15 @@ namespace SparkleLib {
new Thread (
new ThreadStart (delegate {
if (!is_syncing && HasRemoteChanges)
if (!is_syncing && !HasLocalChanges && HasRemoteChanges)
SyncDownBase ();
})
).Start ();
}
// Stop polling when the connection to the irc channel is succesful
this.listener.Connected += delegate {
this.poll_interval = PollInterval.Long;
this.last_poll = DateTime.Now;
if (!is_syncing) {
// Check for changes manually one more time
if (HasRemoteChanges)
SyncDownBase ();
// Push changes that were made since the last disconnect
if (HasUnsyncedChanges)
SyncUpBase ();
}
};
// Start polling when the connection to the channel is lost
this.listener.Disconnected += delegate {
this.poll_interval = PollInterval.Short;
SparkleHelpers.DebugInfo (Name, "Falling back to polling");
};
// Fetch changes when there is a message in the irc channel
this.listener.Received += delegate (SparkleAnnouncement announcement) {
string identifier = Identifier;
if (announcement.FolderIdentifier.Equals (identifier) &&
!announcement.Message.Equals (CurrentRevision)) {
while (this.is_syncing)
System.Threading.Thread.Sleep (100);
SparkleHelpers.DebugInfo ("Listener", "Syncing due to announcement");
SyncDownBase ();
} else {
if (announcement.FolderIdentifier.Equals (identifier))
SparkleHelpers.DebugInfo ("Listener", "Not syncing, message is for current revision");
}
};
this.listener.Connected += ListenerConnectedDelegate;
this.listener.Disconnected += ListenerDisconnectedDelegate;
this.listener.AnnouncementReceived += ListenerAnnouncementReceivedDelegate;
// Start listening
if (!this.listener.IsConnected && !this.listener.IsConnecting)
@ -480,6 +421,53 @@ namespace SparkleLib {
}
// Stop polling when the connection to the irc channel is succesful
private void ListenerConnectedDelegate ()
{
this.poll_interval = PollInterval.Long;
this.last_poll = DateTime.Now;
if (!is_syncing) {
// Check for changes manually one more time
if (HasRemoteChanges)
SyncDownBase ();
// Push changes that were made since the last disconnect
if (HasUnsyncedChanges)
SyncUpBase ();
}
}
// Start polling when the connection to the channel is lost
private void ListenerDisconnectedDelegate ()
{
this.poll_interval = PollInterval.Short;
SparkleHelpers.DebugInfo (Name, "Falling back to polling");
}
// Fetch changes when there is an announcement
private void ListenerAnnouncementReceivedDelegate (SparkleAnnouncement announcement)
{
string identifier = Identifier;
if (announcement.FolderIdentifier.Equals (identifier) &&
!announcement.Message.Equals (CurrentRevision)) {
while (this.is_syncing)
System.Threading.Thread.Sleep (100);
SparkleHelpers.DebugInfo ("Listener", "Syncing due to announcement");
SyncDownBase ();
} else {
if (announcement.FolderIdentifier.Equals (identifier))
SparkleHelpers.DebugInfo ("Listener", "Not syncing, message is for current revision");
}
}
private DateTime progress_last_change = DateTime.Now;
private TimeSpan progress_change_interval = new TimeSpan (0, 0, 0, 1);
@ -535,8 +523,15 @@ namespace SparkleLib {
public void Dispose ()
{
this.remote_timer.Stop ();
this.remote_timer.Dispose ();
this.listener.Connected -= ListenerConnectedDelegate;
this.listener.Disconnected -= ListenerDisconnectedDelegate;
this.listener.AnnouncementReceived -= ListenerAnnouncementReceivedDelegate;
this.listener.Dispose ();
this.watcher.Dispose ();
}
}
}

View file

@ -910,7 +910,8 @@ namespace SparkleShare {
Directory.Move (this.fetcher.TargetFolder, target_folder_path);
string backend = SparkleFetcherBase.GetBackend (this.fetcher.RemoteUrl.AbsolutePath);
SparkleConfig.DefaultConfig.AddFolder (target_folder_name, this.fetcher.RemoteUrl.ToString (), backend);
SparkleConfig.DefaultConfig.AddFolder (target_folder_name, this.fetcher.Identifier,
this.fetcher.RemoteUrl.ToString (), backend);
if (FolderFetched != null)
FolderFetched (this.fetcher.RemoteUrl.ToString (), this.fetcher.Warnings.ToArray ());