public static void StartCommand(string[] args)
        {
            if (m_server == null)
            {
                if (m_targets.Count > 0)
                {
                    KeyValuePairList <string, string> parameters = ParseParameters(args, 1);
                    if (!VerifyParameters(parameters, "port", "log"))
                    {
                        Console.WriteLine();
                        Console.WriteLine("Invalid parameter");
                        HelpStart();
                        return;
                    }

                    int port = DefaultISCSIPort;
                    if (parameters.ContainsKey("port"))
                    {
                        port = Conversion.ToInt32(parameters.ValueOf("port"), DefaultISCSIPort);
                    }
                    string logFile = String.Empty;
                    if (parameters.ContainsKey("log"))
                    {
                        logFile = parameters.ValueOf("log");
                    }
                    m_server = new ISCSIServer(m_targets, port, logFile);
                    try
                    {
                        ISCSIServer.Log("Starting Server");
                    }
                    catch (IOException)
                    {
                        Console.WriteLine("Could not append to log file");
                        return;
                    }

                    try
                    {
                        m_server.Start();
                        Console.WriteLine("Server started, listening on port {0}", port);
                    }
                    catch (SocketException)
                    {
                        Console.WriteLine("Could not start iSCSI server");
                        m_server.Stop();
                        m_server = null;
                    }
                }
                else
                {
                    Console.WriteLine("No disks have been attached");
                }
            }
        }
示例#2
0
 private void btnStart_Click(object sender, EventArgs e)
 {
     if (!m_started)
     {
         IPAddress serverAddress = (IPAddress)comboIPAddress.SelectedValue;
         int       port          = Conversion.ToInt32(txtPort.Text, 0);
         if (port <= 0 || port > UInt16.MaxValue)
         {
             MessageBox.Show("Invalid TCP port", "Error");
             return;
         }
         IPEndPoint endpoint = new IPEndPoint(serverAddress, port);
         try
         {
             m_server.Start(endpoint);
         }
         catch (SocketException ex)
         {
             MessageBox.Show("Cannot start server, " + ex.Message, "Error");
             return;
         }
         btnStart.Text          = "Stop";
         txtPort.Enabled        = false;
         comboIPAddress.Enabled = false;
         m_started = true;
         UpdateUI();
     }
     else
     {
         m_server.Stop();
         lblStatus.Text         = String.Empty;
         m_started              = false;
         btnStart.Text          = "Start";
         txtPort.Enabled        = true;
         comboIPAddress.Enabled = true;
     }
 }
示例#3
0
        private static void ReadConfig()
        {
            if (!File.Exists(CONFIG_FILE))
            {
                Console.WriteLine($"{CONFIG_FILE} not found, starting in interactive mode");
                return;
            }

            var config = JsonConvert.DeserializeObject <Config>(File.ReadAllText(CONFIG_FILE));

            if (config.Targets == null || config.Targets.Length == 0)
            {
                Console.WriteLine($"No targets configured, starting in interactive mode");
                return;
            }

            var targets = new List <ISCSITarget>();

            foreach (var targetConfig in config.Targets)
            {
                Console.WriteLine("Adding target '{0}'", targetConfig.Name);
                var disks = new List <Disk>();

                foreach (var diskConfig in targetConfig.Disks)
                {
                    Disk disk;
                    var  parameters = diskConfig.Parameters as JObject;
                    switch (diskConfig.Kind)
                    {
                    case DiskKind.Raw:
                        Console.WriteLine("  Adding Raw disk image '{0}'. Filename: '{1}'",
                                          diskConfig.Name, parameters["File"].Value <string>());
                        disk = DiskImage.GetDiskImage(parameters["File"].Value <string>());
                        break;

                    case DiskKind.External:
                        Console.WriteLine("  Adding External disk '{0}'", diskConfig.Name);
                        var type    = Type.GetType(parameters["Type"].Value <string>());
                        var extDisk = Activator.CreateInstance(type) as ExternalDisk;
                        extDisk.SetParameters(parameters);
                        disk = extDisk;
                        break;

                    default:
                        throw new NotImplementedException();
                    }
                    if (disk is DiskImage)
                    {
                        bool isLocked = ((DiskImage)disk).ExclusiveLock();
                        if (!isLocked)
                        {
                            Console.WriteLine("Error: Cannot lock the disk image for exclusive access");
                            return;
                        }
                    }
                    disks.Add(disk);
                }
                targets.Add(new ISCSITarget(targetConfig.Name, disks));
            }
            m_server                 = new ISCSIServer(targets, config.Port, config.Logging?.File);
            ISCSIServer.LogLevel     = config.Logging.Level;
            ISCSIServer.LogToConsole = config.Logging.LogToConsole;
            m_server.Start();
            Console.WriteLine("Server started, listening on port {0}", config.Port);
        }