1 | /** @file
|
---|
2 | *
|
---|
3 | * VBox frontends: VBoxHeadless frontend:
|
---|
4 | * Testcases
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (C) 2006-2007 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/EventQueue.h>
|
---|
28 |
|
---|
29 | #include <VBox/com/VirtualBox.h>
|
---|
30 |
|
---|
31 | using namespace com;
|
---|
32 |
|
---|
33 | #include <iprt/runtime.h>
|
---|
34 | #include <iprt/stream.h>
|
---|
35 |
|
---|
36 |
|
---|
37 | ////////////////////////////////////////////////////////////////////////////////
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Entry point.
|
---|
41 | */
|
---|
42 | int main (int argc, char **argv)
|
---|
43 | {
|
---|
44 | // initialize VBox Runtime
|
---|
45 | RTR3Init();
|
---|
46 |
|
---|
47 | // the below cannot be Bstr because on Linux Bstr doesn't work
|
---|
48 | // until XPCOM (nsMemory) is initialized
|
---|
49 | const char *name = NULL;
|
---|
50 | const char *operation = NULL;
|
---|
51 |
|
---|
52 | // parse the command line
|
---|
53 | if (argc > 1)
|
---|
54 | name = argv [1];
|
---|
55 | if (argc > 2)
|
---|
56 | operation = argv [2];
|
---|
57 |
|
---|
58 | if (!name || !operation)
|
---|
59 | {
|
---|
60 | RTPrintf ("\nUsage:\n\n"
|
---|
61 | "%s <machine_name> [on|off|pause|resume]\n\n",
|
---|
62 | argv [0]);
|
---|
63 | return -1;
|
---|
64 | }
|
---|
65 |
|
---|
66 | RTPrintf ("\n");
|
---|
67 | RTPrintf ("tstHeadless STARTED.\n");
|
---|
68 |
|
---|
69 | RTPrintf ("VM name : {%s}\n"
|
---|
70 | "Operation : %s\n\n",
|
---|
71 | name, operation);
|
---|
72 |
|
---|
73 | HRESULT rc;
|
---|
74 |
|
---|
75 | CHECK_RC_RET (com::Initialize());
|
---|
76 |
|
---|
77 | do
|
---|
78 | {
|
---|
79 | ComPtr <IVirtualBox> virtualBox;
|
---|
80 | ComPtr <ISession> session;
|
---|
81 |
|
---|
82 | RTPrintf ("Creating VirtualBox object...\n");
|
---|
83 | CHECK_ERROR_NI_BREAK (virtualBox.createLocalObject (CLSID_VirtualBox));
|
---|
84 |
|
---|
85 | RTPrintf ("Creating Session object...\n");
|
---|
86 | CHECK_ERROR_NI_BREAK (session.createInprocObject (CLSID_Session));
|
---|
87 |
|
---|
88 | // create the event queue
|
---|
89 | // (here it is necessary only to process remaining XPCOM/IPC events
|
---|
90 | // after the session is closed)
|
---|
91 | EventQueue eventQ;
|
---|
92 |
|
---|
93 | // find ID by name
|
---|
94 | Guid id;
|
---|
95 | {
|
---|
96 | ComPtr <IMachine> m;
|
---|
97 | CHECK_ERROR_BREAK (virtualBox, FindMachine (Bstr (name), m.asOutParam()));
|
---|
98 | CHECK_ERROR_BREAK (m, COMGETTER(Id) (id.asOutParam()));
|
---|
99 | }
|
---|
100 |
|
---|
101 | if (!strcmp (operation, "on"))
|
---|
102 | {
|
---|
103 | ComPtr <IProgress> progress;
|
---|
104 | RTPrintf ("Opening a new (remote) session...\n");
|
---|
105 | CHECK_ERROR_BREAK (virtualBox,
|
---|
106 | OpenRemoteSession (session, id, Bstr ("vrdp"),
|
---|
107 | NULL, progress.asOutParam()));
|
---|
108 |
|
---|
109 | RTPrintf ("Waiting for the remote session to open...\n");
|
---|
110 | CHECK_ERROR_BREAK (progress, WaitForCompletion (-1));
|
---|
111 |
|
---|
112 | BOOL completed;
|
---|
113 | CHECK_ERROR_BREAK (progress, COMGETTER(Completed) (&completed));
|
---|
114 | ASSERT (completed);
|
---|
115 |
|
---|
116 | HRESULT resultCode;
|
---|
117 | CHECK_ERROR_BREAK (progress, COMGETTER(ResultCode) (&resultCode));
|
---|
118 | if (FAILED (resultCode))
|
---|
119 | {
|
---|
120 | ComPtr <IVirtualBoxErrorInfo> errorInfo;
|
---|
121 | CHECK_ERROR_BREAK (progress,
|
---|
122 | COMGETTER(ErrorInfo) (errorInfo.asOutParam()));
|
---|
123 | ErrorInfo info (errorInfo);
|
---|
124 | PRINT_ERROR_INFO (info);
|
---|
125 | }
|
---|
126 | else
|
---|
127 | {
|
---|
128 | RTPrintf ("Remote session has been successfully opened.\n");
|
---|
129 | }
|
---|
130 | }
|
---|
131 | else
|
---|
132 | {
|
---|
133 | RTPrintf ("Opening an existing session...\n");
|
---|
134 | CHECK_ERROR_BREAK (virtualBox, OpenExistingSession (session, id));
|
---|
135 |
|
---|
136 | ComPtr <IConsole> console;
|
---|
137 | CHECK_ERROR_BREAK (session, COMGETTER (Console) (console.asOutParam()));
|
---|
138 |
|
---|
139 | if (!strcmp (operation, "off"))
|
---|
140 | {
|
---|
141 | RTPrintf ("Powering the VM off...\n");
|
---|
142 | CHECK_ERROR_BREAK (console, PowerDown());
|
---|
143 | }
|
---|
144 | else
|
---|
145 | if (!strcmp (operation, "pause"))
|
---|
146 | {
|
---|
147 | RTPrintf ("Pausing the VM...\n");
|
---|
148 | CHECK_ERROR_BREAK (console, Pause());
|
---|
149 | }
|
---|
150 | else
|
---|
151 | if (!strcmp (operation, "resume"))
|
---|
152 | {
|
---|
153 | RTPrintf ("Resuming the VM...\n");
|
---|
154 | CHECK_ERROR_BREAK (console, Resume());
|
---|
155 | }
|
---|
156 | else
|
---|
157 | {
|
---|
158 | RTPrintf ("Invalid operation!\n");
|
---|
159 | }
|
---|
160 | }
|
---|
161 |
|
---|
162 | RTPrintf ("Closing the session (may fail after power off)...\n");
|
---|
163 | CHECK_ERROR (session, Close());
|
---|
164 | }
|
---|
165 | while (0);
|
---|
166 | RTPrintf ("\n");
|
---|
167 |
|
---|
168 | com::Shutdown();
|
---|
169 |
|
---|
170 | RTPrintf ("tstHeadless FINISHED.\n");
|
---|
171 |
|
---|
172 | return rc;
|
---|
173 | }
|
---|
174 |
|
---|