From bb5229d4d35de1e799b3e42aa7e445e37f357b9b Mon Sep 17 00:00:00 2001 From: Hylke Bons Date: Tue, 1 Mar 2011 23:13:43 +0000 Subject: [PATCH] [notifications] Reconnect to the IRC server after a disconnect --- SparkleLib/SparkleListener.cs | 28 +++++++-------- SparkleLib/SparkleRepo.cs | 64 +++++++++++++++++++---------------- 2 files changed, 47 insertions(+), 45 deletions(-) diff --git a/SparkleLib/SparkleListener.cs b/SparkleLib/SparkleListener.cs index 0c0aac1f..9cf65806 100644 --- a/SparkleLib/SparkleListener.cs +++ b/SparkleLib/SparkleListener.cs @@ -40,28 +40,26 @@ namespace SparkleLib { public SparkleListener (string server, string folder_name, string user_email) { - Server = server; - //Channel = GetSHA1 (server + folder_name); - Channel = folder_name; + // This is SparkleShare's centralized notification service. + // Don't worry, we only use this server as a backup if you + // don't have your own. All data needed to connect is hashed and + // we don't store any personal information ever. + // Server = "204.62.14.135"; if (!user_email.Equals ("") && user_email != null) - Nick = GetSHA1 (user_email + "sparkles"); + Nick = GetSHA1 (folder_name + user_email + "sparkles"); else Nick = GetSHA1 (DateTime.Now.ToString () + "sparkles"); - - Nick = "s" + Nick.Substring (0, 7); - // TODO: remove + Nick = "s" + Nick.Substring (0, 7); + // Channel = "#" + GetSHA1 (server + folder_name); + + Server = "irc.gnome.org"; Channel = "#sparkletest"; - Server = "irc.gnome.org"; - + Client = new IrcClient () { - PingTimeout = 120, - SocketSendTimeout = 120, - SocketReceiveTimeout = 120, - AutoRetry = true, - AutoReconnect = true, - AutoRejoin = true + PingTimeout = 180, + PingInterval = 90 }; } diff --git a/SparkleLib/SparkleRepo.cs b/SparkleLib/SparkleRepo.cs index 2f91c002..fc50e100 100644 --- a/SparkleLib/SparkleRepo.cs +++ b/SparkleLib/SparkleRepo.cs @@ -308,32 +308,37 @@ namespace SparkleLib { // Listen to the irc channel on the server... Listener = new SparkleListener (Domain, "#" + RemoteName, UserEmail); - // ...fetch remote changes every 90 seconds if that fails + // ...fetch remote changes every 60 seconds if that fails RemoteTimer = new Timer () { - Interval = 90000 + Interval = 60000 }; RemoteTimer.Elapsed += delegate { - - if (_IsPolling) + + if (_IsPolling) { + CheckForRemoteChanges (); + + if (!Listener.Client.IsConnected) { + + SparkleHelpers.DebugInfo ("Irc", "[" + Name + "] Trying to reconnect..."); + Listener.Listen (); + + } + + } if (_HasUnsyncedChanges) Push (); - if (!Listener.Client.IsConnected) { - - SparkleHelpers.DebugInfo ("Irc", "[" + Name + "] Trying to reconnect..."); - Listener.Client.Reconnect (true, true); - - } - }; // Stop polling when the connection to the irc channel is succesful Listener.Client.OnConnected += delegate { - + + _IsPolling = false; + // Check for changes manually one more time CheckForRemoteChanges (); @@ -341,19 +346,23 @@ namespace SparkleLib { if (_HasUnsyncedChanges) Push (); - SparkleHelpers.DebugInfo ("Irc", "[" + Name + "] Connected. Now listening..."); - - _IsPolling = false; + SparkleHelpers.DebugInfo ("Irc", "[" + Name + "] Connected. Now listening... (" + Listener.Server + ")"); }; + // Start polling when the connection to the irc channel is lost + Listener.Client.OnConnectionError += delegate { + + SparkleHelpers.DebugInfo ("Irc", "[" + Name + "] Lost connection. Falling back to polling..."); + _IsPolling = true; + + }; + // Start polling when the connection to the irc channel is lost Listener.Client.OnDisconnected += delegate { SparkleHelpers.DebugInfo ("Irc", "[" + Name + "] Lost connection. Falling back to polling..."); - _IsPolling = true; - CheckForRemoteChanges (); }; @@ -361,8 +370,9 @@ namespace SparkleLib { Listener.Client.OnChannelMessage += delegate (object o, IrcEventArgs args) { SparkleHelpers.DebugInfo ("Irc", "[" + Name + "] Was notified of a remote change."); - - if (!args.Data.Message.Equals (_CurrentHash)) { + string message = args.Data.Message.Trim (); + + if (!message.Equals (_CurrentHash) && message.Length == 40) { FetchRequests++; @@ -406,9 +416,7 @@ namespace SparkleLib { }; - if (_IsPolling) - RemoteTimer.Start (); - + RemoteTimer.Start (); LocalTimer.Start (); // Add everything that changed @@ -555,9 +563,7 @@ namespace SparkleLib { } finally { - if (_IsPolling) - RemoteTimer.Start (); - + RemoteTimer.Start (); LocalTimer.Start (); } @@ -678,9 +684,7 @@ namespace SparkleLib { } - - if (_IsPolling) - RemoteTimer.Start (); + RemoteTimer.Start (); }; @@ -989,8 +993,8 @@ namespace SparkleLib { if (entry_line.StartsWith (":")) { - string change_type = entry_line.Substring (37, 1); - string file_path = entry_line.Substring (39, entry_line.Length - 39); + string change_type = entry_line [37].ToString (); + string file_path = entry_line.Substring (39); if (change_type.Equals ("A")) {