当前位置: 首页>>代码示例>>C++>>正文


C++ Ptr::GetPort方法代码示例

本文整理汇总了C++中url::Ptr::GetPort方法的典型用法代码示例。如果您正苦于以下问题:C++ Ptr::GetPort方法的具体用法?C++ Ptr::GetPort怎么用?C++ Ptr::GetPort使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在url::Ptr的用法示例。


在下文中一共展示了Ptr::GetPort方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: MainFormBase

MainForm::MainForm(wxWindow *parent, const Url::Ptr& url)
	: MainFormBase(parent)
{
#ifdef _WIN32
	SetIcon(wxICON(icinga));
	SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE));
#endif /* _WIN32 */

	String port = url->GetPort();

	if (port.IsEmpty())
		port = "5665";

	m_ApiClient = new ApiClient(url->GetHost(), port, url->GetUsername(), url->GetPassword());
	m_ApiClient->GetTypes(boost::bind(&MainForm::TypesCompletionHandler, this, _1, _2, true));

	std::string title = url->Format() + " - Icinga Studio";
	SetTitle(title);

	m_ObjectsList->InsertColumn(0, "Name", 0, 300);
	
	m_PropertyGrid->SetColumnCount(3);
}
开发者ID:LMNetworks,项目名称:icinga2,代码行数:23,代码来源:mainform.cpp

示例2: move

/**
 * Connects to host:port and performs a TLS shandshake
 *
 * @returns AsioTlsStream pointer for future HTTP connections.
 */
std::shared_ptr<AsioTlsStream> ConsoleCommand::Connect()
{
	std::shared_ptr<boost::asio::ssl::context> sslContext;

	try {
		sslContext = MakeAsioSslContext(Empty, Empty, Empty); //TODO: Add support for cert, key, ca parameters
	} catch(const std::exception& ex) {
		Log(LogCritical, "DebugConsole")
			<< "Cannot make SSL context: " << ex.what();
		throw;
	}

	String host = l_Url->GetHost();
	String port = l_Url->GetPort();

	std::shared_ptr<AsioTlsStream> stream = std::make_shared<AsioTlsStream>(IoEngine::Get().GetIoService(), *sslContext, host);

	try {
		icinga::Connect(stream->lowest_layer(), host, port);
	} catch (const std::exception& ex) {
		Log(LogWarning, "DebugConsole")
			<< "Cannot connect to REST API on host '" << host << "' port '" << port << "': " << ex.what();
		throw;
	}

	auto& tlsStream (stream->next_layer());

	try {
		tlsStream.handshake(tlsStream.client);
	} catch (const std::exception& ex) {
		Log(LogWarning, "DebugConsole")
			<< "TLS handshake with host '" << host << "' failed: " << ex.what();
		throw;
	}

	return std::move(stream);
}
开发者ID:Icinga,项目名称:icinga2,代码行数:42,代码来源:consolecommand.cpp

示例3: SendRequest

/**
 * Sends the request via REST API and returns the parsed response.
 *
 * @param tlsStream Caller must prepare TLS stream/handshake.
 * @param url Fully prepared Url object.
 * @return A dictionary decoded from JSON.
 */
Dictionary::Ptr ConsoleCommand::SendRequest()
{
	namespace beast = boost::beast;
	namespace http = beast::http;

	l_TlsStream = ConsoleCommand::Connect();

	Defer s ([&]() {
		l_TlsStream->next_layer().shutdown();
	});

	http::request<http::string_body> request(http::verb::post, std::string(l_Url->Format(false)), 10);

	request.set(http::field::user_agent, "Icinga/DebugConsole/" + Application::GetAppVersion());
	request.set(http::field::host, l_Url->GetHost() + ":" + l_Url->GetPort());

	request.set(http::field::accept, "application/json");
	request.set(http::field::authorization, "Basic " + Base64::Encode(l_Url->GetUsername() + ":" + l_Url->GetPassword()));

	try {
		http::write(*l_TlsStream, request);
		l_TlsStream->flush();
	} catch (const std::exception &ex) {
		Log(LogWarning, "DebugConsole")
			<< "Cannot write HTTP request to REST API at URL '" << l_Url->Format(true) << "': " << ex.what();
		throw;
	}

	http::parser<false, http::string_body> parser;
	beast::flat_buffer buf;

	try {
		http::read(*l_TlsStream, buf, parser);
	} catch (const std::exception &ex) {
		Log(LogWarning, "DebugConsole")
			<< "Failed to parse HTTP response from REST API at URL '" << l_Url->Format(true) << "': " << ex.what();
		throw;
	}

	auto &response(parser.get());

	/* Handle HTTP errors first. */
	if (response.result() != http::status::ok) {
		String message = "HTTP request failed; Code: " + Convert::ToString(response.result())
			+ "; Body: " + response.body();
		BOOST_THROW_EXCEPTION(ScriptError(message));
	}

	Dictionary::Ptr jsonResponse;
	auto &body(response.body());

	//Log(LogWarning, "Console")
	//	<< "Got response: " << response.body();

	try {
		jsonResponse = JsonDecode(body);
	} catch (...) {
		String message = "Cannot parse JSON response body: " + response.body();
		BOOST_THROW_EXCEPTION(ScriptError(message));
	}

	return jsonResponse;
}
开发者ID:Icinga,项目名称:icinga2,代码行数:70,代码来源:consolecommand.cpp

示例4: Run

/**
 * The entry point for the "console" CLI command.
 *
 * @returns An exit status.
 */
int ConsoleCommand::Run(const po::variables_map& vm, const std::vector<std::string>& ap) const
{
#ifdef HAVE_EDITLINE
	rl_completion_entry_function = ConsoleCommand::ConsoleCompleteHelper;
	rl_completion_append_character = '\0';
#endif /* HAVE_EDITLINE */

	String addr, session;
	ScriptFrame scriptFrame(true);

	session = Utility::NewUniqueID();

	if (vm.count("sandbox"))
		scriptFrame.Sandboxed = true;

	scriptFrame.Self = scriptFrame.Locals;

	if (!vm.count("eval") && !vm.count("file"))
		std::cout << "Icinga 2 (version: " << Application::GetAppVersion() << ")\n"
			<< "Type $help to view available commands.\n";

	String addrEnv = Utility::GetFromEnvironment("ICINGA2_API_URL");
	if (!addrEnv.IsEmpty())
		addr = addrEnv;

	/* Initialize remote connect parameters. */
	if (vm.count("connect")) {
		addr = vm["connect"].as<std::string>();

		try {
			l_Url = new Url(addr);
		} catch (const std::exception& ex) {
			Log(LogCritical, "ConsoleCommand", ex.what());
			return EXIT_FAILURE;
		}

		String usernameEnv = Utility::GetFromEnvironment("ICINGA2_API_USERNAME");
		String passwordEnv = Utility::GetFromEnvironment("ICINGA2_API_PASSWORD");

		if (!usernameEnv.IsEmpty())
			l_Url->SetUsername(usernameEnv);
		if (!passwordEnv.IsEmpty())
			l_Url->SetPassword(passwordEnv);

		if (l_Url->GetPort().IsEmpty())
			l_Url->SetPort("5665");

		/* User passed --connect and wants to run the expression via REST API.
		 * Evaluate this now before any user input happens.
		 */
		try {
			l_TlsStream = ConsoleCommand::Connect();
		} catch (const std::exception& ex) {
			return EXIT_FAILURE;
		}
	}

	String command;
	bool syntaxOnly = false;

	if (vm.count("syntax-only")) {
		if (vm.count("eval") || vm.count("file"))
			syntaxOnly = true;
		else {
			std::cerr << "The option --syntax-only can only be used in combination with --eval or --file." << std::endl;
			return EXIT_FAILURE;
		}
	}

	String commandFileName;

	if (vm.count("eval"))
		command = vm["eval"].as<std::string>();
	else if (vm.count("file")) {
		commandFileName = vm["file"].as<std::string>();

		try {
			std::ifstream fp(commandFileName.CStr());
			fp.exceptions(std::ifstream::failbit | std::ifstream::badbit);
			command = String(std::istreambuf_iterator<char>(fp), std::istreambuf_iterator<char>());
		} catch (const std::exception&) {
			std::cerr << "Could not read file '" << commandFileName << "'." << std::endl;
			return EXIT_FAILURE;
		}
	}

	return RunScriptConsole(scriptFrame, addr, session, command, commandFileName, syntaxOnly);
}
开发者ID:Icinga,项目名称:icinga2,代码行数:93,代码来源:consolecommand.cpp

示例5: RunScriptConsole

int ConsoleCommand::RunScriptConsole(ScriptFrame& scriptFrame, const String& addr, const String& session, const String& commandOnce, const String& commandOnceFileName, bool syntaxOnly)
{
	std::map<String, String> lines;
	int next_line = 1;

#ifdef HAVE_EDITLINE
	char *homeEnv = getenv("HOME");

	String historyPath;
	std::fstream historyfp;

	if (homeEnv) {
		historyPath = String(homeEnv) + "/.icinga2_history";

		historyfp.open(historyPath.CStr(), std::fstream::in);

		String line;
		while (std::getline(historyfp, line.GetData()))
			add_history(line.CStr());

		historyfp.close();
	}
#endif /* HAVE_EDITLINE */

	l_ScriptFrame = &scriptFrame;
	l_Session = session;

	if (!addr.IsEmpty()) {
		Url::Ptr url;

		try {
			url = new Url(addr);
		} catch (const std::exception& ex) {
			Log(LogCritical, "ConsoleCommand", ex.what());
			return EXIT_FAILURE;
		}

		const char *usernameEnv = getenv("ICINGA2_API_USERNAME");
		const char *passwordEnv = getenv("ICINGA2_API_PASSWORD");

		if (usernameEnv)
			url->SetUsername(usernameEnv);
		if (passwordEnv)
			url->SetPassword(passwordEnv);

		if (url->GetPort().IsEmpty())
			url->SetPort("5665");

		l_ApiClient = new ApiClient(url->GetHost(), url->GetPort(), url->GetUsername(), url->GetPassword());
	}

	while (std::cin.good()) {
		String fileName;

		if (commandOnceFileName.IsEmpty())
			fileName = "<" + Convert::ToString(next_line) + ">";
		else
			fileName = commandOnceFileName;

		next_line++;

		bool continuation = false;
		std::string command;

incomplete:
		std::string line;

		if (commandOnce.IsEmpty()) {
#ifdef HAVE_EDITLINE
			std::ostringstream promptbuf;
			std::ostream& os = promptbuf;
#else /* HAVE_EDITLINE */
			std::ostream& os = std::cout;
#endif /* HAVE_EDITLINE */

			os << fileName;

			if (!continuation)
				os << " => ";
			else
				os << " .. ";

#ifdef HAVE_EDITLINE
			String prompt = promptbuf.str();

			char *cline;
			cline = readline(prompt.CStr());

			if (!cline)
				break;

			if (commandOnce.IsEmpty() && cline[0] != '\0') {
				add_history(cline);

				if (!historyPath.IsEmpty()) {
					historyfp.open(historyPath.CStr(), std::fstream::out | std::fstream::app);
					historyfp << cline << "\n";
					historyfp.close();
				}
			}
//.........这里部分代码省略.........
开发者ID:dupondje,项目名称:icinga2,代码行数:101,代码来源:consolecommand.cpp

示例6: SendRequest

void ElasticsearchWriter::SendRequest(const String& body)
{
	namespace beast = boost::beast;
	namespace http = beast::http;

	Url::Ptr url = new Url();

	url->SetScheme(GetEnableTls() ? "https" : "http");
	url->SetHost(GetHost());
	url->SetPort(GetPort());

	std::vector<String> path;

	/* Specify the index path. Best practice is a daily rotation.
	 * Example: http://localhost:9200/icinga2-2017.09.11?pretty=1
	 */
	path.emplace_back(GetIndex() + "-" + Utility::FormatDateTime("%Y.%m.%d", Utility::GetTime()));

	/* ES 6 removes multiple _type mappings: https://www.elastic.co/guide/en/elasticsearch/reference/6.x/removal-of-types.html
	 * Best practice is to statically define 'doc', as ES 5.X does not allow types starting with '_'.
	 */
	path.emplace_back("doc");

	/* Use the bulk message format. */
	path.emplace_back("_bulk");

	url->SetPath(path);

	OptionalTlsStream stream;

	try {
		stream = Connect();
	} catch (const std::exception& ex) {
		Log(LogWarning, "ElasticsearchWriter")
			<< "Flush failed, cannot connect to Elasticsearch: " << DiagnosticInformation(ex, false);
		return;
	}

	Defer s ([&stream]() {
		if (stream.first) {
			stream.first->next_layer().shutdown();
		}
	});

	http::request<http::string_body> request (http::verb::post, std::string(url->Format(true)), 10);

	request.set(http::field::user_agent, "Icinga/" + Application::GetAppVersion());
	request.set(http::field::host, url->GetHost() + ":" + url->GetPort());

	/* Specify required headers by Elasticsearch. */
	request.set(http::field::accept, "application/json");

	/* Use application/x-ndjson for bulk streams. While ES
	 * is able to handle application/json, the newline separator
	 * causes problems with Logstash (#6609).
	 */
	request.set(http::field::content_type, "application/x-ndjson");

	/* Send authentication if configured. */
	String username = GetUsername();
	String password = GetPassword();

	if (!username.IsEmpty() && !password.IsEmpty())
		request.set(http::field::authorization, "Basic " + Base64::Encode(username + ":" + password));

	request.body() = body;
	request.set(http::field::content_length, request.body().size());

	/* Don't log the request body to debug log, this is already done above. */
	Log(LogDebug, "ElasticsearchWriter")
		<< "Sending " << request.method_string() << " request" << ((!username.IsEmpty() && !password.IsEmpty()) ? " with basic auth" : "" )
		<< " to '" << url->Format() << "'.";

	try {
		if (stream.first) {
			http::write(*stream.first, request);
			stream.first->flush();
		} else {
			http::write(*stream.second, request);
			stream.second->flush();
		}
	} catch (const std::exception&) {
		Log(LogWarning, "ElasticsearchWriter")
			<< "Cannot write to HTTP API on host '" << GetHost() << "' port '" << GetPort() << "'.";
		throw;
	}

	http::parser<false, http::string_body> parser;
	beast::flat_buffer buf;

	try {
		if (stream.first) {
			http::read(*stream.first, buf, parser);
		} else {
			http::read(*stream.second, buf, parser);
		}
	} catch (const std::exception& ex) {
		Log(LogWarning, "ElasticsearchWriter")
			<< "Failed to parse HTTP response from host '" << GetHost() << "' port '" << GetPort() << "': " << DiagnosticInformation(ex, false);
		throw;
//.........这里部分代码省略.........
开发者ID:Icinga,项目名称:icinga2,代码行数:101,代码来源:elasticsearchwriter.cpp


注:本文中的url::Ptr::GetPort方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。