Skip to content

Commit

Permalink
iox-eclipse-iceoryx#2185 Consolidate prefix creation
Browse files Browse the repository at this point in the history
  • Loading branch information
elBoberido committed Feb 19, 2024
1 parent d390fd9 commit 26394e6
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 64 deletions.
6 changes: 6 additions & 0 deletions iceoryx_posh/include/iceoryx_posh/iceoryx_posh_types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include "iceoryx_platform/platform_settings.hpp"
#include "iceoryx_posh/iceoryx_posh_deployment.hpp"
#include "iox/detail/convert.hpp"
#include "iox/duration.hpp"
#include "iox/function.hpp"
#include "iox/log/logstream.hpp"
Expand Down Expand Up @@ -198,6 +199,11 @@ struct DefaultChunkQueueConfig

constexpr const char ICEORYX_RESOURCE_PREFIX[] = "iox1";

using ResourcePrefix_t = string<13>; // 'iox1_' + MAX_UINT16_SIZE + '_' + optional 'x_'
/// @brief Returns the prefix string used for resources
/// @param[in] uniqueRouDiID to use for the prefix string
inline ResourcePrefix_t iceoryxResourcePrefix(uint16_t uniqueRouDiID, iox::string<1> customizer = "");

// alias for string
using RuntimeName_t = string<MAX_RUNTIME_NAME_LENGTH>;
using NodeName_t = string<build::IOX_MAX_NODE_NAME_LENGTH>;
Expand Down
10 changes: 10 additions & 0 deletions iceoryx_posh/include/iceoryx_posh/iceoryx_posh_types.inl
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ log::LogStream& operator<<(log::LogStream& stream, ConnectionState value) noexce
return stream;
}

ResourcePrefix_t iceoryxResourcePrefix(uint16_t uniqueRouDiID, iox::string<1> customizer)
{
static_assert(std::is_same_v<uint16_t, std::remove_const_t<decltype(uniqueRouDiID)>>);
constexpr auto MAX_UINT16_WIDTH{5};
iox::string<MAX_UINT16_WIDTH> uniqueRoudiIdString{TruncateToCapacity,
iox::convert::toString(uniqueRouDiID).c_str()};
auto end = customizer.empty() ? "_" : concatenate("_", customizer, "_");
return concatenate(ICEORYX_RESOURCE_PREFIX, "_", uniqueRoudiIdString, end);
}

namespace roudi
{
inline iox::log::LogStream& operator<<(iox::log::LogStream& logstream, const MonitoringMode& mode) noexcept
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,10 @@ inline SharedMemoryObjectType MePooSegment<SharedMemoryObjectType, MemoryManager
return std::move(
typename SharedMemoryObjectType::Builder()
.name([&writerGroup] {
iox::string<1> uniqueRoudiIdString{TruncateToCapacity,
iox::convert::toString(roudi::DEFAULT_UNIQUE_ROUDI_ID).c_str()};
using ShmName_t = detail::PosixSharedMemory::Name_t;
ShmName_t shmName = concatenate(ICEORYX_RESOURCE_PREFIX,
"_",
uniqueRoudiIdString,
"_p_"); // add a '_p_' to prevent creating a payload segment with
// the same name as the management segment
ShmName_t shmName = iceoryxResourcePrefix(roudi::DEFAULT_UNIQUE_ROUDI_ID,
"p"); // use an additional 'p' to prevent creating a payload
// segment with the same name as the management segment
if (shmName.size() + writerGroup.getName().size() > ShmName_t::capacity())
{
IOX_LOG(FATAL,
Expand Down
41 changes: 18 additions & 23 deletions iceoryx_posh/source/roudi/memory/iceoryx_roudi_memory_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,24 @@ namespace iox
namespace roudi
{
IceOryxRouDiMemoryManager::IceOryxRouDiMemoryManager(const RouDiConfig_t& roudiConfig) noexcept
: m_fileLock(std::move(
FileLockBuilder()
.name([] {
iox::string<1> uniqueRoudiIdString{TruncateToCapacity,
iox::convert::toString(DEFAULT_UNIQUE_ROUDI_ID).c_str()};
auto lockName = concatenate(ICEORYX_RESOURCE_PREFIX, "_", uniqueRoudiIdString, "_", ROUDI_LOCK_NAME);
return lockName;
}())
.permission(iox::perms::owner_read | iox::perms::owner_write)
.create()
.or_else([](auto& error) {
if (error == FileLockError::LOCKED_BY_OTHER_PROCESS)
{
IOX_LOG(FATAL, "Could not acquire lock, is RouDi still running?");
IOX_REPORT_FATAL(PoshError::ICEORYX_ROUDI_MEMORY_MANAGER__ROUDI_STILL_RUNNING);
}
else
{
IOX_LOG(FATAL, "Error occurred while acquiring file lock named " << ROUDI_LOCK_NAME);
IOX_REPORT_FATAL(PoshError::ICEORYX_ROUDI_MEMORY_MANAGER__COULD_NOT_ACQUIRE_FILE_LOCK);
}
})
.value()))
: m_fileLock(
std::move(FileLockBuilder()
.name(concatenate(iceoryxResourcePrefix(DEFAULT_UNIQUE_ROUDI_ID), ROUDI_LOCK_NAME))
.permission(iox::perms::owner_read | iox::perms::owner_write)
.create()
.or_else([](auto& error) {
if (error == FileLockError::LOCKED_BY_OTHER_PROCESS)
{
IOX_LOG(FATAL, "Could not acquire lock, is RouDi still running?");
IOX_REPORT_FATAL(PoshError::ICEORYX_ROUDI_MEMORY_MANAGER__ROUDI_STILL_RUNNING);
}
else
{
IOX_LOG(FATAL, "Error occurred while acquiring file lock named " << ROUDI_LOCK_NAME);
IOX_REPORT_FATAL(PoshError::ICEORYX_ROUDI_MEMORY_MANAGER__COULD_NOT_ACQUIRE_FILE_LOCK);
}
})
.value()))
, m_defaultMemory(roudiConfig)
{
m_defaultMemory.m_managementShm.addMemoryBlock(&m_portPoolBlock).or_else([](auto) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,7 @@ expected<void*, MemoryProviderError> PosixShmMemoryProvider::createMemory(const
}

if (!PosixSharedMemoryObjectBuilder()
.name([this] {
iox::string<1> uniqueRoudiIdString{TruncateToCapacity,
iox::convert::toString(DEFAULT_UNIQUE_ROUDI_ID).c_str()};
auto shmName = concatenate(ICEORYX_RESOURCE_PREFIX, "_", uniqueRoudiIdString, "_", m_shmName);
return shmName;
}())
.name(concatenate(iceoryxResourcePrefix(DEFAULT_UNIQUE_ROUDI_ID), m_shmName))
.memorySizeInBytes(size)
.accessMode(m_accessMode)
.openMode(m_openMode)
Expand Down
25 changes: 17 additions & 8 deletions iceoryx_posh/source/roudi/roudi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,19 +289,28 @@ void RouDi::processMessage(const runtime::IpcMessage& message,
const iox::runtime::IpcMessageType& cmd,
const RuntimeName_t& runtimeName) noexcept
{
switch (cmd)
if (runtimeName.empty())
{
case runtime::IpcMessageType::REG:
IOX_LOG(ERROR, "Got message with empty runtime name!");
return;
}


for (const auto s : platform::IOX_PATH_SEPARATORS)
{
if (runtimeName.empty())
{
IOX_LOG(ERROR, "Got message with empty runtime name!");
}
else if (runtimeName.find(platform::IOX_PATH_SEPARATORS).has_value())
const char separator[2]{s};
if (runtimeName.find(separator).has_value())
{
IOX_LOG(ERROR, "Got message with a runtime name with invalid characters: \"" << runtimeName << "\"!");
return;
}
else if (message.getNumberOfElements() != 6)
}

switch (cmd)
{
case runtime::IpcMessageType::REG:
{
if (message.getNumberOfElements() != 6)
{
IOX_LOG(ERROR,
"Wrong number of parameters for \"IpcMessageType::REG\" from \"" << runtimeName << "\"received!");
Expand Down
14 changes: 8 additions & 6 deletions iceoryx_posh/source/runtime/ipc_interface_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,7 @@ std::string IpcMessageErrorTypeToString(const IpcMessageErrorType msg) noexcept

iox::optional<RuntimeName_t> ipcChannelNameToInterfaceName(RuntimeName_t channelName)
{
iox::string<1> uniqueRoudiIdString{TruncateToCapacity,
iox::convert::toString(roudi::DEFAULT_UNIQUE_ROUDI_ID).c_str()};
RuntimeName_t interfaceName = concatenate(ICEORYX_RESOURCE_PREFIX, "_", uniqueRoudiIdString, "_");
RuntimeName_t interfaceName = iceoryxResourcePrefix(roudi::DEFAULT_UNIQUE_ROUDI_ID);
if (interfaceName.size() + channelName.size() > RuntimeName_t::capacity())
{
return nullopt;
Expand All @@ -99,10 +97,14 @@ IpcInterface<IpcChannelType>::IpcInterface(const RuntimeName_t& runtimeName,
{
IOX_PANIC("Then runtime name must not be empty");
}
else if (runtimeName.find(iox::platform::IOX_PATH_SEPARATORS).has_value())
for (const auto s : platform::IOX_PATH_SEPARATORS)
{
IOX_LOG(FATAL, "The runtime name '" << runtimeName << "' contains path separators");
IOX_PANIC("Invalid characters for runtime name");
const char separator[2]{s};
if (runtimeName.find(separator).has_value())
{
IOX_LOG(FATAL, "The runtime name '" << runtimeName << "' contains path separators");
IOX_PANIC("Invalid characters for runtime name");
}
}

m_interfaceName =
Expand Down
17 changes: 4 additions & 13 deletions iceoryx_posh/source/runtime/shared_memory_user.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,7 @@ SharedMemoryUser::SharedMemoryUser(const size_t topicSize,
const UntypedRelativePointer::offset_t segmentManagerAddressOffset) noexcept
{
PosixSharedMemoryObjectBuilder()
.name([] {
iox::string<1> uniqueRoudiIdString{TruncateToCapacity,
iox::convert::toString(roudi::DEFAULT_UNIQUE_ROUDI_ID).c_str()};
auto shmName = concatenate(ICEORYX_RESOURCE_PREFIX, "_", uniqueRoudiIdString, "_", roudi::SHM_NAME);
return shmName;
}())
.name(concatenate(iceoryxResourcePrefix(roudi::DEFAULT_UNIQUE_ROUDI_ID), roudi::SHM_NAME))
.memorySizeInBytes(topicSize)
.accessMode(AccessMode::READ_WRITE)
.openMode(OpenMode::OPEN_EXISTING)
Expand Down Expand Up @@ -80,14 +75,10 @@ void SharedMemoryUser::openDataSegments(const uint64_t segmentId,
auto accessMode = segment.m_isWritable ? AccessMode::READ_WRITE : AccessMode::READ_ONLY;
PosixSharedMemoryObjectBuilder()
.name([&segment] {
iox::string<1> uniqueRoudiIdString{TruncateToCapacity,
iox::convert::toString(roudi::DEFAULT_UNIQUE_ROUDI_ID).c_str()};
using ShmName_t = detail::PosixSharedMemory::Name_t;
ShmName_t shmName = concatenate(ICEORYX_RESOURCE_PREFIX,
"_",
uniqueRoudiIdString,
"_p_"); // add a '_p_' to prevent creating a payload segment with the
// same name as the management segment
ShmName_t shmName = iceoryxResourcePrefix(roudi::DEFAULT_UNIQUE_ROUDI_ID,
"p"); // use an additional 'p' to prevent creating a payload
// segment with the same name as the management segment
if (shmName.size() + segment.m_sharedMemoryName.size() > ShmName_t::capacity())
{
IOX_LOG(FATAL,
Expand Down
43 changes: 43 additions & 0 deletions iceoryx_posh/test/moduletests/test_posh_types.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (c) 2024 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.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0

#include "iceoryx_posh/iceoryx_posh_types.hpp"

#include "test.hpp"

using namespace ::testing;
using namespace iox;

TEST(PoshTypes_test, IceoryxResourcePrefixWithDefaultRouDiIdWorks)
{
::testing::Test::RecordProperty("TEST_ID", "35f1d638-8efa-41dd-859b-bcc23450844f");

EXPECT_THAT(iceoryxResourcePrefix(roudi::DEFAULT_UNIQUE_ROUDI_ID).c_str(), StrEq("iox1_0_"));
}

TEST(PoshTypes_test, IceoryxResourcePrefixWithMaxRouDiIdWorks)
{
::testing::Test::RecordProperty("TEST_ID", "049e79d7-d0ca-4951-8d44-c80aebab7a88");

EXPECT_THAT(iceoryxResourcePrefix(std::numeric_limits<uint16_t>::max()).c_str(), StrEq("iox1_65535_"));
}

TEST(PoshTypes_test, IceoryxResourcePrefixWithMaxRouDiIdAndCustomizerWorks)
{
::testing::Test::RecordProperty("TEST_ID", "b63bbdca-ff19-41bc-9f8a-c657b0ee8009");

EXPECT_THAT(iceoryxResourcePrefix(std::numeric_limits<uint16_t>::max(), "a").c_str(), StrEq("iox1_65535_a_"));
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ namespace
{
using namespace ::testing;

using namespace iox;
using namespace iox::roudi;

using iox::ShmName_t;
Expand All @@ -48,7 +49,7 @@ class PosixShmMemoryProvider_Test : public Test
bool shmExists()
{
return !iox::PosixSharedMemoryObjectBuilder()
.name(TEST_SHM_NAME)
.name(concatenate(iceoryxResourcePrefix(DEFAULT_UNIQUE_ROUDI_ID), TEST_SHM_NAME))
.memorySizeInBytes(8)
.accessMode(iox::AccessMode::READ_ONLY)
.openMode(iox::OpenMode::OPEN_EXISTING)
Expand Down

0 comments on commit 26394e6

Please sign in to comment.