Skip to content

Commit

Permalink
iox-eclipse-iceoryx#1036 Use builder API for 'IpcInterface'
Browse files Browse the repository at this point in the history
  • Loading branch information
elBoberido committed Sep 4, 2023
1 parent c92eafb commit 17c8af1
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 16 deletions.
19 changes: 12 additions & 7 deletions iceoryx_dust/include/iceoryx_dust/posix_wrapper/message_queue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ namespace iox
{
namespace posix
{
class MessageQueueBuilder;

/// @brief Wrapper class for posix message queue
///
/// @code
Expand All @@ -54,7 +56,9 @@ class MessageQueue
static constexpr uint64_t SHORTEST_VALID_QUEUE_NAME = 2;
static constexpr uint64_t NULL_TERMINATOR_SIZE = 1;
static constexpr uint64_t MAX_MESSAGE_SIZE = 4096;
static constexpr uint64_t MAX_MESSAGE_NUMBER = 10;
static constexpr uint64_t MAX_NUMBER_OF_MESSAGES = 10;

using Builder_t = MessageQueueBuilder;

MessageQueue() noexcept = delete;
MessageQueue(const MessageQueue& other) = delete;
Expand All @@ -65,10 +69,11 @@ class MessageQueue
~MessageQueue() noexcept;

/// @todo iox-#1036 Remove when all channels are ported to the builder pattern
static expected<MessageQueue, IpcChannelError> create(const IpcChannelName_t& name,
const IpcChannelSide channelSide,
const uint64_t maxMsgSize = MAX_MESSAGE_SIZE,
const uint64_t maxMsgNumber = MAX_MESSAGE_NUMBER) noexcept;
static expected<MessageQueue, IpcChannelError>
create(const IpcChannelName_t& name,
const IpcChannelSide channelSide,
const uint64_t maxMsgSize = MAX_MESSAGE_SIZE,
const uint64_t maxMsgNumber = MAX_NUMBER_OF_MESSAGES) noexcept;

/// @todo iox-#1036 Remove when all channels are ported to the builder pattern
bool isInitialized() const noexcept
Expand Down Expand Up @@ -101,7 +106,7 @@ class MessageQueue
private:
friend class MessageQueueBuilder;

MessageQueue(const IpcChannelName_t&& name,
MessageQueue(const IpcChannelName_t& name,
const mq_attr attributes,
mqd_t mqDescriptor,
const IpcChannelSide channelSide) noexcept;
Expand Down Expand Up @@ -146,7 +151,7 @@ class MessageQueueBuilder
IOX_BUILDER_PARAMETER(uint64_t, maxMsgSize, MessageQueue::MAX_MESSAGE_SIZE)

/// @brief Defines the max number of messages for the message queue.
IOX_BUILDER_PARAMETER(uint64_t, maxMsgNumber, MessageQueue::MAX_MESSAGE_NUMBER)
IOX_BUILDER_PARAMETER(uint64_t, maxMsgNumber, MessageQueue::MAX_NUMBER_OF_MESSAGES)

public:
/// @brief create a message queue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "iceoryx_hoofs/internal/posix_wrapper/shared_memory_object.hpp"
#include "iceoryx_hoofs/posix_wrapper/unnamed_semaphore.hpp"
#include "iceoryx_platform/semaphore.hpp"
#include "iox/builder.hpp"
#include "iox/duration.hpp"
#include "iox/expected.hpp"
#include "iox/optional.hpp"
Expand All @@ -35,6 +36,8 @@ namespace iox
{
namespace posix
{
class NamedPipeBuilder;

class NamedPipe
{
public:
Expand All @@ -52,6 +55,8 @@ class NamedPipe
/// NOLINTNEXTLINE(hicpp-avoid-c-arrays,cppcoreguidelines-avoid-c-arrays)
static constexpr const char NAMED_PIPE_PREFIX[] = "iox_np_";

using Builder_t = NamedPipeBuilder;

using Message_t = string<MAX_MESSAGE_SIZE>;
using MessageQueue_t = concurrent::LockFreeQueue<Message_t, MAX_NUMBER_OF_MESSAGES>;

Expand Down
4 changes: 2 additions & 2 deletions iceoryx_dust/source/posix_wrapper/message_queue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ expected<MessageQueue, IpcChannelError> MessageQueueBuilder::create() const noex
return ok(MessageQueue{std::move(sanitizedName), attributes, mqDescriptor, m_channelSide});
}

MessageQueue::MessageQueue(const IpcChannelName_t&& name,
MessageQueue::MessageQueue(const IpcChannelName_t& name,
const mq_attr attributes,
mqd_t mqDescriptor,
const IpcChannelSide channelSide) noexcept
: m_name(std::move(name))
: m_name(name)
, m_attributes(attributes)
, m_mqDescriptor(mqDescriptor)
, m_channelSide(channelSide)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ class UnixDomainSocket
/// @brief The name length is limited by the size of the sockaddr_un::sun_path buffer and the IOX_SOCKET_PATH_PREFIX
static constexpr size_t LONGEST_VALID_NAME = sizeof(sockaddr_un::sun_path) - 1;

using Builder_t = UnixDomainSocketBuilder;

using UdsName_t = string<LONGEST_VALID_NAME>;
using Message_t = string<MAX_MESSAGE_SIZE>;

Expand Down
9 changes: 8 additions & 1 deletion iceoryx_posh/source/runtime/ipc_interface_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,14 @@ template <typename IpcChannelType>
bool IpcInterface<IpcChannelType>::openIpcChannel(const posix::IpcChannelSide channelSide) noexcept
{
m_channelSide = channelSide;
IpcChannelType::create(m_runtimeName, m_channelSide, m_maxMessageSize, m_maxMessages)

using IpcChannelBuilder_t = typename IpcChannelType::Builder_t;
IpcChannelBuilder_t()
.name(m_runtimeName)
.channelSide(m_channelSide)
.maxMsgSize(m_maxMessageSize)
.maxMsgNumber(m_maxMessages)
.create()
.and_then([this](auto& ipcChannel) { this->m_ipcChannel.emplace(std::move(ipcChannel)); })
.or_else([](auto& err) {
IOX_LOG(ERROR) << "unable to create ipc channel with error code: " << static_cast<uint8_t>(err);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ class CMqInterfaceStartupRace_test : public Test
public:
virtual void SetUp()
{
platform::IoxIpcChannelType::create(roudi::IPC_CHANNEL_ROUDI_NAME, IpcChannelSide::SERVER)
platform::IoxIpcChannelType::Builder_t()
.name(roudi::IPC_CHANNEL_ROUDI_NAME)
.channelSide(IpcChannelSide::SERVER)
.create()
.and_then([this](auto& channel) { this->m_roudiQueue.emplace(std::move(channel)); });
ASSERT_THAT(m_roudiQueue.has_value(), true);
}
Expand Down Expand Up @@ -101,9 +104,11 @@ class CMqInterfaceStartupRace_test : public Test

if (!m_appQueue.has_value())
{
platform::IoxIpcChannelType::create(MqAppName, IpcChannelSide::CLIENT).and_then([this](auto& channel) {
this->m_appQueue.emplace(std::move(channel));
});
platform::IoxIpcChannelType::Builder_t()
.name(MqAppName)
.channelSide(IpcChannelSide::CLIENT)
.create()
.and_then([this](auto& channel) { this->m_appQueue.emplace(std::move(channel)); });
}
ASSERT_THAT(m_appQueue.has_value(), true);

Expand Down Expand Up @@ -143,7 +148,10 @@ TEST_F(CMqInterfaceStartupRace_test, ObsoleteRouDiMq)
exit(EXIT_FAILURE);
});

auto m_roudiQueue2 = platform::IoxIpcChannelType::create(roudi::IPC_CHANNEL_ROUDI_NAME, IpcChannelSide::SERVER);
auto m_roudiQueue2 = platform::IoxIpcChannelType::Builder_t()
.name(roudi::IPC_CHANNEL_ROUDI_NAME)
.channelSide(IpcChannelSide::SERVER)
.create();

// check if the app retries to register at RouDi
request = m_roudiQueue2->timedReceive(15_s);
Expand Down Expand Up @@ -191,7 +199,10 @@ TEST_F(CMqInterfaceStartupRace_test, ObsoleteRouDiMqWithFullMq)
exit(EXIT_FAILURE);
});

auto newRoudi = platform::IoxIpcChannelType::create(roudi::IPC_CHANNEL_ROUDI_NAME, IpcChannelSide::SERVER);
auto newRoudi = platform::IoxIpcChannelType::Builder_t()
.name(roudi::IPC_CHANNEL_ROUDI_NAME)
.channelSide(IpcChannelSide::SERVER)
.create();

// check if the app retries to register at RouDi
auto request = newRoudi->timedReceive(15_s);
Expand Down

0 comments on commit 17c8af1

Please sign in to comment.