VirtualBox

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

Last change on this file since 28152 was 20928, checked in by vboxsync, 16 years ago

API/others: Renamed IConsole::discardSavedState to IConsole::forgetSavedState, added parameter. Deleted old IConsole::powerDown, renamed IConsole::powerDownAsync to IConsole::powerDown (as promised for 2.1). Implemented perl sample code for registering a hard disk. Cleaned up constant formatting in the API docs. Updated SDK changelog. Renamed com/errorprint2.h to com/errorprint.h, added a few assertion variants. Eliminated com/errorprint_legacy.h. Adjusted all files using the affected headers and APIs. Renamed tstHeadless2 to tstHeadless.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.7 KB
Line 
1/** @file
2 *
3 * VBox frontends: VBoxHeadless frontend:
4 * Testcases
5 */
6
7/*
8 * Copyright (C) 2006-2009 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
19 * Clara, CA 95054 USA or visit http://www.sun.com if you need
20 * additional information or have any questions.
21 */
22
23#include <VBox/com/com.h>
24#include <VBox/com/string.h>
25#include <VBox/com/Guid.h>
26#include <VBox/com/ErrorInfo.h>
27#include <VBox/com/errorprint.h>
28#include <VBox/com/EventQueue.h>
29
30#include <VBox/com/VirtualBox.h>
31
32using namespace com;
33
34#include <VBox/log.h>
35#include <iprt/initterm.h>
36#include <iprt/stream.h>
37
38
39////////////////////////////////////////////////////////////////////////////////
40
41/**
42 * Entry point.
43 */
44int main (int argc, char **argv)
45{
46 // initialize VBox Runtime
47 RTR3Init();
48
49 // the below cannot be Bstr because on Linux Bstr doesn't work
50 // until XPCOM (nsMemory) is initialized
51 const char *name = NULL;
52 const char *operation = NULL;
53
54 // parse the command line
55 if (argc > 1)
56 name = argv [1];
57 if (argc > 2)
58 operation = argv [2];
59
60 if (!name || !operation)
61 {
62 RTPrintf ("\nUsage:\n\n"
63 "%s <machine_name> [on|off|pause|resume]\n\n",
64 argv [0]);
65 return -1;
66 }
67
68 RTPrintf ("\n");
69 RTPrintf ("tstHeadless STARTED.\n");
70
71 RTPrintf ("VM name : {%s}\n"
72 "Operation : %s\n\n",
73 name, operation);
74
75 HRESULT rc;
76
77 rc = com::Initialize();
78 if (FAILED(rc))
79 {
80 RTPrintf("ERROR: failed to initialize COM!\n");
81 return rc;
82 }
83
84 do
85 {
86 ComPtr <IVirtualBox> virtualBox;
87 ComPtr <ISession> session;
88
89 RTPrintf ("Creating VirtualBox object...\n");
90 rc = virtualBox.createLocalObject (CLSID_VirtualBox);
91 if (FAILED(rc))
92 RTPrintf("ERROR: failed to create the VirtualBox object!\n");
93 else
94 {
95 rc = session.createInprocObject(CLSID_Session);
96 if (FAILED(rc))
97 RTPrintf("ERROR: failed to create a session object!\n");
98 }
99
100 if (FAILED (rc))
101 {
102 com::ErrorInfo info;
103 if (!info.isFullAvailable() && !info.isBasicAvailable())
104 {
105 com::GluePrintRCMessage(rc);
106 RTPrintf("Most likely, the VirtualBox COM server is not running or failed to start.\n");
107 }
108 else
109 com::GluePrintErrorInfo(info);
110 break;
111 }
112
113 // create the event queue
114 // (here it is necessary only to process remaining XPCOM/IPC events
115 // after the session is closed)
116 EventQueue eventQ;
117
118 // find ID by name
119 Bstr id;
120 {
121 ComPtr <IMachine> m;
122 CHECK_ERROR_BREAK (virtualBox, FindMachine (Bstr (name), m.asOutParam()));
123 CHECK_ERROR_BREAK (m, COMGETTER(Id) (id.asOutParam()));
124 }
125
126 if (!strcmp (operation, "on"))
127 {
128 ComPtr <IProgress> progress;
129 RTPrintf ("Opening a new (remote) session...\n");
130 CHECK_ERROR_BREAK (virtualBox,
131 OpenRemoteSession (session, id, Bstr ("vrdp"),
132 NULL, progress.asOutParam()));
133
134 RTPrintf ("Waiting for the remote session to open...\n");
135 CHECK_ERROR_BREAK (progress, WaitForCompletion (-1));
136
137 BOOL completed;
138 CHECK_ERROR_BREAK (progress, COMGETTER(Completed) (&completed));
139 ASSERT (completed);
140
141 LONG resultCode;
142 CHECK_ERROR_BREAK (progress, COMGETTER(ResultCode) (&resultCode));
143 if (FAILED (resultCode))
144 {
145 ComPtr <IVirtualBoxErrorInfo> errorInfo;
146 CHECK_ERROR_BREAK (progress,
147 COMGETTER(ErrorInfo) (errorInfo.asOutParam()));
148 ErrorInfo info (errorInfo);
149 com::GluePrintErrorInfo(info);
150 }
151 else
152 {
153 RTPrintf ("Remote session has been successfully opened.\n");
154 }
155 }
156 else
157 {
158 RTPrintf ("Opening an existing session...\n");
159 CHECK_ERROR_BREAK (virtualBox, OpenExistingSession (session, id));
160
161 ComPtr <IConsole> console;
162 CHECK_ERROR_BREAK (session, COMGETTER (Console) (console.asOutParam()));
163
164 if (!strcmp (operation, "off"))
165 {
166 ComPtr <IProgress> progress;
167 RTPrintf ("Powering the VM off...\n");
168 CHECK_ERROR_BREAK (console, PowerDown(progress.asOutParam()));
169
170 RTPrintf ("Waiting for the VM to power down...\n");
171 CHECK_ERROR_BREAK (progress, WaitForCompletion (-1));
172
173 BOOL completed;
174 CHECK_ERROR_BREAK (progress, COMGETTER(Completed) (&completed));
175 ASSERT (completed);
176
177 LONG resultCode;
178 CHECK_ERROR_BREAK (progress, COMGETTER(ResultCode) (&resultCode));
179 if (FAILED (resultCode))
180 {
181 ComPtr <IVirtualBoxErrorInfo> errorInfo;
182 CHECK_ERROR_BREAK (progress,
183 COMGETTER(ErrorInfo) (errorInfo.asOutParam()));
184 ErrorInfo info (errorInfo);
185 com::GluePrintErrorInfo(info);
186 }
187 else
188 {
189 RTPrintf ("VM is powered down.\n");
190 }
191 }
192 else
193 if (!strcmp (operation, "pause"))
194 {
195 RTPrintf ("Pausing the VM...\n");
196 CHECK_ERROR_BREAK (console, Pause());
197 }
198 else
199 if (!strcmp (operation, "resume"))
200 {
201 RTPrintf ("Resuming the VM...\n");
202 CHECK_ERROR_BREAK (console, Resume());
203 }
204 else
205 {
206 RTPrintf ("Invalid operation!\n");
207 }
208 }
209
210 RTPrintf ("Closing the session (may fail after power off)...\n");
211 CHECK_ERROR (session, Close());
212 }
213 while (0);
214 RTPrintf ("\n");
215
216 com::Shutdown();
217
218 RTPrintf ("tstHeadless FINISHED.\n");
219
220 return rc;
221}
222
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