listener: move reconnect timer from repo. rename channelmessage event. add debuginfo
This commit is contained in:
parent
9549ae3816
commit
ec6c69452a
|
@ -17,6 +17,7 @@
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Timers;
|
||||||
|
|
||||||
namespace SparkleLib {
|
namespace SparkleLib {
|
||||||
|
|
||||||
|
@ -87,8 +88,8 @@ namespace SparkleLib {
|
||||||
|
|
||||||
// We've been notified about a remote
|
// We've been notified about a remote
|
||||||
// change by the channel
|
// change by the channel
|
||||||
public event RemoteChangeEventHandler RemoteChange;
|
public event AnnouncementEventHandler Announcement;
|
||||||
public delegate void RemoteChangeEventHandler (SparkleAnnouncement announcement);
|
public delegate void AnnouncementEventHandler (SparkleAnnouncement announcement);
|
||||||
|
|
||||||
|
|
||||||
public abstract void Connect ();
|
public abstract void Connect ();
|
||||||
|
@ -103,9 +104,16 @@ namespace SparkleLib {
|
||||||
protected List<SparkleAnnouncement> queue_down = new List<SparkleAnnouncement> ();
|
protected List<SparkleAnnouncement> queue_down = new List<SparkleAnnouncement> ();
|
||||||
protected bool is_connecting;
|
protected bool is_connecting;
|
||||||
protected string server;
|
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) {
|
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 ()
|
public void OnConnected ()
|
||||||
{
|
{
|
||||||
SparkleHelpers.DebugInfo ("Listener", "Connected to " + Server);
|
SparkleHelpers.DebugInfo ("Listener", "Connected to " + Server);
|
||||||
|
@ -142,10 +157,10 @@ namespace SparkleLib {
|
||||||
|
|
||||||
if (this.queue_up.Count > 0) {
|
if (this.queue_up.Count > 0) {
|
||||||
SparkleHelpers.DebugInfo ("Listener", "Delivering queued messages...");
|
SparkleHelpers.DebugInfo ("Listener", "Delivering queued messages...");
|
||||||
foreach (SparkleAnnouncement announcement in this.queue_up)
|
foreach (SparkleAnnouncement announcement in this.queue_up) {
|
||||||
AnnounceBase (announcement);
|
AnnounceBase (announcement);
|
||||||
|
this.queue_up.Remove (announcement);
|
||||||
this.queue_up = new List<SparkleAnnouncement> ();
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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);
|
SparkleHelpers.DebugInfo ("Listener", "Got message from " + announcement.FolderIdentifier + " on " + this.server);
|
||||||
|
|
||||||
this.queue_down.Add (announcement);
|
this.queue_down.Add (announcement);
|
||||||
|
|
||||||
if (RemoteChange != null)
|
if (Announcement != null)
|
||||||
RemoteChange (announcement);
|
Announcement (announcement);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -46,8 +46,8 @@ namespace SparkleLib {
|
||||||
base.channels.Add ("#" + folder_identifier);
|
base.channels.Add ("#" + folder_identifier);
|
||||||
|
|
||||||
this.client = new IrcClient () {
|
this.client = new IrcClient () {
|
||||||
PingTimeout = 180,
|
PingTimeout = 130,
|
||||||
PingInterval = 90
|
PingInterval = 60
|
||||||
};
|
};
|
||||||
|
|
||||||
this.client.OnConnected += delegate {
|
this.client.OnConnected += delegate {
|
||||||
|
@ -59,10 +59,14 @@ namespace SparkleLib {
|
||||||
OnDisconnected ();
|
OnDisconnected ();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
this.client.OnError += delegate {
|
||||||
|
OnDisconnected ();
|
||||||
|
};
|
||||||
|
|
||||||
this.client.OnChannelMessage += delegate (object o, IrcEventArgs args) {
|
this.client.OnChannelMessage += delegate (object o, IrcEventArgs args) {
|
||||||
string message = args.Data.Message.Trim ();
|
string message = args.Data.Message.Trim ();
|
||||||
string folder_id = args.Data.Channel.Substring (1); // remove the starting hash
|
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.Connect (new string [] {base.server}, 6667);
|
||||||
this.client.Login (this.nick, this.nick);
|
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);
|
this.client.RfcJoin (channel);
|
||||||
|
}
|
||||||
|
|
||||||
// List to the channel, this blocks the thread
|
// List to the channel, this blocks the thread
|
||||||
this.client.Listen ();
|
this.client.Listen ();
|
||||||
|
|
||||||
// Disconnect when we time out
|
// Disconnect when we time out
|
||||||
this.client.Disconnect ();
|
this.client.Disconnect ();
|
||||||
} catch (Meebey.SmartIrc4net.ConnectionException e) {
|
|
||||||
|
} catch (ConnectionException e) {
|
||||||
SparkleHelpers.DebugInfo ("ListenerIrc", "Could not connect to " + Server + ": " + e.Message);
|
SparkleHelpers.DebugInfo ("ListenerIrc", "Could not connect to " + Server + ": " + e.Message);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -110,6 +117,7 @@ namespace SparkleLib {
|
||||||
public override void AlsoListenTo (string folder_identifier)
|
public override void AlsoListenTo (string folder_identifier)
|
||||||
{
|
{
|
||||||
string channel = "#" + folder_identifier;
|
string channel = "#" + folder_identifier;
|
||||||
|
SparkleHelpers.DebugInfo ("ListenerIrc", "Joining channel " + channel);
|
||||||
base.channels.Add (channel);
|
base.channels.Add (channel);
|
||||||
this.client.RfcJoin (channel);
|
this.client.RfcJoin (channel);
|
||||||
}
|
}
|
||||||
|
|
|
@ -99,9 +99,6 @@ namespace SparkleLib {
|
||||||
SyncDownBase ();
|
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
|
// In the unlikely case that we haven't synced up our
|
||||||
// changes or the server was down, sync up again
|
// changes or the server was down, sync up again
|
||||||
if (HasUnsyncedChanges)
|
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 {
|
public bool ServerOnline {
|
||||||
get {
|
get {
|
||||||
return this.server_online;
|
return this.server_online;
|
||||||
|
@ -211,9 +197,6 @@ namespace SparkleLib {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Disposes all resourses of this object
|
// Disposes all resourses of this object
|
||||||
public void Dispose ()
|
public void Dispose ()
|
||||||
{
|
{
|
||||||
|
@ -268,7 +251,7 @@ namespace SparkleLib {
|
||||||
};
|
};
|
||||||
|
|
||||||
// Fetch changes when there is a message in the irc channel
|
// 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;
|
string identifier = Identifier;
|
||||||
|
|
||||||
if (announcement.FolderIdentifier == identifier &&
|
if (announcement.FolderIdentifier == identifier &&
|
||||||
|
@ -295,15 +278,15 @@ namespace SparkleLib {
|
||||||
{
|
{
|
||||||
lock (this.change_lock) {
|
lock (this.change_lock) {
|
||||||
if (this.has_changed) {
|
if (this.has_changed) {
|
||||||
if ( this.sizebuffer.Count >= 4)
|
if (this.sizebuffer.Count >= 4)
|
||||||
this.sizebuffer.RemoveAt (0);
|
this.sizebuffer.RemoveAt (0);
|
||||||
|
|
||||||
DirectoryInfo dir_info = new DirectoryInfo (LocalPath);
|
DirectoryInfo dir_info = new DirectoryInfo (LocalPath);
|
||||||
this.sizebuffer.Add (CalculateFolderSize (dir_info));
|
this.sizebuffer.Add (CalculateFolderSize (dir_info));
|
||||||
|
|
||||||
if ( this.sizebuffer [0].Equals (this.sizebuffer [1]) &&
|
if (this.sizebuffer [0].Equals (this.sizebuffer [1]) &&
|
||||||
this.sizebuffer [1].Equals (this.sizebuffer [2]) &&
|
this.sizebuffer [1].Equals (this.sizebuffer [2]) &&
|
||||||
this.sizebuffer [2].Equals (this.sizebuffer [3])) {
|
this.sizebuffer [2].Equals (this.sizebuffer [3])) {
|
||||||
|
|
||||||
SparkleHelpers.DebugInfo ("Local", "[" + Name + "] Changes have settled.");
|
SparkleHelpers.DebugInfo ("Local", "[" + Name + "] Changes have settled.");
|
||||||
this.is_buffering = false;
|
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
|
// Recursively gets a folder's size in bytes
|
||||||
private double CalculateFolderSize (DirectoryInfo parent)
|
private double CalculateFolderSize (DirectoryInfo parent)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in a new issue