make DaemonApp a win32-only target

This commit is contained in:
walker0643 2018-03-11 13:14:45 -04:00
parent b9a59c014f
commit 51abdc281d
3 changed files with 6 additions and 52 deletions

View File

@ -16,7 +16,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "barrier/DaemonApp.h" #include "barrier/win32/DaemonApp.h"
#include <iostream> #include <iostream>

View File

@ -16,10 +16,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
// TODO: split this class into windows and unix to get rid #include "barrier/win32/DaemonApp.h"
// of all the #ifdefs!
#include "barrier/DaemonApp.h"
#include "barrier/App.h" #include "barrier/App.h"
#include "barrier/ArgParser.h" #include "barrier/ArgParser.h"
@ -37,8 +34,6 @@
#include "base/log_outputters.h" #include "base/log_outputters.h"
#include "base/Log.h" #include "base/Log.h"
#if SYSAPI_WIN32
#include "arch/win32/ArchMiscWindows.h" #include "arch/win32/ArchMiscWindows.h"
#include "arch/win32/XArchWindows.h" #include "arch/win32/XArchWindows.h"
#include "barrier/Screen.h" #include "barrier/Screen.h"
@ -50,8 +45,6 @@
#define WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN
#include <Windows.h> #include <Windows.h>
#endif
#include <string> #include <string>
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
@ -68,25 +61,15 @@ mainLoopStatic()
} }
int int
unixMainLoopStatic(int, const char**) mainLoopStatic(int, const char**)
{
return mainLoopStatic();
}
#if SYSAPI_WIN32
int
winMainLoopStatic(int, const char**)
{ {
return ArchMiscWindows::runDaemon(mainLoopStatic); return ArchMiscWindows::runDaemon(mainLoopStatic);
} }
#endif
DaemonApp::DaemonApp() : DaemonApp::DaemonApp() :
m_ipcServer(nullptr), m_ipcServer(nullptr),
m_ipcLogOutputter(nullptr), m_ipcLogOutputter(nullptr),
#if SYSAPI_WIN32
m_watchdog(nullptr), m_watchdog(nullptr),
#endif
m_events(nullptr), m_events(nullptr),
m_fileLogOutputter(nullptr) m_fileLogOutputter(nullptr)
{ {
@ -100,10 +83,8 @@ DaemonApp::~DaemonApp()
int int
DaemonApp::run(int argc, char** argv) DaemonApp::run(int argc, char** argv)
{ {
#if SYSAPI_WIN32
// win32 instance needed for threading, etc. // win32 instance needed for threading, etc.
ArchMiscWindows::setInstanceWin32(GetModuleHandle(NULL)); ArchMiscWindows::setInstanceWin32(GetModuleHandle(NULL));
#endif
Arch arch; Arch arch;
arch.init(); arch.init();
@ -115,10 +96,8 @@ DaemonApp::run(int argc, char** argv)
bool uninstall = false; bool uninstall = false;
try try
{ {
#if SYSAPI_WIN32
// sends debug messages to visual studio console window. // sends debug messages to visual studio console window.
log.insert(new MSWindowsDebugOutputter()); log.insert(new MSWindowsDebugOutputter());
#endif
// default log level to system setting. // default log level to system setting.
string logLevel = arch.setting("LogLevel"); string logLevel = arch.setting("LogLevel");
@ -133,7 +112,6 @@ DaemonApp::run(int argc, char** argv)
if (arg == "/f" || arg == "-f") { if (arg == "/f" || arg == "-f") {
foreground = true; foreground = true;
} }
#if SYSAPI_WIN32
else if (arg == "/install") { else if (arg == "/install") {
uninstall = true; uninstall = true;
arch.installDaemon(); arch.installDaemon();
@ -143,7 +121,6 @@ DaemonApp::run(int argc, char** argv)
arch.uninstallDaemon(); arch.uninstallDaemon();
return kExitSuccess; return kExitSuccess;
} }
#endif
else { else {
stringstream ss; stringstream ss;
ss << "Unrecognized argument: " << arg; ss << "Unrecognized argument: " << arg;
@ -158,11 +135,7 @@ DaemonApp::run(int argc, char** argv)
mainLoop(false); mainLoop(false);
} }
else { else {
#if SYSAPI_WIN32 arch.daemonize("Barrier", mainLoopStatic);
arch.daemonize("Barrier", winMainLoopStatic);
#elif SYSAPI_UNIX
arch.daemonize("Barrier", unixMainLoopStatic);
#endif
} }
return kExitSuccess; return kExitSuccess;
@ -214,10 +187,8 @@ DaemonApp::mainLoop(bool daemonized)
m_ipcLogOutputter = new IpcLogOutputter(*m_ipcServer, kIpcClientGui, true); m_ipcLogOutputter = new IpcLogOutputter(*m_ipcServer, kIpcClientGui, true);
CLOG->insert(m_ipcLogOutputter); CLOG->insert(m_ipcLogOutputter);
#if SYSAPI_WIN32
m_watchdog = new MSWindowsWatchdog(daemonized, false, *m_ipcServer, *m_ipcLogOutputter); m_watchdog = new MSWindowsWatchdog(daemonized, false, *m_ipcServer, *m_ipcLogOutputter);
m_watchdog->setFileLogOutputter(m_fileLogOutputter); m_watchdog->setFileLogOutputter(m_fileLogOutputter);
#endif
m_events->adoptHandler( m_events->adoptHandler(
m_events->forIpcServer().messageReceived(), m_ipcServer, m_events->forIpcServer().messageReceived(), m_ipcServer,
@ -225,8 +196,6 @@ DaemonApp::mainLoop(bool daemonized)
m_ipcServer->listen(); m_ipcServer->listen();
#if SYSAPI_WIN32
// install the platform event queue to handle service stop events. // install the platform event queue to handle service stop events.
m_events->adoptBuffer(new MSWindowsEventQueueBuffer(m_events)); m_events->adoptBuffer(new MSWindowsEventQueueBuffer(m_events));
@ -238,13 +207,11 @@ DaemonApp::mainLoop(bool daemonized)
} }
m_watchdog->startAsync(); m_watchdog->startAsync();
#endif
m_events->loop(); m_events->loop();
#if SYSAPI_WIN32
m_watchdog->stop(); m_watchdog->stop();
delete m_watchdog; delete m_watchdog;
#endif
m_events->removeHandler( m_events->removeHandler(
m_events->forIpcServer().messageReceived(), m_ipcServer); m_events->forIpcServer().messageReceived(), m_ipcServer);
@ -266,11 +233,7 @@ DaemonApp::mainLoop(bool daemonized)
void void
DaemonApp::foregroundError(const char* message) DaemonApp::foregroundError(const char* message)
{ {
#if SYSAPI_WIN32
MessageBox(NULL, message, "Barrier Service", MB_OK | MB_ICONERROR); MessageBox(NULL, message, "Barrier Service", MB_OK | MB_ICONERROR);
#elif SYSAPI_UNIX
cerr << message << endl;
#endif
} }
std::string std::string
@ -338,7 +301,6 @@ DaemonApp::handleIpcMessage(const Event& e, void*)
} }
} }
#if SYSAPI_WIN32
// eg. no log-to-file while running in foreground // eg. no log-to-file while running in foreground
if (m_fileLogOutputter != nullptr) { if (m_fileLogOutputter != nullptr) {
String logFilename; String logFilename;
@ -354,7 +316,6 @@ DaemonApp::handleIpcMessage(const Event& e, void*)
} }
m_fileLogOutputter->setLogFilename(logFilename.c_str()); m_fileLogOutputter->setLogFilename(logFilename.c_str());
} }
#endif
} }
else { else {
LOG((CLOG_DEBUG "empty command, elevate=%d", cm->elevate())); LOG((CLOG_DEBUG "empty command, elevate=%d", cm->elevate()));
@ -372,12 +333,11 @@ DaemonApp::handleIpcMessage(const Event& e, void*)
LOG((CLOG_ERR "failed to save settings, %s", e.what())); LOG((CLOG_ERR "failed to save settings, %s", e.what()));
} }
#if SYSAPI_WIN32
// tell the relauncher about the new command. this causes the // tell the relauncher about the new command. this causes the
// relauncher to stop the existing command and start the new // relauncher to stop the existing command and start the new
// command. // command.
m_watchdog->setCommand(command, cm->elevate()); m_watchdog->setCommand(command, cm->elevate());
#endif
break; break;
} }
@ -392,10 +352,8 @@ DaemonApp::handleIpcMessage(const Event& e, void*)
LOG((CLOG_DEBUG "ipc hello, type=%s", type.c_str())); LOG((CLOG_DEBUG "ipc hello, type=%s", type.c_str()));
#if SYSAPI_WIN32
const char * serverstatus = m_watchdog->isProcessActive() ? "active" : "not active"; const char * serverstatus = m_watchdog->isProcessActive() ? "active" : "not active";
LOG((CLOG_INFO "server status: %s", serverstatus)); LOG((CLOG_INFO "server status: %s", serverstatus));
#endif
m_ipcLogOutputter->notifyBuffer(); m_ipcLogOutputter->notifyBuffer();
break; break;

View File

@ -27,9 +27,7 @@ class Event;
class IpcLogOutputter; class IpcLogOutputter;
class FileLogOutputter; class FileLogOutputter;
#if SYSAPI_WIN32
class MSWindowsWatchdog; class MSWindowsWatchdog;
#endif
class DaemonApp { class DaemonApp {
@ -48,9 +46,7 @@ private:
public: public:
static DaemonApp* s_instance; static DaemonApp* s_instance;
#if SYSAPI_WIN32
MSWindowsWatchdog* m_watchdog; MSWindowsWatchdog* m_watchdog;
#endif
private: private:
IpcServer* m_ipcServer; IpcServer* m_ipcServer;