Changeset - ae953ed2e030
[Not reviewed]
Merge
0 1 0
Jan Kaluza - 13 years ago 2012-11-21 11:28:47
hanzz.k@gmail.com
Merge pull request #16 from vitalyster/win_service

Multiple Windows services support
1 file changed with 31 insertions and 22 deletions:
0 comments (0 inline, 0 general)
spectrum/src/main.cpp
Show inline comments
 
@@ -263,13 +263,19 @@ int main(int argc, char **argv)
 
	Config config(argc, argv);
 
	config_ = &config;
 
	boost::program_options::variables_map vm;
 
	bool no_daemon = false;
 
	std::string config_file;
 
	std::string jid;
 

	
 
#ifdef WIN32
 
	std::string install_service_name, uninstall_service_name, run_service_name;
 
	// determine the name of the currently executing file
 
	char szFilePath[MAX_PATH];
 
	GetModuleFileNameA(NULL, szFilePath, sizeof(szFilePath));
 
	std::string exe_file(szFilePath);					
 
#endif	
 
	setlocale(LC_ALL, "");
 
#ifndef WIN32
 
#ifndef __FreeBSD__
 
	mallopt(M_CHECK_ACTION, 2);
 
	mallopt(M_PERTURB, 0xb);
 
#endif
 
@@ -294,14 +300,15 @@ int main(int argc, char **argv)
 
		("jid,j", boost::program_options::value<std::string>(&jid)->default_value(""), "Specify JID of transport manually")
 
		("config", boost::program_options::value<std::string>(&config_file)->default_value(""), "Config file")
 
		("version,v", "Shows Spectrum version")
 
		;
 
#ifdef WIN32
 
 	desc.add_options()
 
 		("install-service,i", "Install spectrum as Windows service")
 
 		("uninstall-service,u", "Uninstall Windows service");
 
		("install-service,i", boost::program_options::value<std::string>(&install_service_name)->default_value(""), "Install spectrum as Windows service")
 
 		("uninstall-service,u", boost::program_options::value<std::string>(&uninstall_service_name)->default_value(""), "Uninstall Windows service")
 
		("run-as-service,r", boost::program_options::value<std::string>(&run_service_name)->default_value(""), "stub for Windows Service Manager");
 
#endif
 
	try
 
	{
 
		boost::program_options::positional_options_description p;
 
		p.add("config", -1);
 
		boost::program_options::store(boost::program_options::command_line_parser(argc, argv).
 
@@ -324,47 +331,44 @@ int main(int argc, char **argv)
 
			return 1;
 
		}
 

	
 
		if(vm.count("no-daemonize")) {
 
			no_daemon = true;
 
		}
 
#ifdef WIN32
 
		if (vm.count("install-service")) {
 
			ServiceWrapper ntservice("Spectrum2");
 
#ifdef WIN32	
 
		if (!install_service_name.empty()) {				
 
			// build command line for Service Manager
 
			std::string service_path = exe_file + std::string(" --config ") + vm["config"].as<std::string>() 
 
						+ std::string(" --run-as-service ") + install_service_name;													
 
		
 
			ServiceWrapper ntservice((char *)install_service_name.c_str());
 
			if (!ntservice.IsInstalled()) {
 
					// determine the name of the currently executing file
 
				char szFilePath[MAX_PATH];
 
				GetModuleFileNameA(NULL, szFilePath, sizeof(szFilePath));
 
				std::string exe_file(szFilePath);
 
				std::string config_file = exe_file.replace(exe_file.end() - 4, exe_file.end(), ".cfg");
 
				std::string service_path = std::string(szFilePath) + std::string(" --config ") + config_file;
 

	
 
				if (ntservice.Install((char *)service_path.c_str())) {
 
					std::cout << "Successfully installed" << std::endl;
 
					std::cout << "Successfully installed " << install_service_name << std::endl;
 
					return 0;
 
				} else {
 
					std::cout << "Error installing service, are you an Administrator?" << std::endl;
 
					return 1;
 
				}                				
 
			} else {
 
				std::cout << "Already installed" << std::endl;
 
				std::cout << "Already installed " << install_service_name << std::endl;
 
				return 1;
 
			}
 
		}
 
		if (vm.count("uninstall-service")) {
 
			ServiceWrapper ntservice("Spectrum2");
 
		if (!uninstall_service_name.empty()) {
 
			ServiceWrapper ntservice((char *)uninstall_service_name.c_str());
 
			if (ntservice.IsInstalled()) {
 
				if (ntservice.UnInstall()) {
 
					std::cout << "Successfully removed" << std::endl;
 
					std::cout << "Successfully removed " << uninstall_service_name << std::endl;
 
					return 0;
 
				} else {
 
					std::cout << "Error removing service, are you an Administrator?" << std::endl;
 
					return 1;
 
				}                               				
 
			} else {
 
				std::cout << "Service not installed" << std::endl;
 
				std::cout << "Service not installed: " << uninstall_service_name << std::endl;
 
				return 1;
 
			}
 
		}		
 
#endif
 
	}
 
	catch (std::runtime_error& e)
 
@@ -438,15 +442,20 @@ int main(int argc, char **argv)
 
		// daemonize
 
		daemonize(CONFIG_STRING(&config, "service.working_dir").c_str(), CONFIG_STRING(&config, "service.pidfile").c_str());
 
// 		removeOldIcons(CONFIG_STRING(&config, "service.working_dir") + "/icons");
 
    }
 
#endif
 
#ifdef WIN32
 
	ServiceWrapper ntservice("Spectrum2");
 
	if (ntservice.IsInstalled()) {
 
		ntservice.RunService();
 
	if (!run_service_name.empty()) {
 
		ServiceWrapper ntservice((char *)run_service_name.c_str());
 
		if (ntservice.IsInstalled()) {
 
			ntservice.RunService();
 
		} else {
 
			std::cerr << "Service not installed: " << run_service_name << std::endl;
 
			return 1;
 
		}
 
	} else {
 
		mainloop();
 
	}
 
#else
 
	mainloop();
 
#endif
0 comments (0 inline, 0 general)