Skip to content

Commit a309a49

Browse files
committed
Merge pull request #380 from OSVR/osvrServerInit
osvrServerAutoStart and osvrServerRelease methods
2 parents 9c9092f + d19ae89 commit a309a49

File tree

7 files changed

+294
-0
lines changed

7 files changed

+294
-0
lines changed

inc/osvr/Client/LocateServer.h

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/** @file
2+
@brief Internal API to locate the current OSVR Server on the file system.
3+
4+
@date 2016
5+
6+
@author
7+
Sensics, Inc.
8+
<http://sensics.com/osvr>
9+
*/
10+
11+
// Copyright 2016 Sensics, Inc.
12+
//
13+
// Licensed under the Apache License, Version 2.0 (the "License");
14+
// you may not use this file except in compliance with the License.
15+
// You may obtain a copy of the License at
16+
//
17+
// http://www.apache.org/licenses/LICENSE-2.0
18+
//
19+
// Unless required by applicable law or agreed to in writing, software
20+
// distributed under the License is distributed on an "AS IS" BASIS,
21+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22+
// See the License for the specific language governing permissions and
23+
// limitations under the License.
24+
25+
#ifndef INCLUDED_LocateServer_h_GUID_7D10F12E_2A83_48BB_7581_ED7A7F5188AC
26+
#define INCLUDED_LocateServer_h_GUID_7D10F12E_2A83_48BB_7581_ED7A7F5188AC
27+
28+
29+
// Internal Includes
30+
#include <osvr/Common/GetEnvironmentVariable.h>
31+
#include <osvr/Util/PlatformConfig.h>
32+
33+
// Library/third-party includes
34+
// - none
35+
36+
// Standard includes
37+
#include <string>
38+
39+
namespace osvr {
40+
namespace client {
41+
42+
/** @brief INTERNAL ONLY - get the current server directory, if available. */
43+
inline boost::optional<std::string> getServerBinaryDirectoryPath() {
44+
auto server = osvr::common::getEnvironmentVariable("OSVR_SERVER_ROOT");
45+
return server;
46+
}
47+
48+
/** @brief INTERNAL ONLY - get the path to the server executable, if available. */
49+
inline boost::optional<std::string> getServerBinaryPath() {
50+
auto binPath = getServerBinaryDirectoryPath();
51+
if (binPath) {
52+
#if defined(OSVR_WINDOWS)
53+
return *binPath + "\\osvr_server.exe";
54+
#else
55+
return *binPath + "/osvr_server";
56+
#endif
57+
}
58+
return boost::none;
59+
}
60+
61+
/** @brief INTERNAL ONLY - get the path to the server launcher executable, if available. */
62+
inline boost::optional<std::string> getServerLauncherBinaryPath() {
63+
return getServerBinaryPath();
64+
}
65+
}
66+
}
67+
68+
#endif // INCLUDED_LocateServer_h_GUID_7D10F12E_2A83_48BB_7581_ED7A7F5188AC
69+
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/** @file
2+
@brief API to attempt OSVR server auto-start.
3+
4+
Must be c-safe!
5+
6+
@date 2016
7+
8+
@author
9+
Sensics, Inc.
10+
<http://sensics.com/osvr>
11+
*/
12+
13+
/*
14+
// Copyright 2016 Sensics, Inc.
15+
//
16+
// Licensed under the Apache License, Version 2.0 (the "License");
17+
// you may not use this file except in compliance with the License.
18+
// You may obtain a copy of the License at
19+
//
20+
// http://www.apache.org/licenses/LICENSE-2.0
21+
//
22+
// Unless required by applicable law or agreed to in writing, software
23+
// distributed under the License is distributed on an "AS IS" BASIS,
24+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25+
// See the License for the specific language governing permissions and
26+
// limitations under the License.
27+
*/
28+
29+
#ifndef INCLUDED_ServerAutoStartC_h_GUID_126F2ADB_CC3E_4F99_86FA_0A58DC9490F6
30+
#define INCLUDED_ServerAutoStartC_h_GUID_126F2ADB_CC3E_4F99_86FA_0A58DC9490F6
31+
32+
/* Internal Includes */
33+
#include <osvr/ClientKit/Export.h>
34+
#include <osvr/Util/APIBaseC.h>
35+
36+
/* Library/third-party includes */
37+
/* none */
38+
39+
/* Standard includes */
40+
/* none */
41+
42+
OSVR_EXTERN_C_BEGIN
43+
44+
/** @addtogroup ClientKit
45+
@{
46+
*/
47+
48+
/** @brief Ensures the OSVR server process/thread is running. Call once per process. The server may
49+
or may not actually start successfully as a result of this call. Continue to check client
50+
context status to determine if the client context has connected successfully.
51+
*/
52+
OSVR_CLIENTKIT_EXPORT void osvrClientAttemptServerAutoStart();
53+
54+
/** @brief Ensures any OSVR server-related resources in the client are released. Call once per process.
55+
*/
56+
OSVR_CLIENTKIT_EXPORT void osvrClientReleaseAutoStartedServer();
57+
58+
/** @} */
59+
OSVR_EXTERN_C_END
60+
61+
#endif

inc/osvr/Util/ProcessUtils.h

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/** @file
2+
@brief Platform agnostic process utilities.
3+
4+
@date 2016
5+
6+
@author
7+
Sensics, Inc.
8+
<http://sensics.com/osvr>
9+
*/
10+
11+
// Copyright 2016 Sensics, Inc.
12+
//
13+
// Licensed under the Apache License, Version 2.0 (the "License");
14+
// you may not use this file except in compliance with the License.
15+
// You may obtain a copy of the License at
16+
//
17+
// http://www.apache.org/licenses/LICENSE-2.0
18+
//
19+
// Unless required by applicable law or agreed to in writing, software
20+
// distributed under the License is distributed on an "AS IS" BASIS,
21+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22+
// See the License for the specific language governing permissions and
23+
// limitations under the License.
24+
25+
#ifndef INCLUDED_ProcessUtils_h_GUID_6C3F7688_AF0D_4094_D629_AF8DBFDF2044
26+
#define INCLUDED_ProcessUtils_h_GUID_6C3F7688_AF0D_4094_D629_AF8DBFDF2044
27+
28+
29+
// Internal Includes
30+
#include <osvr/Util/APIBaseC.h>
31+
#include <osvr/Util/ReturnCodesC.h>
32+
#include <osvr/Util/PlatformConfig.h>
33+
#include <osvr/Util/Verbosity.h>
34+
35+
// Library/third-party includes
36+
#if !defined(OSVR_ANDROID)
37+
#if defined(OSVR_WINDOWS)
38+
#include <windows.h>
39+
#else
40+
#include <unistd.h>
41+
#endif
42+
#endif
43+
44+
// Standard includes
45+
// - none
46+
47+
/** @brief INTERNAL ONLY - start a process in a platform agnostic way */
48+
OSVR_INLINE OSVR_ReturnCode osvrStartProcess(const char* executablePath, const char* workingDirectory)
49+
{
50+
#if defined(OSVR_ANDROID)
51+
// @todo: can we just use the *nix implementation for android?
52+
OSVR_DEV_VERBOSE("osvrStartProces not yet implemented for Android");
53+
return OSVR_RETURN_FAILURE;
54+
#elif defined(OSVR_WINDOWS)
55+
STARTUPINFO startupInfo = { 0 };
56+
PROCESS_INFORMATION processInfo = { 0 };
57+
58+
startupInfo.dwFlags |= STARTF_USESHOWWINDOW;
59+
startupInfo.wShowWindow = SW_SHOW;
60+
if (!CreateProcess(executablePath, nullptr, nullptr, nullptr, FALSE, CREATE_NEW_CONSOLE, nullptr,
61+
workingDirectory, &startupInfo, &processInfo)) {
62+
OSVR_DEV_VERBOSE("Could not start process.");
63+
return OSVR_RETURN_FAILURE;
64+
}
65+
#else
66+
OSVR_DEV_VERBOSE("osvrStartProcess not yet implemented for non-Windows platforms");
67+
return OSVR_RETURN_FAILURE;
68+
// UNTESTED (or even compiled)
69+
//pid_t pid = fork();
70+
//if (pid == -1) {
71+
// OSVR_DEV_VERBOSE("Could not fork the process.");
72+
// return OSVR_RETURN_FAILURE;
73+
//} else if (pid == 0) {
74+
// // @todo set the current working directory to workingDirectory here
75+
// execl(executablePath, NULL);
76+
// OSVR_DEV_VERBOSE("Could not execute the process.");
77+
// return OSVR_RETURN_FAILURE;
78+
//} else {
79+
// // @todo: is this too verbose?
80+
// //OSVR_DEV_VERBOSE("Started OSVR server process successfully.");
81+
// return OSVR_RETURN_SUCCESS;
82+
//}
83+
#endif
84+
return OSVR_RETURN_SUCCESS;
85+
}
86+
87+
#endif // INCLUDED_ProcessUtils_h_GUID_6C3F7688_AF0D_4094_D629_AF8DBFDF2044
88+

src/osvr/Client/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ set(API
77
"${HEADER_LOCATION}/DisplayInput.h"
88
"${HEADER_LOCATION}/HandlerContainer.h"
99
"${HEADER_LOCATION}/InternalInterfaceOwner.h"
10+
"${HEADER_LOCATION}/LocateServer.h"
1011
"${HEADER_LOCATION}/InterfaceTree.h"
1112
"${HEADER_LOCATION}/RemoteHandler.h"
1213
"${HEADER_LOCATION}/RemoteHandlerFactory.h"

src/osvr/ClientKit/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ set(API
1919
"${HEADER_LOCATION}/InterfaceStateC.h"
2020
"${HEADER_LOCATION}/Parameters.h"
2121
"${HEADER_LOCATION}/ParametersC.h"
22+
"${HEADER_LOCATION}/ServerAutoStartC.h"
2223
"${HEADER_LOCATION}/SystemCallbackC.h"
2324
"${HEADER_LOCATION}/TransformsC.h")
2425

@@ -31,6 +32,7 @@ set(SOURCE
3132
InterfaceCallbackC.cpp
3233
InterfaceStateC.cpp
3334
ParametersC.cpp
35+
ServerAutoStartC.cpp
3436
SystemCallbackC.cpp
3537
TransformsC.cpp)
3638

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/** @file
2+
@brief Implementation
3+
4+
@date 2016
5+
6+
@author
7+
Sensics, Inc.
8+
<http://sensics.com/osvr>
9+
*/
10+
11+
// Copyright 2016 Sensics, Inc.
12+
//
13+
// Licensed under the Apache License, Version 2.0 (the "License");
14+
// you may not use this file except in compliance with the License.
15+
// You may obtain a copy of the License at
16+
//
17+
// http://www.apache.org/licenses/LICENSE-2.0
18+
//
19+
// Unless required by applicable law or agreed to in writing, software
20+
// distributed under the License is distributed on an "AS IS" BASIS,
21+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22+
// See the License for the specific language governing permissions and
23+
// limitations under the License.
24+
25+
// Internal Includes
26+
#include <osvr/ClientKit/ServerAutoStartC.h>
27+
#include <osvr/Util/ProcessUtils.h>
28+
#include <osvr/Util/PlatformConfig.h>
29+
#include <osvr/Client/LocateServer.h>
30+
#include <osvr/Common/GetEnvironmentVariable.h>
31+
32+
// Library/third-party includes
33+
// - none
34+
35+
// Standard includes
36+
// - none
37+
38+
void osvrClientAttemptServerAutoStart()
39+
{
40+
// @todo start the server.
41+
#if defined(OSVR_ANDROID)
42+
// @todo implement auto-start for android
43+
#else
44+
auto server = osvr::client::getServerBinaryDirectoryPath();
45+
if (server) {
46+
OSVR_DEV_VERBOSE("Attempting to auto-start OSVR server from path " << *server);
47+
auto exePath = osvr::client::getServerLauncherBinaryPath();
48+
if (!exePath) {
49+
OSVR_DEV_VERBOSE("No server launcher binary available.");
50+
return;
51+
}
52+
53+
if (osvrStartProcess(exePath->c_str(), server->c_str()) == OSVR_RETURN_FAILURE) {
54+
OSVR_DEV_VERBOSE("Could not auto-start server process.");
55+
return;
56+
}
57+
} else {
58+
OSVR_DEV_VERBOSE("The current server location is currently unknown. Assuming server is already running.");
59+
}
60+
#endif
61+
return;
62+
}
63+
64+
void osvrClientReleaseAutoStartedServer()
65+
{
66+
#if defined(OSVR_ANDROID)
67+
// @todo cleanup server thread
68+
#else
69+
// no-op. Leave the server running.
70+
#endif
71+
return;
72+
}

src/osvr/Util/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ set(API
8787
"${HEADER_LOCATION}/PointerWrapper.h"
8888
"${HEADER_LOCATION}/PortFlags.h"
8989
"${HEADER_LOCATION}/Pose3C.h"
90+
"${HEADER_LOCATION}/ProcessUtils.h"
9091
"${HEADER_LOCATION}/ProgramOptionsToggleFlags.h"
9192
"${HEADER_LOCATION}/ProjectionMatrix.h"
9293
"${HEADER_LOCATION}/ProjectionMatrixFromFOV.h"

0 commit comments

Comments
 (0)