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