SparkleShare/Sparkles/BaseListener.cs

217 lines
7.2 KiB
C#
Raw Permalink Normal View History

// SparkleShare, a collaboration and sharing tool.
2017-07-23 12:47:54 +00:00
// Copyright (C) 2010 Hylke Bons <hi@planetpeanut.uk>
//
// 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.
//
// 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;
using System.Timers;
2016-03-31 08:35:26 +00:00
namespace Sparkles {
public enum DisconnectReason {
None,
TimeOut,
SystemSleep
}
// A persistent connection to the server that
// listens for change notifications
2016-03-30 23:36:31 +00:00
public abstract class BaseListener {
public event Action Connected = delegate { };
2018-04-09 03:41:59 +00:00
public event DisconnectedEventHandler Disconnected = delegate { };
public delegate void DisconnectedEventHandler (DisconnectReason reason);
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;
public abstract void Connect ();
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
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
};
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;
this.reconnect_timer.Start ();
}
2015-10-13 15:02:22 +00:00
private void OnTimerElapsed(object sender, EventArgs args)
{
if (!IsConnected && !IsConnecting)
Reconnect ();
}
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)) {
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);
2012-02-11 19:23:38 +00:00
AnnounceInternal (announcement);
2012-02-01 16:23:49 +00:00
AddRecentAnnouncement (announcement);
} else {
2016-03-30 23:36:31 +00:00
Logger.LogInfo ("Listener", "Can't send message to " + Server + ". Queuing message");
this.queue_up [announcement.FolderIdentifier] = announcement;
}
2011-05-22 17:52:50 +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);
}
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
{
if (!this.channels.Contains (channel))
2012-02-01 16:23:49 +00:00
this.channels.Add (channel);
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
}
}
public void Reconnect ()
{
2016-03-30 23:36:31 +00:00
Logger.LogInfo ("Listener", "Trying to reconnect to " + Server);
Connect ();
}
public void OnConnected ()
{
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);
AlsoListenToInternal (channel);
}
2016-03-30 23:36:31 +00:00
Logger.LogInfo ("Listener", "Listening for announcements on " + Server);
Connected ();
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);
}
}
}
public void OnDisconnected (DisconnectReason reason, string message)
{
2016-03-30 23:36:31 +00:00
Logger.LogInfo ("Listener", "Disconnected from " + Server + ": " + message);
Disconnected (reason);
}
2016-03-30 23:56:48 +00:00
public void OnAnnouncement (Announcement announcement)
{
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);
2012-09-08 09:48:01 +00:00
if (IsRecentAnnouncement (announcement))
return;
AddRecentAnnouncement (announcement);
AnnouncementReceived (announcement);
}
2011-05-22 17:52:50 +00:00
2012-02-01 16:23:49 +00:00
public virtual void Dispose ()
{
if (this.reconnect_timer != null) {
this.reconnect_timer.Stop ();
2015-10-13 15:02:22 +00:00
this.reconnect_timer.Elapsed -= OnTimerElapsed;
this.reconnect_timer.Dispose ();
2015-10-13 15:02:22 +00:00
this.reconnect_timer = null;
}
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)) {
return false;
} else {
2016-03-30 23:56:48 +00:00
foreach (Announcement recent_announcement in GetRecentAnnouncements (announcement.FolderIdentifier)) {
if (recent_announcement.Message.Equals (announcement.Message))
return true;
}
return false;
}
}
2016-03-30 23:56:48 +00:00
private List<Announcement> GetRecentAnnouncements (string folder_identifier)
{
if (!this.recent_announcements.ContainsKey (folder_identifier))
2016-03-30 23:56:48 +00:00
this.recent_announcements [folder_identifier] = new List<Announcement> ();
2012-07-17 13:09:57 +00:00
return this.recent_announcements [folder_identifier];
}
2016-03-30 23:56:48 +00:00
private void AddRecentAnnouncement (Announcement announcement)
{
2016-03-30 23:56:48 +00:00
List<Announcement> recent_announcements =
2012-02-01 16:23:49 +00:00
GetRecentAnnouncements (announcement.FolderIdentifier);
2012-02-01 16:23:49 +00:00
if (!IsRecentAnnouncement (announcement))
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
}
}
}