Skip to content

Commit

Permalink
Merge pull request eclipse-iceoryx#785 from ApexAI/iox-#418-replace-s…
Browse files Browse the repository at this point in the history
…mart-c-with-posix-call

Iox eclipse-iceoryx#418 replace smart c with posix call
  • Loading branch information
elfenpiff committed May 14, 2021
2 parents 22f1425 + afcf541 commit db2db48
Show file tree
Hide file tree
Showing 65 changed files with 1,228 additions and 1,660 deletions.
148 changes: 64 additions & 84 deletions iceoryx_examples/iceperf/mq.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
// SPDX-License-Identifier: Apache-2.0

#include "mq.hpp"
#include "iceoryx_utils/cxx/smart_c.hpp"
#include "iceoryx_utils/internal/posix_wrapper/message_queue.hpp"
#include "iceoryx_utils/platform/fcntl.hpp"
#include "iceoryx_utils/platform/platform_correction.hpp"
#include "iceoryx_utils/posix_wrapper/posix_call.hpp"

#include <chrono>
#include <thread>
Expand All @@ -34,22 +34,22 @@ MQ::MQ(const std::string& publisherName, const std::string& subscriberName) noex
void MQ::cleanupOutdatedResources(const std::string& publisherName, const std::string& subscriberName) noexcept
{
auto publisherMqName = PREFIX + publisherName;
auto mqCallPublisher = iox::cxx::makeSmartC(
mq_unlink, iox::cxx::ReturnMode::PRE_DEFINED_ERROR_CODE, {ERROR_CODE}, {ENOENT}, publisherMqName.c_str());
if (mqCallPublisher.hasErrors())
{
std::cout << "mq_unlink error for " << publisherMqName << std::endl;
exit(1);
}
iox::posix::posixCall(mq_unlink)(publisherMqName.c_str())
.failureReturnValue(ERROR_CODE)
.evaluateWithIgnoredErrnos(ENOENT)
.or_else([&](auto& r) {
std::cout << "mq_unlink error for " << publisherMqName << ", " << r.getHumanReadableErrnum() << std::endl;
exit(1);
});

auto subscriberMqName = PREFIX + subscriberName;
auto mqCallSubscriber = iox::cxx::makeSmartC(
mq_unlink, iox::cxx::ReturnMode::PRE_DEFINED_ERROR_CODE, {ERROR_CODE}, {ENOENT}, subscriberMqName.c_str());
if (mqCallSubscriber.hasErrors())
{
std::cout << "mq_unlink error for " << subscriberMqName << std::endl;
exit(1);
}
iox::posix::posixCall(mq_unlink)(subscriberMqName.c_str())
.failureReturnValue(ERROR_CODE)
.evaluateWithIgnoredErrnos(ENOENT)
.or_else([&](auto& r) {
std::cout << "mq_unlink error for " << subscriberMqName << ", " << r.getHumanReadableErrnum() << std::endl;
exit(1);
});
}

void MQ::initLeader() noexcept
Expand Down Expand Up @@ -90,32 +90,30 @@ void MQ::initMqAttributes() noexcept

void MQ::shutdown() noexcept
{
auto mqCallSubClose = iox::cxx::makeSmartC(
mq_close, iox::cxx::ReturnMode::PRE_DEFINED_ERROR_CODE, {ERROR_CODE}, {}, m_mqDescriptorSubscriber);

if (mqCallSubClose.hasErrors())
{
std::cout << "mq_close error for " << m_subscriberMqName << std::endl;
exit(1);
}

auto mqCallSubUnlink = iox::cxx::makeSmartC(
mq_unlink, iox::cxx::ReturnMode::PRE_DEFINED_ERROR_CODE, {ERROR_CODE}, {ENOENT}, m_subscriberMqName.c_str());

if (mqCallSubUnlink.hasErrors())
{
std::cout << "mq_unlink error for " << m_subscriberMqName << std::endl;
exit(1);
}

auto mqCallPubClose = iox::cxx::makeSmartC(
mq_close, iox::cxx::ReturnMode::PRE_DEFINED_ERROR_CODE, {ERROR_CODE}, {}, m_mqDescriptorPublisher);
iox::posix::posixCall(mq_close)(m_mqDescriptorSubscriber)
.failureReturnValue(ERROR_CODE)
.evaluate()
.or_else([&](auto& r) {
std::cout << "mq_close error for " << m_subscriberMqName << ", " << r.getHumanReadableErrnum() << std::endl;
exit(1);
});

iox::posix::posixCall(mq_unlink)(m_subscriberMqName.c_str())
.failureReturnValue(ERROR_CODE)
.evaluateWithIgnoredErrnos(ENOENT)
.or_else([&](auto& r) {
std::cout << "mq_unlink error for " << m_subscriberMqName << ", " << r.getHumanReadableErrnum()
<< std::endl;
exit(1);
});

if (mqCallPubClose.hasErrors())
{
std::cout << "mq_close error for " << m_publisherMqName << std::endl;
exit(1);
}
iox::posix::posixCall(mq_close)(m_mqDescriptorPublisher)
.failureReturnValue(ERROR_CODE)
.evaluate()
.or_else([&](auto& r) {
std::cout << "mq_close error for " << m_publisherMqName << ", " << r.getHumanReadableErrnum() << std::endl;
exit(1);
});
}

void MQ::sendPerfTopic(const uint32_t payloadSizeInBytes, const RunFlag runFlag) noexcept
Expand Down Expand Up @@ -173,22 +171,17 @@ void MQ::open(const std::string& name, const iox::posix::IpcChannelSide channelS
// the mask will be applied to the permissions, therefore we need to set it to 0
mode_t umaskSaved = umask(0);

auto mqCall = iox::cxx::makeSmartC(mq_open,
iox::cxx::ReturnMode::PRE_DEFINED_ERROR_CODE,
{ERROR_CODE},
{ENOENT},
name.c_str(),
openFlags,
m_filemode,
&m_attributes);
auto mqCall = iox::posix::posixCall(iox_mq_open4)(name.c_str(), openFlags, m_filemode, &m_attributes)
.failureReturnValue(ERROR_CODE)
.evaluateWithIgnoredErrnos(ENOENT)
.or_else([&](auto& r) {
std::cout << "mq_open error for " << name << ", " << r.getHumanReadableErrnum()
<< std::endl;
exit(1);
});
umask(umaskSaved);

if (mqCall.hasErrors())
{
std::cout << "mq_open error for " << name << std::endl;
exit(1);
}
else if (mqCall.getErrNum() == ENOENT)
if (mqCall->errnum == ENOENT)
{
constexpr std::chrono::milliseconds RETRY_INTERVAL{10};
std::this_thread::sleep_for(RETRY_INTERVAL);
Expand All @@ -197,11 +190,11 @@ void MQ::open(const std::string& name, const iox::posix::IpcChannelSide channelS

if (channelSide == iox::posix::IpcChannelSide::SERVER)
{
m_mqDescriptorSubscriber = mqCall.getReturnValue();
m_mqDescriptorSubscriber = mqCall->value;
}
else
{
m_mqDescriptorPublisher = mqCall.getReturnValue();
m_mqDescriptorPublisher = mqCall->value;
}

break;
Expand All @@ -210,36 +203,23 @@ void MQ::open(const std::string& name, const iox::posix::IpcChannelSide channelS

void MQ::send(const char* buffer, uint32_t length) noexcept
{
auto mqCall = iox::cxx::makeSmartC(mq_send,
iox::cxx::ReturnMode::PRE_DEFINED_ERROR_CODE,
{ERROR_CODE},
{},
m_mqDescriptorPublisher,
buffer,
length,
1U);

if (mqCall.hasErrors())
{
std::cout << std::endl << "send error for " << m_publisherMqName << std::endl;
exit(1);
}
iox::posix::posixCall(mq_send)(m_mqDescriptorPublisher, buffer, length, 1U)
.failureReturnValue(ERROR_CODE)
.evaluate()
.or_else([&](auto& r) {
std::cout << std::endl
<< "send error for " << m_publisherMqName << ", " << r.getHumanReadableErrnum() << std::endl;
exit(1);
});
}

void MQ::receive(char* buffer) noexcept
{
auto mqCall = iox::cxx::makeSmartC(mq_receive,
iox::cxx::ReturnMode::PRE_DEFINED_ERROR_CODE,
{static_cast<ssize_t>(ERROR_CODE)},
{},
m_mqDescriptorSubscriber,
buffer,
MAX_MESSAGE_SIZE,
nullptr);

if (mqCall.hasErrors())
{
std::cout << "receive error for " << m_subscriberMqName << std::endl;
exit(1);
}
iox::posix::posixCall(mq_receive)(m_mqDescriptorSubscriber, buffer, MAX_MESSAGE_SIZE, nullptr)
.failureReturnValue(ERROR_CODE)
.evaluate()
.or_else([&](auto& r) {
std::cout << "receive error for " << m_subscriberMqName << ", " << r.getHumanReadableErrnum() << std::endl;
exit(1);
});
}
Loading

0 comments on commit db2db48

Please sign in to comment.