PageRenderTime 25ms CodeModel.GetById 9ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

/install/xbt/linux/Tracker/XBT Tracker.cpp

http://torrentpier2.googlecode.com/
C++ | 135 lines | 129 code | 6 blank | 0 comment | 25 complexity | 13f41349467ea5eabcadd60e63f49022 MD5 | raw file
  1#include "stdafx.h"
  2#include <windows/nt_service.h>
  3#include <iostream>
  4#include "config.h"
  5#include "server.h"
  6
  7std::string g_conf_file = "xbt_tracker.conf";
  8const char* g_service_name = "XBT Tracker";
  9
 10int main1()
 11{
 12	srand(static_cast<int>(time(NULL)));
 13	Cconfig config;
 14	if (config.load(g_conf_file))
 15#ifdef WIN32
 16	{
 17		char b[MAX_PATH];
 18		*b = 0;
 19		GetModuleFileName(NULL, b, MAX_PATH);
 20		if (*b)
 21			strrchr(b, '\\')[1] = 0;
 22		strcat(b, "xbt_tracker.conf");
 23		if (config.load(b))
 24			std::cerr << "Unable to read " << g_conf_file << std::endl;
 25		else
 26			g_conf_file = b;
 27	}
 28#else
 29		std::cerr << "Unable to read " << g_conf_file << std::endl;
 30#endif
 31	Cdatabase database;
 32	try
 33	{
 34		if (config.m_mysql_host != "-")
 35			database.open(config.m_mysql_host, config.m_mysql_user, config.m_mysql_password, config.m_mysql_database, true);
 36	}
 37	catch (Cdatabase::exception& e)
 38	{
 39		std::cerr << e.what() << std::endl;
 40		return 1;
 41	}
 42	database.set_query_log(config.m_query_log);
 43	return Cserver(database, config.m_mysql_table_prefix, config.m_mysql_host != "-", g_conf_file).run();
 44}
 45
 46#ifdef WIN32
 47static SERVICE_STATUS g_service_status;
 48static SERVICE_STATUS_HANDLE gh_service_status;
 49
 50void WINAPI nt_service_handler(DWORD op)
 51{
 52	switch (op)
 53	{
 54	case SERVICE_CONTROL_STOP:
 55		g_service_status.dwCurrentState = SERVICE_STOP_PENDING;
 56		SetServiceStatus(gh_service_status, &g_service_status);
 57		Cserver::term();
 58		break;
 59	}
 60	SetServiceStatus(gh_service_status, &g_service_status);
 61}
 62
 63void WINAPI nt_service_main(DWORD argc, LPTSTR* argv)
 64{
 65	g_service_status.dwCheckPoint = 0;
 66	g_service_status.dwControlsAccepted = SERVICE_ACCEPT_STOP;
 67	g_service_status.dwCurrentState = SERVICE_START_PENDING;
 68	g_service_status.dwServiceSpecificExitCode = 0;
 69	g_service_status.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
 70	g_service_status.dwWaitHint = 0;
 71	g_service_status.dwWin32ExitCode = NO_ERROR;
 72	if (!(gh_service_status = RegisterServiceCtrlHandler(g_service_name, nt_service_handler)))
 73		return;
 74	SetServiceStatus(gh_service_status, &g_service_status);
 75	g_service_status.dwCurrentState = SERVICE_RUNNING;
 76	SetServiceStatus(gh_service_status, &g_service_status);
 77	main1();
 78	g_service_status.dwCurrentState = SERVICE_STOPPED;
 79	SetServiceStatus(gh_service_status, &g_service_status);
 80}
 81#endif
 82
 83int main(int argc, char* argv[])
 84{
 85#ifdef WIN32
 86	if (argc >= 2)
 87	{
 88		if (!strcmp(argv[1], "--install"))
 89		{
 90			if (nt_service_install(g_service_name))
 91			{
 92				std::cerr << "Failed to install service " << g_service_name << "." << std::endl;
 93				return 1;
 94			}
 95			std::cout << "Service " << g_service_name << " has been installed." << std::endl;
 96			return 0;
 97		}
 98		else if (!strcmp(argv[1], "--uninstall"))
 99		{
100			if (nt_service_uninstall(g_service_name))
101			{
102				std::cerr << "Failed to uninstall service " << g_service_name << "." << std::endl;
103				return 1;
104			}
105			std::cout << "Service " << g_service_name << " has been uninstalled." << std::endl;
106			return 0;
107		}
108		else if (!strcmp(argv[1], "--conf_file") && argc >= 3)
109			g_conf_file = argv[2];
110		else
111			return 1;
112	}
113#ifdef NDEBUG
114	SERVICE_TABLE_ENTRY st[] =
115	{
116		{ "", nt_service_main },
117		{ NULL, NULL }
118	};
119	if (StartServiceCtrlDispatcher(st))
120		return 0;
121	if (GetLastError() != ERROR_CALL_NOT_IMPLEMENTED
122		&& GetLastError() != ERROR_FAILED_SERVICE_CONTROLLER_CONNECT)
123		return 1;
124#endif
125#else
126	if (argc >= 2)
127	{
128		if (!strcmp(argv[1], "--conf_file") && argc >= 3)
129			g_conf_file = argv[2];
130		else
131			return 1;
132	}
133#endif
134	return main1();
135}