1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Threading;
- namespace ISCSIConsole
- {
- public class UsageCounter
- {
- private SortedList<string, int> m_targetsInUse = new SortedList<string, int>();
- private int m_sessionCount = 0;
- public void NotifySessionStart(string targetName)
- {
- Interlocked.Increment(ref m_sessionCount);
- lock (m_targetsInUse)
- {
- int index = m_targetsInUse.IndexOfKey(targetName);
- if (index >= 0)
- {
- m_targetsInUse[targetName]++;
- }
- else
- {
- m_targetsInUse.Add(targetName, 1);
- }
- }
- }
- public void NotifySessionTermination(string targetName)
- {
- Interlocked.Decrement(ref m_sessionCount);
- lock (m_targetsInUse)
- {
- int index = m_targetsInUse.IndexOfKey(targetName);
- if (index >= 0)
- {
- int useCount = m_targetsInUse[targetName];
- useCount--;
- if (useCount == 0)
- {
- m_targetsInUse.Remove(targetName);
- }
- else
- {
- m_targetsInUse[targetName] = useCount;
- }
- }
- }
- }
- public bool IsTargetInUse(string targetName)
- {
- lock (m_targetsInUse)
- {
- return m_targetsInUse.ContainsKey(targetName);
- }
- }
- public int SessionCount
- {
- get
- {
- return m_sessionCount;
- }
- }
- }
- }
|