Permalink
Browse files

controller: Code cleanups

  • Loading branch information...
1 parent 6603a18 commit 6072f3177cef43161ad0b492d0fc58e6b9615bed @hbons committed Jul 17, 2012
@@ -105,7 +105,6 @@ public SparkleConfig (string config_path, string config_file_name)
CreateInitialConfig ();
} catch (XmlException) {
-
FileInfo file = new FileInfo (FullPath);
if (file.Length == 0) {
@@ -180,10 +179,10 @@ private void CreateInitialConfig ()
set {
SparkleUser user = (SparkleUser) value;
- XmlNode name_node = SelectSingleNode ("/sparkleshare/user/name/text()");
+ XmlNode name_node = SelectSingleNode ("/sparkleshare/user/name/text()");
name_node.InnerText = user.Name;
- XmlNode email_node = SelectSingleNode ("/sparkleshare/user/email/text()");
+ XmlNode email_node = SelectSingleNode ("/sparkleshare/user/email/text()");
email_node.InnerText = user.Email;
Save ();
@@ -205,16 +204,16 @@ private void CreateInitialConfig ()
public void AddFolder (string name, string identifier, string url, string backend)
{
- XmlNode node_name = CreateElement ("name");
+ XmlNode node_name = CreateElement ("name");
node_name.InnerText = name;
- XmlNode node_identifier = CreateElement ("identifier");
+ XmlNode node_identifier = CreateElement ("identifier");
node_identifier.InnerText = identifier;
- XmlNode node_url = CreateElement ("url");
+ XmlNode node_url = CreateElement ("url");
node_url.InnerText = url;
- XmlNode node_backend = CreateElement ("backend");
+ XmlNode node_backend = CreateElement ("backend");
node_backend.InnerText = backend;
XmlNode node_folder = CreateNode (XmlNodeType.Element, "folder", null);
@@ -365,27 +364,18 @@ public void SetConfigOption (string name, string content)
node_root.AppendChild (node);
}
- SparkleHelpers.DebugInfo ("Config", "Updated " + name + ":" + content);
+ SparkleHelpers.DebugInfo ("Config", "Updated option " + name + ":" + content);
Save ();
}
private void Save ()
{
if (!File.Exists (FullPath))
- throw new ConfigFileNotFoundException (FullPath + " does not exist");
+ throw new FileNotFoundException (FullPath + " does not exist");
Save (FullPath);
- SparkleHelpers.DebugInfo ("Config", "Updated \"" + FullPath + "\"");
- }
- }
-
-
- public class ConfigFileNotFoundException : Exception {
-
- public ConfigFileNotFoundException (string message) : base (message)
- {
+ SparkleHelpers.DebugInfo ("Config", "Writen to '" + FullPath + "'");
}
}
}
-
@@ -36,14 +36,14 @@ public abstract class SparkleListenerBase {
public readonly Uri Server;
-
public abstract void Connect ();
public abstract bool IsConnected { get; }
public abstract bool IsConnecting { get; }
+
+
protected abstract void AnnounceInternal (SparkleAnnouncement announcent);
protected abstract void AlsoListenToInternal (string folder_identifier);
-
protected List<string> channels = new List<string> ();
@@ -52,11 +52,8 @@ public abstract class SparkleListenerBase {
private Dictionary<string, List<SparkleAnnouncement>> recent_announcements =
new Dictionary<string, List<SparkleAnnouncement>> ();
- private Dictionary<string, SparkleAnnouncement> queue_up =
- new Dictionary<string, SparkleAnnouncement> ();
-
- private Dictionary<string, SparkleAnnouncement> queue_down =
- new Dictionary<string, SparkleAnnouncement> ();
+ private Dictionary<string, SparkleAnnouncement> queue_up = new Dictionary<string, SparkleAnnouncement> ();
+ private Dictionary<string, SparkleAnnouncement> queue_down = new Dictionary<string, SparkleAnnouncement> ();
private Timer reconnect_timer = new Timer {
Interval = 60 * 1000,
@@ -82,34 +79,28 @@ public void Announce (SparkleAnnouncement announcement)
{
if (!IsRecentAnnouncement (announcement)) {
if (IsConnected) {
- SparkleHelpers.DebugInfo ("Listener",
- "Announcing message " + announcement.Message + " to " +
- announcement.FolderIdentifier + " on " + Server);
+ SparkleHelpers.DebugInfo ("Listener", "Announcing message " + announcement.Message +
+ " to " + announcement.FolderIdentifier + " on " + Server);
AnnounceInternal (announcement);
AddRecentAnnouncement (announcement);
} else {
- SparkleHelpers.DebugInfo ("Listener",
- "Can't send message to " +
- Server + ". Queuing message");
-
+ SparkleHelpers.DebugInfo ("Listener", "Can't send message to " + Server + ". Queuing message");
this.queue_up [announcement.FolderIdentifier] = announcement;
}
} else {
- SparkleHelpers.DebugInfo ("Listener",
- "Already processed message " + announcement.Message + " to " +
- announcement.FolderIdentifier + " from " + Server);
+ SparkleHelpers.DebugInfo ("Listener", "Already processed message " + announcement.Message +
+ " to " + announcement.FolderIdentifier + " from " + Server);
}
}
public void AlsoListenTo (string channel)
{
if (!this.channels.Contains (channel) && IsConnected) {
- SparkleHelpers.DebugInfo ("Listener",
- "Subscribing to channel " + channel + " on " + Server);
+ SparkleHelpers.DebugInfo ("Listener", "Subscribing to channel " + channel + " on " + Server);
this.channels.Add (channel);
AlsoListenToInternal (channel);
@@ -132,8 +123,7 @@ public void OnConnected ()
Connected ();
if (this.queue_up.Count > 0) {
- SparkleHelpers.DebugInfo ("Listener",
- "Delivering " + this.queue_up.Count + " queued messages...");
+ SparkleHelpers.DebugInfo ("Listener", "Delivering " + this.queue_up.Count + " queued messages...");
foreach (KeyValuePair<string, SparkleAnnouncement> item in this.queue_up) {
SparkleAnnouncement announcement = item.Value;
@@ -161,15 +151,13 @@ public void OnAnnouncement (SparkleAnnouncement announcement)
announcement.FolderIdentifier + " on " + Server);
if (IsRecentAnnouncement (announcement)) {
- SparkleHelpers.DebugInfo ("Listener",
- "Ignoring previously processed message " + announcement.Message +
+ SparkleHelpers.DebugInfo ("Listener", "Ignoring previously processed message " + announcement.Message +
" from " + announcement.FolderIdentifier + " on " + Server);
return;
}
- SparkleHelpers.DebugInfo ("Listener",
- "Processing message " + announcement.Message + " from " +
+ SparkleHelpers.DebugInfo ("Listener", "Processing message " + announcement.Message + " from " +
announcement.FolderIdentifier + " on " + Server);
AddRecentAnnouncement (announcement);
@@ -188,16 +176,12 @@ public virtual void Dispose ()
private bool IsRecentAnnouncement (SparkleAnnouncement announcement)
{
- if (!this.recent_announcements
- .ContainsKey (announcement.FolderIdentifier)) {
-
+ if (!this.recent_announcements.ContainsKey (announcement.FolderIdentifier)) {
return false;
} else {
- foreach (SparkleAnnouncement recent_announcement in
- GetRecentAnnouncements (announcement.FolderIdentifier)) {
-
- if (recent_announcement.Message.Equals (announcement.Message))
+ foreach (SparkleAnnouncement recent_announcement in GetRecentAnnouncements (announcement.FolderIdentifier)) {
+ if (recent_announcement.Message.Equals (recent_announcement.Message))
return true;
}
@@ -211,7 +195,7 @@ private List<SparkleAnnouncement> GetRecentAnnouncements (string folder_identifi
if (!this.recent_announcements.ContainsKey (folder_identifier))
this.recent_announcements [folder_identifier] = new List<SparkleAnnouncement> ();
- return (List<SparkleAnnouncement>) this.recent_announcements [folder_identifier];
+ return this.recent_announcements [folder_identifier];
}
@@ -224,8 +208,7 @@ private void AddRecentAnnouncement (SparkleAnnouncement announcement)
recent_announcements.Add (announcement);
if (recent_announcements.Count > this.max_recent_announcements)
- recent_announcements.RemoveRange (0,
- (recent_announcements.Count - this.max_recent_announcements));
+ recent_announcements.RemoveRange (0, recent_announcements.Count - this.max_recent_announcements);
}
}
}
@@ -31,8 +31,7 @@ public class SparkleListenerTcp : SparkleListenerBase {
private DateTime last_ping = DateTime.Now;
- public SparkleListenerTcp (Uri server, string folder_identifier) :
- base (server, folder_identifier)
+ public SparkleListenerTcp (Uri server, string folder_identifier) : base (server, folder_identifier)
{
}
@@ -179,12 +178,9 @@ public override void Connect ()
string folder_identifier = line.Substring (0, line.IndexOf ("!"));
string message = CleanMessage (line.Substring (line.IndexOf ("!") + 1));
- if (!folder_identifier.Equals ("debug") &&
- !String.IsNullOrEmpty (message)) {
-
- // We have a message!
+ // We have a message!
+ if (!folder_identifier.Equals ("debug") && !String.IsNullOrEmpty (message))
OnAnnouncement (new SparkleAnnouncement (folder_identifier, message));
- }
}
}
});
@@ -212,8 +208,7 @@ protected override void AlsoListenToInternal (string folder_identifier)
protected override void AnnounceInternal (SparkleAnnouncement announcement)
{
- string to_send = "announce " + announcement.FolderIdentifier
- + " " + announcement.Message + "\n";
+ string to_send = "announce " + announcement.FolderIdentifier + " " + announcement.Message + "\n";
try {
if (this.socket != null)
@@ -244,9 +239,9 @@ public override void Dispose ()
private string CleanMessage (string message)
{
- return message.Trim ()
- .Replace ("\n", "")
- .Replace ("\0", "");
+ message = message.Replace ("\n", "");
+ message = message.Replace ("\0", "");
+ return message.Trim ();
}
}
}
@@ -116,8 +116,11 @@ public abstract class SparkleRepoBase {
private string identifier;
private SparkleListenerBase listener;
- private TimeSpan poll_interval = PollInterval.Short;
- private DateTime last_poll = DateTime.Now;
+ 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
@@ -138,22 +141,19 @@ private static class PollInterval {
public SparkleRepoBase (string path, SparkleConfig config)
{
this.local_config = config;
-
- LocalPath = path;
- Name = Path.GetFileName (LocalPath);
- RemoteUrl = new Uri (this.local_config.GetUrlForFolder (Name));
- IsBuffering = false;
- ServerOnline = true;
+ LocalPath = path;
+ Name = Path.GetFileName (LocalPath);
+ RemoteUrl = new Uri (this.local_config.GetUrlForFolder (Name));
+ IsBuffering = false;
+ ServerOnline = true;
+ this.identifier = Identifier;
+ ChangeSets = GetChangeSets ();
SyncStatusChanged += delegate (SyncStatus status) {
Status = status;
};
- this.identifier = Identifier;
-
- ChangeSets = GetChangeSets ();
SparkleWatcherFactory.CreateWatcher (this);
-
new Thread (() => CreateListener ()).Start ();
this.remote_timer.Elapsed += delegate {
@@ -252,7 +252,6 @@ public void OnFileActivity (FileSystemEventArgs args)
} while (IsBuffering);
-
this.remote_timer.Start ();
}
@@ -264,6 +263,25 @@ protected void OnConflictResolved ()
}
+ protected void OnProgressChanged (double progress_percentage, string progress_speed)
+ {
+ // Only trigger the ProgressChanged event once per second
+ if (DateTime.Compare (this.progress_last_change, DateTime.Now.Subtract (this.progress_change_interval)) >= 0)
+ return;
+
+ if (ProgressChanged != null) {
+ if (progress_percentage == 100.0)
+ progress_percentage = 99.0;
+
+ ProgressPercentage = progress_percentage;
+ ProgressSpeed = progress_speed;
+ this.progress_last_change = DateTime.Now;
+
+ ProgressChanged (progress_percentage, progress_speed);
+ }
+ }
+
+
private void SyncUpBase ()
{
SparkleHelpers.DebugInfo ("SyncUp", Name + " | Initiated");
@@ -274,7 +292,6 @@ private void SyncUpBase ()
if (SyncStatusChanged != null)
SyncStatusChanged (SyncStatus.SyncUp);
-
if (SyncUp ()) {
SparkleHelpers.DebugInfo ("SyncUp", Name + " | Done");
HasUnsyncedChanges = false;
@@ -442,29 +459,6 @@ private void ListenerAnnouncementReceivedDelegate (SparkleAnnouncement announcem
}
- private DateTime progress_last_change = DateTime.Now;
- private TimeSpan progress_change_interval = new TimeSpan (0, 0, 0, 1);
-
- protected void OnProgressChanged (double progress_percentage, string progress_speed)
- {
- // Only trigger the ProgressChanged event once per second
- if (DateTime.Compare (this.progress_last_change, DateTime.Now.Subtract (this.progress_change_interval)) >= 0)
- return;
-
- if (ProgressChanged != null) {
- if (progress_percentage == 100.0)
- progress_percentage = 99.0;
-
- ProgressPercentage = progress_percentage;
- ProgressSpeed = progress_speed;
-
- this.progress_last_change = DateTime.Now;
-
- ProgressChanged (progress_percentage, progress_speed);
- }
- }
-
-
// Recursively gets a folder's size in bytes
private double CalculateSize (DirectoryInfo parent)
{
Oops, something went wrong.

0 comments on commit 6072f31

Please sign in to comment.