Skip to content

Commit

Permalink
Merge pull request eclipse-iceoryx#2015 from elBoberido/iox-1036-buil…
Browse files Browse the repository at this point in the history
…der-pattern-for-named-pipe

iox-eclipse-iceoryx#1036 builder pattern for named pipe
  • Loading branch information
elBoberido authored Sep 4, 2023
2 parents e70767a + eed1224 commit 4406d46
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 161 deletions.
21 changes: 21 additions & 0 deletions doc/website/release-notes/iceoryx-unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
- `MemoryMap`
- `SharedMemory`
- `MessageQueue`
- `NamedPipe`
- `FileLock`
- Add the ability to adjust path and file permissions of the file lock
- `Mutex`
Expand Down Expand Up @@ -1151,3 +1152,23 @@

// after
auto e = exp.error();
```

52. `UnixDomainSocket`, `MessageQueue` and `NamedPipe` are not default constructible anymore

```cpp
// before
iox::posix::UnixDomainSocket socket;

// after
// option 1
iox::optional<iox::posix::UnixDomainSocket> socket;
// option 2
iox::posix::UnixDomainSocket socket { UnixDomainSocketBuilder()
.name("foo")
.channelSide(iox::posix::IpcChannelSide::CLIENT)
.create()
.expect("Valid UnixDomainSocket")
};

```
70 changes: 46 additions & 24 deletions iceoryx_dust/include/iceoryx_dust/posix_wrapper/named_pipe.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright (c) 2021 - 2022 by Apex.AI Inc. All rights reserved.
// Copyright (c) 2023 by Mathias Kraus <[email protected]>. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -16,7 +17,6 @@
#ifndef IOX_DUST_POSIX_WRAPPER_NAMED_PIPE_HPP
#define IOX_DUST_POSIX_WRAPPER_NAMED_PIPE_HPP

#include "iceoryx_dust/design/creation.hpp"
#include "iceoryx_dust/iceoryx_dust_deployment.hpp"
#include "iceoryx_hoofs/concurrent/lockfree_queue.hpp"
#include "iceoryx_hoofs/internal/posix_wrapper/ipc_channel.hpp"
Expand All @@ -35,7 +35,7 @@ namespace iox
{
namespace posix
{
class NamedPipe : public DesignPattern::Creation<NamedPipe, IpcChannelError>
class NamedPipe
{
public:
// no system restrictions at all, except available memory. MAX_MESSAGE_SIZE and MAX_NUMBER_OF_MESSAGES can be
Expand All @@ -55,17 +55,26 @@ class NamedPipe : public DesignPattern::Creation<NamedPipe, IpcChannelError>
using Message_t = string<MAX_MESSAGE_SIZE>;
using MessageQueue_t = concurrent::LockFreeQueue<Message_t, MAX_NUMBER_OF_MESSAGES>;

NamedPipe() noexcept = delete;
NamedPipe(const NamedPipe&) = delete;
NamedPipe& operator=(const NamedPipe&) = delete;

/// @brief For compatibility with IpcChannel alias, default ctor which creates
/// an uninitialized NamedPipe.
NamedPipe() noexcept;

NamedPipe(NamedPipe&& rhs) noexcept;
NamedPipe& operator=(NamedPipe&& rhs) noexcept;
~NamedPipe() noexcept;

/// @todo iox-#1036 Remove when all channels are ported to the builder pattern
static expected<NamedPipe, IpcChannelError> create(const IpcChannelName_t& name,
const IpcChannelSide channelSide,
const size_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
{
return m_data != nullptr;
}

/// @brief removes a named pipe artifact from the system
/// @return true if the artifact was removed, false when no artifact was found and
/// IpcChannelError::INTERNAL_LOGIC_ERROR when shm_unlink failed
Expand Down Expand Up @@ -103,33 +112,22 @@ class NamedPipe : public DesignPattern::Creation<NamedPipe, IpcChannelError>
expected<std::string, IpcChannelError> timedReceive(const units::Duration& timeout) const noexcept;

private:
friend class DesignPattern::Creation<NamedPipe, IpcChannelError>;

/// @brief constructor which creates a named pipe. This creates a shared memory file with the
/// prefix NAMED_PIPE_PREFIX concatenated with name.
/// @param[in] name the name of the named pipe
/// @param[in] channelSide defines the channel side (server creates the shared memory, clients opens it)
/// @param[in] maxMsgSize maximum message size, must be less or equal than MAX_MESSAGE_SIZE
/// @param[in] maxMsgNumber the maximum number of messages, must be less or equal than MAX_NUMBER_OF_MESSAGES
NamedPipe(const IpcChannelName_t& name,
const IpcChannelSide channelSide,
const size_t maxMsgSize = MAX_MESSAGE_SIZE,
const uint64_t maxMsgNumber = MAX_NUMBER_OF_MESSAGES) noexcept;
friend class NamedPipeBuilder;

class NamedPipeData;
NamedPipe(SharedMemoryObject&& sharedMemory, NamedPipeData* data) noexcept;

template <typename Prefix>
static IpcChannelName_t convertName(const Prefix& p, const IpcChannelName_t& name) noexcept;
static IpcChannelName_t mapToSharedMemoryName(const Prefix& p, const IpcChannelName_t& name) noexcept;

/// @brief destroys an initialized named pipe.
/// @return is always successful
expected<void, IpcChannelError> destroy() noexcept;

private:
optional<SharedMemoryObject> m_sharedMemory;

class NamedPipeData
{
public:
NamedPipeData(bool& isInitialized, IpcChannelError& error, const uint32_t maxMsgNumber) noexcept;
NamedPipeData() noexcept = default;
NamedPipeData(const NamedPipeData&) = delete;
NamedPipeData(NamedPipeData&& rhs) = delete;

Expand All @@ -140,6 +138,8 @@ class NamedPipe : public DesignPattern::Creation<NamedPipe, IpcChannelError>
UnnamedSemaphore& sendSemaphore() noexcept;
UnnamedSemaphore& receiveSemaphore() noexcept;

expected<void, IpcChannelError> initialize(const uint32_t maxMsgNumber) noexcept;

bool waitForInitialization() const noexcept;
bool hasValidState() const noexcept;

Expand All @@ -156,9 +156,31 @@ class NamedPipe : public DesignPattern::Creation<NamedPipe, IpcChannelError>
optional<UnnamedSemaphore> m_receiveSemaphore;
};


private:
SharedMemoryObject m_sharedMemory;
NamedPipeData* m_data = nullptr;
};

class NamedPipeBuilder
{
/// @brief Defines the named pipe name
IOX_BUILDER_PARAMETER(IpcChannelName_t, name, "")

/// @brief Defines how the named pipe is opened, i.e. as client or server
IOX_BUILDER_PARAMETER(IpcChannelSide, channelSide, IpcChannelSide::CLIENT)

/// @brief Defines the max message size of the named pipe
IOX_BUILDER_PARAMETER(size_t, maxMsgSize, NamedPipe::MAX_MESSAGE_SIZE)

/// @brief Defines the max number of messages for the named pipe.
IOX_BUILDER_PARAMETER(uint64_t, maxMsgNumber, NamedPipe::MAX_NUMBER_OF_MESSAGES)

public:
/// @brief create a named pipe
/// @return On success a 'NamedPipe' is returned and on failure an 'IpcChannelError'.
expected<NamedPipe, IpcChannelError> create() const noexcept;
};

} // namespace posix
} // namespace iox

Expand Down
Loading

0 comments on commit 4406d46

Please sign in to comment.