listener: move reconnect timer from repo. rename channelmessage event. add debuginfo

This commit is contained in:
Hylke Bons 2011-05-29 00:52:46 +01:00
parent 9549ae3816
commit ec6c69452a
3 changed files with 54 additions and 37 deletions

View file

@ -17,6 +17,7 @@
using System;
using System.Collections.Generic;
using System.Timers;
namespace SparkleLib {
@ -87,8 +88,8 @@ namespace SparkleLib {
// We've been notified about a remote
// change by the channel
public event RemoteChangeEventHandler RemoteChange;
public delegate void RemoteChangeEventHandler (SparkleAnnouncement announcement);
public event AnnouncementEventHandler Announcement;
public delegate void AnnouncementEventHandler (SparkleAnnouncement announcement);
public abstract void Connect ();
@ -103,9 +104,16 @@ namespace SparkleLib {
protected List<SparkleAnnouncement> queue_down = new List<SparkleAnnouncement> ();
protected bool is_connecting;
protected string server;
protected Timer reconnect_timer = new Timer { Interval = 60 * 1000};
public SparkleListenerBase (string server, string folder_identifier, NotificationServerType type) {
this.reconnect_timer.Elapsed += delegate {
if (!IsConnected && !IsConnecting)
Reconnect ();
};
public SparkleListenerBase (string server, string folder_identifier, NotificationServerType type) { }
this.reconnect_timer.Start ();
}
public void AnnounceBase (SparkleAnnouncement announcement) {
@ -133,6 +141,13 @@ namespace SparkleLib {
}
public void Reconnect ()
{
SparkleHelpers.DebugInfo ("Listener", "Trying to reconnect to " + this.server);
Connect ();
}
public void OnConnected ()
{
SparkleHelpers.DebugInfo ("Listener", "Connected to " + Server);
@ -142,10 +157,10 @@ namespace SparkleLib {
if (this.queue_up.Count > 0) {
SparkleHelpers.DebugInfo ("Listener", "Delivering queued messages...");
foreach (SparkleAnnouncement announcement in this.queue_up)
foreach (SparkleAnnouncement announcement in this.queue_up) {
AnnounceBase (announcement);
this.queue_up = new List<SparkleAnnouncement> ();
this.queue_up.Remove (announcement);
}
}
}
@ -159,14 +174,14 @@ namespace SparkleLib {
}
public void OnRemoteChange (SparkleAnnouncement announcement)
public void OnAnnouncement (SparkleAnnouncement announcement)
{
SparkleHelpers.DebugInfo ("Listener", "Got message from " + announcement.FolderIdentifier + " on " + this.server);
this.queue_down.Add (announcement);
if (RemoteChange != null)
RemoteChange (announcement);
if (Announcement != null)
Announcement (announcement);
}

View file

@ -46,8 +46,8 @@ namespace SparkleLib {
base.channels.Add ("#" + folder_identifier);
this.client = new IrcClient () {
PingTimeout = 180,
PingInterval = 90
PingTimeout = 130,
PingInterval = 60
};
this.client.OnConnected += delegate {
@ -59,10 +59,14 @@ namespace SparkleLib {
OnDisconnected ();
};
this.client.OnError += delegate {
OnDisconnected ();
};
this.client.OnChannelMessage += delegate (object o, IrcEventArgs args) {
string message = args.Data.Message.Trim ();
string folder_id = args.Data.Channel.Substring (1); // remove the starting hash
OnRemoteChange (new SparkleAnnouncement (folder_id, message));
OnAnnouncement (new SparkleAnnouncement (folder_id, message));
};
}
@ -89,15 +93,18 @@ namespace SparkleLib {
this.client.Connect (new string [] {base.server}, 6667);
this.client.Login (this.nick, this.nick);
foreach (string channel in base.channels)
foreach (string channel in base.channels) {
SparkleHelpers.DebugInfo ("ListenerIrc", "Joining channel " + channel);
this.client.RfcJoin (channel);
}
// List to the channel, this blocks the thread
this.client.Listen ();
// Disconnect when we time out
this.client.Disconnect ();
} catch (Meebey.SmartIrc4net.ConnectionException e) {
} catch (ConnectionException e) {
SparkleHelpers.DebugInfo ("ListenerIrc", "Could not connect to " + Server + ": " + e.Message);
}
})
@ -110,6 +117,7 @@ namespace SparkleLib {
public override void AlsoListenTo (string folder_identifier)
{
string channel = "#" + folder_identifier;
SparkleHelpers.DebugInfo ("ListenerIrc", "Joining channel " + channel);
base.channels.Add (channel);
this.client.RfcJoin (channel);
}

View file

@ -99,9 +99,6 @@ namespace SparkleLib {
SyncDownBase ();
}
if (this.is_polling && !this.listener.IsConnecting && !this.listener.IsConnected)
this.listener.Connect ();
// In the unlikely case that we haven't synced up our
// changes or the server was down, sync up again
if (HasUnsyncedChanges)
@ -124,17 +121,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");
TextWriter writer = new StreamWriter (file_path);
writer.WriteLine (":)");
writer.Close ();
}
public bool ServerOnline {
get {
return this.server_online;
@ -211,9 +197,6 @@ namespace SparkleLib {
}
// Disposes all resourses of this object
public void Dispose ()
{
@ -268,7 +251,7 @@ namespace SparkleLib {
};
// Fetch changes when there is a message in the irc channel
this.listener.RemoteChange += delegate (SparkleAnnouncement announcement) {
this.listener.Announcement += delegate (SparkleAnnouncement announcement) {
string identifier = Identifier;
if (announcement.FolderIdentifier == identifier &&
@ -295,15 +278,15 @@ namespace SparkleLib {
{
lock (this.change_lock) {
if (this.has_changed) {
if ( this.sizebuffer.Count >= 4)
this.sizebuffer.RemoveAt (0);
if (this.sizebuffer.Count >= 4)
this.sizebuffer.RemoveAt (0);
DirectoryInfo dir_info = new DirectoryInfo (LocalPath);
this.sizebuffer.Add (CalculateFolderSize (dir_info));
if ( this.sizebuffer [0].Equals (this.sizebuffer [1]) &&
this.sizebuffer [1].Equals (this.sizebuffer [2]) &&
this.sizebuffer [2].Equals (this.sizebuffer [3])) {
if (this.sizebuffer [0].Equals (this.sizebuffer [1]) &&
this.sizebuffer [1].Equals (this.sizebuffer [2]) &&
this.sizebuffer [2].Equals (this.sizebuffer [3])) {
SparkleHelpers.DebugInfo ("Local", "[" + Name + "] Changes have settled.");
this.is_buffering = false;
@ -447,6 +430,17 @@ 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");
TextWriter writer = new StreamWriter (file_path);
writer.WriteLine (":)");
writer.Close ();
}
// Recursively gets a folder's size in bytes
private double CalculateFolderSize (DirectoryInfo parent)
{