//******************************************************************************************************
// SubscriberHub.cs - Gbtc
//
// Copyright © 2018, Grid Protection Alliance. All Rights Reserved.
//
// Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
// the NOTICE file distributed with this work for additional information regarding copyright ownership.
// The GPA licenses this file to you under the MIT License (MIT), the "License"; you may not use this
// file except in compliance with the License. You may obtain a copy of the License at:
//
// http://opensource.org/licenses/MIT
//
// Unless agreed to in writing, the subject software distributed under the License is distributed on an
// "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
// License for the specific language governing permissions and limitations.
//
// Code Modification History:
// ----------------------------------------------------------------------------------------------------
// 07/12/2018 - Stephen C. Wills
// Generated original version of source code.
//
//******************************************************************************************************
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using GSF.Collections;
using GSF.Configuration;
using GSF.TimeSeries;
using GSF.TimeSeries.Transport;
using GSF.Web.Security;
using Microsoft.AspNet.SignalR;
using Newtonsoft.Json.Linq;
namespace GSF.Web.Shared
{
///
/// SignalR hub that exposes server-side functions for the subscriber API.
///
[AuthorizeHubRole]
public class SubscriberHub : Hub
{
#region [ Members ]
// Nested Types
private sealed class Connection : IDisposable
{
public readonly Dictionary SubscriberLookup;
private bool m_disposed;
public Connection()
{
SubscriberLookup = new Dictionary();
}
public void Dispose()
{
if (m_disposed)
return;
try
{
foreach (Subscriber subscriptionState in SubscriberLookup.Values)
subscriptionState.Dispose();
}
finally
{
m_disposed = true;
}
}
}
private sealed class Subscriber : IDisposable
{
public DataSubscriber DataSubscriber;
public DataSet Metadata;
private Dictionary> m_formats;
private bool m_disposed;
public void SetFormat(Guid signalID, string format, string dataType)
{
if (m_formats is null)
m_formats = new Dictionary>();
m_formats[signalID] = new Tuple(format, dataType);
}
public bool TryGetFormat(Guid signalID, out string format, out string dataType)
{
format = null;
dataType = null;
if (m_formats is null)
return false;
if (m_formats.TryGetValue(signalID, out Tuple tuple))
{
format = tuple.Item1;
dataType = tuple.Item2;
return true;
}
return false;
}
public void RemoveFormat(Guid signalID)
{
if (m_formats is null || !m_formats.ContainsKey(signalID))
return;
if (m_formats.Remove(signalID) && m_formats.Count == 0)
m_formats = null;
}
public void Dispose()
{
if (m_disposed)
return;
try
{
DataSubscriber?.Dispose();
Metadata?.Dispose();
}
finally
{
m_disposed = true;
}
}
}
#endregion
#region [ Methods ]
///
/// Initiates the subscriber connection.
///
/// The ID of the subscriber to be connected.
public void Connect(string subscriberID)
{
Subscriber subscriber = GetOrCreate(subscriberID);
subscriber.DataSubscriber.Start();
}
///
/// Sends a command to the publisher.
///
/// The ID of the subscriber.
/// The command to be sent.
/// The message to be sent to the publisher.
public void SendCommand(string subscriberID, ServerCommand commandCode, string message)
{
Subscriber subscriber = GetOrCreate(subscriberID);
subscriber.DataSubscriber.SendServerCommand(commandCode, message);
}
///
/// Filters metadata and returns the result.
///
/// The ID of the subscriber.
/// The metadata table from which to return rows.
/// The filter to apply to the metadata table.
/// The field by which to sort the result set.
/// The maximum number of records to be returned from the result set.
/// The result of the metadata query.
public IEnumerable