VirtualBox

source: vbox/trunk/src/VBox/Frontends/VBoxHeadless/testcase/tstHeadless.cpp@ 59036

Last change on this file since 59036 was 50117, checked in by vboxsync, 11 years ago

Main+Frontends: clear out some cruft code, outdated EventQueue stuff and whitespace cleanup

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.8 KB
Line 
1/** @file
2 *
3 * VBox frontends: VBoxHeadless frontend:
4 * Testcases
5 */
6
7/*
8 * Copyright (C) 2006-2014 Oracle Corporation
9 *
10 * This file is part of VirtualBox Open Source Edition (OSE), as
11 * available from http://www.virtualbox.org. This file is free software;
12 * you can redistribute it and/or modify it under the terms of the GNU
13 * General Public License (GPL) as published by the Free Software
14 * Foundation, in version 2 as it comes in the "COPYING" file of the
15 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
16 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
17 */
18
19#include <VBox/com/com.h>
20#include <VBox/com/string.h>
21#include <VBox/com/Guid.h>
22#include <VBox/com/ErrorInfo.h>
23#include <VBox/com/errorprint.h>
24
25#include <VBox/com/VirtualBox.h>
26
27using namespace com;
28
29#include <VBox/log.h>
30#include <iprt/initterm.h>
31#include <iprt/stream.h>
32
33
34////////////////////////////////////////////////////////////////////////////////
35
36/**
37 * Entry point.
38 */
39int main(int argc, char **argv)
40{
41 // initialize VBox Runtime
42 RTR3InitExe(argc, &argv, 0);
43
44 // the below cannot be Bstr because on Linux Bstr doesn't work
45 // until XPCOM (nsMemory) is initialized
46 const char *name = NULL;
47 const char *operation = NULL;
48
49 // parse the command line
50 if (argc > 1)
51 name = argv [1];
52 if (argc > 2)
53 operation = argv [2];
54
55 if (!name || !operation)
56 {
57 RTPrintf("\nUsage:\n\n"
58 "%s <machine_name> [on|off|pause|resume]\n\n",
59 argv [0]);
60 return 0;
61 }
62
63 RTPrintf("\n");
64 RTPrintf("tstHeadless STARTED.\n");
65
66 RTPrintf("VM name : {%s}\n"
67 "Operation : %s\n\n",
68 name, operation);
69
70 HRESULT rc;
71
72 rc = com::Initialize();
73 if (FAILED(rc))
74 {
75 RTPrintf("ERROR: failed to initialize COM!\n");
76 return rc;
77 }
78
79 do
80 {
81 ComPtr <IVirtualBox> virtualBox;
82 ComPtr <ISession> session;
83
84 RTPrintf("Creating VirtualBox object...\n");
85 rc = virtualBox.createLocalObject(CLSID_VirtualBox);
86 if (FAILED(rc))
87 RTPrintf("ERROR: failed to create the VirtualBox object!\n");
88 else
89 {
90 rc = session.createInprocObject(CLSID_Session);
91 if (FAILED(rc))
92 RTPrintf("ERROR: failed to create a session object!\n");
93 }
94
95 if (FAILED(rc))
96 {
97 com::ErrorInfo info;
98 if (!info.isFullAvailable() && !info.isBasicAvailable())
99 {
100 com::GluePrintRCMessage(rc);
101 RTPrintf("Most likely, the VirtualBox COM server is not running or failed to start.\n");
102 }
103 else
104 com::GluePrintErrorInfo(info);
105 break;
106 }
107
108 ComPtr <IMachine> m;
109
110 // find ID by name
111 CHECK_ERROR_BREAK(virtualBox, FindMachine(Bstr(name).raw(),
112 m.asOutParam()));
113
114 if (!strcmp(operation, "on"))
115 {
116 ComPtr <IProgress> progress;
117 RTPrintf("Opening a new (remote) session...\n");
118 CHECK_ERROR_BREAK(m,
119 LaunchVMProcess(session, Bstr("vrdp").raw(),
120 NULL, progress.asOutParam()));
121
122 RTPrintf("Waiting for the remote session to open...\n");
123 CHECK_ERROR_BREAK(progress, WaitForCompletion(-1));
124
125 BOOL completed;
126 CHECK_ERROR_BREAK(progress, COMGETTER(Completed)(&completed));
127 ASSERT(completed);
128
129 LONG resultCode;
130 CHECK_ERROR_BREAK(progress, COMGETTER(ResultCode)(&resultCode));
131 if (FAILED(resultCode))
132 {
133 ProgressErrorInfo info(progress);
134 com::GluePrintErrorInfo(info);
135 }
136 else
137 {
138 RTPrintf("Remote session has been successfully opened.\n");
139 }
140 }
141 else
142 {
143 RTPrintf("Opening an existing session...\n");
144 CHECK_ERROR_BREAK(m, LockMachine(session, LockType_Shared));
145
146 ComPtr <IConsole> console;
147 CHECK_ERROR_BREAK(session, COMGETTER(Console)(console.asOutParam()));
148
149 if (!strcmp(operation, "off"))
150 {
151 ComPtr <IProgress> progress;
152 RTPrintf("Powering the VM off...\n");
153 CHECK_ERROR_BREAK(console, PowerDown(progress.asOutParam()));
154
155 RTPrintf("Waiting for the VM to power down...\n");
156 CHECK_ERROR_BREAK(progress, WaitForCompletion(-1));
157
158 BOOL completed;
159 CHECK_ERROR_BREAK(progress, COMGETTER(Completed)(&completed));
160 ASSERT(completed);
161
162 LONG resultCode;
163 CHECK_ERROR_BREAK(progress, COMGETTER(ResultCode)(&resultCode));
164 if (FAILED(resultCode))
165 {
166 ProgressErrorInfo info(progress);
167 com::GluePrintErrorInfo(info);
168 }
169 else
170 {
171 RTPrintf("VM is powered down.\n");
172 }
173 }
174 else
175 if (!strcmp(operation, "pause"))
176 {
177 RTPrintf("Pausing the VM...\n");
178 CHECK_ERROR_BREAK(console, Pause());
179 }
180 else
181 if (!strcmp(operation, "resume"))
182 {
183 RTPrintf("Resuming the VM...\n");
184 CHECK_ERROR_BREAK(console, Resume());
185 }
186 else
187 {
188 RTPrintf("Invalid operation!\n");
189 }
190 }
191
192 RTPrintf("Closing the session (may fail after power off)...\n");
193 CHECK_ERROR(session, UnlockMachine());
194 }
195 while (0);
196 RTPrintf("\n");
197
198 com::Shutdown();
199
200 RTPrintf("tstHeadless FINISHED.\n");
201
202 return rc;
203}
204
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