Skip to content

Commit

Permalink
Merge pull request #25 from ichiro-its/feature/using-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 9d986f9 + e50302a commit d7c8ecb
Show file tree
Hide file tree
Showing 30 changed files with 921 additions and 50 deletions.
73 changes: 67 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,92 @@ find_package(rclcpp REQUIRED)
find_package(shisen_interfaces REQUIRED)
find_package(sensor_msgs REQUIRED)
find_package(std_msgs REQUIRED)
find_package(Protobuf CONFIG REQUIRED)
message(STATUS "Using protobuf ${Protobuf_VERSION}")

install(DIRECTORY "include" DESTINATION ".")
# 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}/camera/node/camera_node.cpp"
"src/${PROJECT_NAME}/camera/provider/camera_config_provider.cpp"
"src/${PROJECT_NAME}/camera/provider/image_provider.cpp"
"src/${PROJECT_NAME}/config/grpc/config.cpp"
"src/${PROJECT_NAME}/config/grpc/call_data_get_capture_setting.cpp"
"src/${PROJECT_NAME}/config/grpc/call_data_get_image.cpp"
"src/${PROJECT_NAME}/config/grpc/call_data_save_capture_setting.cpp"
"src/${PROJECT_NAME}/config/grpc/call_data_set_capture_setting.cpp"
"src/${PROJECT_NAME}/config/utils/config.cpp"
"src/${PROJECT_NAME}/utility/capture_setting.cpp"
"src/${PROJECT_NAME}/utility/interface.cpp"
"src/${PROJECT_NAME}/node/shisen_cpp_node.cpp"
"src/${PROJECT_NAME}/viewer/consumer/image_consumer.cpp"
"src/${PROJECT_NAME}/viewer/node/viewer_node.cpp")
"src/${PROJECT_NAME}/viewer/node/viewer_node.cpp"
)

add_library(${PROJECT_NAME}_exported SHARED
"src/${PROJECT_NAME}/camera/node/camera_node.cpp"
"src/${PROJECT_NAME}/camera/provider/camera_config_provider.cpp"
"src/${PROJECT_NAME}/camera/provider/image_provider.cpp"
"src/${PROJECT_NAME}/utility/capture_setting.cpp"
"src/${PROJECT_NAME}/utility/interface.cpp"
"src/${PROJECT_NAME}/viewer/consumer/image_consumer.cpp"
"src/${PROJECT_NAME}/viewer/node/viewer_node.cpp"
)

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

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

ament_target_dependencies(${PROJECT_NAME}
keisan OpenCV rclcpp shisen_interfaces sensor_msgs cv_bridge std_msgs)
keisan
OpenCV
rclcpp
shisen_interfaces
sensor_msgs
cv_bridge
std_msgs
gRPC)

ament_target_dependencies(${PROJECT_NAME}_exported
keisan
OpenCV
rclcpp
shisen_interfaces
sensor_msgs
cv_bridge
std_msgs)

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

install(DIRECTORY "include" DESTINATION ".")

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

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

target_compile_options(${PROJECT_NAME} PRIVATE -fPIC)

add_executable(camera "src/shisen_cpp_main.cpp")
target_include_directories(camera PUBLIC
Expand Down Expand Up @@ -83,6 +144,6 @@ endif()

ament_export_dependencies(keisan OpenCV rclcpp shisen_interfaces sensor_msgs cv_bridge std_msgs)
ament_export_include_directories("include")
ament_export_libraries(${PROJECT_NAME})
ament_export_libraries(${PROJECT_NAME}_exported)

ament_package()
8 changes: 3 additions & 5 deletions include/shisen_cpp/camera/node/camera_node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,13 @@
#define SHISEN_CPP__CAMERA__NODE__CAMERA_NODE_HPP_

#include <rclcpp/rclcpp.hpp>
#include <shisen_cpp/camera/provider/camera_config_provider.hpp>
#include <shisen_cpp/camera/provider/image_provider.hpp>
#include <shisen_cpp/utility.hpp>

#include <memory>
#include <string>

#include "shisen_cpp/camera/provider/camera_config_provider.hpp"
#include "shisen_cpp/camera/provider/image_provider.hpp"

#include "shisen_cpp/utility.hpp"

namespace shisen_cpp::camera
{

Expand Down
5 changes: 2 additions & 3 deletions include/shisen_cpp/camera/provider/camera_config_provider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,12 @@
#define SHISEN_CPP__CAMERA__PROVIDER__CAMERA_CONFIG_PROVIDER_HPP_

#include <keisan/keisan.hpp>
#include <shisen_cpp/utility/options.hpp>
#include <shisen_interfaces/msg/camera_config.hpp>

#include <memory>
#include <string>

#include "shisen_cpp/utility/options.hpp"
#include "shisen_interfaces/msg/camera_config.hpp"

namespace shisen_cpp::camera
{

Expand Down
9 changes: 4 additions & 5 deletions include/shisen_cpp/camera/provider/image_provider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,13 @@
#include <cv_bridge/cv_bridge.hpp>
#include <opencv2/core.hpp>
#include <opencv2/videoio.hpp>
#include <sensor_msgs/msg/image.hpp>
#include <shisen_cpp/utility.hpp>
#include <std_msgs/msg/header.hpp>

#include <memory>
#include <string>

#include "sensor_msgs/msg/image.hpp"
#include "std_msgs/msg/header.hpp"

#include "shisen_cpp/utility.hpp"

namespace shisen_cpp::camera
{

Expand Down
93 changes: 93 additions & 0 deletions include/shisen_cpp/config/grpc/call_data.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// 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 __SHISEN_CPP__CONFIG__GRPC__CALL_DATA_HPP__
#define __SHISEN_CPP__CONFIG__GRPC__CALL_DATA_HPP__

#include <shisen_cpp/config/grpc/call_data_base.hpp>
#include <shisen_interfaces/shisen.grpc.pb.h>
#include <shisen_interfaces/shisen.pb.h>

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

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

void Proceed() override;

protected:
virtual void AddNextToCompletionQueue() = 0;

CallStatus status_; // The current serving state.

shisen_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(
shisen_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq,
const std::string & path)
: status_(CallStatus::CREATE), service_(service), cq_(cq), responder_(&ctx_), path_(path)
{
}

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

} // namespace shisen_cpp

#endif // __SHISEN_CPP__CONFIG__GRPC__CALL_DATA_HPP__
37 changes: 37 additions & 0 deletions include/shisen_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 SHISEN_CPP__CONFIG__GRPC__CALL_DATA_BASE_HPP_
#define SHISEN_CPP__CONFIG__GRPC__CALL_DATA_BASE_HPP_

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

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

#endif // SHISEN_CPP__CONFIG__GRPC__CALL_DATA_BASE_HPP_
43 changes: 43 additions & 0 deletions include/shisen_cpp/config/grpc/call_data_get_capture_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 SHISEN_CPP__CONFIG__GRPC__CALL_DATA_GET_CAPTURE_SETTING_HPP__
#define SHISEN_CPP__CONFIG__GRPC__CALL_DATA_GET_CAPTURE_SETTING_HPP__

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

namespace shisen_cpp
{
class CallDataGetCaptureSetting
: CallData<shisen_interfaces::proto::Empty, shisen_interfaces::proto::ConfigCapture>
{
public:
CallDataGetCaptureSetting(
shisen_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq,
const std::string & path);

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

#endif // SHISEN_CPP__CONFIG__GRPC__CALL_DATA_GET_CAPTURE_SETTING_HPP__
45 changes: 45 additions & 0 deletions include/shisen_cpp/config/grpc/call_data_get_image.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 SHISEN_CPP__CONFIG__GRPC__CALL_DATA_GET_IMAGE_HPP__
#define SHISEN_CPP__CONFIG__GRPC__CALL_DATA_GET_IMAGE_HPP__

#include <shisen_cpp/camera/node/camera_node.hpp>
#include <shisen_cpp/config/grpc/call_data.hpp>

namespace shisen_cpp
{
class CallDataGetImage
: CallData<shisen_interfaces::proto::Empty, shisen_interfaces::proto::Image>
{
public:
CallDataGetImage(
shisen_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq,
const std::string & path, const std::shared_ptr<camera::CameraNode>& camera_node);

protected:
void AddNextToCompletionQueue() override;
void WaitForRequest() override;
void HandleRequest() override;
std::shared_ptr<camera::CameraNode> camera_node_;
};
} // namespace shisen_cpp

#endif // SHISEN_CPP__CONFIG__GRPC__CALL_DATA_GET_IMAGE_HPP__
Loading

0 comments on commit d7c8ecb

Please sign in to comment.