Branch data Line data Source code
1 : : /**
2 : : ***********************************************************************************************************************
3 : : * @file client.cpp
4 : : * @author Diego Martínez García (dmg0345@gmail.com)
5 : : * @date 08-01-2025 17:09:18 (UTC)
6 : : * @version 1.0.0
7 : : * @copyright github.com/dmg0345/commonapi_cpp_examples/blob/master/LICENSE
8 : : ***********************************************************************************************************************
9 : : */
10 : :
11 : : #include "app/client/client.hpp"
12 : :
13 : : #include <thread>
14 : : #include <chrono>
15 : :
16 : : #include "utils/capi/capi.hpp"
17 : : #include "utils/error/error.hpp"
18 : :
19 : : namespace Client = App::Client;
20 : : namespace Capi = Utils::Capi;
21 : : namespace Error = Utils::Error;
22 : :
23 : 2 : Client::AppClient::AppClient(const std::string & domain, const std::string & instance, const std::string & connection) :
24 : 2 : Capi::AppClientBase(domain, instance, connection)
25 : 2 : { }
26 : :
27 : 2 : Client::AppClient::~AppClient(void) { }
28 : :
29 : 2 : Utils::Error::ID Client::AppClient::ping(const std::string & request, std::string & response)
30 : : {
31 : 2 : auto error = Error::ID::UNKNOWN;
32 : 2 : std::string response_msg;
33 : 2 : CommonAPI::CallStatus call_status = CommonAPI::CallStatus::SUCCESS;
34 : 2 : const CommonAPI::CallInfo call_info(1000, 1234); // TODO: Double check this values.
35 : :
36 : : // Perform synchronous call.
37 : 6 : this->proxy->ping(request, call_status, response_msg, &call_info);
38 [ - + ]: 2 : END_ON_TRUE_WITH_ERROR(call_status != CommonAPI::CallStatus::SUCCESS, Error::ID::CAPI_CLIENT);
39 : :
40 : : // All good.
41 : 2 : response = std::move(response_msg);
42 : 2 : error = Error::ID::OK;
43 : :
44 : 2 : end:
45 : :
46 : 4 : return error;
47 : 2 : }
48 : :
49 : 0 : Error::ID Client::main(void)
50 : : {
51 : 0 : auto error = Error::ID::UNKNOWN;
52 : 0 : std::string response;
53 : :
54 : : // Create application client.
55 : 0 : auto app_client = Client::AppClient::make<Client::AppClient>("local", "commonapi.app.App", "client-sample");
56 : :
57 : : // Create client manager with all clients.
58 : 0 : auto client = Capi::Client<Client::AppClient>(app_client);
59 : :
60 : : // Start all clients.
61 [ - - ]: 0 : END_ON_ERROR(client.start());
62 : :
63 : : // Send messages every second until stopped.
64 : 0 : while (true)
65 : : {
66 [ - - ]: 0 : END_ON_ERROR(client.app->ping("Hello Server, I am the Client!", response));
67 : 0 : std::this_thread::sleep_for(std::chrono::seconds(1));
68 : : }
69 : :
70 : : // Stop all clients.
71 : : END_ON_ERROR(client.stop());
72 : :
73 : : // All good.
74 : : error = Error::ID::OK;
75 : :
76 : 0 : end:
77 : :
78 : 0 : return error;
79 : 0 : }
80 : :
81 : : /******************************************************************************************************END OF FILE*****/
|