Program.StartCommand.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* Copyright (C) 2012-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.IO;
  10. using System.Net.Sockets;
  11. using ISCSI.Server;
  12. using Utilities;
  13. namespace ISCSIConsole
  14. {
  15. partial class Program
  16. {
  17. public const int DefaultISCSIPort = 3260;
  18. private static List<ISCSITarget> m_targets = new List<ISCSITarget>();
  19. private static ISCSIServer m_server;
  20. public static void StartCommand(string[] args)
  21. {
  22. if (m_server == null)
  23. {
  24. if (m_targets.Count > 0)
  25. {
  26. KeyValuePairList<string, string> parameters = ParseParameters(args, 1);
  27. if (!VerifyParameters(parameters, "port", "log"))
  28. {
  29. Console.WriteLine();
  30. Console.WriteLine("Invalid parameter");
  31. HelpStart();
  32. return;
  33. }
  34. int port = DefaultISCSIPort;
  35. if (parameters.ContainsKey("port"))
  36. {
  37. port = Conversion.ToInt32(parameters.ValueOf("port"), DefaultISCSIPort);
  38. }
  39. string logFile = String.Empty;
  40. if (parameters.ContainsKey("log"))
  41. {
  42. logFile = parameters.ValueOf("log");
  43. }
  44. m_server = new ISCSIServer();
  45. m_server.AddTargets(m_targets);
  46. m_server.OnLogEntry += new EventHandler<LogEntry>(OnLogEntry);
  47. if (logFile != String.Empty)
  48. {
  49. try
  50. {
  51. OpenLogFile(logFile);
  52. }
  53. catch (IOException)
  54. {
  55. Console.WriteLine("Could not append to log file");
  56. return;
  57. }
  58. }
  59. try
  60. {
  61. m_server.Start(port);
  62. Console.WriteLine("Server started, listening on port {0}", port);
  63. }
  64. catch (SocketException)
  65. {
  66. Console.WriteLine("Could not start iSCSI server");
  67. m_server.Stop();
  68. m_server = null;
  69. }
  70. }
  71. else
  72. {
  73. Console.WriteLine("No disks have been attached");
  74. }
  75. }
  76. }
  77. public static void StopCommand(string[] args)
  78. {
  79. if (m_server != null)
  80. {
  81. m_server.Stop();
  82. m_server = null;
  83. Console.WriteLine("iSCSI target is stopping");
  84. CloseLogFile();
  85. }
  86. else
  87. {
  88. Console.WriteLine("iSCSI target has not been started");
  89. }
  90. }
  91. public static void HelpStart()
  92. {
  93. Console.WriteLine();
  94. Console.WriteLine("ISCSI START [PORT=<port>] [LOG=<path>] - starts the iSCSI server");
  95. }
  96. public static void HelpStop()
  97. {
  98. Console.WriteLine();
  99. Console.WriteLine("ISCSI STOP - stops the iSCSI server");
  100. }
  101. }
  102. }