VirtualBox

source: vbox/trunk/src/VBox/Main/src-server/HostPower.cpp@ 45434

Last change on this file since 45434 was 44528, checked in by vboxsync, 12 years ago

header (C) fixes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.5 KB
Line 
1/** @file
2 *
3 * VirtualBox interface to host's power notification service
4 */
5
6/*
7 * Copyright (C) 2006-2011 Oracle Corporation
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
18
19/*******************************************************************************
20* Header Files *
21*******************************************************************************/
22
23#include "HostPower.h"
24#include "Logging.h"
25
26#include <VBox/com/ptr.h>
27
28#include "VirtualBoxImpl.h"
29
30#include <iprt/mem.h>
31
32HostPowerService::HostPowerService (VirtualBox *aVirtualBox)
33{
34 Assert(aVirtualBox != NULL);
35 mVirtualBox = aVirtualBox;
36}
37
38HostPowerService::~HostPowerService()
39{
40}
41
42void HostPowerService::notify(HostPowerEvent aEvent)
43{
44 SessionMachinesList machines;
45 VirtualBox::InternalControlList controls;
46
47 HRESULT rc = S_OK;
48
49 switch (aEvent)
50 {
51 case HostPowerEvent_Suspend:
52 {
53 LogFunc (("SUSPEND\n"));
54
55#ifdef VBOX_WITH_RESOURCE_USAGE_API
56 /* Suspend performance sampling to avoid unnecessary callbacks due to jumps in time. */
57 PerformanceCollector *perfcollector = mVirtualBox->performanceCollector();
58
59 if (perfcollector)
60 perfcollector->suspendSampling();
61#endif
62 mVirtualBox->getOpenedMachines(machines, &controls);
63
64 /* pause running VMs */
65 for (VirtualBox::InternalControlList::const_iterator it = controls.begin();
66 it != controls.end();
67 ++it)
68 {
69 ComPtr<IInternalSessionControl> pControl = *it;
70
71 /* get the remote console */
72 ComPtr<IConsole> console;
73 rc = pControl->GetRemoteConsole(console.asOutParam());
74 /* the VM could have been powered down and closed or whatever */
75 if (FAILED(rc))
76 continue;
77
78 /* note that Pause() will simply return a failure if the VM is
79 * in an inappropriate state */
80 rc = console->Pause();
81 if (FAILED(rc))
82 continue;
83
84 /* save the control to un-pause the VM later */
85 mConsoles.push_back(console);
86 }
87
88 LogFunc (("Suspended %d VMs\n", mConsoles.size()));
89
90 break;
91 }
92
93 case HostPowerEvent_Resume:
94 {
95 LogFunc (("RESUME\n"));
96
97 size_t resumed = 0;
98
99 /* go through VMs we paused on Suspend */
100 for (size_t i = 0; i < mConsoles.size(); ++i)
101 {
102 /* note that Resume() will simply return a failure if the VM is
103 * in an inappropriate state (it will also fail if the VM has
104 * been somehow closed by this time already so that the
105 * console reference we have is dead) */
106 rc = mConsoles[i]->Resume();
107 if (FAILED(rc))
108 continue;
109
110 ++ resumed;
111 }
112
113 LogFunc (("Resumed %d VMs\n", resumed));
114
115#ifdef VBOX_WITH_RESOURCE_USAGE_API
116 /* Resume the performance sampling. */
117 PerformanceCollector *perfcollector = mVirtualBox->performanceCollector();
118
119 if (perfcollector)
120 perfcollector->resumeSampling();
121#endif
122
123 mConsoles.clear();
124
125 break;
126 }
127
128 case HostPowerEvent_BatteryLow:
129 {
130 LogFunc (("BATTERY LOW\n"));
131
132 mVirtualBox->getOpenedMachines(machines, &controls);
133
134 size_t saved = 0;
135
136 /* save running VMs */
137 for (VirtualBox::InternalControlList::const_iterator it = controls.begin();
138 it != controls.end();
139 ++it)
140 {
141 ComPtr<IInternalSessionControl> pControl = *it;
142 /* get the remote console */
143 ComPtr<IConsole> console;
144 rc = pControl->GetRemoteConsole (console.asOutParam());
145 /* the VM could have been powered down and closed or whatever */
146 if (FAILED(rc))
147 continue;
148
149 ComPtr<IProgress> progress;
150
151 /* note that SaveState() will simply return a failure if the VM
152 * is in an inappropriate state */
153 rc = console->SaveState (progress.asOutParam());
154 if (FAILED(rc))
155 continue;
156
157 /* Wait until the operation has been completed. */
158 rc = progress->WaitForCompletion(-1);
159 if (SUCCEEDED(rc))
160 {
161 LONG iRc;
162 progress->COMGETTER(ResultCode)(&iRc);
163 rc = iRc;
164 }
165
166 AssertMsg (SUCCEEDED(rc), ("SaveState WaitForCompletion "
167 "failed with %Rhrc (%#08X)\n", rc, rc));
168
169 if (SUCCEEDED(rc))
170 ++ saved;
171 }
172
173 LogFunc (("Saved %d VMs\n", saved));
174
175 break;
176 }
177 }
178}
179/* vi: set tabstop=4 shiftwidth=4 expandtab: */
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