Program.StartCommand.cs 3.0 KB

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