Program.StartCommand.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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(m_targets, port);
  45. m_server.OnLogEntry += new EventHandler<LogEntry>(OnLogEntry);
  46. if (logFile != String.Empty)
  47. {
  48. try
  49. {
  50. OpenLogFile(logFile);
  51. }
  52. catch (IOException)
  53. {
  54. Console.WriteLine("Could not append to log file");
  55. return;
  56. }
  57. }
  58. try
  59. {
  60. m_server.Start();
  61. Console.WriteLine("Server started, listening on port {0}", port);
  62. }
  63. catch (SocketException)
  64. {
  65. Console.WriteLine("Could not start iSCSI server");
  66. m_server.Stop();
  67. m_server = null;
  68. }
  69. }
  70. else
  71. {
  72. Console.WriteLine("No disks have been attached");
  73. }
  74. }
  75. }
  76. public static void StopCommand(string[] args)
  77. {
  78. if (m_server != null)
  79. {
  80. m_server.Stop();
  81. m_server = null;
  82. Console.WriteLine("iSCSI target is stopping");
  83. CloseLogFile();
  84. }
  85. else
  86. {
  87. Console.WriteLine("iSCSI target has not been started");
  88. }
  89. }
  90. public static void HelpStart()
  91. {
  92. Console.WriteLine();
  93. Console.WriteLine("ISCSI START [PORT=<port>] [LOG=<path>] - starts the iSCSI server");
  94. }
  95. public static void HelpStop()
  96. {
  97. Console.WriteLine();
  98. Console.WriteLine("ISCSI STOP - stops the iSCSI server");
  99. }
  100. }
  101. }