77#include < csignal>
88#include < chrono>
99#include < thread>
10+ #include < optional>
1011
1112#include " ../common/helpers.h"
1213
@@ -28,9 +29,7 @@ class HealthServiceImpl final : public grpc::health::v1::Health::Service
2829 }
2930};
3031
31- static std::unique_ptr<grpc::Server> gServer ;
32-
33- void RunServer ( const consulcpp::Service & service )
32+ void TestService ( consulcpp::Service & service )
3433{
3534 std::string serverAddress = fmt::format ( " {}:{}" , service.mAddress , service.mPort );
3635 HealthServiceImpl healthService;
@@ -39,21 +38,30 @@ void RunServer( const consulcpp::Service & service )
3938 builder.AddListeningPort ( serverAddress, grpc::InsecureServerCredentials () );
4039 builder.RegisterService ( &healthService );
4140
42- gServer = builder.BuildAndStart ();
43- if ( gServer ) {
44- spdlog::info ( " Server listening on {}" , serverAddress );
45- gServer ->Wait ();
41+ if ( std::unique_ptr<grpc::Server> server = builder.BuildAndStart (); server ){
42+ std::thread grpcThread ( &grpc::Server::Wait, server.get () );
43+ spdlog::info ( " Server listening on {}:{}" , service.mAddress , service.mPort );
44+ loop ();
45+ server->Shutdown ();
46+ grpcThread.join ();
4647 spdlog::info ( " Server stopped" );
4748 } else {
48- spdlog::critical ( " Cannot start gRPC server on {}" , serverAddress );
49+ spdlog::critical ( " Cannot start gRPC server on {}:{} " , service. mAddress , service. mPort );
4950 }
5051}
51-
5252int main ( int argc, char * argv[] )
5353{
54- consulcpp::Consul consul;
55-
56- if ( consul.connect () ) {
54+ unsigned short port = 50051 ;
55+
56+ if ( argc > 1 ) {
57+ if ( auto maybePort = consulcpp::utils::asPort ( argv[1 ] ); maybePort ){
58+ port = maybePort.value ();
59+ }else {
60+ spdlog::error ( " This is not a valid port number {}" , argv[1 ] );
61+ return 1 ;
62+ }
63+ }
64+ if ( consulcpp::Consul consul; consul.connect () ) {
5765 spdlog::info ( " Agent address {}" , consul.address () );
5866
5967 consulcpp::Service service;
@@ -62,20 +70,18 @@ int main( int argc, char * argv[] )
6270 service.mId = fmt::format ( " {}" , std::chrono::duration_cast<std::chrono::milliseconds>( std::chrono::system_clock::now ().time_since_epoch () ).count () );
6371 service.mName = " leader-demo" ;
6472 service.mAddress = consul.address ();
65- if ( argc > 1 ) {
66- service.mPort = std::stoi ( argv[ 1 ] );
67- } else {
68- service.mPort = 50051 ;
69- }
73+ service.mPort = port;
74+
75+ check.mId = " presence" ;
7076 check.mInterval = " 5s" ;
7177 check.mDeregisterCriticalServiceAfter = " 1m" ;
7278 check.mGRPC = fmt::format ( " {}:{}/Health" , service.mAddress , service.mPort );
7379 service.mChecks = { check };
7480
7581 // Purge death services
7682 std::vector<consulcpp::Service> otherServices = consul.services ().findInCatalog ( service.mName , service.mTags );
77- for ( auto service : otherServices ) {
78- consul.services ().destroy ( service );
83+ for ( const auto & otherService : otherServices ) {
84+ consul.services ().destroy ( otherService );
7985 }
8086
8187 consul.services ().create ( service );
@@ -84,21 +90,15 @@ int main( int argc, char * argv[] )
8490
8591 consul.kv ().set ( " my-key" , " my-value" );
8692
87- consulcpp::Leader::Status leader = consul.leader ().acquire ( service, session );
88- if ( leader == consulcpp::Leader::Status::Yes ) {
93+ if ( consulcpp::Leader::Status leader = consul.leader ().acquire ( service, session ); leader == consulcpp::Leader::Status::Yes ) {
8994 spdlog::info ( " I'm the leader" );
9095 } else {
9196 spdlog::info ( " I'm a follower" );
9297 }
93- std::thread grpcThread ( RunServer, service );
94- loop ();
95- if ( gServer ) {
96- gServer ->Shutdown ();
97- }
98- grpcThread.join ();
9998
100- auto val = consul.kv ().get ( " my-key" );
101- if ( val ) {
99+ TestService ( service );
100+
101+ if ( auto val = consul.kv ().get ( " my-key" ); val ) {
102102 spdlog::info ( " Key value was {}" , val.value () );
103103 if ( consul.kv ().destroy ( " my-key" ) ) {
104104 spdlog::info ( " Key deleted" );
0 commit comments