/// <summary>
        /// Add new server definition to server.
        /// </summary>
        /// <param name="newServer">Server definition</param>
        private void AddServer(Server newServer)
        {
            if (string.IsNullOrEmpty(newServer.GetName()))
            {
                throw new ConfigurationReaderException("Name is required for all servers.");
            }
            else if (string.IsNullOrEmpty(newServer.GetMachine()))
            {
                throw new ConfigurationReaderException("Machine is required for all servers.");
            }

            if (servers.ContainsKey(newServer.GetMachine().ToLower()))
            {
                throw new ConfigurationReaderException("Server machine \"" + newServer.GetMachine() + "\" is already registered.");
            }

            servers.Add(newServer.GetMachine().ToLower(), newServer);
        }
示例#2
0
        /// <summary>
        /// Update or set server state.
        /// </summary>
        /// <param name="server">Server</param>
        /// <param name="initializing">True if is called while initializing application, false otherwise</param>
        private void UpdateServerState(Server server, bool initializing)
        {
            if (!machines.ContainsKey(server))
            {
                throw new VirtualBoxServerException("Server " + server.GetName() + " is not registered.");
            }

            IMachine machine = machines[server];

            Status newStatus;
            Status oldStatus = server.GetStatus();

            switch (machine.State)
            {
                case MachineState.MachineState_Running:
                    newStatus = Status.RUNNING;
                    break;
                case MachineState.MachineState_Starting:
                case MachineState.MachineState_Restoring:
                    newStatus = Status.STARTING;
                    break;
                case MachineState.MachineState_Stopping:
                case MachineState.MachineState_Saving:
                    newStatus = Status.STOPING;
                    break;
                default :
                    newStatus = Status.POWEREDOFF;
                    break;
            }

            if (newStatus == oldStatus)
            {
                return;
            }

            server.SetStatus(newStatus, initializing);
        }
示例#3
0
 /// <summary>
 /// Update or ser server state.
 /// </summary>
 /// <param name="server">Server</param>
 private void UpdateServerState(Server server)
 {
     UpdateServerState(server, false);
 }
示例#4
0
        /// <summary>
        /// Get or create server machine session.
        /// </summary>
        /// <param name="server">Server</param>
        /// <returns>New or existing session</returns>
        private Session getSession(Server server)
        {
            Session serverSession;
            if (!serverSessions.ContainsKey(server))
            {
                serverSession = new SessionClass();
                serverSessions.Add(server, serverSession);
            }
            else
            {
                serverSession = serverSessions[server];
            }

            return serverSession;
        }
示例#5
0
        /// <summary>
        /// Stop server.
        /// </summary>
        /// <param name="server">Server</param>
        public void StopServer(Server server)
        {
            if (!machines.ContainsKey(server))
            {
                throw new VirtualBoxServerException("Server " + server.GetName() + " is not registered.");
            }

            if (server.GetStatus() == Status.POWEREDOFF)
            {
                return;
            }
            else if (server.GetStatus() != Status.RUNNING)
            {
                throw new VirtualBoxServerException("Server " + server.GetName() + " is not running.");
            }

            IMachine machine = machines[server];
            Session serverSession = getSession(server);

            server.SetStoping();

            if (serverSession.State == SessionState.SessionState_Locked)
            {
                serverSession.UnlockMachine();
            }
            machine.LockMachine(serverSession, LockType.LockType_Shared);
            serverSession.Console.PowerButton();
            serverSession.UnlockMachine();
        }
示例#6
0
        /// <summary>
        /// Start server.
        /// </summary>
        /// <param name="server">Server</param>
        public void StartServer(Server server)
        {
            if (!machines.ContainsKey(server))
            {
                throw new VirtualBoxServerException("Server " + server.GetName() + " is not registered.");
            }

            if (server.GetStatus() == Status.RUNNING)
            {
                return;
            }
            else if (server.GetStatus() != Status.POWEREDOFF)
            {
                throw new VirtualBoxServerException("Server " + server.GetName() + " is not powered off.");
            }

            try
            {
                IMachine machine = machines[server];
                Session serverSession = getSession(server);
                DateTime startTime = DateTime.Now;

                while (machine.SessionState != SessionState.SessionState_Unlocked)
                {
                    long ticks = DateTime.Now.Ticks - startTime.Ticks;

                    if (ticks >= (WAIT_FOR_RESTART_SERVER_SECONDS * 10000000))
                    {
                        break;
                    }
                }

                if (machine.SessionState == SessionState.SessionState_Unlocked)
                {
                    server.SetStarting();

                    IProgress progress = machine.LaunchVMProcess(serverSession, "headless", "");
                }
            }
            catch
            {
                throw new VirtualBoxServerException("Server " + server.GetName() + " can't be run.");
            }
        }
示例#7
0
        /// <summary>
        /// Update tray icon.
        /// </summary>
        public void UpdateTray()
        {
            Server defaultServer = null;
            bool isSomeServerPoweredOff = false;
            string title = "";

            foreach (Server server in servers.Values)
            {
                ToolStripMenuItem miServer = miServers[server];
                if (server.IsRunning())
                {
                    if (defaultServer == null)
                    {
                        defaultServer = server;
                    }
                    miServer.Visible = true;
                    miServer.Enabled = true;

                    title += server.GetName() + " | ";
                }
                else if (server.IsRestarting())
                {
                    miServer.Enabled = false;
                }
                else
                {
                    isSomeServerPoweredOff = true;
                    miServer.Visible = false;
                }
            }

            if (defaultServer == null) // No server is running...
            {
                trayIcon.Icon = iconStop;

                miShowDefaultConsole.Visible = false;
                separatorShowDefaultConsole.Visible = false;
                separatorServers.Visible = false;
            }
            else
            {
                trayIcon.Icon = iconRun;

                miShowDefaultConsole.Text = "Show " + defaultServer.GetName() + " &console";
                miShowDefaultConsole.Visible = true;
                separatorShowDefaultConsole.Visible = true;
                separatorServers.Visible = true;
            }

            if (isSomeServerPoweredOff)
            {
                miStartServers.Visible = true;
                separatorStartServers.Visible = true;
            }
            else
            {
                miStartServers.Visible = false;
                separatorStartServers.Visible = false;
            }

            trayIcon.Text = Devel79Tray.TRAY_TITLE + ((defaultServer == null) ? "" : (" [" + title.Substring(0, title.Length - 3) + "]"));

            this.defaultServer = defaultServer;
        }
        /// <summary>
        /// Read configuration file.
        /// </summary>
        private void ReadConfiguration()
        {
            if (File.Exists(configurationFile))
            {
                try
                {
                    Server server = null;

                    StreamReader configuration = File.OpenText(configurationFile);

                    string line = null;
                    while ((line = configuration.ReadLine()) != null)
                    {
                        line = line.Trim();

                        if (line.Equals("") || line.StartsWith("#"))
                        {
                            continue;
                        }
                        else if (line.ToLower() == "[server]")
                        {
                            if (server != null)
                            {
                                AddServer(server);
                            }
                            server = new Server(this.tray, this.vboxServer);
                            continue;
                        }

                        if (server == null)
                        {
                            throw new ConfigurationReaderException("No [server] section defined for data.");
                        }

                        string[] settings = new string[2];
                        settings = line.Split("=".ToCharArray(), 2);

                        string key = settings[0].Trim();
                        string value = settings[1].Trim();

                        switch (key.ToLower())
                        {
                            case "name":
                                server.SetName(value);
                                break;
                            case "machine":
                                server.SetMachine(value);
                                break;
                            case "ssh":
                                server.SetSSHCommand(value);
                                break;
                            case "watch":
                                string[] watchingDirectory = value.Split("|".ToCharArray(), 3);

                                if (watchingDirectory.Length != 3)
                                {
                                    throw new ConfigurationReaderException("Parameter \"watch\" must have 3 parameters divided by \"|\" (name | message | directory)");
                                }
                                else if (!Directory.Exists(watchingDirectory[2]))
                                {
                                    throw new ConfigurationReaderException("Directory \"" + watchingDirectory[2].Trim() + "\" for watching not exists.");
                                }
                                server.AddDirectoryWatching(watchingDirectory[0].Trim(), watchingDirectory[1].Trim(), watchingDirectory[2].Trim());
                                break;
                            case "command":
                                string[] command = value.Split("|".ToCharArray(), 2);

                                if (command.Length != 2)
                                {
                                    throw new ConfigurationReaderException("Parameter \"command\" must have 2 parameters divided by \"|\" (name | command)");
                                }

                                server.AddCommand(command[0].Trim(), command[1].Trim());
                                break;
                        }
                    }

                    configuration.Close();

                    if (server == null)
                    {
                        throw new ConfigurationReaderException("No server defined in configuration.");
                    }

                    AddServer(server);
                }
                catch (ConfigurationReaderException e)
                {
                    throw e;
                }
                catch
                {
                    throw new ConfigurationReaderException("Can't read from configuration file \"" + configurationFile + "\".");
                }
            }
            else
            {
                throw new ConfigurationReaderException("Configuration file \"" + configurationFile + "\" not exists.");
            }
        }