Sonarr/src/NzbDrone.SignalR/NzbDronePersistentConnectio...

115 lines
3.3 KiB
C#
Raw Normal View History

2017-02-11 06:46:39 +00:00
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNet.SignalR;
2013-09-11 06:33:47 +00:00
using Microsoft.AspNet.SignalR.Infrastructure;
2017-02-11 06:46:39 +00:00
using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Common.Serializer;
2017-07-26 14:09:22 +00:00
using NzbDrone.Common.Extensions;
using NzbDrone.Core.Configuration;
2017-02-11 06:46:39 +00:00
using NzbDrone.Core.Datastore.Events;
2013-09-11 06:33:47 +00:00
namespace NzbDrone.SignalR
{
public interface IBroadcastSignalRMessage
{
bool IsConnected { get; }
void BroadcastMessage(SignalRMessage message);
}
public sealed class NzbDronePersistentConnection : PersistentConnection, IBroadcastSignalRMessage
2013-09-11 06:33:47 +00:00
{
2016-12-09 06:54:15 +00:00
private IPersistentConnectionContext Context => ((ConnectionManager)GlobalHost.ConnectionManager).GetConnection(GetType());
2013-09-11 06:33:47 +00:00
2017-07-26 14:09:22 +00:00
private static string API_KEY;
private readonly Dictionary<string, string> _messageHistory;
private HashSet<string> _connections = new HashSet<string>();
2017-07-26 14:09:22 +00:00
public NzbDronePersistentConnection(IConfigFileProvider configFileProvider)
{
API_KEY = configFileProvider.ApiKey;
2017-02-11 06:46:39 +00:00
_messageHistory = new Dictionary<string, string>();
2017-07-26 14:09:22 +00:00
}
public bool IsConnected
{
get
{
lock (_connections)
{
return _connections.Count != 0;
}
}
}
2017-02-11 06:46:39 +00:00
public void BroadcastMessage(SignalRMessage message)
2013-09-11 06:33:47 +00:00
{
2017-02-11 06:46:39 +00:00
string lastMessage;
if (_messageHistory.TryGetValue(message.Name, out lastMessage))
{
if (message.Action == ModelAction.Updated && message.Body.ToJson() == lastMessage)
{
return;
}
}
_messageHistory[message.Name] = message.Body.ToJson();
Context.Connection.Broadcast(message);
2013-09-11 06:33:47 +00:00
}
2017-02-11 06:46:39 +00:00
2017-07-26 14:09:22 +00:00
protected override bool AuthorizeRequest(IRequest request)
{
var apiKey = request.QueryString["apiKey"];
if (apiKey.IsNotNullOrWhiteSpace() && apiKey.Equals(API_KEY))
{
return true;
}
return false;
}
2017-02-11 06:46:39 +00:00
protected override Task OnConnected(IRequest request, string connectionId)
{
lock (_connections)
{
_connections.Add(connectionId);
}
2017-02-11 06:46:39 +00:00
return SendVersion(connectionId);
}
protected override Task OnReconnected(IRequest request, string connectionId)
{
lock (_connections)
{
_connections.Add(connectionId);
}
2017-02-11 06:46:39 +00:00
return SendVersion(connectionId);
}
protected override Task OnDisconnected(IRequest request, string connectionId, bool stopCalled)
{
lock (_connections)
{
_connections.Remove(connectionId);
}
return base.OnDisconnected(request, connectionId, stopCalled);
}
2017-02-11 06:46:39 +00:00
private Task SendVersion(string connectionId)
{
return Context.Connection.Send(connectionId, new SignalRMessage
{
Name = "version",
Body = new
{
Version = BuildInfo.Version.ToString()
}
});
}
2013-09-11 06:33:47 +00:00
}
}