Program.StartCommand.cs 3.4 KB

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