Example sequence
(For parsing JSON using boost/property_tree):
/// testconsoleapilib.cpp : Defines the entry point for the console application. // #include <windows.h> #include <iostream> #include <string> #include <boost/property_tree/json_parser.hpp> #include <boost/property_tree/ptree.hpp> #include <sstream> #include <boost/foreach.hpp> #include <boost/scope_exit.hpp>
typedef int (*era_process_request)(const char* request, char** response); typedef void (*era_free)(char* s); typedef int (*era_init_lib)(); typedef void (*era_deinit_lib)();
int main(int argc, char** argv) { HMODULE hMod = LoadLibrary(L"ServerApi.dll"); BOOST_SCOPE_EXIT(&hMod) { if(hMod) { FreeLibrary(hMod); } } BOOST_SCOPE_EXIT_END if (!hMod) { std::cout<< "Cannot load library error. Last error is:" << GetLastError() << std::endl; return 1; }
era_init_lib init_lib = (era_init_lib)GetProcAddress(hMod,"era_init_lib"); era_deinit_lib deinit_lib = (era_deinit_lib)GetProcAddress(hMod,"era_deinit_lib");
if (!init_lib ) { std::cout<<"Cannot init library" << std::endl; return 1; } if (!deinit_lib ) { std::cout<<"Cannot deinit libraries" << std::endl; return 1; }
int res = init_lib();
BOOST_SCOPE_EXIT(&deinit_lib,&res) { if(!res) { if(deinit_lib) { deinit_lib(); } } } BOOST_SCOPE_EXIT_END
if(res) { std::cout<<"Init lib result:" << res << std::endl; return 1; }
era_process_request send_request = (era_process_request)GetProcAddress(hMod,"era_process_request"); if (!send_request) { std::cout<<"Cannot load era_process_request" << std::endl; return 1; }
era_free free_response = (era_free)GetProcAddress(hMod,"era_free"); if (!free_response) { std::cout<<"Cannot load era_free" << std::endl; return 1; }
//Start ServerApi 1 std::string request("{\"Era.ServerApi.StartRequest\":{}}"); std::cout<<"Executing json " << request << " ..." << std::endl; char* szRes = NULL; res = send_request(request.c_str(),&szRes); if (!res) { std::string response(szRes);
std::cout<<"Response is: " << response << std::endl; boost::property_tree::ptree pt2; std::istringstream is (response); read_json (is, pt2);
bool resultMessageHasTrueResult = pt2.get (boost::property_tree::ptree::path_type("Era.ServerApi.SimpleResponse/result", '/'),false);
if(!resultMessageHasTrueResult) { std::cout << "ServerApi was not started properly." << std::endl; return 1; } } else { std::cout << "Error in ServerApi: " << res << std::endl; free_response(szRes); return 1;
} BOOST_SCOPE_EXIT(&send_request,&free_response){ //Stop ServerApi 7 std::string request="{\"Era.ServerApi.StopRequest\":{}}"; std::cout<<"Executing json " << request << " ..." << std::endl; char* szRes = NULL; int res = send_request(request.c_str(),&szRes); if (!res) { std::string response(szRes);
std::cout<<"Response is: " << response << std::endl; boost::property_tree::ptree pt2; std::istringstream is (response); read_json (is, pt2);
bool resultMessageHasTrueResult = pt2.get (boost::property_tree::ptree::path_type("Era.ServerApi.SimpleResponse/result", '/'),false);
if(!resultMessageHasTrueResult) { std::cout << "Stop request failed." << std::endl; free_response(szRes); }
} else { std::cout << "Error in ServerApi: " << res << std::endl; free_response(szRes); } free_response(szRes); } BOOST_SCOPE_EXIT_END free_response(szRes);
//CreateConfiguration 2 request = "{\"Era.ServerApi.CreateConnectionRequest\":{\"host\":\"127.0.0.1\",\"port\":2223}}"; std::cout<<"Executing json " << request << " ..." << std::endl; szRes = NULL; res = send_request(request.c_str(),&szRes); if (!res) { std::string response(szRes);
std::cout<<"Response is: " << response << std::endl; boost::property_tree::ptree pt2; std::istringstream is (response); read_json (is, pt2);
try{ std::string resultMessageHasTrueResult = pt2.get<std::string> (boost::property_tree::ptree::path_type("Era.ServerApi.VerifyUserRequest/result", '/')); } catch(const std::exception& e) { std::cout << "ServerApi cannot connect." << std::endl; free_response(szRes); return 1; } } else { std::cout << "Error in ServerApi: " << res << std::endl; free_response(szRes); return 1; } BOOST_SCOPE_EXIT(&send_request,&free_response){ //CloseConnection 6 std::string request="{\"Era.ServerApi.CloseConnectionRequest\":{}}"; std::cout<<"Executing json " << request << " ..." << std::endl; char* szRes = NULL; int res = send_request(request.c_str(),&szRes); if (!res) { std::string response(szRes); std::cout<<"Response is: " << response << std::endl; boost::property_tree::ptree pt2; std::istringstream is (response); read_json (is, pt2);
bool resultMessageHasTrueResult = pt2.get (boost::property_tree::ptree::path_type("Era.ServerApi.SimpleResponse/result", '/'),false);
if(!resultMessageHasTrueResult) { std::cout << "Connection was not closed properly." << std::endl; free_response(szRes); } } else { std::cout << "Error in ServerApi: " << res << std::endl; free_response(szRes); } free_response(szRes); } BOOST_SCOPE_EXIT_END free_response(szRes);
//VerifyUserResponse 3 request="{\"Era.ServerApi.VerifyUserResponse\":{\"VerifyResult\":true}}"; std::cout<<"Executing json " << request << " ..." << std::endl; szRes = NULL; res = send_request(request.c_str(),&szRes); if (!res) { std::string response(szRes);
std::cout<<"Response is: " << response << std::endl; boost::property_tree::ptree pt2; std::istringstream is (response); read_json (is, pt2);
bool resultMessageHasTrueResult = pt2.get (boost::property_tree::ptree::path_type("Era.ServerApi.SimpleResponse/result", '/'),false);
if(!resultMessageHasTrueResult) { std::cout << "ServerApi was not started properly." << std::endl; free_response(szRes); return 1; } } else { std::cout << "Error in ServerApi: " << res << std::endl; free_response(szRes); return 1; } free_response(szRes);
//Authenticate 4 request="{\"Era.Common.NetworkMessage.ConsoleApi.SessionManagement.RpcAuthLoginRequest\" : {\"username\":\"Administrator\", \"password\":\"secret\", \"isDomainUser\":false, \"locale\":\"en-US\"}}"; std::cout<<"Executing json " << request << " ..." << std::endl; szRes = NULL; res = send_request(request.c_str(),&szRes); if (!res) { std::string response(szRes);
std::cout<<"Response is: " << response << std::endl; boost::property_tree::ptree pt2; std::istringstream is (response); read_json (is, pt2);
try{ std::string resultMessageHasTrueResult = pt2.get<std::string> (boost::property_tree::ptree::path_type("Era.Common.NetworkMessage.ConsoleApi.SessionManagement.RpcAuthLoginResponse/userUuid/uuid", '/')); } catch(const std::exception& e) { std::cout << "ServerApi cannot authenticate. Error is: " << e.what() << std::endl; free_response(szRes); return 1; } } else { std::cout << "Error in ServerApi: " << res << std::endl; free_response(szRes); return 1; } free_response(szRes);
//CreateStaticGroup 5
request="{\"Era.Common.NetworkMessage.ConsoleApi.Groups.RpcCreateStaticGroupRequest\":{\"staticObjectData\":{\"name\":\"group1\",\"description\":\"description\"},\"parentGroupUuid\":{\"uuid\":\"00000000-0000-0000-7001-000000000001\"}}}"; std::cout<<"Executing json " << request << " ..." << std::endl; szRes = NULL; res = send_request(request.c_str(),&szRes); std::string uuidOfGroup = ""; if (!res) { std::string response(szRes); std::cout<<"Response is: " << response << std::endl; boost::property_tree::ptree pt2; std::istringstream is (response); read_json (is, pt2);
try{ uuidOfGroup = pt2.get<std::string> (boost::property_tree::ptree::path_type("Era.Common.NetworkMessage.ConsoleApi.Groups.RpcCreateStaticGroupResponse/staticObjectIdentification/uuid/uuid", '/')); int version = pt2.get<int> (boost::property_tree::ptree::path_type("Era.Common.NetworkMessage.ConsoleApi.Groups.RpcCreateStaticGroupResponse/staticObjectIdentification/versionGuard", '/')); std::string name = pt2.get<std::string> (boost::property_tree::ptree::path_type("Era.Common.NetworkMessage.ConsoleApi.Groups.RpcCreateStaticGroupResponse/staticObjectData/name", '/')); std::string description = pt2.get<std::string> (boost::property_tree::ptree::path_type("Era.Common.NetworkMessage.ConsoleApi.Groups.RpcCreateStaticGroupResponse/staticObjectData/description", '/')); std::string parentuuid = pt2.get<std::string> (boost::property_tree::ptree::path_type("Era.Common.NetworkMessage.ConsoleApi.Groups.RpcCreateStaticGroupResponse/staticGroupRelations/parentGroup/uuid", '/')); if(parentuuid != "00000000-0000-0000-7001-000000000001" || name !="group1" || description!="description") { throw std::runtime_error("Result is not expected"); } } catch(const std::exception& e) { std::cout << "ServerApi cannot create group. Error is: " << e.what() << std::endl; free_response(szRes); return 1; }
} else { std::cout << "Error in ServerApi: " << res << std::endl; free_response(szRes); return 1; } free_response(szRes);
//CreateComputer 5
request="{\"Era.Common.NetworkMessage.ConsoleApi.Groups.RpcCreateComputerRequest\":{\"parentGroupUuid\":{\"uuid\":\"" + uuidOfGroup + "\"},\"computerNames\":[\"computer1\"],\"commonDescription\":\"first\",\"collisionsHandling\":1}}"; std::cout<<"Executing json " << request << " ..." << std::endl; szRes = NULL; res = send_request(request.c_str(),&szRes); if (!res) { std::string response(szRes);
std::cout<<"Response is: " << response << std::endl; boost::property_tree::ptree pt2; std::istringstream is (response); read_json (is, pt2);
try{ boost::property_tree::ptree& pos = pt2.get_child(boost::property_tree::ptree::path_type("Era.Common.NetworkMessage.ConsoleApi.Groups.RpcCreateComputerResponse/results", '/')); int size = 0 ; BOOST_FOREACH (const boost::property_tree::ptree::value_type& resultCreateComputer, pos) { if(size == 1) { throw std::runtime_error("Result in create compures has to have one result"); }
size++; int resultCreateComp = resultCreateComputer.second.get<int> (boost::property_tree::ptree::path_type("result", '/')); int requestIndex = resultCreateComputer.second.get<int> (boost::property_tree::ptree::path_type("requestIndex", '/')); std::string uuid = resultCreateComputer.second.get<std::string>(boost::property_tree::ptree::path_type("staticObjectIdentification/uuid/uuid", '/')); std::string version = resultCreateComputer.second.get<std::string>(boost::property_tree::ptree::path_type("staticObjectIdentification/versionGuard", '/')); std::string name = resultCreateComputer.second.get<std::string>(boost::property_tree::ptree::path_type("staticObjectData/name", '/')); std::string description = resultCreateComputer.second.get<std::string>(boost::property_tree::ptree::path_type("staticObjectData/description", '/')); if(resultCreateComp!=1 || requestIndex != 0 || name !="computer1" || description!="first") { throw std::runtime_error("Result is not expected"); } } } catch(const std::exception& e) { std::cout << "ServerApi cannot create computer. Error is: " << e.what() << std::endl; free_response(szRes); return 1; } } else { std::cout << "Error in ServerApi: " << res << std::endl; free_response(szRes); return 1; } free_response(szRes);
return 0; } |