TargetList.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. public bool RemoveTarget(string targetName)
  42. {
  43. lock (Lock)
  44. {
  45. int index = IndexOfTarget(targetName);
  46. if (index >= 0)
  47. {
  48. m_targets.RemoveAt(index);
  49. return true;
  50. }
  51. return false;
  52. }
  53. }
  54. /// <summary>
  55. /// Caller MUST obtain a lock on TargetList.Lock before calling this method
  56. /// </summary>
  57. public List<ISCSITarget> GetList()
  58. {
  59. return new List<ISCSITarget>(m_targets);
  60. }
  61. private int IndexOfTarget(string targetName)
  62. {
  63. for (int index = 0; index < m_targets.Count; index++)
  64. {
  65. if (String.Equals(m_targets[index].TargetName, targetName, StringComparison.OrdinalIgnoreCase))
  66. {
  67. return index;
  68. }
  69. }
  70. return -1;
  71. }
  72. }
  73. }