//****************************************************************************************************** // InputAdapterCollection.cs - Gbtc // // Copyright © 2010, 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 Eclipse Public License -v 1.0 (the "License"); you may // not use this file except in compliance with the License. You may obtain a copy of the License at: // // http://www.opensource.org/licenses/eclipse-1.0.php // // 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: // ---------------------------------------------------------------------------------------------------- // 09/02/2010 - J. Ritchie Carroll // Generated original version of source code. // //****************************************************************************************************** using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Threading; namespace GSF.TimeSeries.Adapters { /// /// Represents a collection of implementations. /// public class InputAdapterCollection : AdapterCollectionBase, IInputAdapter { #region [ Members ] // Events /// /// This event will be raised when there are new measurements available from the input data source. /// /// /// is collection of new measurements for host to process. /// public event EventHandler>> NewMeasurements; /// /// Indicates to the host that processing for one of the input adapters has completed. /// /// /// This event is expected to only be raised when an input adapter has been designed to process /// a finite amount of data, e.g., reading a historical range of data during temporal procesing. /// public event EventHandler ProcessingComplete; #endregion #region [ Constructors ] /// /// Creates a new . /// public InputAdapterCollection() : this(null) { // When collection is spawned as an adapter, it needs a parameterless constructor } /// /// Creates a new . /// /// Wait handle dictionary. public InputAdapterCollection(ConcurrentDictionary waitHandles) : base(waitHandles) { base.Name = "Input Adapter Collection"; base.DataMember = "InputAdapters"; base.MonitorTimerEnabled = true; } #endregion #region [ Methods ] /// /// Raises the event. /// /// New measurements. protected virtual void OnNewMeasurements(ICollection measurements) { try { if (NewMeasurements != null) NewMeasurements(this, new EventArgs>(measurements)); } catch (Exception ex) { // We protect our code from consumer thrown exceptions OnProcessException(new InvalidOperationException(string.Format("Exception in consumer handler for NewMeasurements event: {0}", ex.Message), ex)); } } /// /// Raises the event. /// protected virtual void OnProcessingComplete() { try { if (ProcessingComplete != null) ProcessingComplete(this, EventArgs.Empty); } catch (Exception ex) { // We protect our code from consumer thrown exceptions OnProcessException(new InvalidOperationException(string.Format("Exception in consumer handler for ProcessingComplete event: {0}", ex.Message), ex)); } } /// /// Wires events and initializes new implementation. /// /// New implementation. protected override void InitializeItem(IInputAdapter item) { if (item != null) { // Wire up new measurement event item.NewMeasurements += item_NewMeasurements; item.ProcessingComplete += item_ProcessingComplete; base.InitializeItem(item); } } /// /// Unwires events and disposes of implementation. /// /// to dispose. protected override void DisposeItem(IInputAdapter item) { if (item != null) { // Un-wire new meaurements event item.NewMeasurements -= item_NewMeasurements; item.ProcessingComplete -= item_ProcessingComplete; base.DisposeItem(item); } } // Raise new measurements event on behalf of each item in collection private void item_NewMeasurements(object sender, EventArgs> e) { if (NewMeasurements != null) NewMeasurements(sender, e); } // Raise processing complete event on behalf of each item in collection private void item_ProcessingComplete(object sender, EventArgs e) { if (ProcessingComplete != null) ProcessingComplete(sender, e); } #endregion } }