Ejemplo n.º 1
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();
        }
Ejemplo n.º 2
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", new string[0]);
                }
            }
            catch
            {
                throw new VirtualBoxServerException("Server " + server.GetName() + " can't be run.");
            }
        }
Ejemplo n.º 3
0
        /// <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);
        }
Ejemplo n.º 4
0
        /// <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);
        }
Ejemplo n.º 5
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);
        }
Ejemplo n.º 6
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);
        }
Ejemplo n.º 7
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();
        }
Ejemplo n.º 8
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.");
            }
        }
Ejemplo n.º 9
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;
        }