-
Notifications
You must be signed in to change notification settings - Fork 176
JSON RPC 2.0 via MQTT #252
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6182e97
JSON RPC 2.0 via MQTT
rovo89 9926210
Add a simple "rpc.ping" method
rovo89 7005625
Add RPC method registration
rovo89 542be5a
Address some AI code review comments
rovo89 991766b
Get rid of "data" for RPC errors
rovo89 559262e
Simplify RPC registration
rovo89 d244199
Rename RPC callback parameter
rovo89 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,7 @@ find_package(catkin REQUIRED COMPONENTS | |
| grid_map_core | ||
| grid_map_ros | ||
| grid_map_msgs | ||
| xbot_rpc | ||
| ) | ||
|
|
||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| cmake_minimum_required(VERSION 3.0.2) | ||
| project(xbot_rpc) | ||
|
|
||
| add_compile_options(-std=c++17) | ||
|
|
||
| find_package(catkin REQUIRED COMPONENTS | ||
| std_msgs | ||
| message_generation) | ||
|
|
||
| add_message_files( | ||
| FILES | ||
| RpcRequest.msg | ||
| RpcResponse.msg | ||
| RpcError.msg | ||
| ) | ||
|
|
||
| add_service_files( | ||
| FILES | ||
| RegisterMethodsSrv.srv | ||
| ) | ||
|
|
||
| generate_messages( | ||
| DEPENDENCIES std_msgs | ||
| ) | ||
|
|
||
| catkin_package( | ||
| INCLUDE_DIRS include | ||
| LIBRARIES xbot_rpc | ||
| CATKIN_DEPENDS std_msgs message_runtime | ||
| ) | ||
|
|
||
| add_subdirectory(../json json EXCLUDE_FROM_ALL) | ||
|
|
||
| include_directories( | ||
| ${catkin_INCLUDE_DIRS} | ||
| include | ||
| ) | ||
|
|
||
| add_library(xbot_rpc | ||
| src/provider.cpp | ||
| ) | ||
|
|
||
| target_link_libraries(xbot_rpc nlohmann_json::nlohmann_json) | ||
|
|
||
| add_dependencies(xbot_rpc | ||
| ${PROJECT_NAME}_gencpp # <-- depends on generated C++ message headers | ||
| ${catkin_EXPORTED_TARGETS} | ||
| ) | ||
|
|
||
| install(TARGETS xbot_rpc | ||
| LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION} | ||
| ) | ||
|
|
||
| install(DIRECTORY include/${PROJECT_NAME}/ | ||
| DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION} | ||
| FILES_MATCHING PATTERN "*.h" | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| #pragma once | ||
|
|
||
| namespace xbot_rpc { | ||
|
|
||
| inline constexpr const char* TOPIC_REQUEST = "/xbot/rpc/request"; | ||
| inline constexpr const char* TOPIC_RESPONSE = "/xbot/rpc/response"; | ||
| inline constexpr const char* TOPIC_ERROR = "/xbot/rpc/error"; | ||
| inline constexpr const char* SERVICE_REGISTER_METHODS = "/xbot/rpc/register"; | ||
|
|
||
| } // namespace xbot_rpc |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| #pragma once | ||
|
|
||
| #include <ros/ros.h> | ||
| #include <xbot_rpc/RpcError.h> | ||
| #include <xbot_rpc/RpcRequest.h> | ||
| #include <xbot_rpc/RpcResponse.h> | ||
| #include <xbot_rpc/constants.h> | ||
|
|
||
| #include <nlohmann/json.hpp> | ||
|
|
||
| #define RPC_METHOD(id, body) \ | ||
| { id, [](const std::string& method, const nlohmann::basic_json<>& params) body } | ||
|
|
||
| namespace xbot_rpc { | ||
|
|
||
| typedef std::function<nlohmann::basic_json<>(const std::string& method, const nlohmann::basic_json<>& params)> callback_t; | ||
|
|
||
| class RpcException : public std::exception { | ||
| public: | ||
| const int16_t code; | ||
| const std::string message; | ||
|
|
||
| RpcException(int16_t code, const std::string& message) | ||
| : code(code), message(message) { | ||
| } | ||
|
|
||
| const char* what() const noexcept override { | ||
| return message.c_str(); | ||
| } | ||
| }; | ||
|
|
||
| class RpcProvider { | ||
| private: | ||
| std::string node_id; | ||
| std::map<std::string, callback_t> methods; | ||
|
|
||
| ros::Subscriber request_sub; | ||
| ros::Publisher response_pub; | ||
| ros::Publisher error_pub; | ||
| ros::ServiceClient registration_client; | ||
|
|
||
| void handleRequest(const xbot_rpc::RpcRequest::ConstPtr& request); | ||
| void publishResponse(const xbot_rpc::RpcRequest::ConstPtr& request, const nlohmann::basic_json<>& response); | ||
| void publishError(const xbot_rpc::RpcRequest::ConstPtr& request, int16_t code, const std::string& message); | ||
|
|
||
| public: | ||
| RpcProvider(const std::string& node_id, const std::map<std::string, callback_t>& methods = {}) : node_id(node_id), methods(methods) {} | ||
|
|
||
| void init(); | ||
|
|
||
| void addMethod(const std::string& id, callback_t callback) { | ||
| methods.emplace(id, callback); | ||
| } | ||
|
|
||
| void addMethod(const std::pair<std::string, callback_t>& method) { | ||
| methods.insert(method); | ||
| } | ||
|
|
||
| void publishMethods(); | ||
| }; | ||
|
|
||
| } // namespace xbot_rpc |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| int16 code | ||
| string message | ||
| string id | ||
|
|
||
| int16 ERROR_INVALID_JSON = -32700 # Invalid JSON was received by the server. | ||
| # An error occurred on the server while parsing the JSON text. | ||
| int16 ERROR_INVALID_REQUEST = -32600 # The JSON sent is not a valid Request object. | ||
| int16 ERROR_METHOD_NOT_FOUND = -32601 # The method does not exist / is not available. | ||
| int16 ERROR_INVALID_PARAMS = -32602 # Invalid method parameter(s). | ||
| int16 ERROR_INTERNAL = -32603 # Internal JSON-RPC error. | ||
| int16 ERROR_SERVER_FIRST = -32000 # Reserved for implementation-defined server-errors. | ||
| int16 ERROR_SERVER_LAST = -32099 # Reserved for implementation-defined server-errors. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| string method | ||
| string params | ||
| string id | ||
|
rovo89 marked this conversation as resolved.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| string result | ||
| string id | ||
|
rovo89 marked this conversation as resolved.
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.