|
|
Adapter
|
public class AdapterLoader<T> : ISupportLifecycle, IDisposable, IProvideStatus, IPersistSettings where T : class, IAdapter
The AdapterLoaderT type exposes the following members.
| Name | Description | |
|---|---|---|
| AdapterLoaderT | Initializes a new instance of the AdapterLoaderT class. |
| Name | Description | |
|---|---|---|
| AdapterDirectory | Gets or sets the directory where Adapters are located. | |
| AdapterFileExtension | Gets or sets the file extension of the Adapters. | |
| AdapterFileFormat | Gets or sets the file format of the Adapters. | |
| Adapters | Gets a list of adapters loaded from the AdapterDirectory. | |
| AdapterWatcher | Gets the FileSystemWatcher object watching for new adapter assemblies added at runtime. | |
| AllowableAdapterMemoryUsage | Gets or sets the memory in megabytes the Adapters are allowed to use before being flagged as offending by the internal monitoring process. | |
| AllowableAdapterProcessorUsage | Gets or sets the processor time in % the Adapters are allowed to use before being flagged as offending by the internal monitoring process. | |
| AllowableProcessMemoryUsage | Gets or sets the memory in megabytes the current process is allowed to use before the internal monitoring process starts looking for offending Adapters. | |
| AllowableProcessProcessorUsage | Gets or sets the processor time in % the current process is allowed to use before the internal monitoring process starts looking for offending Adapters. | |
| Enabled | Gets or sets a boolean value that indicates whether the AdapterLoaderT is currently enabled. | |
| IsDisposed | Gets a flag that indicates whether the object has been disposed. | |
| IsolateAdapters | Gets or sets a boolean value that indicates whether Adapters are loaded in separate AppDomain for isolated execution. | |
| MonitorAdapters | Gets or sets a boolean value that indicates whether resource utilization of Adapters executing in isolation is to be monitored. | |
| Name | Gets the unique identifier of the AdapterLoaderT. | |
| OperationQueue | Gets the ProcessQueueT object to be used for queuing operations to be executed on Adapters. | |
| PersistSettings | Gets or sets a boolean value that indicates whether AdapterLoaderT settings are to be saved to the config file. | |
| SettingsCategory | 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. | |
| Status | Gets the descriptive status of the AdapterLoaderT. | |
| WatchForAdapters | Gets or sets a boolean value that indicates whether the AdapterDirectory is to be monitored for new Adapters. |
| Name | Description | |
|---|---|---|
| Dispose | Releases all the resources used by the AdapterLoaderT. | |
| Dispose(Boolean) | Releases the unmanaged resources used by the AdapterLoaderT and optionally releases the managed resources. | |
| Equals | Determines whether the specified object is equal to the current object. (Inherited from Object) | |
| ExecuteAdapterOperation | Executes an operation on the adapter with the given data. | |
| Finalize |
Releases the unmanaged resources before the AdapterLoaderT is reclaimed by GC.
(Overrides ObjectFinalize) | |
| GetHashCode | Serves as the default hash function. (Inherited from Object) | |
| GetMemoryUsage | Gets the memory usage in bytes of the specified process. | |
| GetProcessorUsage | Gets the % processor usage of the specified process. | |
| GetType | Gets the Type of the current instance. (Inherited from Object) | |
| Initialize | Initializes the AdapterLoaderT. | |
| Initialize(IEnumerableType) | Initializes the AdapterLoaderT. | |
| LoadSettings | Loads saved AdapterLoaderT settings from the config file if the PersistSettings property is set to true. | |
| MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object) | |
| MonitorAdapterResources | Monitors the resource utilization of Adapters. | |
| OnAdapterCreated | Raises the AdapterCreated event. | |
| OnAdapterLoaded | Raises the AdapterLoaded event. | |
| OnAdapterLoadException | Raises the AdapterLoadException event. | |
| OnAdapterResourceUsageExceeded | Raises the AdapterResourceUsageExceeded event. | |
| OnAdapterUnloaded | Raises the AdapterUnloaded event. | |
| OnOperationExecutionException | Raises the OperationExecutionException event. | |
| ProcessAdapter(String) | Processes the adapterFile by deserializing it. | |
| ProcessAdapter(Type) | Processes the adapterType by instantiating it. | |
| SaveSettings | Saves AdapterLoaderT settings to the config file if the PersistSettings property is set to true. | |
| ToString | Returns a string that represents the current object. (Inherited from Object) |
| Name | Description | |
|---|---|---|
| AdapterCreated | Occurs when a new adapter is found and instantiated. | |
| AdapterLoaded | Occurs when a new adapter is loaded to the Adapters list. | |
| AdapterLoadException | Occurs when an Exception is encountered when loading an adapter. | |
| AdapterResourceUsageExceeded | Occurs when an adapter has exceeded either the AllowableAdapterMemoryUsage or AllowableAdapterProcessorUsage. | |
| AdapterUnloaded | Occurs when an existing adapter is unloaded from the Adapters list. | |
| Disposed | Occurs when the class has been disposed. | |
| OperationExecutionException | Occurs when an Exception is encountered while executing a queued operation on one the Adapters. |
| Name | Description | |
|---|---|---|
| DefaultAdapterDirectory | Specifies the default value for the AdapterDirectory property. | |
| DefaultAdapterFileExtension | Specifies the default value for the AdapterFileExtension property. | |
| DefaultAdapterFileFormat | Specifies the default value for the AdapterFileFormat property. | |
| DefaultAllowableAdapterMemoryUsage | Specifies the default value for the AllowableAdapterMemoryUsage property. | |
| DefaultAllowableAdapterProcessorUsage | Specifies the default value for the AllowableAdapterProcessorUsage property. | |
| DefaultAllowableProcessMemoryUsage | Specifies the default value for the AllowableProcessMemoryUsage property | |
| DefaultAllowableProcessProcessorUsage | Specifies the default value for the AllowableProcessProcessorUsage property. | |
| DefaultIsolateAdapters | Specifies the default value for the IsolateAdapters property. | |
| DefaultMonitorAdapters | Specifies the default value for the MonitorAdapters property. | |
| DefaultWatchForAdapters | Specifies the default value for the WatchForAdapters property. |
| Name | Description | |
|---|---|---|
| GetEnumValueOrDefault |
Gets the enumeration constant for value, if defined in the enumeration, or a default value.
(Defined by EnumExtensions) | |
| GetEnumValueOrDefaultT |
Gets the enumeration constant for this value, if defined in the enumeration, or a default value.
(Defined by EnumExtensions) |
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);
}
}
}