[notifications] Reconnect to the IRC server after a disconnect

This commit is contained in:
Hylke Bons 2011-03-01 23:13:43 +00:00
parent 60ffb7450d
commit bb5229d4d3
2 changed files with 47 additions and 45 deletions

View file

@ -40,28 +40,26 @@ namespace SparkleLib {
public SparkleListener (string server, string folder_name, string user_email) public SparkleListener (string server, string folder_name, string user_email)
{ {
Server = server; // This is SparkleShare's centralized notification service.
//Channel = GetSHA1 (server + folder_name); // Don't worry, we only use this server as a backup if you
Channel = folder_name; // 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) if (!user_email.Equals ("") && user_email != null)
Nick = GetSHA1 (user_email + "sparkles"); Nick = GetSHA1 (folder_name + user_email + "sparkles");
else else
Nick = GetSHA1 (DateTime.Now.ToString () + "sparkles"); 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"; Channel = "#sparkletest";
Server = "irc.gnome.org";
Client = new IrcClient () { Client = new IrcClient () {
PingTimeout = 120, PingTimeout = 180,
SocketSendTimeout = 120, PingInterval = 90
SocketReceiveTimeout = 120,
AutoRetry = true,
AutoReconnect = true,
AutoRejoin = true
}; };
} }

View file

@ -308,32 +308,37 @@ namespace SparkleLib {
// Listen to the irc channel on the server... // Listen to the irc channel on the server...
Listener = new SparkleListener (Domain, "#" + RemoteName, UserEmail); 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 () { RemoteTimer = new Timer () {
Interval = 90000 Interval = 60000
}; };
RemoteTimer.Elapsed += delegate { RemoteTimer.Elapsed += delegate {
if (_IsPolling) if (_IsPolling) {
CheckForRemoteChanges (); CheckForRemoteChanges ();
if (!Listener.Client.IsConnected) {
SparkleHelpers.DebugInfo ("Irc", "[" + Name + "] Trying to reconnect...");
Listener.Listen ();
}
}
if (_HasUnsyncedChanges) if (_HasUnsyncedChanges)
Push (); 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 // Stop polling when the connection to the irc channel is succesful
Listener.Client.OnConnected += delegate { Listener.Client.OnConnected += delegate {
_IsPolling = false;
// Check for changes manually one more time // Check for changes manually one more time
CheckForRemoteChanges (); CheckForRemoteChanges ();
@ -341,19 +346,23 @@ namespace SparkleLib {
if (_HasUnsyncedChanges) if (_HasUnsyncedChanges)
Push (); Push ();
SparkleHelpers.DebugInfo ("Irc", "[" + Name + "] Connected. Now listening..."); SparkleHelpers.DebugInfo ("Irc", "[" + Name + "] Connected. Now listening... (" + Listener.Server + ")");
_IsPolling = false;
}; };
// 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 // Start polling when the connection to the irc channel is lost
Listener.Client.OnDisconnected += delegate { Listener.Client.OnDisconnected += delegate {
SparkleHelpers.DebugInfo ("Irc", "[" + Name + "] Lost connection. Falling back to polling..."); SparkleHelpers.DebugInfo ("Irc", "[" + Name + "] Lost connection. Falling back to polling...");
_IsPolling = true; _IsPolling = true;
CheckForRemoteChanges ();
}; };
@ -361,8 +370,9 @@ namespace SparkleLib {
Listener.Client.OnChannelMessage += delegate (object o, IrcEventArgs args) { Listener.Client.OnChannelMessage += delegate (object o, IrcEventArgs args) {
SparkleHelpers.DebugInfo ("Irc", "[" + Name + "] Was notified of a remote change."); SparkleHelpers.DebugInfo ("Irc", "[" + Name + "] Was notified of a remote change.");
string message = args.Data.Message.Trim ();
if (!args.Data.Message.Equals (_CurrentHash)) {
if (!message.Equals (_CurrentHash) && message.Length == 40) {
FetchRequests++; FetchRequests++;
@ -406,9 +416,7 @@ namespace SparkleLib {
}; };
if (_IsPolling) RemoteTimer.Start ();
RemoteTimer.Start ();
LocalTimer.Start (); LocalTimer.Start ();
// Add everything that changed // Add everything that changed
@ -555,9 +563,7 @@ namespace SparkleLib {
} finally { } finally {
if (_IsPolling) RemoteTimer.Start ();
RemoteTimer.Start ();
LocalTimer.Start (); LocalTimer.Start ();
} }
@ -678,9 +684,7 @@ namespace SparkleLib {
} }
RemoteTimer.Start ();
if (_IsPolling)
RemoteTimer.Start ();
}; };
@ -989,8 +993,8 @@ namespace SparkleLib {
if (entry_line.StartsWith (":")) { if (entry_line.StartsWith (":")) {
string change_type = entry_line.Substring (37, 1); string change_type = entry_line [37].ToString ();
string file_path = entry_line.Substring (39, entry_line.Length - 39); string file_path = entry_line.Substring (39);
if (change_type.Equals ("A")) { if (change_type.Equals ("A")) {