Commit 8d961484 authored by Georg Vinogradov's avatar Georg Vinogradov

Adapt callback/generator classes to use proper c++ code

parent 6139193d
Pipeline #165531 passed with stages
in 2 minutes and 56 seconds
......@@ -20,7 +20,7 @@ void <@m.mwIdent/>Adapter_${model.getEscapedCompName()}::init(${model.getEscaped
// Intitialize callbacks, subscribers and publishers
<#list model.getIncomingPorts() as sub>
_sub_${sub.getName()} = new client(SERVER_ADDRESS, ${sub.getName()});
_callback_${sub.getName()} = new Callback(*_sub_${sub.getName()}, comp, component->getIncomingPorts().get(component->getIncomingPorts().indexOf(${sub.getName()})));
_callback_${sub.getName()} = new Callback(*_sub_${sub.getName()}, component->${sub.getName()});
</#list>
<#list model.getOutgoingPorts() as pub>
......
......@@ -2,7 +2,7 @@
#include "Callback.hpp"
Callback::Callback(client& cli, ${model.getEscapedCompName()}* comp, EMAPortInstanceSymbol port) : callback(), cli_(cli)
Callback::Callback(client& cli, double port) : callback(), cli_(cli)
{
comp_ = comp;
port_ = port;
......@@ -28,5 +28,5 @@ void Callback::message_arrived(const_message_ptr msg)
cout << "Message received "<< msg->get_topic() << ": " << msg->get_payload_str() << endl;
string::size_type sz;
double value = std::stod (msg->get_payload_str(),&sz);
comp_->port_.toString() = value;
port_ = value;
}
......@@ -13,7 +13,7 @@ class Callback : public virtual callback
client& cli_;
public:
Callback(client& cli, ${model.getEscapedCompName()}* comp, EMAPortInstanceSymbol port);
Callback(client& cli, double port);
void connected(const string& cause);
......@@ -23,8 +23,7 @@ public:
private:
${model.getEscapedCompName()}* comp_;
EMAPortInstanceSymbol port_;
double port_;
};
#endif /* Callback_hpp */
......@@ -32,7 +32,9 @@ public class AdapterGenerationTest extends AbstractSymtabTest {
// Connect component's ports to topics
componentInstanceSymbol.getPortInstance("portA").orElse(null).setMiddlewareSymbol(new MqttConnectionSymbol("/clock"));
componentInstanceSymbol.getPortInstance("portB").orElse(null).setMiddlewareSymbol(new MqttConnectionSymbol("/clock"));
componentInstanceSymbol.getPortInstance("portC").orElse(null).setMiddlewareSymbol(new MqttConnectionSymbol("/clock"));
componentInstanceSymbol.getPortInstance("portD").orElse(null).setMiddlewareSymbol(new MqttConnectionSymbol("/clock"));
List<File> files = generatorMqtt.generateMqttAdapter(componentInstanceSymbol);
......
#include "Callback.hpp"
Callback::Callback(client& cli, tests_a_compA* comp, EMAPortInstanceSymbol port) : callback(), cli_(cli)
Callback::Callback(client& cli, double port) : callback(), cli_(cli)
{
comp_ = comp;
port_ = port;
......@@ -27,5 +27,5 @@ void Callback::message_arrived(const_message_ptr msg)
cout << "Message received "<< msg->get_topic() << ": " << msg->get_payload_str() << endl;
string::size_type sz;
double value = std::stod (msg->get_payload_str(),&sz);
comp_->port_.toString() = value;
port_ = value;
}
......@@ -12,7 +12,7 @@ class Callback : public virtual callback
client& cli_;
public:
Callback(client& cli, tests_a_compA* comp, EMAPortInstanceSymbol port);
Callback(client& cli, double port);
void connected(const string& cause);
......@@ -22,8 +22,7 @@ public:
private:
tests_a_compA* comp_;
EMAPortInstanceSymbol port_;
double port_;
};
#endif /* Callback_hpp */
......@@ -18,16 +18,23 @@ void MqttAdapter_tests_a_compA::init(tests_a_compA *comp)
// Intitialize callbacks, subscribers and publishers
_sub_portA = new client(SERVER_ADDRESS, portA);
_callback_portA = new Callback(*_sub_portA, comp, component->getIncomingPorts().get(component->getIncomingPorts().indexOf(portA)));
_callback_portA = new Callback(*_sub_portA, component->portA);
_sub_portB = new client(SERVER_ADDRESS, portB);
_callback_portB = new Callback(*_sub_portB, component->portB);
_pub_portC = new client(SERVER_ADDRESS, portC);
_pub_portD = new client(SERVER_ADDRESS, portD);
// Connect subscribers, publishers and subscribe to the topics
try {
_sub_portA->set_callback(*_callback_portA);
_sub_portA->connect(connOpts);
_sub_portA->subscribe("/clock", 1);
_sub_portB->set_callback(*_callback_portB);
_sub_portB->connect(connOpts);
_sub_portB->subscribe("/clock", 1);
_pub_portC->connect(connOpts);
_pub_portD->connect(connOpts);
} catch (const mqtt::exception& exc) {
cerr << exc.what() << endl;
......@@ -48,9 +55,23 @@ void MqttAdapter_tests_a_compA::publish_echo_portC()
cerr << exc.to_string() << endl;
}
}
void MqttAdapter_tests_a_compA::publish_echo_portD()
{
string value = to_string(component->portD);
auto pubmsg = make_message("/clock", value);
try {
_pub_portD->publish(pubmsg);
}
catch (const exception& exc) {
cerr << exc.to_string() << endl;
}
}
void MqttAdapter_tests_a_compA::tick()
{
publish_echo_portC();
publish_echo_portD();
}
......@@ -9,26 +9,30 @@ using namespace std;
using namespace mqtt;
class MqttAdapter_tests_a_compA {
public:
MqttAdapter_tests_a_compA();
void init(tests_a_compA* comp);
void publish_echo_portC();
void publish_echo_portD();
void tick();
private:
const string SERVER_ADDRESS = "tcp://localhost:1883";
tests_a_compA* component = nullptr;
// Callbacks, subscribers
Callback* _callback_portA = nullptr;
client* _sub_portA = nullptr;
client* _sub_portA = nullptr;
Callback* _callback_portB = nullptr;
client* _sub_portB = nullptr;
// Publishers
client* _pub_portC = nullptr;
client* _pub_portC = nullptr;
client* _pub_portD = nullptr;
};
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment