2011-06-01 23:08:05 +00:00
|
|
|
// SparkleShare, a collaboration and sharing tool.
|
2017-07-23 12:47:54 +00:00
|
|
|
// Copyright (C) 2010 Hylke Bons <hi@planetpeanut.uk>
|
2011-05-14 17:10:24 +00:00
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
2018-04-09 03:41:59 +00:00
|
|
|
// it under the terms of the GNU Lesser General Public License as
|
|
|
|
// published by the Free Software Foundation, either version 3 of the
|
2013-10-11 15:13:46 +00:00
|
|
|
// License, or (at your option) any later version.
|
2011-05-14 17:10:24 +00:00
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2011-05-28 23:52:46 +00:00
|
|
|
using System.Timers;
|
2011-05-14 17:10:24 +00:00
|
|
|
|
2016-03-31 08:35:26 +00:00
|
|
|
namespace Sparkles {
|
2011-05-14 17:10:24 +00:00
|
|
|
|
2014-04-18 13:06:46 +00:00
|
|
|
public enum DisconnectReason {
|
|
|
|
None,
|
|
|
|
TimeOut,
|
|
|
|
SystemSleep
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-14 17:10:24 +00:00
|
|
|
// A persistent connection to the server that
|
|
|
|
// listens for change notifications
|
2016-03-30 23:36:31 +00:00
|
|
|
public abstract class BaseListener {
|
2011-05-14 17:10:24 +00:00
|
|
|
|
2012-07-18 10:35:22 +00:00
|
|
|
public event Action Connected = delegate { };
|
2018-04-09 03:41:59 +00:00
|
|
|
|
2014-04-18 13:06:46 +00:00
|
|
|
public event DisconnectedEventHandler Disconnected = delegate { };
|
|
|
|
public delegate void DisconnectedEventHandler (DisconnectReason reason);
|
2011-05-14 17:10:24 +00:00
|
|
|
|
2012-07-18 10:35:22 +00:00
|
|
|
public event AnnouncementReceivedEventHandler AnnouncementReceived = delegate { };
|
2016-03-30 23:56:48 +00:00
|
|
|
public delegate void AnnouncementReceivedEventHandler (Announcement announcement);
|
2012-02-01 16:23:49 +00:00
|
|
|
|
|
|
|
public readonly Uri Server;
|
|
|
|
|
2011-05-22 00:02:16 +00:00
|
|
|
public abstract void Connect ();
|
2011-05-14 17:10:24 +00:00
|
|
|
public abstract bool IsConnected { get; }
|
2012-02-01 16:23:49 +00:00
|
|
|
public abstract bool IsConnecting { get; }
|
2012-07-17 13:09:57 +00:00
|
|
|
|
|
|
|
|
2016-03-30 23:56:48 +00:00
|
|
|
protected abstract void AnnounceInternal (Announcement announcent);
|
2012-02-11 19:23:38 +00:00
|
|
|
protected abstract void AlsoListenToInternal (string folder_identifier);
|
2012-02-01 16:23:49 +00:00
|
|
|
|
2011-11-12 17:47:39 +00:00
|
|
|
protected List<string> channels = new List<string> ();
|
2012-02-01 16:23:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
private int max_recent_announcements = 10;
|
|
|
|
|
2016-03-30 23:56:48 +00:00
|
|
|
private Dictionary<string, List<Announcement>> recent_announcements =
|
|
|
|
new Dictionary<string, List<Announcement>> ();
|
2012-02-01 16:23:49 +00:00
|
|
|
|
2016-03-30 23:56:48 +00:00
|
|
|
private Dictionary<string, Announcement> queue_up = new Dictionary<string, Announcement> ();
|
2012-02-01 16:23:49 +00:00
|
|
|
|
|
|
|
private Timer reconnect_timer = new Timer {
|
|
|
|
Interval = 60 * 1000,
|
|
|
|
Enabled = true
|
|
|
|
};
|
2011-05-22 00:02:16 +00:00
|
|
|
|
2011-11-12 17:47:39 +00:00
|
|
|
|
2016-03-30 23:36:31 +00:00
|
|
|
public BaseListener (Uri server, string folder_identifier)
|
2011-07-23 14:27:07 +00:00
|
|
|
{
|
2012-02-01 16:23:49 +00:00
|
|
|
Server = server;
|
|
|
|
this.channels.Add (folder_identifier);
|
2011-07-23 14:27:07 +00:00
|
|
|
|
2015-10-13 15:02:22 +00:00
|
|
|
this.reconnect_timer.Elapsed += OnTimerElapsed;
|
2011-06-28 18:36:47 +00:00
|
|
|
this.reconnect_timer.Start ();
|
2011-05-28 23:52:46 +00:00
|
|
|
}
|
2011-05-22 00:02:16 +00:00
|
|
|
|
2015-10-13 15:02:22 +00:00
|
|
|
private void OnTimerElapsed(object sender, EventArgs args)
|
|
|
|
{
|
|
|
|
if (!IsConnected && !IsConnecting)
|
|
|
|
Reconnect ();
|
|
|
|
}
|
2011-05-22 00:02:16 +00:00
|
|
|
|
2016-03-30 23:56:48 +00:00
|
|
|
public void Announce (Announcement announcement)
|
2011-07-23 14:27:07 +00:00
|
|
|
{
|
2012-02-01 16:23:49 +00:00
|
|
|
if (!IsRecentAnnouncement (announcement)) {
|
2011-11-05 21:09:09 +00:00
|
|
|
if (IsConnected) {
|
2016-03-30 23:36:31 +00:00
|
|
|
Logger.LogInfo ("Listener", "Announcing message " + announcement.Message +
|
2012-07-17 13:09:57 +00:00
|
|
|
" to " + announcement.FolderIdentifier + " on " + Server);
|
2011-11-05 21:09:09 +00:00
|
|
|
|
2012-02-11 19:23:38 +00:00
|
|
|
AnnounceInternal (announcement);
|
2012-02-01 16:23:49 +00:00
|
|
|
AddRecentAnnouncement (announcement);
|
|
|
|
|
2011-11-05 21:09:09 +00:00
|
|
|
} else {
|
2016-03-30 23:36:31 +00:00
|
|
|
Logger.LogInfo ("Listener", "Can't send message to " + Server + ". Queuing message");
|
2011-11-05 21:09:09 +00:00
|
|
|
this.queue_up [announcement.FolderIdentifier] = announcement;
|
2011-05-22 17:37:36 +00:00
|
|
|
}
|
2011-05-22 17:52:50 +00:00
|
|
|
|
2011-05-22 00:02:16 +00:00
|
|
|
} else {
|
2016-03-30 23:36:31 +00:00
|
|
|
Logger.LogInfo ("Listener", "Already processed message " + announcement.Message +
|
2012-07-17 13:09:57 +00:00
|
|
|
" to " + announcement.FolderIdentifier + " from " + Server);
|
2011-05-22 17:37:36 +00:00
|
|
|
}
|
2012-02-01 16:23:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-11 19:23:38 +00:00
|
|
|
public void AlsoListenTo (string channel)
|
2012-02-01 16:23:49 +00:00
|
|
|
{
|
2012-08-10 19:18:41 +00:00
|
|
|
if (!this.channels.Contains (channel))
|
2012-02-01 16:23:49 +00:00
|
|
|
this.channels.Add (channel);
|
2012-08-10 19:18:41 +00:00
|
|
|
|
|
|
|
if (IsConnected) {
|
2016-03-30 23:36:31 +00:00
|
|
|
Logger.LogInfo ("Listener", "Subscribing to channel " + channel + " on " + Server);
|
2012-02-11 19:23:38 +00:00
|
|
|
AlsoListenToInternal (channel);
|
2012-02-01 16:23:49 +00:00
|
|
|
}
|
2011-05-14 17:10:24 +00:00
|
|
|
}
|
|
|
|
|
2011-05-22 00:02:16 +00:00
|
|
|
|
2011-05-28 23:52:46 +00:00
|
|
|
public void Reconnect ()
|
|
|
|
{
|
2016-03-30 23:36:31 +00:00
|
|
|
Logger.LogInfo ("Listener", "Trying to reconnect to " + Server);
|
2011-05-28 23:52:46 +00:00
|
|
|
Connect ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-14 17:10:24 +00:00
|
|
|
public void OnConnected ()
|
|
|
|
{
|
2012-08-10 19:18:41 +00:00
|
|
|
foreach (string channel in this.channels.GetRange (0, this.channels.Count)) {
|
2016-03-30 23:36:31 +00:00
|
|
|
Logger.LogInfo ("Listener", "Subscribing to channel " + channel + " on " + Server);
|
2012-08-10 19:18:41 +00:00
|
|
|
AlsoListenToInternal (channel);
|
|
|
|
}
|
|
|
|
|
2016-03-30 23:36:31 +00:00
|
|
|
Logger.LogInfo ("Listener", "Listening for announcements on " + Server);
|
2012-07-18 10:35:22 +00:00
|
|
|
Connected ();
|
2011-05-19 15:41:46 +00:00
|
|
|
|
2011-05-22 17:37:36 +00:00
|
|
|
if (this.queue_up.Count > 0) {
|
2016-03-30 23:36:31 +00:00
|
|
|
Logger.LogInfo ("Listener", "Delivering " + this.queue_up.Count + " queued messages...");
|
2011-07-23 14:27:07 +00:00
|
|
|
|
2016-03-30 23:56:48 +00:00
|
|
|
foreach (KeyValuePair<string, Announcement> item in this.queue_up) {
|
|
|
|
Announcement announcement = item.Value;
|
2012-02-11 19:23:38 +00:00
|
|
|
Announce (announcement);
|
2011-05-28 23:52:46 +00:00
|
|
|
}
|
2011-05-19 15:41:46 +00:00
|
|
|
}
|
2011-05-14 17:10:24 +00:00
|
|
|
}
|
|
|
|
|
2011-05-22 00:02:16 +00:00
|
|
|
|
2014-04-18 13:06:46 +00:00
|
|
|
public void OnDisconnected (DisconnectReason reason, string message)
|
2011-05-14 17:10:24 +00:00
|
|
|
{
|
2016-03-30 23:36:31 +00:00
|
|
|
Logger.LogInfo ("Listener", "Disconnected from " + Server + ": " + message);
|
2014-04-18 13:06:46 +00:00
|
|
|
Disconnected (reason);
|
2011-05-14 17:10:24 +00:00
|
|
|
}
|
|
|
|
|
2011-05-22 00:02:16 +00:00
|
|
|
|
2016-03-30 23:56:48 +00:00
|
|
|
public void OnAnnouncement (Announcement announcement)
|
2011-05-14 17:10:24 +00:00
|
|
|
{
|
2016-03-30 23:36:31 +00:00
|
|
|
Logger.LogInfo ("Listener", "Got message " + announcement.Message + " from " +
|
2012-02-01 16:23:49 +00:00
|
|
|
announcement.FolderIdentifier + " on " + Server);
|
2011-11-03 16:22:12 +00:00
|
|
|
|
2012-09-08 09:48:01 +00:00
|
|
|
if (IsRecentAnnouncement (announcement))
|
|
|
|
return;
|
2011-11-05 21:09:09 +00:00
|
|
|
|
2011-11-12 17:47:39 +00:00
|
|
|
AddRecentAnnouncement (announcement);
|
2012-07-18 10:35:22 +00:00
|
|
|
AnnouncementReceived (announcement);
|
2011-05-14 17:10:24 +00:00
|
|
|
}
|
2011-05-22 17:52:50 +00:00
|
|
|
|
|
|
|
|
2012-02-01 16:23:49 +00:00
|
|
|
public virtual void Dispose ()
|
2011-11-05 21:09:09 +00:00
|
|
|
{
|
2015-01-13 22:18:47 +00:00
|
|
|
if (this.reconnect_timer != null) {
|
|
|
|
this.reconnect_timer.Stop ();
|
2015-10-13 15:02:22 +00:00
|
|
|
|
|
|
|
this.reconnect_timer.Elapsed -= OnTimerElapsed;
|
2015-01-13 22:18:47 +00:00
|
|
|
this.reconnect_timer.Dispose ();
|
2015-10-13 15:02:22 +00:00
|
|
|
|
|
|
|
this.reconnect_timer = null;
|
2015-01-13 22:18:47 +00:00
|
|
|
}
|
2012-02-01 16:23:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-30 23:56:48 +00:00
|
|
|
private bool IsRecentAnnouncement (Announcement announcement)
|
2012-02-01 16:23:49 +00:00
|
|
|
{
|
2012-07-17 13:09:57 +00:00
|
|
|
if (!this.recent_announcements.ContainsKey (announcement.FolderIdentifier)) {
|
2011-11-05 21:09:09 +00:00
|
|
|
return false;
|
2011-11-12 17:47:39 +00:00
|
|
|
|
2011-11-05 21:09:09 +00:00
|
|
|
} else {
|
2016-03-30 23:56:48 +00:00
|
|
|
foreach (Announcement recent_announcement in GetRecentAnnouncements (announcement.FolderIdentifier)) {
|
2012-09-08 09:05:54 +00:00
|
|
|
if (recent_announcement.Message.Equals (announcement.Message))
|
2011-11-05 21:09:09 +00:00
|
|
|
return true;
|
|
|
|
}
|
2011-11-12 17:47:39 +00:00
|
|
|
|
2011-11-05 21:09:09 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-30 23:56:48 +00:00
|
|
|
private List<Announcement> GetRecentAnnouncements (string folder_identifier)
|
2011-11-05 21:09:09 +00:00
|
|
|
{
|
2011-11-12 17:47:39 +00:00
|
|
|
if (!this.recent_announcements.ContainsKey (folder_identifier))
|
2016-03-30 23:56:48 +00:00
|
|
|
this.recent_announcements [folder_identifier] = new List<Announcement> ();
|
2011-11-12 17:47:39 +00:00
|
|
|
|
2012-07-17 13:09:57 +00:00
|
|
|
return this.recent_announcements [folder_identifier];
|
2011-11-05 21:09:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-30 23:56:48 +00:00
|
|
|
private void AddRecentAnnouncement (Announcement announcement)
|
2011-11-05 21:09:09 +00:00
|
|
|
{
|
2016-03-30 23:56:48 +00:00
|
|
|
List<Announcement> recent_announcements =
|
2012-02-01 16:23:49 +00:00
|
|
|
GetRecentAnnouncements (announcement.FolderIdentifier);
|
2011-11-05 21:09:09 +00:00
|
|
|
|
2012-02-01 16:23:49 +00:00
|
|
|
if (!IsRecentAnnouncement (announcement))
|
2011-11-05 21:09:09 +00:00
|
|
|
recent_announcements.Add (announcement);
|
|
|
|
|
|
|
|
if (recent_announcements.Count > this.max_recent_announcements)
|
2012-07-17 13:09:57 +00:00
|
|
|
recent_announcements.RemoveRange (0, recent_announcements.Count - this.max_recent_announcements);
|
2011-05-22 17:52:50 +00:00
|
|
|
}
|
2011-05-14 17:10:24 +00:00
|
|
|
}
|
|
|
|
}
|