Commit 75e2790c authored by Florian Raeth's avatar Florian Raeth

fixed publisher and added mutex to subscriber

parent 7bb171b3
Pipeline #186133 failed with stages
in 1 minute and 54 seconds
......@@ -13,59 +13,63 @@ void <@m.mwIdent/>Adapter_${model.getEscapedCompName()}::init(${model.getEscaped
// Intitialize Subscriber
<#list model.getIncomingPorts() as sub>
${sub.getName()}_Subscriber = vsomeip::runtime::get()->create_application("Subscriber");
${sub.getName()}_Subscriber = vsomeip::runtime::get()->create_application("Subscriber ${sub.getName()}");
if (!${sub.getName()}_Subscriber->init()) {
std::cerr << "Couldn't initialize Subscriber ${sub.getName()}" << std::endl;
return false;
}
${sub.getName()}_Subscriber->register_state_handler(std::bind(&SomeIPAdapter_${model.getEscapedCompName()}::on_state, this, std::placeholders::_1);
${sub.getName()}_Subscriber->register_availability_handler(${model.getServiceId(sub)}, ${model.getInstanceId(sub)}, std::bind(&SomeIPAdapter_${model.getEscapedCompName()}::on_availability_${sub.getName()}, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
${sub.getName()}_Subscriber->request_service(${model.getServiceId(sub)}, ${model.getInstanceId(sub)});
${sub.getName()}_Subscriber->register_message_handler(${model.getServiceId(sub)}, ${model.getInstanceId(sub)}, ${model.getMethodId(sub)}, std::bind(&SomeIPAdapter_${model.getEscapedCompName()}::on_message_${sub.getName()}, this, std::placeholders::_1));
${sub.getName()}_Subscriber->register_availability_handler(${model.getServiceId(sub)}, ${model.getInstanceId(sub)}, std::bind(&SomeIPAdapter_${model.getEscapedCompName()}::on_availability, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
// Subscribe
std::set<vsomeip::eventgroup_t> event_group;
event_group.insert(${model.getEventGroupId(sub)});
${sub.getName()}_Subscriber->request_event(${model.getServiceId(sub)}, ${model.getInstanceId(sub)}, ${model.getEventId(sub)}, event_group, true);
${sub.getName()}_Subscriber->subscribe(${model.getServiceId(sub)}, ${model.getInstanceId(sub)}, ${model.getEventGroupId(sub)});
std::thread sender_${sub.getName()}(std::bind(&SomeIPAdapter_${model.getEscapedCompName()}::run_${sub.getName()}, this));
${sub.getName()}_Subscriber->start();
</#list>
// Intitialize Publisher
<#list model.getOutgoingPorts() as pub>
${pub.getName()}_Publisher = vsomeip::runtime::get()->create_application("Publisher");
${pub.getName()}_Publisher = vsomeip::runtime::get()->create_application("Publisher ${pub.getName()}");
if (!${pub.getName()}_Publisher->init()) {
std::cerr << "Couldn't initialize Publisher ${pub.getName()}" << std::endl;
return false;
}
${pub.getName()}_Publisher->offer_service(${model.getServiceId(pub)}, ${model.getInstanceId(pub)});
</#list>
// Start Subscriber
<#list model.getIncomingPorts() as sub>
${sub.getName()}_Subscriber->start();
</#list>
// Start Publisher
<#list model.getOutgoingPorts() as pub>
${pub.getName()}_Publisher->start();
</#list>
}
<#list model.getIncomingPorts() as sub>
void <@m.mwIdent/>Adapter_${model.getEscapedCompName()}::on_message_${sub.getName()}(const std::shared_ptr<vsomeip::message> &_request) {
void <@m.mwIdent/>Adapter_${model.getEscapedCompName()}::run_${sub.getName()}() {
std::unique_lock<std::mutex> its_lock(mutex_${sub.getName()});
condition_${sub.getName()}.wait(its_lock);
std::set<vsomeip::eventgroup_t> event_group;
event_group.insert(${model.getEventGroupId(sub)});
${sub.getName()}_Subscriber->request_event(${model.getServiceId(sub)}, ${model.getInstanceId(sub)}, ${model.getEventId(sub)}, event_group, true);
${sub.getName()}_Subscriber->subscribe(${model.getServiceId(sub)}, ${model.getInstanceId(sub)}, ${model.getEventGroupId(sub)});
}
void <@m.mwIdent/>Adapter_${model.getEscapedCompName()}::on_message_${sub.getName()}(const std::shared_ptr<vsomeip::message> &_message) {
//read received message
std::shared_ptr<vsomeip::payload> its_payload = _request->get_payload();
std::shared_ptr<vsomeip::payload> its_payload = _message->get_payload();
vsomeip::length_t l = its_payload->get_length();
double dataFromMessage = *((double*)its_payload->get_data());
component->${sub.getName()} = dataFromMessage;
//print data to std out
std::cout << "SERVICE ${sub.getName()}: Received message from ["
<< std::setw(4) << std::setfill('0') << std::hex << _request->get_client() << "/"
<< std::setw(4) << std::setfill('0') << std::hex << _request->get_session() << "]: "
<< std::setw(4) << std::setfill('0') << std::hex << _message->get_client() << "/"
<< std::setw(4) << std::setfill('0') << std::hex << _message->get_session() << "]: "
<< dataFromMessage << std::endl;
}
void <@m.mwIdent/>Adapter_${model.getEscapedCompName()}::on_availability_${sub.getName()}(vsomeip::service_t _service, vsomeip::instance_t _instance, bool _is_available) {
std::cout << "Service ["
<< std::setw(4) << std::setfill('0') << std::hex << _service << "." << _instance
<< "] is "
<< (_is_available ? "available." : "NOT available.")
<< std::endl;
condition_${sub.getName()}.notify_one();
}
</#list>
......@@ -75,11 +79,9 @@ void <@m.mwIdent/>Adapter_${model.getEscapedCompName()}::publish${pub.getName()}
//Read data from component
double d = component->${pub.getName()};
//Create message
uint8_t *byteArray = (uint8_t*)&d;
vsomeip::byte_t *p;
p = byteArray;
std::shared_ptr< vsomeip::payload > payload = vsomeip::runtime::get()->create_payload(p,8);
const vsomeip::byte_t its_data[] = { (uint8_t) d };
std::shared_ptr<vsomeip::payload> payload = vsomeip::runtime::get()->create_payload();
payload->set_data(its_data, sizeof(its_data));
//Publish
std::set<vsomeip::eventgroup_t> event_group;
......@@ -89,23 +91,6 @@ void <@m.mwIdent/>Adapter_${model.getEscapedCompName()}::publish${pub.getName()}
}
</#list>
<#list model.getIncomingPorts() as sub>
void <@m.mwIdent/>Adapter_${model.getEscapedCompName()}::on_state_${sub.getName()}(vsomeip::state_type_e _state) {
if (_state == vsomeip::state_type_e::ST_REGISTERED) {
${sub.getName()}_Subscriber->request_service(${model.getServiceId(sub)}, ${model.getInstanceId(sub)});
}
}
</#list>
void <@m.mwIdent/>Adapter_${model.getEscapedCompName()}::on_availability(vsomeip::service_t _service, vsomeip::instance_t _instance, bool _is_available) {
std::cout << "Service ["
<< std::setw(4) << std::setfill('0') << std::hex << _service << "." << _instance
<< "] is "
<< (_is_available ? "available." : "NOT available.")
<< std::endl;
}
void <@m.mwIdent/>Adapter_${model.getEscapedCompName()}::tick()
{
......
......@@ -3,7 +3,6 @@
#pragma once
#include "${model.getEscapedCompName()}.h"
#include "IAdapter_${model.getEscapedCompName()}.h"
#include <iomanip>
#include <iostream>
#include <sstream>
......@@ -32,16 +31,17 @@ private:
<#list model.getIncomingPorts() as sub>
std::shared_ptr<vsomeip::application> ${sub.getName()}_Subscriber;
std::mutex mutex_${sub.getName()};
std::condition_variable condition_${sub.getName()};
void on_message_${sub.getName()}(const std::shared_ptr<vsomeip::message> &_request);
void on_state_${sub.getName()}(vsomeip::state_type_e _state);
void run_${sub.getName()}();
void on_message_${sub.getName()}(const std::shared_ptr<vsomeip::message> &_message);
void on_availability_${sub.getName()}(vsomeip::service_t _service, vsomeip::instance_t _instance, bool _is_available);
</#list>
<#list model.getOutgoingPorts() as pub>
std::shared_ptr<vsomeip::application> ${pub.getName()}_Publisher;
void publish${pub.getName()}_Publisher()();
void publish${pub.getName()}_Publisher();
</#list>
void on_availability(vsomeip::service_t _service, vsomeip::instance_t _instance, bool _is_available);
};
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