VirtualBox

source: vbox/trunk/src/VBox/Main/HostPower.cpp@ 13836

Last change on this file since 13836 was 13713, checked in by vboxsync, 16 years ago

Main: #3276: Simplified suspend/resume event handling in VBoxSVC.

File size: 4.9 KB
Line 
1/** @file
2 *
3 * VirtualBox interface to host's power notification service
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21
22
23/*******************************************************************************
24* Header Files *
25*******************************************************************************/
26#include <iprt/mem.h>
27#include <VBox/com/ptr.h>
28#include "HostPower.h"
29#include "Logging.h"
30
31HostPowerService::HostPowerService (VirtualBox *aVirtualBox)
32{
33 Assert (aVirtualBox != NULL);
34 mVirtualBox = aVirtualBox;
35}
36
37HostPowerService::~HostPowerService()
38{
39}
40
41void HostPowerService::notify (HostPowerEvent aEvent)
42{
43 VirtualBox::SessionMachineVector machines;
44 VirtualBox::InternalControlVector controls;
45
46 HRESULT rc = S_OK;
47
48 switch (aEvent)
49 {
50 case HostPowerEvent_Suspend:
51 {
52 LogFunc (("SUSPEND\n"));
53
54 mVirtualBox->getOpenedMachinesAndControls (machines, controls);
55
56 /* pause running VMs */
57 for (size_t i = 0; i < controls.size(); ++ i)
58 {
59 /* get the remote console */
60 ComPtr <IConsole> console;
61 rc = controls [i]->GetRemoteConsole (console.asOutParam());
62 /* the VM could have been powered down and closed or whatever */
63 if (FAILED (rc))
64 continue;
65
66 /* note that Pause() will simply return a failure if the VM is
67 * in an inappropriate state */
68 rc = console->Pause();
69 if (FAILED (rc))
70 continue;
71
72 /* save the control to un-pause the VM later */
73 mConsoles.push_back (console);
74 }
75
76 LogFunc (("Suspended %d VMs\n", mConsoles.size()));
77
78 break;
79 }
80
81 case HostPowerEvent_Resume:
82 {
83 LogFunc (("RESUME\n"));
84
85 size_t resumed = 0;
86
87 /* go through VMs we paused on Suspend */
88 for (size_t i = 0; i < mConsoles.size(); ++ i)
89 {
90 /* note that Resume() will simply return a failure if the VM is
91 * in an inappropriate state (it will also fail if the VM has
92 * been somehow closed by this time already so that the
93 * console reference we have is dead) */
94 rc = mConsoles [i]->Resume();
95 if (FAILED (rc))
96 continue;
97
98 ++ resumed;
99 }
100
101 LogFunc (("Resumed %d VMs\n", resumed));
102
103 mConsoles.clear();
104
105 break;
106 }
107
108 case HostPowerEvent_BatteryLow:
109 {
110 LogFunc (("BATTERY LOW\n"));
111
112 mVirtualBox->getOpenedMachinesAndControls (machines, controls);
113
114 size_t saved = 0;
115
116 /* save running VMs */
117 for (size_t i = 0; i < controls.size(); ++ i)
118 {
119 /* get the remote console */
120 ComPtr <IConsole> console;
121 rc = controls [i]->GetRemoteConsole (console.asOutParam());
122 /* the VM could have been powered down and closed or whatever */
123 if (FAILED (rc))
124 continue;
125
126 ComPtr<IProgress> progress;
127
128 /* note that SaveState() will simply return a failure if the VM
129 * is in an inappropriate state */
130 rc = console->SaveState (progress.asOutParam());
131 if (FAILED (rc))
132 continue;
133
134 /* Wait until the operation has been completed. */
135 rc = progress->WaitForCompletion(-1);
136 if (SUCCEEDED (rc))
137 progress->COMGETTER(ResultCode) (&rc);
138
139 AssertMsg (SUCCEEDED (rc), ("SaveState WaitForCompletion "
140 "failed with %Rhrc (%#08X)\n", rc, rc));
141
142 if (SUCCEEDED (rc))
143 ++ saved;
144 }
145
146 LogFunc (("Saved %d VMs\n", saved));
147
148 break;
149 }
150 }
151}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette