Skip to content

Commit

Permalink
enh(ccc): new grpc client
Browse files Browse the repository at this point in the history
At the moment, we can execute:

ccc -p 31001   # to see what the server is
ccc -p 31991 -l  # to see the available methods

REFS: MON-13947
  • Loading branch information
bouda1 authored Jun 20, 2022
1 parent fc0efa5 commit e6f241a
Show file tree
Hide file tree
Showing 9 changed files with 720 additions and 123 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ add_subdirectory(broker)
add_subdirectory(clib)
add_subdirectory(engine)
add_subdirectory(connectors)
add_subdirectory(ccc)

add_custom_target(test-broker
COMMAND tests/ut_broker
Expand Down
55 changes: 55 additions & 0 deletions ccc/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
##
## Copyright 2022 Centreon
##
## Licensed under the Apache License, Version 2.0 (the "License");
## you may not use this file except in compliance with the License.
## You may obtain a copy of the License at
##
## http://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
##
## For more information : contact@centreon.com
##

#
# Global settings.
#

# Set necessary settings.
project("Centreon Collect Client" C CXX)

# set -latomic if OS is Raspbian.
if (CMAKE_SYSTEM_PROCESSOR MATCHES "arm")
set(CMAKE_CXX_LINK_FLAGS "${CMAKE_CXX_LINK_FLAGS} -latomic")
endif ()

add_definitions("-D_GLIBCXX_USE_CXX11_ABI=1")

option(WITH_LIBCXX "compiles and link cbd with clang++/libc++")
if (WITH_LIBCXX)
set(CMAKE_CXX_COMPILER "clang++")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++")
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread -Werror -O1 -fno-omit-frame-pointer")
endif ()

include_directories(
${CMAKE_SOURCE_DIR}/broker/core/src
${CMAKE_SOURCE_DIR}/engine/enginerpc
)
set(ccc_files
main.cc
client.cc
)

add_executable(ccc ${ccc_files})
target_link_libraries(ccc CONAN_PKG::grpc cerpc berpc CONAN_PKG::abseil CONAN_PKG::fmt)
set_target_properties(ccc PROPERTIES COMPILE_FLAGS "-fPIC")

install(TARGETS ccc
RUNTIME DESTINATION "${CMAKE_INSTALL_FULL_BINDIR}"
)
126 changes: 126 additions & 0 deletions ccc/client.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
** Copyright 2022 Centreon
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
**
** For more information : contact@centreon.com
*/

#include "client.hh"
#include <absl/strings/str_format.h>
#include <google/protobuf/dynamic_message.h>
#include <google/protobuf/text_format.h>
#include "broker/core/src/broker.grpc.pb.h"
#include "com/centreon/exceptions/msg_fmt.hh"
#include "engine.grpc.pb.h"

using namespace com::centreon::ccc;

constexpr const char color_method[] = "\u001b[32;1m";
constexpr const char color_message[] = "\u001b[34;1m";
constexpr const char color_reset[] = "\u001b[0m";
client::client(std::shared_ptr<grpc::Channel> channel)
: _stub{std::make_unique<grpc::GenericStub>(channel)}, _server{CCC_NONE} {
const ::google::protobuf::Empty e;
com::centreon::broker::Version broker_v;
com::centreon::engine::Version engine_v;
grpc::ByteBuffer request_buf;

std::array<std::string, 2> service_name{"com.centreon.broker.Broker",
"com.centreon.engine.Engine"};

for (auto& sn : service_name) {
_context = std::make_unique<grpc::ClientContext>();

bool own_buffer = false;
grpc::Status status = grpc::GenericSerialize<grpc::ProtoBufferWriter,
google::protobuf::Message>(
e, &request_buf, &own_buffer);
auto resp = _stub->PrepareUnaryCall(_context.get(),
absl::StrFormat("/%s/GetVersion", sn),
request_buf, &_cq);
resp->StartCall();
grpc::ByteBuffer resp_buf;
resp->Finish(&resp_buf, &status, reinterpret_cast<void*>(1));

google::protobuf::DynamicMessageFactory factory;

void* tag;
bool ok = false;
_cq.Next(&tag, &ok);
grpc::ProtoBufferReader reader(&resp_buf);

if (sn == "com.centreon.broker.Broker") {
if (broker_v.ParseFromZeroCopyStream(&reader)) {
std::string output_str;
google::protobuf::TextFormat::PrintToString(broker_v, &output_str);
if (!output_str.empty()) {
std::cout << "Connected to a Broker "
<< absl::StrFormat("%02d.%02d.%d gRPC server",
broker_v.major(), broker_v.minor(),
broker_v.patch())
<< std::endl;
_server = CCC_BROKER;
break;
}
}
} else if (sn == "com.centreon.engine.Engine") {
if (engine_v.ParseFromZeroCopyStream(&reader)) {
std::string output_str;
google::protobuf::TextFormat::PrintToString(engine_v, &output_str);
if (!output_str.empty()) {
std::cout << "Connected to an Engine "
<< absl::StrFormat("%02d.%02d.%d gRPC server",
engine_v.major(), engine_v.minor(),
engine_v.patch())
<< std::endl;
_server = CCC_ENGINE;
break;
}
}
}
}
if (_server == CCC_NONE)
throw com::centreon::exceptions::msg_fmt(
"Cannot connect to a Broker/Engine gRPC server");
}

std::list<std::string> client::methods() const {
std::list<std::string> retval;
const google::protobuf::DescriptorPool* p =
google::protobuf::DescriptorPool::generated_pool();
const google::protobuf::ServiceDescriptor* service_descriptor;
switch (_server) {
case CCC_BROKER:
service_descriptor = p->FindServiceByName("com.centreon.broker.Broker");
break;
case CCC_ENGINE:
service_descriptor = p->FindServiceByName("com.centreon.engine.Engine");
break;
default:
// Should not occur
assert(1 == 0);
}
size_t size = service_descriptor->method_count();
for (uint32_t i = 0; i < size; i++) {
const google::protobuf::MethodDescriptor* method =
service_descriptor->method(i);
const google::protobuf::Descriptor* input_message = method->input_type();
const google::protobuf::Descriptor* output_message = method->output_type();
retval.emplace_back(absl::StrFormat(
"%s%s%s(%s%s%s) -> %s%s%s", color_method, method->name(), color_reset,
color_message, input_message->name(), color_reset, color_message,
output_message->name(), color_reset));
}
return retval;
}
43 changes: 43 additions & 0 deletions ccc/client.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
** Copyright 2022 Centreon
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
**
** For more information : contact@centreon.com
*/

#ifndef _CCC_CLIENT_HH
#define _CCC_CLIENT_HH
#include <grpcpp/channel.h>
#include <grpcpp/create_channel.h>
#include <grpcpp/generic/generic_stub.h>
#include <list>

namespace com {
namespace centreon {
namespace ccc {
class client {
enum type { CCC_NONE, CCC_BROKER, CCC_ENGINE };
std::unique_ptr<grpc::GenericStub> _stub;
type _server;
grpc::CompletionQueue _cq;
std::unique_ptr<grpc::ClientContext> _context;

public:
client(std::shared_ptr<grpc::Channel> channel);
std::list<std::string> methods() const;
};
} // namespace ccc
} // namespace centreon
} // namespace com
#endif
Loading

0 comments on commit e6f241a

Please sign in to comment.