Skip to content

Commit

Permalink
feat: refractor and resolve pull request
Browse files Browse the repository at this point in the history
  • Loading branch information
marfanr committed Dec 26, 2023
1 parent 0f771fe commit f04416b
Show file tree
Hide file tree
Showing 17 changed files with 60 additions and 128 deletions.
21 changes: 5 additions & 16 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ endif()
if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 17)
endif()
set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC" )

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wall -Wextra -Wpedantic -fPIC)
Expand All @@ -27,25 +26,13 @@ set(protobuf_MODULE_COMPATIBLE TRUE)
find_package(Protobuf CONFIG REQUIRED)
message(STATUS "Using protobuf ${Protobuf_VERSION}")

set(_PROTOBUF_LIBPROTOBUF protobuf::libprotobuf)
set(_REFLECTION gRPC::grpc++_reflection)
if(CMAKE_CROSSCOMPILING)
find_program(_PROTOBUF_PROTOC protoc)
else()
set(_PROTOBUF_PROTOC $<TARGET_FILE:protobuf::protoc>)
endif()

# Find gRPC installation
# Looks for gRPCConfig.cmake file installed by gRPC's cmake installation.
find_package(gRPC CONFIG REQUIRED)
message(STATUS "Using gRPC ${gRPC_VERSION}")

set(_GRPC_GRPCPP gRPC::grpc++)
if(CMAKE_CROSSCOMPILING)
find_program(_GRPC_CPP_PLUGIN_EXECUTABLE grpc_cpp_plugin)
else()
set(_GRPC_CPP_PLUGIN_EXECUTABLE $<TARGET_FILE:gRPC::grpc_cpp_plugin>)
endif()
set(_GRPC_CPP_PLUGIN_EXECUTABLE $<TARGET_FILE:gRPC::grpc_cpp_plugin>)

# Proto file
get_filename_component(akushon_proto "../akushon_interfaces/proto/akushon.proto" ABSOLUTE)
Expand Down Expand Up @@ -104,9 +91,9 @@ ament_target_dependencies(${PROJECT_NAME}
gRPC)

target_link_libraries(${PROJECT_NAME}
${_REFLECTION}
gRPC::grpc++_reflection
${_GRPC_GRPCPP}
${_PROTOBUF_LIBPROTOBUF}
protobuf::libprotobuf
)

install(DIRECTORY "include" DESTINATION ".")
Expand All @@ -118,6 +105,8 @@ install(TARGETS ${PROJECT_NAME}
RUNTIME DESTINATION "bin"
INCLUDES DESTINATION "include")

target_compile_options(${PROJECT_NAME} PRIVATE -fPIC)

add_executable(action "src/action_main.cpp")
target_include_directories(action PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
Expand Down
24 changes: 19 additions & 5 deletions include/akushon/config/grpc/call_data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,34 @@ class CallData : CallDataBase
public:
CallData(
akushon_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq,
const std::string path);

virtual void Proceed() override;
const std::string& path) : status_(CallStatus::CREATE), service_(service), cq_(cq), responder_(&ctx_), path_(path)
{
}
void Proceed() override {
if (status_ == CallStatus::CREATE) {
status_ = CallStatus::PROCESS;
WaitForRequest();
} else if (status_ == CallStatus::PROCESS) {
AddNextToCompletionQueue();
HandleRequest();
status_ = CallStatus::FINISH;
responder_.Finish(reply_, grpc::Status::OK, this);
} else {
GPR_ASSERT(status_ == CallStatus::FINISH);
delete this;
}
}

protected:
virtual void AddNextToCompletionQueue() = 0;

enum CallStatus { CREATE, PROCESS, FINISH };
enum class CallStatus { CREATE, PROCESS, FINISH };

CallStatus status_; // The current serving state.

akushon_interfaces::proto::Config::AsyncService * service_;

const std::string path_;
const std::string& path_;

grpc::ServerCompletionQueue * cq_;
grpc::ServerContext ctx_;
Expand Down
8 changes: 4 additions & 4 deletions include/akushon/config/grpc/call_data_get_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ class CallDataGetConfig
public:
CallDataGetConfig(
akushon_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq,
const std::string path);
const std::string& path);

protected:
virtual void AddNextToCompletionQueue() override;
virtual void WaitForRequest() override;
virtual void HandleRequest() override;
void AddNextToCompletionQueue() override;
void WaitForRequest() override;
void HandleRequest() override;
};
} // namespace akushon

Expand Down
8 changes: 4 additions & 4 deletions include/akushon/config/grpc/call_data_publish_set_joints.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ class CallDataPublishSetJoints
public:
CallDataPublishSetJoints(
akushon_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq,
const std::string path, rclcpp::Node::SharedPtr node);
const std::string& path, rclcpp::Node::SharedPtr& node);

protected:
virtual void AddNextToCompletionQueue() override;
virtual void WaitForRequest() override;
virtual void HandleRequest() override;
void AddNextToCompletionQueue() override;
void WaitForRequest() override;
void HandleRequest() override;
rclcpp::Node::SharedPtr node_;
rclcpp::Publisher<tachimawari_interfaces::msg::SetJoints>::SharedPtr set_joints_publisher_;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class CallDataPublishSetTorques
public:
CallDataPublishSetTorques(
akushon_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq,
const std::string path, rclcpp::Node::SharedPtr node);
const std::string& path, rclcpp::Node::SharedPtr& node);

protected:
virtual void AddNextToCompletionQueue() override;
Expand Down
8 changes: 4 additions & 4 deletions include/akushon/config/grpc/call_data_run_action.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ class CallDataRunAction
public:
CallDataRunAction(
akushon_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq,
const std::string path, rclcpp::Node::SharedPtr node);
const std::string& path, rclcpp::Node::SharedPtr& node);

protected:
virtual void AddNextToCompletionQueue() override;
virtual void WaitForRequest() override;
virtual void HandleRequest() override;
void AddNextToCompletionQueue() override;
void WaitForRequest() override;
void HandleRequest() override;
rclcpp::Node::SharedPtr node_;
rclcpp::Publisher<akushon_interfaces::msg::RunAction>::SharedPtr run_action_publisher_;
};
Expand Down
8 changes: 4 additions & 4 deletions include/akushon/config/grpc/call_data_save_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ class CallDataSaveConfig
public:
CallDataSaveConfig(
akushon_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq,
const std::string path);
const std::string& path);

protected:
virtual void AddNextToCompletionQueue() override;
virtual void WaitForRequest() override;
virtual void HandleRequest() override;
void AddNextToCompletionQueue() override;
void WaitForRequest() override;
void HandleRequest() override;
};
} // namespace akushon

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ class CallDataSubscribeCurrentJoints
public:
CallDataSubscribeCurrentJoints(
akushon_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq,
const std::string path, rclcpp::Node::SharedPtr node);
const std::string& path, rclcpp::Node::SharedPtr& node);

protected:
virtual void AddNextToCompletionQueue() override;
virtual void WaitForRequest() override;
virtual void HandleRequest() override;
void AddNextToCompletionQueue() override;
void WaitForRequest() override;
void HandleRequest() override;
rclcpp::Node::SharedPtr node_;
rclcpp::Subscription<tachimawari_interfaces::msg::CurrentJoints>::SharedPtr
current_joint_subscription_;
Expand Down
5 changes: 2 additions & 3 deletions include/akushon/config/grpc/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,10 @@ class ConfigGrpc

~ConfigGrpc();

void Run(uint16_t port, const std::string path, rclcpp::Node::SharedPtr node);
void Run(uint16_t port, const std::string& path, rclcpp::Node::SharedPtr& node);

private:
std::string path;
static void SignIntHandler(int signum);
std::string path;

static inline std::unique_ptr<grpc::ServerCompletionQueue> cq_;
static inline std::unique_ptr<grpc::Server> server_;
Expand Down
58 changes: 0 additions & 58 deletions src/akushon/config/grpc/call_data.cpp

This file was deleted.

2 changes: 1 addition & 1 deletion src/akushon/config/grpc/call_data_get_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace akushon

CallDataGetConfig::CallDataGetConfig(
akushon_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq,
const std::string path)
const std::string& path)
: CallData(service, cq, path)
{
Proceed();
Expand Down
4 changes: 1 addition & 3 deletions src/akushon/config/grpc/call_data_publish_set_joints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace akushon
{
CallDataPublishSetJoints::CallDataPublishSetJoints(
akushon_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq,
const std::string path, rclcpp::Node::SharedPtr node)
const std::string& path, rclcpp::Node::SharedPtr& node)
: CallData(service, cq, path), node_(node)
{
set_joints_publisher_ =
Expand Down Expand Up @@ -62,8 +62,6 @@ void CallDataPublishSetJoints::HandleRequest()
set_joints_publisher_->publish(set_joints_);

RCLCPP_INFO(rclcpp::get_logger("PublishSetJoints"), "set joints has been published!");
} catch (std::ofstream::failure f) {
RCLCPP_ERROR(rclcpp::get_logger("PublishSetJoints"), f.what());
} catch (nlohmann::json::exception e) {
RCLCPP_ERROR(rclcpp::get_logger("PublishSetJoints"), e.what());
}
Expand Down
5 changes: 1 addition & 4 deletions src/akushon/config/grpc/call_data_publish_set_torques.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace akushon
{
CallDataPublishSetTorques::CallDataPublishSetTorques(
akushon_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq,
const std::string path, rclcpp::Node::SharedPtr node)
const std::string& path, rclcpp::Node::SharedPtr& node)
: CallData(service, cq, path), node_(node)
{
set_torque_publisher_ =
Expand Down Expand Up @@ -59,9 +59,6 @@ void CallDataPublishSetTorques::HandleRequest()
set_torque_publisher_->publish(set_torque);
RCLCPP_INFO(rclcpp::get_logger("PublishSetTorques"), "set torques has been published!");

} catch (std::ofstream::failure f) {
RCLCPP_ERROR(rclcpp::get_logger("PublishSetTorques"), f.what());

} catch (nlohmann::json::exception e) {
RCLCPP_ERROR(rclcpp::get_logger("PublishSetTorques"), e.what());
}
Expand Down
2 changes: 1 addition & 1 deletion src/akushon/config/grpc/call_data_run_action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace akushon
{
CallDataRunAction::CallDataRunAction(
akushon_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq,
const std::string path, rclcpp::Node::SharedPtr node)
const std::string& path, rclcpp::Node::SharedPtr& node)
: CallData(service, cq, path), node_(node)
{
run_action_publisher_ =
Expand Down
4 changes: 1 addition & 3 deletions src/akushon/config/grpc/call_data_save_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace akushon

CallDataSaveConfig::CallDataSaveConfig(
akushon_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq,
const std::string path)
const std::string& path)
: CallData(service, cq, path)
{
Proceed();
Expand All @@ -51,8 +51,6 @@ void CallDataSaveConfig::HandleRequest()
nlohmann::json akushon_data = nlohmann::json::parse(request_.json_actions());
config.save_config(akushon_data);
RCLCPP_INFO(rclcpp::get_logger("SaveConfig"), "config has been saved!");
} catch (std::ofstream::failure f) {
RCLCPP_ERROR(rclcpp::get_logger("SaveConfig"), f.what());
} catch (nlohmann::json::exception e) {
RCLCPP_ERROR(rclcpp::get_logger("SaveConfig"), e.what());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace akushon
{
CallDataSubscribeCurrentJoints::CallDataSubscribeCurrentJoints(
akushon_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq,
const std::string path, rclcpp::Node::SharedPtr node)
const std::string& path, rclcpp::Node::SharedPtr& node)
: CallData(service, cq, path), node_(node)
{
Proceed();
Expand Down Expand Up @@ -66,8 +66,6 @@ void CallDataSubscribeCurrentJoints::HandleRequest()
reply_.set_msg_joints(curr_joints.dump());
RCLCPP_INFO(rclcpp::get_logger("PublishSetTorques"), "curr joints has been sended!");
current_joint_subscription_.reset();
} catch (std::ofstream::failure f) {
RCLCPP_ERROR(rclcpp::get_logger("PublishSetTorques"), f.what());
} catch (nlohmann::json::exception e) {
RCLCPP_ERROR(rclcpp::get_logger("PublishSetTorques"), e.what());
}
Expand Down
17 changes: 7 additions & 10 deletions src/akushon/config/grpc/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,7 @@ ConfigGrpc::~ConfigGrpc()
cq_->Shutdown();
}

void ConfigGrpc::SignIntHandler(int signum)
{
server_->Shutdown();
cq_->Shutdown();
exit(signum);
}

void ConfigGrpc::Run(uint16_t port, const std::string path, rclcpp::Node::SharedPtr node)
void ConfigGrpc::Run(uint16_t port, const std::string& path, rclcpp::Node::SharedPtr& node)
{
std::string server_address = absl::StrFormat("0.0.0.0:%d", port);

Expand All @@ -67,8 +60,12 @@ void ConfigGrpc::Run(uint16_t port, const std::string path, rclcpp::Node::Shared
server_ = builder.BuildAndStart();
std::cout << "Server listening on " << server_address << std::endl;

signal(SIGINT, SignIntHandler);
async_server = std::thread([&path, this, &node]() {
std::signal(SIGINT, [](int signum) {
server_->Shutdown();
cq_->Shutdown();
exit(signum);
});
async_server = std::thread([path, this, &node]() {
new CallDataGetConfig(&service_, cq_.get(), path);
new CallDataSaveConfig(&service_, cq_.get(), path);
new CallDataPublishSetJoints(&service_, cq_.get(), path, node);
Expand Down

0 comments on commit f04416b

Please sign in to comment.