/* ======================================================================== * Copyright (c) 2005-2020 The OPC Foundation, Inc. All rights reserved. * * OPC Foundation MIT License 1.00 * * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software and associated documentation * files (the "Software"), to deal in the Software without * restriction, including without limitation the rights to use, * copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following * conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. * * The complete license agreement can be found here: * http://opcfoundation.org/License/MIT/1.00/ * ======================================================================*/ using System; using System.Collections.Generic; using System.Text; using System.Threading; using System.Security.Principal; namespace Opc.Ua.Server { /// /// An object that manages all events raised within the server. /// public class EventManager : IDisposable { #region Constructors /// /// Creates a new instance of a sampling group. /// public EventManager(IServerInternal server, uint maxQueueSize) { if (server == null) throw new ArgumentNullException(nameof(server)); m_server = server; m_monitoredItems = new Dictionary(); m_maxEventQueueSize = maxQueueSize; } #endregion #region IDisposable Members /// /// Frees any unmanaged resources. /// public void Dispose() { Dispose(true); } /// /// An overrideable version of the Dispose. /// protected virtual void Dispose(bool disposing) { if (disposing) { List monitoredItems = null; lock (m_lock) { monitoredItems = new List(m_monitoredItems.Values); m_monitoredItems.Clear(); } foreach (IEventMonitoredItem monitoredItem in monitoredItems) { Utils.SilentDispose(monitoredItem); } } } #endregion #region Public Methods /// /// Reports an event. /// public static void ReportEvent(IFilterTarget e, IList monitoredItems) { if (e == null) throw new ArgumentNullException(nameof(e)); foreach (IEventMonitoredItem monitoredItem in monitoredItems) { monitoredItem.QueueEvent(e); } } /// /// Creates a set of monitored items. /// [Obsolete("Replaced by variant that includes the publishingInterval")] public MonitoredItem CreateMonitoredItem( OperationContext context, INodeManager nodeManager, object handle, uint subscriptionId, uint monitoredItemId, TimestampsToReturn timestampsToReturn, MonitoredItemCreateRequest itemToCreate, EventFilter filter) { return CreateMonitoredItem( context, nodeManager, handle, subscriptionId, monitoredItemId, timestampsToReturn, 0, itemToCreate, filter); } /// /// Creates a set of monitored items. /// public MonitoredItem CreateMonitoredItem( OperationContext context, INodeManager nodeManager, object handle, uint subscriptionId, uint monitoredItemId, TimestampsToReturn timestampsToReturn, double publishingInterval, MonitoredItemCreateRequest itemToCreate, EventFilter filter) { lock (m_lock) { // calculate sampling interval. double samplingInterval = itemToCreate.RequestedParameters.SamplingInterval; if (samplingInterval < 0) { samplingInterval = publishingInterval; } // limit the queue size. uint queueSize = itemToCreate.RequestedParameters.QueueSize; if (queueSize > m_maxEventQueueSize) { queueSize = m_maxEventQueueSize; } // create the monitored item. MonitoredItem monitoredItem = new MonitoredItem( m_server, nodeManager, handle, subscriptionId, monitoredItemId, context.Session, itemToCreate.ItemToMonitor, context.DiagnosticsMask, timestampsToReturn, itemToCreate.MonitoringMode, itemToCreate.RequestedParameters.ClientHandle, filter, filter, null, samplingInterval, queueSize, itemToCreate.RequestedParameters.DiscardOldest, MinimumSamplingIntervals.Continuous); // save the monitored item. m_monitoredItems.Add(monitoredItemId, monitoredItem); return monitoredItem; } } /// /// Modifies a monitored item. /// public void ModifyMonitoredItem( OperationContext context, IEventMonitoredItem monitoredItem, TimestampsToReturn timestampsToReturn, MonitoredItemModifyRequest itemToModify, EventFilter filter) { lock (m_lock) { // should never be called with items that it does not own. if (!m_monitoredItems.ContainsKey(monitoredItem.Id)) { return; } // limit the queue size. uint queueSize = itemToModify.RequestedParameters.QueueSize; if (queueSize > m_maxEventQueueSize) { queueSize = m_maxEventQueueSize; } // modify the attributes. monitoredItem.ModifyAttributes( context.DiagnosticsMask, timestampsToReturn, itemToModify.RequestedParameters.ClientHandle, filter, filter, null, itemToModify.RequestedParameters.SamplingInterval, queueSize, itemToModify.RequestedParameters.DiscardOldest); } } /// /// Deletes a monitored item. /// public void DeleteMonitoredItem(uint monitoredItemId) { lock (m_lock) { m_monitoredItems.Remove(monitoredItemId); } } /// /// Returns the currently active monitored items. /// public IList GetMonitoredItems() { lock (m_lock) { return new List(m_monitoredItems.Values); } } #endregion #region Private Fields private object m_lock = new object(); private IServerInternal m_server; private Dictionary m_monitoredItems; private uint m_maxEventQueueSize; #endregion } }