Skip to content

Commit

Permalink
feat: add config for grpc port on akushon
Browse files Browse the repository at this point in the history
  • Loading branch information
marfanr committed Jun 16, 2024
1 parent 26a6923 commit 8c00775
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 10 deletions.
3 changes: 2 additions & 1 deletion include/akushon/config/grpc/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ class ConfigGrpc

~ConfigGrpc();

void Run(uint16_t port, const std::string & path, rclcpp::Node::SharedPtr & node,
void Run(
const std::string & path, rclcpp::Node::SharedPtr & node,
const std::shared_ptr<akushon::ActionManager> & action_manager);

private:
Expand Down
3 changes: 3 additions & 0 deletions include/akushon/config/utils/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include <fstream>
#include <string>
#include "nlohmann/json.hpp"

namespace akushon
{
Expand All @@ -35,6 +36,8 @@ class Config
std::string get_config() const;
void save_config(const std::string & actions_data);

nlohmann::json get_grpc_config() const;

private:
std::string path;
};
Expand Down
4 changes: 4 additions & 0 deletions src/akushon/action/node/action_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ void ActionManager::load_config(const std::string & path)
}

try {
if (name == "grpc") {
printf("skipping grpc.json\n");
continue;
}
std::ifstream file(file_name);
nlohmann::json action_data = nlohmann::json::parse(file);

Expand Down
18 changes: 11 additions & 7 deletions src/akushon/config/grpc/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@
#include <future>
#include <string>

#include "akushon/config/utils/config.hpp"
#include "rclcpp/rclcpp.hpp"
#include "akushon/config/grpc/call_data_get_config.hpp"
#include "akushon/config/grpc/call_data_load_config.hpp"
#include "akushon/config/grpc/call_data_save_config.hpp"
#include "akushon/config/grpc/call_data_publish_set_joints.hpp"
#include "akushon/config/grpc/call_data_publish_set_torques.hpp"
#include "akushon/config/grpc/call_data_subscribe_current_joints.hpp"
#include "akushon/config/grpc/call_data_run_action.hpp"
#include "akushon/config/grpc/call_data_save_config.hpp"
#include "akushon/config/grpc/call_data_subscribe_current_joints.hpp"
#include "akushon/config/utils/config.hpp"
#include "rclcpp/rclcpp.hpp"

using grpc::ServerBuilder;
using namespace std::chrono_literals;
Expand All @@ -49,10 +49,13 @@ ConfigGrpc::~ConfigGrpc()
cq_->Shutdown();
}

void ConfigGrpc::Run(uint16_t port, const std::string& path, rclcpp::Node::SharedPtr& node,
const std::shared_ptr<ActionManager>& action_manager)
void ConfigGrpc::Run(
const std::string & path, rclcpp::Node::SharedPtr & node,
const std::shared_ptr<ActionManager> & action_manager)
{
std::string server_address = absl::StrFormat("0.0.0.0:%d", port);
Config config(path);
std::string server_address =
absl::StrFormat("0.0.0.0:%d", config.get_grpc_config()["port"].get<uint16_t>());

ServerBuilder builder;
builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
Expand Down Expand Up @@ -85,6 +88,7 @@ void ConfigGrpc::Run(uint16_t port, const std::string& path, rclcpp::Node::Share
}
});
std::this_thread::sleep_for(200ms);
async_server.detach();
}

} // namespace akushon
2 changes: 1 addition & 1 deletion src/akushon/config/node/config_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ ConfigNode::ConfigNode(rclcpp::Node::SharedPtr node, const std::string & path,
response->status = "SAVED";
}
);
config_grpc.Run(5060, path, node, action_manager);
config_grpc.Run(path, node, action_manager);
RCLCPP_INFO(rclcpp::get_logger("GrpcServers"), "grpc running");
}

Expand Down
12 changes: 11 additions & 1 deletion src/akushon/config/utils/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

#include "akushon/action/model/action_name.hpp"
#include "akushon/config/utils/config.hpp"
#include "nlohmann/json.hpp"

namespace akushon
{
Expand All @@ -47,6 +46,9 @@ std::string Config::get_config() const
for (auto i = path.size(); i < file_name.size() - 5; i++) {
action_name += file_name[i];
}
if (action_name == "grpc") {
continue;
}
std::cout << action_name << " | ";

std::ifstream file(action_file.path());
Expand All @@ -61,6 +63,14 @@ std::string Config::get_config() const
return actions_list.dump();
}

nlohmann::json Config::get_grpc_config() const
{
std::ifstream grpc_file(this->path + "grpc.json");
nlohmann::json grpc_data = nlohmann::json::parse(grpc_file);
grpc_file.close();
return grpc_data;
}

void Config::save_config(const std::string & actions_data)
{
nlohmann::json actions_list = nlohmann::json::parse(actions_data);
Expand Down

0 comments on commit 8c00775

Please sign in to comment.