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