Skip to content

Commit

Permalink
Corrections from review
Browse files Browse the repository at this point in the history
Signed-off-by: Javier Gil Aviles <javiergil@eprosima.com>
  • Loading branch information
Javgilavi committed Jan 23, 2025
1 parent 499b96a commit eea063a
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 55 deletions.
8 changes: 4 additions & 4 deletions sustainml_cpp/include/sustainml_cpp/core/RequestReplier.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 Proyectos y Sistemas de Mantenimiento SL (eProsima).
// Copyright 2025 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -54,7 +54,8 @@ class RequestReplier
RequestReplier(
std::function<void(void*)> callback,
const char* topicw,
const char* topicr);
const char* topicr,
void* data);

~RequestReplier();

Expand All @@ -79,6 +80,7 @@ class RequestReplier
std::function<void(void*)> callback_;
const char* topicr_;
const char* topicw_;
void* data_;

eprosima::fastdds::dds::DomainParticipant* participant_;

Expand Down Expand Up @@ -129,8 +131,6 @@ class RequestReplier

RequestReplier* node_;
size_t matched_;
RequestTypeImpl req_;
ResponseTypeImpl res_;

}
listener_;
Expand Down
18 changes: 4 additions & 14 deletions sustainml_cpp/src/cpp/core/NodeImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,14 @@ NodeImpl::NodeImpl(
RequestTypeImpl* in = static_cast<RequestTypeImpl*>(input);
types::RequestType req;
req = in;
std::cout << "Size of request received: " << sizeof(req) << " bytes" << std::endl;
std::cout << "Configuration received: " << static_cast<RequestTypeImpl*>(input)->configuration() << std::endl;
std::cout << "Node ID " << static_cast<int32_t>(common::get_node_id_from_name(name))
<< " receive request for node_id " << req.node_id() << std::endl;

if (req.node_id() == static_cast<int32_t>(common::get_node_id_from_name(name)))
{
std::cout << "Request successfully receive in " << name << std::endl;
std::cout << "Configuring..." << std::endl;
types::ResponseType res;
req_res_listener_.on_configuration_request(req, res);
req_res_->write_res(res.get_impl());
}
}, "sustainml/response", "sustainml/request");
}, "sustainml/response", "sustainml/request", &req_data_);

if (!init(name))
{
Expand All @@ -127,19 +122,14 @@ NodeImpl::NodeImpl(
RequestTypeImpl* in = static_cast<RequestTypeImpl*>(input);
types::RequestType req;
req = in;
std::cout << "Size of request received: " << sizeof(req) << " bytes" << std::endl;
std::cout << "Configuration received: " << static_cast<RequestTypeImpl*>(input)->configuration() << std::endl;
std::cout << "Node ID " << static_cast<int32_t>(common::get_node_id_from_name(name))
<< " receive request for node_id " << req.node_id() << std::endl;

if (req.node_id() == static_cast<int32_t>(common::get_node_id_from_name(name)))
{
std::cout << "Request successfully receive in " << name << std::endl;
std::cout << "Configuring..." << std::endl;
types::ResponseType res;
req_res_listener_.on_configuration_request(req, res);
req_res_->write_res(res.get_impl());
}
}, "sustainml/response", "sustainml/request");
}, "sustainml/response", "sustainml/request", &req_data_);

if (!init(name, opts))
{
Expand Down
2 changes: 2 additions & 0 deletions sustainml_cpp/src/cpp/core/NodeImpl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ class NodeImpl

RequestReplyListener& req_res_listener_;

RequestTypeImpl req_data_;

private:

/**
Expand Down
42 changes: 9 additions & 33 deletions sustainml_cpp/src/cpp/core/RequestReplier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ namespace core {
RequestReplier::RequestReplier(
std::function<void(void*)> callback,
const char* topicw,
const char* topicr)
const char* topicr,
void* data)
: callback_(callback)
, topicw_(topicw)
, topicr_(topicr)
, data_(data)
, participant_(nullptr)
, typeRes_(new ResponseTypeImplPubSubType())
, typeReq_(new RequestTypeImplPubSubType())
Expand All @@ -58,13 +60,13 @@ RequestReplier::RequestReplier(
// Create a Topics
if(std::string(topicw_) == "sustainml/request")
{
topicR_= participant_->create_topic(topicr_, typeRes_.get_type_name(), TOPIC_QOS_DEFAULT);
topicW_= participant_->create_topic(topicw_, typeReq_.get_type_name(), TOPIC_QOS_DEFAULT);
topicR_= participant_->create_topic(topicr_, typeRes_.get_type_name(), TOPIC_QOS_DEFAULT);
topicW_= participant_->create_topic(topicw_, typeReq_.get_type_name(), TOPIC_QOS_DEFAULT);
}
else
{
topicR_= participant_->create_topic(topicr_, typeReq_.get_type_name(), TOPIC_QOS_DEFAULT);
topicW_= participant_->create_topic(topicw_, typeRes_.get_type_name(), TOPIC_QOS_DEFAULT);
topicR_= participant_->create_topic(topicr_, typeReq_.get_type_name(), TOPIC_QOS_DEFAULT);
topicW_= participant_->create_topic(topicw_, typeRes_.get_type_name(), TOPIC_QOS_DEFAULT);
}

// Configure DataReader QoS
Expand Down Expand Up @@ -122,39 +124,13 @@ void RequestReplier::RequestReplyControlListener::on_data_available(
{
// Create a data and SampleInfo instance
SampleInfo info;
void* data;

if (reader->get_topicdescription()->get_type_name() == node_->typeReq_.get_type_name())
{
data = &req_;

}
else if (reader->get_topicdescription()->get_type_name() == node_->typeRes_.get_type_name())
{
data = &res_;
}
else
{
return;
}

// Keep taking data until there is nothing to take
while (reader->take_next_sample(data, &info) == RETCODE_OK)
while (reader->take_next_sample(node_->data_, &info) == RETCODE_OK)
{
if (info.valid_data)
{
std::cout << "Received new data value for topic "
<< reader->get_topicdescription()->get_name()
<< " with type "
<< reader->get_topicdescription()->get_type_name()
<< std::endl;
node_->callback_(data);
}
else
{
std::cout << "Remote writer for topic "
<< reader->get_topicdescription()->get_name()
<< " is dead" << std::endl;
node_->callback_(node_->data_);
}
}
}
Expand Down
6 changes: 2 additions & 4 deletions sustainml_cpp/src/cpp/orchestrator/OrchestratorNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,13 @@ OrchestratorNode::OrchestratorNode(
ResponseTypeImpl* in = static_cast<ResponseTypeImpl*>(input);
types::ResponseType res;
res = in;
std::cout << "Size of response received: " << sizeof(res) << " bytes" << std::endl;
std::cout << "Configuration received: " << static_cast<ResponseTypeImpl*>(input)->configuration() << std::endl;
std::cout << "Response receive response from node_id " << res.node_id() << std::endl;

{
std::lock_guard<std::mutex> lock(this->mtx_);
this->res_ = res;
}
this->cv_.notify_all();
}, "sustainml/request", "sustainml/response");
}, "sustainml/request", "sustainml/response", res_.get_impl());

if (!init())
{
Expand Down

0 comments on commit eea063a

Please sign in to comment.