11using System ;
22using System . Collections . Generic ;
3+ using System . Linq ;
34using System . Text ;
45using Automatak . DNP3 . Interface ;
56using GSF . Diagnostics ;
@@ -14,6 +15,11 @@ namespace DNP3Adapters;
1415/// </summary>
1516public interface IDnp3Adapter : IAdapter
1617{
18+ /// <summary>
19+ /// Gets the ID of the channel added to the DNP3 manager.
20+ /// </summary>
21+ public string ChannelID { get ; }
22+
1723 /// <summary>
1824 /// Raises the <see cref="IAdapter.ProcessException"/> event.
1925 /// </summary>
@@ -34,7 +40,7 @@ internal class IaonProxyLogHandler<T> : ILogHandler where T : IDnp3Adapter
3440 /// <summary>
3541 /// Gets the static adapters that are currently registered with this proxy.
3642 /// </summary>
37- public List < IDnp3Adapter > Adapters { get ; } = [ ] ;
43+ public Dictionary < string , IDnp3Adapter > Adapters { get ; } = [ ] ;
3844
3945 /// <summary>
4046 /// Gets the static status proxy that is used to process exceptions and status messages.
@@ -50,7 +56,7 @@ public void RegisterAdapter(T adapter)
5056 lock ( Adapters )
5157 {
5258 // Add adapter to list of available adapters
53- Adapters . Add ( adapter ) ;
59+ Adapters . Add ( adapter . ChannelID , adapter ) ;
5460
5561 // If no adapter has been designated as the status proxy, assign this one
5662 StatusProxy ??= adapter ;
@@ -66,13 +72,13 @@ public void UnregisterAdapter(T adapter)
6672 lock ( Adapters )
6773 {
6874 // Remove this adapter from the available list
69- Adapters . Remove ( adapter ) ;
75+ Adapters . Remove ( adapter . ChannelID ) ;
7076
7177 // See if we are disposing the status proxy instance
7278 if ( ReferenceEquals ( StatusProxy , adapter ) )
7379 {
7480 // Attempt to find a new status proxy
75- StatusProxy = Adapters . Count > 0 ? Adapters [ 0 ] : null ;
81+ StatusProxy = Adapters . Values . FirstOrDefault ( ) ;
7682 }
7783 }
7884 }
@@ -87,26 +93,29 @@ public void Log(LogEntry entry)
8793 // contends with adapter initialization and disposal so contention will not be the normal case
8894 lock ( Adapters )
8995 {
90- if ( StatusProxy is null || StatusProxy . IsDisposed )
96+ if ( ! Adapters . TryGetValue ( entry . alias , out IDnp3Adapter ? adapter ) )
97+ adapter = StatusProxy ;
98+
99+ if ( adapter is null || adapter . IsDisposed )
91100 return ;
92101
93102 if ( ( entry . filter . Flags & LogFilters . ERROR ) > 0 )
94103 {
95104 // Expose errors through exception processor
96105 InvalidOperationException exception = new ( FormatLogEntry ( entry ) ) ;
97- StatusProxy . OnProcessException ( MessageLevel . Error , exception ) ;
106+ adapter . OnProcessException ( MessageLevel . Error , exception ) ;
98107 }
99108 else
100109 {
101110 // For other messages, we just expose as a normal status
102111 string message = FormatLogEntry ( entry ) ;
103112
104113 if ( ( entry . filter . Flags & LogFilters . WARNING ) > 0 )
105- StatusProxy . OnStatusMessage ( MessageLevel . Warning , message ) ;
114+ adapter . OnStatusMessage ( MessageLevel . Warning , message ) ;
106115 else if ( ( entry . filter . Flags & LogFilters . DEBUG ) > 0 )
107- StatusProxy . OnStatusMessage ( MessageLevel . Debug , message ) ;
116+ adapter . OnStatusMessage ( MessageLevel . Debug , message ) ;
108117 else
109- StatusProxy . OnStatusMessage ( MessageLevel . Info , message ) ;
118+ adapter . OnStatusMessage ( MessageLevel . Info , message ) ;
110119 }
111120 }
112121 }
0 commit comments