Quand le code est clair, même le C++ est easy…

Voici la méthode GET de mon serveur HTTP REST fait en C++:

Il suffit d’extracter la variable request et le tour est joué… L’avantage du C++ c’est que le code généré est tellement efficace qu’il consomme peu de mémoire et est d’une rapidité extrème surtout en Release x64. Et ce n’est pas plus difficile que le C# ou le Java…

void MyServer::handle_get(http_request message)
{
	std::wcout << _T("handle_get") << std::endl;
	std::wcout << _T("Message") << _T(" ") << message.to_string() << endl;
	std::wcout << _T("Relative URI") << _T(" ") << message.relative_uri().to_string() << endl;

	auto paths = uri::split_path(uri::decode(message.relative_uri().path()));
	for (auto it1 = paths.begin(); it1 != paths.end(); it1++)
	{
		std::wcout << _T("Path") << _T(" ") << *it1 << endl;
	}

	auto query = uri::split_query(uri::decode(message.relative_uri().query()));
	for (auto it2 = query.begin(); it2 != query.end(); it2++)
	{
		std::wcout << _T("Query") << _T(" ") << it2->first << _T(" ") << it2->second << endl;
	}

	std::wstring request = CHelper::FindParameterInQuery(query, _T("request"));
	std::wstring server = CHelper::FindParameterInQuery(query, _T("server"));
	std::wstring port = CHelper::FindParameterInQuery(query, _T("port"));
	std::wstring name = CHelper::FindParameterInQuery(query, _T("name"));
	std::wstring dbname = CHelper::FindParameterInQuery(query, _T("dbname"));

	if (request == _VERB_REGISTER_NODE_)
	{
		std::wcout << _VERB_REGISTER_NODE_ << std::endl;

		if (MyServer::ExistsNode(server, port, name) == true)
		{
			std::wcout << _T("Node already registered !") << std::endl;
		}
		else
		{
			std::shared_ptr<WorkerNodeAttributes> pNode = std::make_shared<WorkerNodeAttributes>();
			pNode->_server = server;
			pNode->_port = port;
			pNode->_name = name;
			pNode->_dbName = _T("");

			g_Guard.WaitToWrite();
			_nodes.push_back(pNode);
			std::wcout << _T("Node registered !") << _T(" count: ") << _nodes.size() << std::endl;
			g_Guard.Done();
		}

		message.reply(status_codes::OK);
		return;
	}
		
	if (request == _VERB_SHOW_NODE_)
	{
		std::wcout << _VERB_SHOW_NODE_ << std::endl;
		MyServer::ShowNodes();
		message.reply(status_codes::OK);
		return;
	}
	
	if (request == _VERB_GET_NODE_ )
	{
		std::wcout << _VERB_GET_NODE_ << std::endl;
			
		std::shared_ptr<WorkerNodeAttributes> pObj = nullptr;

		g_Guard.WaitToWrite();

		for (auto itr = _nodes.begin(); itr != _nodes.end(); itr++)
		{
			pObj = *itr;

			if (pObj->_dbName == dbname) // && pObj->_isActive == true
			{
				// We find an existing entry
				*itr = pObj;
				break;
			}
			else if (pObj->_isActive == false)
			{
				// We find an entry
				pObj->_isActive = true;
				pObj->_dbName = dbname;
				*itr = pObj;
				break;
			}
			else
				pObj = nullptr;
		}
				
		if (pObj != nullptr)
		{
			GetNodeData data;
			data.ip = pObj->_server;
			data.port = pObj->_port;
			data.name = pObj->_name;
			data.dbName = dbname;

			std::wstring response = data.AsJSON().serialize();
			std::wcout << response << endl;

			message.reply(status_codes::OK, data.AsJSON());

			MyServer::SendDbName(data);
		}
		else
		{
			std::wcout << _T("No node available...") << std::endl;
			message.reply(status_codes::OK);
		}

		g_Guard.Done();

		return;
	}
		
	message.reply(status_codes::OK);
};

 

Leave a Reply

Your email address will not be published. Required fields are marked *