Coverage Report
Current view: top level - src/app/server - server.cpp (source / functions) Hit Total Coverage
Test: !coverage.info Lines: 9 19 47.4 %
Date: 2025-01-08 17:56:07 Functions: 3 7 42.9 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 2 0.0 %

           Branch data     Line data    Source code
       1                 :            : /**
       2                 :            :  ***********************************************************************************************************************
       3                 :            :  * @file        server.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/server/server.hpp"
      12                 :            : 
      13                 :            : #include <thread>
      14                 :            : #include <chrono>
      15                 :            : 
      16                 :            : #include "utils/capi/capi.hpp"
      17                 :            : #include "utils/error/error.hpp"
      18                 :            : 
      19                 :            : namespace Server = App::Server;
      20                 :            : namespace Capi = Utils::Capi;
      21                 :            : namespace Error = Utils::Error;
      22                 :            : 
      23                 :          2 : Server::AppService::AppService(const std::string & domain,
      24                 :            :                                const std::string & instance,
      25                 :          2 :                                const std::string & connection) :
      26                 :          4 :     Capi::AppServiceBase(domain, instance, connection)
      27                 :          2 : { }
      28                 :            : 
      29                 :          2 : Server::AppService::~AppService(void) { }
      30                 :            : 
      31                 :          2 : void Server::AppService::ping(const std::shared_ptr<CommonAPI::ClientId> client, std::string request, pingReply_t reply)
      32                 :            : {
      33                 :          2 :     (void)client;
      34                 :          4 :     reply(request);
      35                 :          2 : }
      36                 :            : 
      37                 :          0 : Error::ID Server::main(void)
      38                 :            : {
      39                 :          0 :     auto error = Error::ID::UNKNOWN;
      40                 :            : 
      41                 :            :     // Create application service.
      42                 :          0 :     auto app_service = Server::AppService::make<Server::AppService>("local", "commonapi.app.App", "server-sample");
      43                 :            : 
      44                 :            :     // Create client with all clients.
      45                 :          0 :     auto server = Capi::Server<AppService>(app_service);
      46                 :            : 
      47                 :            :     // Start all clients.
      48         [ -  - ]:          0 :     END_ON_ERROR(server.start());
      49                 :            : 
      50                 :            :     // Send messages every second until stopped.
      51                 :          0 :     while (true)
      52                 :            :     {
      53                 :          0 :         std::this_thread::sleep_for(std::chrono::seconds(1));
      54                 :            :     }
      55                 :            : 
      56                 :            :     // Stop all clients.
      57                 :            :     END_ON_ERROR(server.stop());
      58                 :            : 
      59                 :            :     // All good.
      60                 :            :     error = Error::ID::OK;
      61                 :            : 
      62                 :          0 : end:
      63                 :            : 
      64                 :          0 :     return error;
      65                 :          0 : }
      66                 :            : 
      67                 :            : /******************************************************************************************************END OF FILE*****/