/* ======================================================================== * 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 Opc.Ua.PubSub.Configuration; namespace Opc.Ua.PubSub { /// /// This entity represents a working connection for PubSub /// internal abstract class UaPubSubConnection : IUaPubSubConnection, IDisposable { #region Fields protected object m_lock = new object(); private bool m_isRunning; private List m_publishers; private PubSubConnectionDataType m_pubSubConnectionDataType; private UaPubSubApplication m_uaPubSubApplication; protected TransportProtocol m_transportProtocol = TransportProtocol.NotAvailable; #endregion #region Constructor /// /// Create new instance of UaPubSubConnection with PubSubConnectionDataType configuration data /// /// /// public UaPubSubConnection(UaPubSubApplication parentUaPubSubApplication, PubSubConnectionDataType pubSubConnectionDataType) { m_uaPubSubApplication = parentUaPubSubApplication; m_uaPubSubApplication.UaPubSubConfigurator.WriterGroupAdded += UaPubSubConfigurator_WriterGroupAdded; m_pubSubConnectionDataType = pubSubConnectionDataType; m_publishers = new List(); if (string.IsNullOrEmpty(pubSubConnectionDataType.Name)) { pubSubConnectionDataType.Name = ""; } } private void UaPubSubConfigurator_WriterGroupAdded(object sender, WriterGroupEventArgs e) { PubSubConnectionDataType pubSubConnectionDataType = m_uaPubSubApplication.UaPubSubConfigurator.FindObjectById(e.ConnectionId) as PubSubConnectionDataType; if (m_pubSubConnectionDataType == pubSubConnectionDataType) { UaPublisher publisher = new UaPublisher(this, e.WriterGroupDataType); m_publishers.Add(publisher); } } #endregion #region Properties /// /// Get assigned transport protocol for this connection instance /// public TransportProtocol TransportProtocol { get { return m_transportProtocol; } } /// /// Get the configuration object for this PubSub connection /// public PubSubConnectionDataType PubSubConnectionConfiguration { get { return m_pubSubConnectionDataType; } } /// /// Get reference to /// public UaPubSubApplication Application { get { return m_uaPubSubApplication; } } /// /// Get flag that indicates if the Connection is in running state /// public bool IsRunning { get { return m_isRunning; } } #endregion #region Internal Properties /// /// Get the list of current publishers associated with this connection /// internal IReadOnlyCollection Publishers { get { return m_publishers.AsReadOnly(); } } /// /// Get the read only list of dataset readers associated with this connection /// internal IReadOnlyCollection DataSetReaders { get { return GetDataSetReaders().AsReadOnly(); } } #endregion #region Public Methods /// /// Start Publish/Subscribe jobs associated with this instance /// public void Start() { lock (m_lock) { m_isRunning = true; foreach (var publisher in m_publishers) { publisher.Start(); } } InternalStart(); } /// /// Stop Publish/Subscribe jobs associated with this instance /// public void Stop() { InternalStop(); lock (m_lock) { m_isRunning = false; foreach (var publisher in m_publishers) { publisher.Stop(); } } } /// /// Determine if the connection has anything to publish -> at least one WriterDataSet is configured as enabled for current writer group /// /// /// public bool CanPublish(WriterGroupDataType writerGroupConfiguration) { if (!m_isRunning) { return false; } // check if connection status is operational if (Application.UaPubSubConfigurator.FindStateForObject(m_pubSubConnectionDataType) != PubSubState.Operational) { return false; } if (writerGroupConfiguration.Enabled) { foreach (DataSetWriterDataType writer in writerGroupConfiguration.DataSetWriters) { if (writer.Enabled) { return true; } } } return false; } /// /// Create the network message built from the provided writerGroupConfiguration /// /// /// public abstract UaNetworkMessage CreateNetworkMessage(WriterGroupDataType writerGroupConfiguration); /// /// Publish the network message /// /// /// public abstract bool PublishNetworkMessage(UaNetworkMessage networkMessage); #endregion #region Protected Methods /// /// Initialize the connection object. Must be implemented by derived classes /// /// protected abstract bool InternalInitialize(); /// /// Perform specific Start tasks /// protected abstract void InternalStart(); /// /// Perform specific Stop tasks /// protected abstract void InternalStop(); #endregion #region Private Methods /// /// Get current list of dataset readers available in this UaSubscriber component /// protected List GetDataSetReaders() { List readersList = new List(); if (Application.UaPubSubConfigurator.FindStateForObject(m_pubSubConnectionDataType) != PubSubState.Operational) { return readersList; } foreach (ReaderGroupDataType readerGroup in m_pubSubConnectionDataType.ReaderGroups) { if (Application.UaPubSubConfigurator.FindStateForObject(readerGroup) == PubSubState.Operational) { foreach (DataSetReaderDataType reader in readerGroup.DataSetReaders) { if (Application.UaPubSubConfigurator.FindStateForObject(reader) == PubSubState.Operational) { readersList.Add(reader); } } } } return readersList; } #endregion #region IDisposable Implementation /// /// Releases all resources used by the current instance of the class. /// public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } /// /// When overridden in a derived class, releases the unmanaged resources used by that class /// and optionally releases the managed resources. /// /// true to release both managed and unmanaged resources; false to release only unmanaged resources. protected virtual void Dispose(bool disposing) { if (disposing) { m_uaPubSubApplication.UaPubSubConfigurator.WriterGroupAdded -= UaPubSubConfigurator_WriterGroupAdded; Stop(); // free managed resources foreach (UaPublisher publisher in m_publishers) { publisher.Dispose(); } } } #endregion } }