TargetList.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /* Copyright (C) 2016 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
  2. *
  3. * You can redistribute this program and/or modify it under the terms of
  4. * the GNU Lesser Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. */
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Text;
  10. using System.Threading;
  11. namespace ISCSI.Server
  12. {
  13. internal class TargetList
  14. {
  15. public object Lock = new object();
  16. private List<ISCSITarget> m_targets = new List<ISCSITarget>();
  17. public void AddTarget(ISCSITarget target)
  18. {
  19. lock (Lock)
  20. {
  21. int index = IndexOfTarget(target.TargetName);
  22. if (index >= 0)
  23. {
  24. throw new ArgumentException("A target with the same iSCSI Target Name already exists");
  25. }
  26. m_targets.Add(target);
  27. }
  28. }
  29. public ISCSITarget FindTarget(string targetName)
  30. {
  31. lock (Lock)
  32. {
  33. int index = IndexOfTarget(targetName);
  34. if (index >= 0)
  35. {
  36. return m_targets[index];
  37. }
  38. return null;
  39. }
  40. }
  41. /// <summary>
  42. /// Caller MUST obtain a lock on TargetList.Lock before calling this method
  43. /// </summary>
  44. public List<ISCSITarget> GetList()
  45. {
  46. return new List<ISCSITarget>(m_targets);
  47. }
  48. private int IndexOfTarget(string targetName)
  49. {
  50. for (int index = 0; index < m_targets.Count; index++)
  51. {
  52. if (String.Equals(m_targets[index].TargetName, targetName, StringComparison.OrdinalIgnoreCase))
  53. {
  54. return index;
  55. }
  56. }
  57. return -1;
  58. }
  59. }
  60. }