listener: move reconnect timer from repo. rename channelmessage event. add debuginfo
This commit is contained in:
parent
aab6e36e4d
commit
d47435573c
|
@ -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);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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 &&
|
||||
|
@ -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)
|
||||
{
|
||||
|
|
Loading…
Reference in a new issue