Click or drag to resize

AdapterLoaderT Class

Represents a generic loader of adapters.
Inheritance Hierarchy
SystemObject
  GSF.AdaptersAdapterLoaderT
    More

Namespace: GSF.Adapters
Assembly: GSF.Core (in GSF.Core.dll) Version: 2.4.218-beta+101eee949414e414795e55a6e73d88938f0177b8
Syntax
public class AdapterLoader<T> : ISupportLifecycle, 
	IDisposable, IProvideStatus, IPersistSettings
where T : class, IAdapter
View Source

Type Parameters

T
Type of adapters to be loaded.

The AdapterLoaderT type exposes the following members.

Constructors
 NameDescription
Public methodAdapterLoaderT Initializes a new instance of the AdapterLoaderT class.
Top
Properties
 NameDescription
Public propertyAdapterDirectory Gets or sets the directory where Adapters are located.
Public propertyAdapterFileExtension Gets or sets the file extension of the Adapters.
Public propertyAdapterFileFormat Gets or sets the file format of the Adapters.
Public propertyAdapters Gets a list of adapters loaded from the AdapterDirectory.
Protected propertyAdapterWatcher Gets the FileSystemWatcher object watching for new adapter assemblies added at runtime.
Public propertyAllowableAdapterMemoryUsage Gets or sets the memory in megabytes the Adapters are allowed to use before being flagged as offending by the internal monitoring process.
Public propertyAllowableAdapterProcessorUsage Gets or sets the processor time in % the Adapters are allowed to use before being flagged as offending by the internal monitoring process.
Public propertyAllowableProcessMemoryUsage Gets or sets the memory in megabytes the current process is allowed to use before the internal monitoring process starts looking for offending Adapters.
Public propertyAllowableProcessProcessorUsage Gets or sets the processor time in % the current process is allowed to use before the internal monitoring process starts looking for offending Adapters.
Public propertyEnabled Gets or sets a boolean value that indicates whether the AdapterLoaderT is currently enabled.
Public propertyIsDisposed Gets a flag that indicates whether the object has been disposed.
Public propertyIsolateAdapters Gets or sets a boolean value that indicates whether Adapters are loaded in separate AppDomain for isolated execution.
Public propertyMonitorAdapters Gets or sets a boolean value that indicates whether resource utilization of Adapters executing in isolation is to be monitored.
Public propertyName Gets the unique identifier of the AdapterLoaderT.
Protected propertyOperationQueue Gets the ProcessQueueT object to be used for queuing operations to be executed on Adapters.
Public propertyPersistSettings Gets or sets a boolean value that indicates whether AdapterLoaderT settings are to be saved to the config file.
Public propertySettingsCategory Gets or sets the category under which AdapterLoaderT settings are to be saved to the config file if the PersistSettings property is set to true.
Public propertyStatus Gets the descriptive status of the AdapterLoaderT.
Public propertyWatchForAdapters Gets or sets a boolean value that indicates whether the AdapterDirectory is to be monitored for new Adapters.
Top
Methods
 NameDescription
Public methodDispose Releases all the resources used by the AdapterLoaderT.
Protected methodDispose(Boolean) Releases the unmanaged resources used by the AdapterLoaderT and optionally releases the managed resources.
Public methodEqualsDetermines whether the specified object is equal to the current object.
(Inherited from Object)
Protected methodExecuteAdapterOperation Executes an operation on the adapter with the given data.
Protected methodFinalize Releases the unmanaged resources before the AdapterLoaderT is reclaimed by GC.
(Overrides ObjectFinalize)
Public methodGetHashCodeServes as the default hash function.
(Inherited from Object)
Protected methodGetMemoryUsage Gets the memory usage in bytes of the specified process.
Protected methodGetProcessorUsage Gets the % processor usage of the specified process.
Public methodGetTypeGets the Type of the current instance.
(Inherited from Object)
Public methodInitialize Initializes the AdapterLoaderT.
Public methodInitialize(IEnumerableType) Initializes the AdapterLoaderT.
Public methodLoadSettings Loads saved AdapterLoaderT settings from the config file if the PersistSettings property is set to true.
Protected methodMemberwiseCloneCreates a shallow copy of the current Object.
(Inherited from Object)
Protected methodMonitorAdapterResources Monitors the resource utilization of Adapters.
Protected methodOnAdapterCreated Raises the AdapterCreated event.
Protected methodOnAdapterLoaded Raises the AdapterLoaded event.
Protected methodOnAdapterLoadException Raises the AdapterLoadException event.
Protected methodOnAdapterResourceUsageExceeded Raises the AdapterResourceUsageExceeded event.
Protected methodOnAdapterUnloaded Raises the AdapterUnloaded event.
Protected methodOnOperationExecutionException Raises the OperationExecutionException event.
Protected methodProcessAdapter(String) Processes the adapterFile by deserializing it.
Protected methodProcessAdapter(Type) Processes the adapterType by instantiating it.
Public methodSaveSettings Saves AdapterLoaderT settings to the config file if the PersistSettings property is set to true.
Public methodToStringReturns a string that represents the current object.
(Inherited from Object)
Top
Events
 NameDescription
Public eventAdapterCreated Occurs when a new adapter is found and instantiated.
Public eventAdapterLoaded Occurs when a new adapter is loaded to the Adapters list.
Public eventAdapterLoadException Occurs when an Exception is encountered when loading an adapter.
Public eventAdapterResourceUsageExceeded Occurs when an adapter has exceeded either the AllowableAdapterMemoryUsage or AllowableAdapterProcessorUsage.
Public eventAdapterUnloaded Occurs when an existing adapter is unloaded from the Adapters list.
Public eventDisposed Occurs when the class has been disposed.
Public eventOperationExecutionException Occurs when an Exception is encountered while executing a queued operation on one the Adapters.
Top
Fields
 NameDescription
Public fieldStatic memberDefaultAdapterDirectory Specifies the default value for the AdapterDirectory property.
Public fieldStatic memberDefaultAdapterFileExtension Specifies the default value for the AdapterFileExtension property.
Public fieldStatic memberDefaultAdapterFileFormat Specifies the default value for the AdapterFileFormat property.
Public fieldStatic memberDefaultAllowableAdapterMemoryUsage Specifies the default value for the AllowableAdapterMemoryUsage property.
Public fieldStatic memberDefaultAllowableAdapterProcessorUsage Specifies the default value for the AllowableAdapterProcessorUsage property.
Public fieldStatic memberDefaultAllowableProcessMemoryUsage Specifies the default value for the AllowableProcessMemoryUsage property
Public fieldStatic memberDefaultAllowableProcessProcessorUsage Specifies the default value for the AllowableProcessProcessorUsage property.
Public fieldStatic memberDefaultIsolateAdapters Specifies the default value for the IsolateAdapters property.
Public fieldStatic memberDefaultMonitorAdapters Specifies the default value for the MonitorAdapters property.
Public fieldStatic memberDefaultWatchForAdapters Specifies the default value for the WatchForAdapters property.
Top
Extension Methods
 NameDescription
Public Extension MethodGetEnumValueOrDefault Gets the enumeration constant for value, if defined in the enumeration, or a default value.
(Defined by EnumExtensions)
Public Extension MethodGetEnumValueOrDefaultT Gets the enumeration constant for this value, if defined in the enumeration, or a default value.
(Defined by EnumExtensions)
Top
Example
This example show how to use the AdapterLoaderT to isolate adapters in separate AppDomains and monitor their resource usage:
C#
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using GSF;
using GSF.Adapters;
using GSF.Security.Cryptography;

class Program
{
    static AdapterLoader<PublishAdapterBase> s_adapterLoader;

    static void Main(string[] args)
    {
        // Enable app domain resource monitoring.
        AppDomain.MonitoringIsEnabled = true;

        // Load adapters that mimic data publishing.
        s_adapterLoader = new AdapterLoader<PublishAdapterBase>();
        s_adapterLoader.IsolateAdapters = true;
        s_adapterLoader.MonitorAdapters = true;
        s_adapterLoader.AdapterFileExtension = "*.exe";
        s_adapterLoader.AllowableProcessMemoryUsage = 200;
        s_adapterLoader.AllowableProcessProcessorUsage = 50;
        s_adapterLoader.AllowableAdapterMemoryUsage = 100;
        s_adapterLoader.AllowableAdapterProcessorUsage = 25;
        s_adapterLoader.AdapterLoaded += OnAdapterLoaded;
        s_adapterLoader.AdapterUnloaded += OnAdapterUnloaded;
        s_adapterLoader.AdapterResourceUsageExceeded += OnAdapterResourceUsageExceeded;
        s_adapterLoader.Initialize();

        // Shutdown.
        Console.ReadLine();
        s_adapterLoader.Dispose();
    }

    static void OnAdapterLoaded(object sender, EventArgs<PublishAdapterBase> e)
    {
        Console.WriteLine("{0} has been loaded\r\n", e.Argument.GetType().Name);
    }

    static void OnAdapterUnloaded(object sender, EventArgs<PublishAdapterBase> e)
    {
        Console.WriteLine("{0} has been unloaded\r\n", e.Argument.GetType().Name);
    }

    static void OnAdapterResourceUsageExceeded(object sender, GSF.EventArgs<PublishAdapterBase> e)
    {
        Console.WriteLine("{0} status:", e.Argument.Name);
        Console.WriteLine(e.Argument.Status);

        // Remove the adapter in order to reclaim the resources used by it.
        lock (s_adapterLoader.Adapters)
        {
            s_adapterLoader.Adapters.Remove(e.Argument);
        }
    }
}

/// <summary>
/// Base adapter class.
/// </summary>
public abstract class PublishAdapterBase : Adapter
{
    public PublishAdapterBase()
    {
        Data = new List<byte[]>();
    }

    public List<byte[]> Data { get; set; }

    public override void Initialize()
    {
        base.Initialize();
        new Thread(Publish).Start();
    }

    protected abstract void Publish();
}

/// <summary>
/// Adapter that does not manage memory well.
/// </summary>
public class PublishAdapterA : PublishAdapterBase
{
    protected override void Publish()
    {
        while (true)
        {
            for (int i = 0; i < 10000; i++)
            {
                Data.Add(new byte[10]);
            }
            Thread.Sleep(100);
        }
    }
}

/// <summary>
/// Adapter that uses the processor in excess.
/// </summary>
public class PublishAdapterB : PublishAdapterBase
{
    protected override void Publish()
    {
        string text = string.Empty;
        System.Random random = new System.Random();
        while (true)
        {
            for (int i = 0; i < 10; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    text += (char)random.Next(256);
                }
                Data.Add(Encoding.ASCII.GetBytes(text.Encrypt("C1pH3r", CipherStrength.Aes256)).BlockCopy(0, 1));
            }
            Thread.Sleep(10);
        }
    }
}
See Also
Inheritance Hierarchy