Skip to content

Commit

Permalink
Merge pull request #15 from ichiro-its/feature/fix-grpc
Browse files Browse the repository at this point in the history
[Sprint 22/23 / PD-405] [Feature] Add implementation GRPC
  • Loading branch information
mbsaloka authored Jun 6, 2024
2 parents 1d6b13f + 2c81127 commit dc1d488
Show file tree
Hide file tree
Showing 18 changed files with 778 additions and 21 deletions.
49 changes: 48 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,20 @@ find_package(shisen_cpp REQUIRED)
find_package(sensor_msgs REQUIRED)
find_package(cv_bridge REQUIRED)

find_package(Protobuf CONFIG REQUIRED)
message(STATUS "Using protobuf ${Protobuf_VERSION}")

# 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}")

add_library(${PROJECT_NAME} SHARED
"src/${PROJECT_NAME}/config/grpc/config.cpp"
"src/${PROJECT_NAME}/config/grpc/call_data_get_color_setting.cpp"
"src/${PROJECT_NAME}/config/grpc/call_data_save_color_setting.cpp"
"src/${PROJECT_NAME}/config/grpc/call_data_set_color_setting.cpp"
"src/${PROJECT_NAME}/config/utils/config.cpp"
"src/${PROJECT_NAME}/detector/color_detector.cpp"
"src/${PROJECT_NAME}/detector/dnn_detector.cpp"
"src/${PROJECT_NAME}/detector/lbp_detector.cpp"
Expand All @@ -33,11 +46,34 @@ add_library(${PROJECT_NAME} SHARED
"src/${PROJECT_NAME}/node/ninshiki_cpp_node.cpp"
"src/${PROJECT_NAME}/utils/utils.cpp")

add_library(${PROJECT_NAME}_exported SHARED
"src/${PROJECT_NAME}/detector/color_detector.cpp"
"src/${PROJECT_NAME}/detector/dnn_detector.cpp"
"src/${PROJECT_NAME}/detector/lbp_detector.cpp"
"src/${PROJECT_NAME}/utils/color.cpp"
"src/${PROJECT_NAME}/utils/contours.cpp"
"src/${PROJECT_NAME}/utils/circle.cpp"
"src/${PROJECT_NAME}/utils/utils.cpp")

target_include_directories(${PROJECT_NAME} PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>)

target_include_directories(${PROJECT_NAME}_exported PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>)

ament_target_dependencies(${PROJECT_NAME}
keisan
ninshiki_interfaces
OpenCV
rclcpp
shisen_cpp
sensor_msgs
cv_bridge
gRPC)

ament_target_dependencies(${PROJECT_NAME}_exported
keisan
ninshiki_interfaces
OpenCV
Expand All @@ -46,6 +82,11 @@ ament_target_dependencies(${PROJECT_NAME}
sensor_msgs
cv_bridge)

target_link_libraries(${PROJECT_NAME}
gRPC::grpc++_reflection
gRPC::grpc++
)

install(DIRECTORY "include" DESTINATION ".")

install(TARGETS ${PROJECT_NAME}
Expand All @@ -54,6 +95,12 @@ install(TARGETS ${PROJECT_NAME}
LIBRARY DESTINATION "lib"
RUNTIME DESTINATION "bin")

install(TARGETS ${PROJECT_NAME}_exported
EXPORT export_${PROJECT_NAME}_exported
ARCHIVE DESTINATION "lib"
LIBRARY DESTINATION "lib"
RUNTIME DESTINATION "bin")

add_executable(detector "src/ninshiki_cpp_detector.cpp")
target_include_directories(detector PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
Expand All @@ -78,5 +125,5 @@ ament_export_dependencies(
sensor_msgs
cv_bridge)
ament_export_include_directories("include")
ament_export_libraries(${PROJECT_NAME})
ament_export_libraries(${PROJECT_NAME}_exported)
ament_package()
87 changes: 87 additions & 0 deletions include/ninshiki_cpp/config/grpc/call_data.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Copyright (c) 2024 Ichiro ITS
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#ifndef __NINSHIKI_CPP__CONFIG__GRPC__CALL_DATA_HPP__
#define __NINSHIKI_CPP__CONFIG__GRPC__CALL_DATA_HPP__

#include "ninshiki_cpp/config/grpc/call_data_base.hpp"
#include "ninshiki_interfaces/ninshiki.grpc.pb.h"
#include "ninshiki_interfaces/ninshiki.pb.h"
#include "grpc/support/log.h"
#include "grpcpp/grpcpp.h"

namespace ninshiki_cpp
{
template <class ConfigRequest, class ConfigReply>
class CallData : public CallDataBase
{
public:
CallData(
ninshiki_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq,
const std::string & path);

void Proceed();

protected:
virtual void AddNextToCompletionQueue() = 0;

enum CallStatus { CREATE, PROCESS, FINISH };

CallStatus status_; // The current serving state.

ninshiki_interfaces::proto::Config::AsyncService * service_;

const std::string path_;

grpc::ServerCompletionQueue * cq_;
grpc::ServerContext ctx_;
ConfigRequest request_;
ConfigReply reply_;
grpc::ServerAsyncResponseWriter<ConfigReply> responder_;
};

template <class ConfigRequest, class ConfigReply>
CallData<ConfigRequest, ConfigReply>::CallData(
ninshiki_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq,
const std::string & path)
: status_(CREATE), service_(service), cq_(cq), responder_(&ctx_), path_(path)
{
}

template <class ConfigRequest, class ConfigReply>
void CallData<ConfigRequest, ConfigReply>::Proceed()
{
if (status_ == CREATE) {
status_ = PROCESS;
WaitForRequest();
} else if (status_ == PROCESS) {
AddNextToCompletionQueue();
HandleRequest();
status_ = FINISH;
responder_.Finish(reply_, grpc::Status::OK, this);
} else {
GPR_ASSERT(status_ == FINISH);
delete this;
}
}

} // namespace ninshiki_cpp

#endif // __NINSHIKI_CPP__CONFIG__GRPC__CALL_DATA_HPP__
37 changes: 37 additions & 0 deletions include/ninshiki_cpp/config/grpc/call_data_base.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (c) 2024 Ichiro ITS
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#ifndef NINSHIKI_CPP__CONFIG__GRPC__CALL_DATA_BASE_HPP_
#define NINSHIKI_CPP__CONFIG__GRPC__CALL_DATA_BASE_HPP_

namespace ninshiki_cpp
{
class CallDataBase
{
public:
virtual void Proceed() = 0;

protected:
virtual void WaitForRequest() = 0;
virtual void HandleRequest() = 0;
};
} // namespace ninshiki_cpp

#endif // NINSHIKI_CPP__CONFIG__GRPC__CALL_DATA_BASE_HPP_
43 changes: 43 additions & 0 deletions include/ninshiki_cpp/config/grpc/call_data_get_color_setting.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (c) 2024 Ichiro ITS
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#ifndef NINSHIKI_CPP__CONFIG__GRPC__CALL_DATA_GET_COLOR_SETTING_HPP__
#define NINSHIKI_CPP__CONFIG__GRPC__CALL_DATA_GET_COLOR_SETTING_HPP__

#include <ninshiki_cpp/config/grpc/call_data.hpp>

namespace ninshiki_cpp
{
class CallDataGetColorSetting
: CallData<ninshiki_interfaces::proto::Empty, ninshiki_interfaces::proto::ConfigColor>
{
public:
CallDataGetColorSetting(
ninshiki_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq,
const std::string & path);

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

#endif // NINSHIKI_CPP__CONFIG__GRPC__CALL_DATA_GET_COLOR_SETTING_HPP__
45 changes: 45 additions & 0 deletions include/ninshiki_cpp/config/grpc/call_data_save_color_setting.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (c) 2024 Ichiro ITS
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#ifndef NINSHIKI_CPP__CONFIG__GRPC__CALL_DATA_SAVE_COLOR_SETTING_HPP__
#define NINSHIKI_CPP__CONFIG__GRPC__CALL_DATA_SAVE_COLOR_SETTING_HPP__

#include <ninshiki_cpp/config/grpc/call_data.hpp>
#include <rclcpp/rclcpp.hpp>

namespace ninshiki_cpp
{
class CallDataSaveColorSetting
: CallData<ninshiki_interfaces::proto::ConfigColor, ninshiki_interfaces::proto::Empty>
{
public:
CallDataSaveColorSetting(
ninshiki_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq,
const std::string & path);

protected:
void AddNextToCompletionQueue() override;
void WaitForRequest() override;
void HandleRequest() override;
};

} // namespace ninshiki_cpp

#endif // NINSHIKI_CPP__CONFIG__GRPC__CALL_DATA_SAVE_COLOR_SETTING_HPP__
48 changes: 48 additions & 0 deletions include/ninshiki_cpp/config/grpc/call_data_set_color_setting.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (c) 2024 Ichiro ITS
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#ifndef NINSHIKI_CPP_CONFIG__GRPC__CALL_DATA_SET_COLOR_SETTING_HPP__
#define NINSHIKI_CPP_CONFIG__GRPC__CALL_DATA_SET_COLOR_SETTING_HPP__

#include <rclcpp/rclcpp.hpp>
#include <ninshiki_cpp/config/grpc/call_data.hpp>
#include <ninshiki_cpp/detector/color_detector.hpp>
#include <ninshiki_cpp/utils/color.hpp>

namespace ninshiki_cpp
{
class CallDataSetColorSetting
: CallData<ninshiki_interfaces::proto::ColorSetting, ninshiki_interfaces::proto::Empty>
{
public:
CallDataSetColorSetting(
ninshiki_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq,
const std::string & path, std::shared_ptr<ninshiki_cpp::detector::ColorDetector> color_detection);
using ColorDetector = ninshiki_cpp::detector::ColorDetector;

protected:
void AddNextToCompletionQueue() override;
void WaitForRequest() override;
void HandleRequest() override;
std::shared_ptr<ColorDetector> color_detection_;
};
} // namespace ninshiki_cpp

#endif // NINSHIKI_CPP__CONFIG__GRPC__CALL_DATA_SET_COLOR_SETTING_HPP__
Loading

0 comments on commit dc1d488

Please sign in to comment.