Changeset 13713 in vbox
- Timestamp:
- Oct 31, 2008 1:35:03 PM (16 years ago)
- svn:sync-xref-src-repo-rev:
- 38680
- Location:
- trunk/src/VBox/Main
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Main/HostPower.cpp
r13706 r13713 29 29 #include "Logging.h" 30 30 31 HostPowerService::HostPowerService (VirtualBox *aVirtualBox) : aMachineSuspended(NULL), cbMachineSuspended(0)31 HostPowerService::HostPowerService (VirtualBox *aVirtualBox) 32 32 { 33 Assert (aVirtualBox != NULL); 33 34 mVirtualBox = aVirtualBox; 34 35 } … … 38 39 } 39 40 40 41 void HostPowerService::notify(HostPowerEvent event) 41 void HostPowerService::notify (HostPowerEvent aEvent) 42 42 { 43 43 VirtualBox::SessionMachineVector machines; 44 mVirtualBox->getOpenedMachines (machines);44 VirtualBox::InternalControlVector controls; 45 45 46 switch (event) 46 HRESULT rc = S_OK; 47 48 switch (aEvent) 47 49 { 48 case HostPowerEvent_Suspend:49 if (machines.size())50 aMachineSuspended = (BOOL *)RTMemAllocZ(sizeof(BOOL) * machines.size());50 case HostPowerEvent_Suspend: 51 { 52 LogFunc (("SUSPEND\n")); 51 53 52 cbMachineSuspended = machines.size();54 mVirtualBox->getOpenedMachinesAndControls (machines, controls); 53 55 54 for (size_t i = 0; i < machines.size(); i++) 55 processEvent(machines[i], HostPowerEvent_Suspend, (aMachineSuspended) ? &aMachineSuspended[i] : NULL); 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; 56 65 57 Log(("HostPowerService::notify SUSPEND\n")); 58 break; 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; 59 71 60 case HostPowerEvent_Resume: 61 Log(("HostPowerService::notify RESUME\n")); 72 /* save the control to un-pause the VM later */ 73 mConsoles.push_back (console); 74 } 62 75 63 if (aMachineSuspended) 64 { 65 /* It's possible (in theory) that machines are created or destroyed between the suspend notification and the actual host suspend. 66 * Ignore this edge case and just make sure not to access invalid data. 67 */ 68 cbMachineSuspended = RT_MIN(machines.size(), cbMachineSuspended); 76 LogFunc (("Suspended %d VMs\n", mConsoles.size())); 69 77 70 for (size_t i = 0; i < cbMachineSuspended; i++) 71 processEvent(machines[i], HostPowerEvent_Resume, &aMachineSuspended[i]); 72 73 RTMemFree(aMachineSuspended); 74 cbMachineSuspended = 0; 75 aMachineSuspended = NULL; 76 } 77 break; 78 79 case HostPowerEvent_BatteryLow: 80 Log(("HostPowerService::notify BATTERY LOW\n")); 81 for (size_t i = 0; i < machines.size(); i++) 82 processEvent(machines[i], HostPowerEvent_BatteryLow, NULL); 83 break; 84 } 85 86 machines.clear(); 87 } 88 89 HRESULT HostPowerService::processEvent(SessionMachine *machine, HostPowerEvent event, BOOL *pMachineSuspended) 90 { 91 MachineState_T state; 92 HRESULT rc; 93 94 rc = machine->COMGETTER(State)(&state); 95 CheckComRCReturnRC (rc); 96 97 /* Power event handling: 98 * - pause running machines for HostPowerEvent_Suspend 99 * - resume paused machines for HostPowerEvent_Resume 100 * - save the state of running and paused machine for HostPowerEvent_BatteryLow 101 */ 102 if ( (state == MachineState_Running && event != HostPowerEvent_Resume) 103 || (state == MachineState_Paused && event != HostPowerEvent_Suspend)) 104 { 105 ComPtr <ISession> session; 106 107 rc = session.createInprocObject (CLSID_Session); 108 if (FAILED (rc)) 109 return rc; 110 111 /* get the IInternalSessionControl interface */ 112 ComPtr <IInternalSessionControl> control = session; 113 if (!control) 114 { 115 rc = E_INVALIDARG; 116 goto fail; 78 break; 117 79 } 118 80 119 rc = machine->openExistingSession (control); 120 if (SUCCEEDED (rc)) 81 case HostPowerEvent_Resume: 121 82 { 122 /* get the associated console */ 123 ComPtr<IConsole> console; 124 rc = session->COMGETTER(Console)(console.asOutParam()); 125 if (SUCCEEDED (rc)) 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) 126 89 { 127 switch (event) 128 { 129 case HostPowerEvent_Suspend: 130 rc = console->Pause(); 131 if ( SUCCEEDED(rc) 132 && pMachineSuspended) 133 *pMachineSuspended = TRUE; 134 break; 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; 135 97 136 case HostPowerEvent_Resume: 137 Assert(pMachineSuspended); 138 if (*pMachineSuspended == TRUE) 139 rc = console->Resume(); 140 break; 98 ++ resumed; 99 } 141 100 142 case HostPowerEvent_BatteryLow: 143 { 144 ComPtr<IProgress> progress; 101 LogFunc (("Resumed %d VMs\n", resumed)); 145 102 146 rc = console->SaveState(progress.asOutParam()); 147 if (SUCCEEDED(rc)) 148 { 149 /* Wait until the operation has been completed. */ 150 progress->WaitForCompletion(-1); 103 mConsoles.clear(); 151 104 152 progress->COMGETTER(ResultCode)(&rc); 153 AssertMsg(SUCCEEDED(rc), ("SaveState WaitForCompletion failed with %x\n", rc)); 154 } 105 break; 106 } 155 107 156 break; 157 } 108 case HostPowerEvent_BatteryLow: 109 { 110 LogFunc (("BATTERY LOW\n")); 158 111 159 } /* switch (event) */ 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; 160 144 } 145 146 LogFunc (("Saved %d VMs\n", saved)); 147 148 break; 161 149 } 162 fail:163 session->Close();164 150 } 165 return rc;166 151 } -
trunk/src/VBox/Main/MachineImpl.cpp
r13696 r13713 3828 3828 * 3829 3829 * @param aMachine Session machine object. 3830 * @param aIPCSem Mutex IPC semaphore handle for this machine. 3830 * @param aControl Direct session control object (optional). 3831 * @param aIPCSem Mutex IPC semaphore handle for this machine (optional). 3831 3832 * 3832 3833 * @note locks this object for reading. … … 3834 3835 #if defined (RT_OS_WINDOWS) 3835 3836 bool Machine::isSessionOpen (ComObjPtr <SessionMachine> &aMachine, 3837 ComPtr <IInternalSessionControl> *aControl /*= NULL*/, 3836 3838 HANDLE *aIPCSem /*= NULL*/, 3837 3839 bool aAllowClosing /*= false*/) 3838 3840 #elif defined (RT_OS_OS2) 3839 3841 bool Machine::isSessionOpen (ComObjPtr <SessionMachine> &aMachine, 3842 ComPtr <IInternalSessionControl> *aControl /*= NULL*/, 3840 3843 HMTX *aIPCSem /*= NULL*/, 3841 3844 bool aAllowClosing /*= false*/); 3842 3845 #else 3843 3846 bool Machine::isSessionOpen (ComObjPtr <SessionMachine> &aMachine, 3847 ComPtr <IInternalSessionControl> *aControl /*= NULL*/, 3844 3848 bool aAllowClosing /*= false*/) 3845 3849 #endif … … 3860 3864 3861 3865 aMachine = mData->mSession.mMachine; 3866 3867 if (aControl != NULL) 3868 *aControl = mData->mSession.mDirectControl; 3862 3869 3863 3870 #if defined (RT_OS_WINDOWS) || defined (RT_OS_OS2) -
trunk/src/VBox/Main/VirtualBoxImpl.cpp
r13696 r13713 2499 2499 2500 2500 /** 2501 * Returns the list of opened machines (machines having direct sessionsopened by 2502 * client processes). 2503 * 2504 * @note The returned list contains smart pointers. So, clear it as soon as 2501 * Returns the list of opened machines (machines having direct sessions opened 2502 * by client processes) and optionally the list of direct session controls. 2503 * 2504 * @param aMachines Where to put opened machines (will be empty if none). 2505 * @param aControls Where to put direct session controls (optional). 2506 * 2507 * @note The returned lists contain smart pointers. So, clear it as soon as 2505 2508 * it becomes no more necessary to release instances. 2506 2509 * … … 2511 2514 * @note Locks objects for reading. 2512 2515 */ 2513 void VirtualBox::getOpenedMachines (SessionMachineVector &aVector) 2516 void VirtualBox::getOpenedMachines (SessionMachineVector &aMachines, 2517 InternalControlVector *aControls /*= NULL*/) 2514 2518 { 2515 2519 AutoCaller autoCaller (this); 2516 2520 AssertComRCReturnVoid (autoCaller.rc()); 2517 2521 2518 aVector.clear(); 2522 aMachines.clear(); 2523 if (aControls) 2524 aControls->clear(); 2519 2525 2520 2526 AutoReadLock alock (this); … … 2525 2531 { 2526 2532 ComObjPtr <SessionMachine> sm; 2527 if ((*it)->isSessionOpen (sm)) 2528 aVector.push_back (sm); 2533 ComPtr <IInternalSessionControl> ctl; 2534 if ((*it)->isSessionOpen (sm, &ctl)) 2535 { 2536 aMachines.push_back (sm); 2537 if (aControls) 2538 aControls->push_back (ctl); 2539 } 2529 2540 } 2530 2541 } … … 4071 4082 ComObjPtr <SessionMachine> sm; 4072 4083 HANDLE ipcSem; 4073 if ((*it)->isSessionOpenOrClosing (sm, &ipcSem))4084 if ((*it)->isSessionOpenOrClosing (sm, NULL, &ipcSem)) 4074 4085 { 4075 4086 machines.push_back (sm); … … 4263 4274 ComObjPtr <SessionMachine> sm; 4264 4275 HMTX ipcSem; 4265 if ((*it)->isSessionOpenOrClosing (sm, &ipcSem))4276 if ((*it)->isSessionOpenOrClosing (sm, NULL, &ipcSem)) 4266 4277 { 4267 4278 machines.push_back (sm); -
trunk/src/VBox/Main/include/HostImpl.h
r13655 r13713 126 126 #endif 127 127 128 HostPowerService *mHostPowerService;129 130 128 // for VirtualBoxSupportErrorInfoImpl 131 129 static const wchar_t *getComponentName() { return L"Host"; } … … 179 177 #endif /* VBOX_WITH_USB */ 180 178 179 HostPowerService *mHostPowerService; 181 180 }; 182 181 -
trunk/src/VBox/Main/include/HostPower.h
r13705 r13713 26 26 #include "MachineImpl.h" 27 27 28 #include <vector> 29 28 30 class VirtualBox; 29 31 … … 38 40 { 39 41 public: 40 HostPowerService(VirtualBox *aVirtualBox); 42 43 HostPowerService (VirtualBox *aVirtualBox); 41 44 virtual ~HostPowerService(); 42 45 43 void notify(HostPowerEvent event); 44 HRESULT processEvent(SessionMachine *machine, HostPowerEvent event, BOOL *paMachineSuspended); 46 void notify (HostPowerEvent aEvent); 45 47 46 48 protected: 49 47 50 ComObjPtr <VirtualBox, ComWeakRef> mVirtualBox; 48 51 49 BOOL *aMachineSuspended; 50 size_t cbMachineSuspended; 52 std::vector <ComPtr <IConsole> > mConsoles; 51 53 }; 52 54 … … 58 60 { 59 61 public: 62 60 63 HostPowerServiceWin(VirtualBox *aVirtualBox); 61 64 virtual ~HostPowerServiceWin(); 62 65 63 66 private: 67 64 68 static DECLCALLBACK(int) NotificationThread (RTTHREAD ThreadSelf, void *pInstance); 65 69 static LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam); -
trunk/src/VBox/Main/include/MachineImpl.h
r13696 r13713 636 636 637 637 bool isSessionOpen (ComObjPtr <SessionMachine> &aMachine, 638 ComPtr <IInternalSessionControl> *aControl = NULL, 638 639 HANDLE *aIPCSem = NULL, bool aAllowClosing = false); 639 640 bool isSessionSpawning (RTPROCESS *aPID = NULL); 640 641 641 642 bool isSessionOpenOrClosing (ComObjPtr <SessionMachine> &aMachine, 643 ComPtr <IInternalSessionControl> *aControl = NULL, 642 644 HANDLE *aIPCSem = NULL) 643 { return isSessionOpen (aMachine, a IPCSem, true /* aAllowClosing */); }645 { return isSessionOpen (aMachine, aControl, aIPCSem, true /* aAllowClosing */); } 644 646 645 647 #elif defined (RT_OS_OS2) 646 648 647 649 bool isSessionOpen (ComObjPtr <SessionMachine> &aMachine, 650 ComPtr <IInternalSessionControl> *aControl = NULL, 648 651 HMTX *aIPCSem = NULL, bool aAllowClosing = false); 649 652 … … 651 654 652 655 bool isSessionOpenOrClosing (ComObjPtr <SessionMachine> &aMachine, 656 ComPtr <IInternalSessionControl> *aControl = NULL, 653 657 HMTX *aIPCSem = NULL) 654 { return isSessionOpen (aMachine, a IPCSem, true /* aAllowClosing */); }658 { return isSessionOpen (aMachine, aControl, aIPCSem, true /* aAllowClosing */); } 655 659 656 660 #else 657 661 658 662 bool isSessionOpen (ComObjPtr <SessionMachine> &aMachine, 663 ComPtr <IInternalSessionControl> *aControl = NULL, 659 664 bool aAllowClosing = false); 660 665 bool isSessionSpawning(); 661 666 662 bool isSessionOpenOrClosing (ComObjPtr <SessionMachine> &aMachine) 663 { return isSessionOpen (aMachine, true /* aAllowClosing */); } 667 bool isSessionOpenOrClosing (ComObjPtr <SessionMachine> &aMachine, 668 ComPtr <IInternalSessionControl> *aControl = NULL) 669 { return isSessionOpen (aMachine, aControl, true /* aAllowClosing */); } 664 670 665 671 #endif … … 961 967 Bstr mIPCSemName; 962 968 friend bool Machine::isSessionOpen (ComObjPtr <SessionMachine> &aMachine, 969 ComPtr <IInternalSessionControl> *aControl, 963 970 HANDLE *aIPCSem, bool aAllowClosing); 964 971 #elif defined (RT_OS_OS2) … … 966 973 Bstr mIPCSemName; 967 974 friend bool Machine::isSessionOpen (ComObjPtr <SessionMachine> &aMachine, 975 ComPtr <IInternalSessionControl> *aControl, 968 976 HMTX *aIPCSem, bool aAllowClosing); 969 977 #elif defined (VBOX_WITH_SYS_V_IPC_SESSION_WATCHER) -
trunk/src/VBox/Main/include/VirtualBoxImpl.h
r13580 r13713 82 82 typedef std::vector <ComObjPtr <SessionMachine> > SessionMachineVector; 83 83 typedef std::vector <ComObjPtr <Machine> > MachineVector; 84 85 typedef std::vector <ComPtr <IInternalSessionControl> > InternalControlVector; 84 86 85 87 class CallbackEvent; … … 216 218 ComObjPtr <GuestOSType> getUnknownOSType(); 217 219 218 void getOpenedMachines (SessionMachineVector &aVector); 220 void getOpenedMachines (SessionMachineVector &aMachines, 221 InternalControlVector *aControls = NULL); 222 223 /** Shortcut to #getOpenedMachines (aMachines, &aControls). */ 224 void getOpenedMachinesAndControls (SessionMachineVector &aMachines, 225 InternalControlVector &aControls) 226 { getOpenedMachines (aMachines, &aControls); } 219 227 220 228 bool isMachineIdValid (const Guid &aId)
Note:
See TracChangeset
for help on using the changeset viewer.