Esempio n. 1
0
void client::configuration::do_query(const Plugin::QueryRequestMessage &request, Plugin::QueryResponseMessage &response) {
	Plugin::QueryResponseMessage local_response;

	std::string target = "default";
	if (request.header().has_recipient_id())
		target = request.header().recipient_id();
	else if (request.header().has_destination_id())
		target = request.header().destination_id();

	BOOST_FOREACH(const std::string t, strEx::s::splitEx(target, std::string(","))) {
		destination_container d;
		destination_container s;

		// If we have a target, apply it
		object_handler_type::object_instance op = targets.find_object(t);
		if (op)
			d.apply(op);
		else {
			object_handler_type::object_instance op = targets.find_object("default");
			if (op)
				d.apply(op);
		}
		// Next apply the header object
		d.apply(t, request.header());
		s.apply(request.header().sender_id(), request.header());
		std::string command = request.header().command();

		if (!command.empty()) {
			// If we have a header command treat the data as a batch
			i_do_query(s, d, command, request, response, true);
		} else {
			// Parse each objects command and execute them
			for (int i=0;i<request.payload_size();i++) {
				::Plugin::QueryRequestMessage local_request_message;
				const ::Plugin::QueryRequestMessage::Request &local_request = request.payload(i);
				local_request_message.mutable_header()->CopyFrom(request.header());
				local_request_message.add_payload()->CopyFrom(local_request);
				std::string command = local_request.command();
				::Plugin::QueryResponseMessage local_response_message;
				i_do_query(s, d, command, local_request_message, local_response_message, false);
				for (int j=0;j<local_response_message.payload_size();j++) {
					response.add_payload()->CopyFrom(local_response_message.payload(j));
				}
			}
		}
	}
}
Esempio n. 2
0
/*
void client::command_manager::parse_query(const std::string &prefix, const std::string &default_command, const std::string &cmd, client::configuration &config, const Plugin::QueryRequestMessage::Request &request, Plugin::QueryResponseMessage::Response &response, const Plugin::QueryRequestMessage &request_message) {
	boost::program_options::variables_map vm;
	std::string real_command;
	try {
		command_type::const_iterator cit = commands.find(cmd);
		boost::program_options::variables_map vm;
		if (cit == commands.end()) {
			real_command = parse_command(cmd, prefix);
			if (real_command == "forward") {
				for (int i=0;i<request_message.header().metadata_size();i++) {
					if (request_message.header().metadata(i).key() == "command")
						config.data->command = request_message.header().metadata(i).value();
				}
				for (int i=0;i<request.arguments_size();i++) {
					config.data->arguments.push_back(request.arguments(i));
				}
			} else {
				po::options_description desc = create_descriptor(real_command, default_command, config);
				if (!nscapi::program_options::process_arguments_from_request(vm, desc, request, response)) 
					return;
			}
		} else {
			std::vector<std::string> args;
			real_command = parse_command(cit->second.command, prefix);
			po::options_description desc = create_descriptor(real_command, default_command, config);
			BOOST_FOREACH(std::string argument, cit->second.arguments) {
				for (int i=0;i<request.arguments_size();i++) {
					strEx::replace(argument, "$ARG" + strEx::s::xtos(i+1) + "$", request.arguments(i));
				}
				args.push_back(argument);
			}
			if (!nscapi::program_options::process_arguments_from_vector(vm, desc, request.command(), args, response)) 
				return;
		}
	} catch (const std::exception &e) {
		return nscapi::protobuf::functions::set_response_bad(response, "Exception processing command line: " + utf8::utf8_from_native(e.what()));
	}

	if (!config.data->target_id.empty()) {
		if (!config.targets.has_object(config.data->target_id))
			return nscapi::protobuf::functions::set_response_bad(response, "Target not found: " + config.data->target_id);
		//TODO: config.data->recipient.import(config.target_lookup->lookup_target(config.data->target_id));
	}
	if (real_command == "query" || (real_command.empty() && default_command == "query")) {
		do_query(config, request_message.header(), response);
	} else if (real_command == "exec" || (real_command.empty() && default_command == "exec")) {
		return nscapi::protobuf::functions::set_response_bad(response, "Paradigm shift currently not supported");
	} else if (real_command == "submit" || (real_command.empty() && default_command == "submit")) {
		return nscapi::protobuf::functions::set_response_bad(response, "Paradigm shift currently not supported");
	} else {
		return nscapi::protobuf::functions::set_response_bad(response, "Invalid command: "  +real_command);
	}
}
*/
void client::configuration::parse_query(const Plugin::QueryRequestMessage &request, Plugin::QueryResponseMessage &response) {

	std::string command = "";

	for (int i = 0; i<request.header().metadata_size(); i++) {
		if (request.header().metadata(i).key() == "command")
			command = request.header().metadata(i).value();
	}
	for (int i = 0; i < request.payload_size(); i++) {
		::Plugin::QueryRequestMessage::Request *reqp = request.mutable_payload(0);
		if (!command.empty()) {
			command = reqp->command();
		}
		// Check pre populated commands
		if (reqp->arguments_size() > 0) {

			boost::program_options::variables_map vm;
			std::string real_command = "query";
			try {
				po::options_description desc = create_descriptor(real_command, real_command, config);
				if (!nscapi::program_options::process_arguments_from_vector(vm, desc, real_command, args, response))
					return;
			}
			catch (const std::exception &e) {
				return nscapi::protobuf::functions::set_response_bad(response, "Exception processing command line: " + utf8::utf8_from_native(e.what()));
			}

			if (real_command == "query") {
				const ::Plugin::Common::Header header;
				do_query(config, header, response);
			}
			else {
				return nscapi::protobuf::functions::set_response_bad(response, "Invalid command: " + real_command);
			}
		}
	}
}