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