//------------------------------------------------------------------------------
//if worker already present remove it and re-insert it in the right
//position
void push(Workers& workers, int id) {

    Workers::iterator it = std::find_if(workers.begin(),
                               workers.end(),
                               [id](const worker_info& wi){
                                   return wi.id() == id;
                               });
    if(it != workers.end()) workers.erase(it);
    
    workers.insert(worker_info(id));
}
Esempio n. 2
0
bool DistributedServer::loadModuleEx(std::string alias, NSCAPI::moduleLoadMode mode) {
	std::wstring host, suffix, server_mode;
	unsigned int thread_count;
	try {
		sh::settings_registry settings(get_settings_proxy());
		settings.set_alias(_T("distributed"), alias, _T("server"));

		settings.alias().add_path_to_settings()
			(_T("DISTRIBUTED NSCP SERVER SECTION"), _T("Section for Distributed NSCP (DistributedServer) (check_nscp) protocol options."))
			;

		settings.alias().add_key_to_settings()
			(_T("host"), sh::wstring_key(&host, _T("tcp://*:5555")),
			_T("HOST TO BIND/CONNECT TO"), _T("The host to bind/connect to"))

			(_T("suffix"), sh::wstring_key(&suffix, _T("ncsp.dist")),
			_T("SUFFIX FOR INTERNAL CHANNELS"), _T("Has to be uniq on each server"))

			(_T("worker pool size"), sh::uint_key(&thread_count, 10),
			_T("WORKER POOL SIZE"), _T("Number of threads to spawn for the worker pool"))

			(_T("mode"), sh::wstring_key(&server_mode, _T("master")),
			_T("OPERATION MODE"), _T("Mode of operation can only be master now but will add more later on (such as slave)"))
			;

		settings.register_all();
		settings.notify();

		if (mode == NSCAPI::normalStart) {
			context = new zmq::context_t(2);

			zeromq_queue::connection_info queue_info(to_string(host), to_string(suffix));
			zeromq_queue::queue_manager queue;
			queue.start(context, threads, queue_info);

			zeromq_worker::connection_info worker_info(queue_info.get_backend(), to_string(suffix), thread_count);
			zeromq_worker::worker_manager workers;
			workers.start(context, threads, worker_info);
		}

	} catch (std::exception &e) {
		NSC_LOG_ERROR_STD(_T("Exception caught: ") + to_wstring(e.what()));
		return false;
	} catch (...) {
		NSC_LOG_ERROR_STD(_T("Exception caught: <UNKNOWN EXCEPTION>"));
		return false;
	}
	return true;
}