VirtualBox

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

Last change on this file since 29437 was 28800, checked in by vboxsync, 15 years ago

Automated rebranding to Oracle copyright/license strings via filemuncher

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