Skip to content

Commit

Permalink
Merge pull request #1134 from ApexAI/iox-#27-make-req-res-ports-searc…
Browse files Browse the repository at this point in the history
…hable-in-service-discovery

iox-#27 Run `ServiceDiscovery` tests for `popo::UntypedServer`
  • Loading branch information
mossmaurice authored Feb 22, 2022
2 parents a1ed109 + 4494b5d commit e219b1f
Show file tree
Hide file tree
Showing 5 changed files with 524 additions and 305 deletions.
27 changes: 19 additions & 8 deletions iceoryx_examples/icediscovery/iox_find_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,27 +48,38 @@ int main()
serviceDiscovery.findService(iox::capro::IdString_t{"Radar"},
iox::capro::IdString_t{"FrontLeft"},
iox::capro::IdString_t{"Image"},
printSearchResult);
printSearchResult,
iox::popo::MessagingPattern::PUB_SUB);
//! [search for unique service]

std::cout << "\nSearched for {'Radar', *, *}. Found the following services:" << std::endl;
serviceDiscovery.findService(
iox::capro::IdString_t{"Radar"}, iox::capro::Wildcard, iox::capro::Wildcard, printSearchResult);
serviceDiscovery.findService(iox::capro::IdString_t{"Radar"},
iox::capro::Wildcard,
iox::capro::Wildcard,
printSearchResult,
iox::popo::MessagingPattern::PUB_SUB);

std::cout << "\nSearched for {*, 'FrontLeft', *}. Found the following services:" << std::endl;
serviceDiscovery.findService(
iox::capro::Wildcard, iox::capro::IdString_t{"FrontLeft"}, iox::capro::Wildcard, printSearchResult);
serviceDiscovery.findService(iox::capro::Wildcard,
iox::capro::IdString_t{"FrontLeft"},
iox::capro::Wildcard,
printSearchResult,
iox::popo::MessagingPattern::PUB_SUB);

std::cout << "\nSearched for {*, 'FrontRight', 'Image'}. Found the following services:" << std::endl;
serviceDiscovery.findService(iox::capro::Wildcard,
iox::capro::IdString_t{"FrontRight"},
iox::capro::IdString_t{"Image"},
printSearchResult);
printSearchResult,
iox::popo::MessagingPattern::PUB_SUB);

std::cout << "\nSearched for {'Camera', *, *}. Found the following services:" << std::endl;
//! [search for all Camera services]
serviceDiscovery.findService(
iox::capro::IdString_t{"Camera"}, iox::capro::Wildcard, iox::capro::Wildcard, printSearchResult);
serviceDiscovery.findService(iox::capro::IdString_t{"Camera"},
iox::capro::Wildcard,
iox::capro::Wildcard,
printSearchResult,
iox::popo::MessagingPattern::PUB_SUB);
//! [search for all Camera services]

std::this_thread::sleep_for(std::chrono::seconds(1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ namespace iox
error(POSH__RUNTIME_NAME_EMPTY) \
error(POSH__RUNTIME_LEADING_SLASH_PROVIDED) \
error(POSH__SERVICE_DISCOVERY_UNKNOWN_EVENT_PROVIDED) \
error(POSH__SERVICE_DISCOVERY_UNKNOWN_MESSAGE_PATTERN_PROVIDED) \
error(POSH__PORT_MANAGER_PUBLISHERPORT_NOT_UNIQUE) \
error(POSH__PORT_MANAGER_SERVERPORT_NOT_UNIQUE) \
error(POSH__PORT_MANAGER_COULD_NOT_ADD_SERVICE_TO_REGISTRY) \
Expand Down
14 changes: 12 additions & 2 deletions iceoryx_posh/include/iceoryx_posh/runtime/service_discovery.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@

namespace iox
{
namespace popo
{
enum class MessagingPattern
{
PUB_SUB,
REQ_RES
};
}
namespace runtime
{
enum class ServiceDiscoveryEvent : popo::EventEnumIdentifier
Expand All @@ -48,7 +56,8 @@ class ServiceDiscovery
/// ServiceContainer: container that is filled with all matching instances
ServiceContainer findService(const cxx::optional<capro::IdString_t>& service,
const cxx::optional<capro::IdString_t>& instance,
const cxx::optional<capro::IdString_t>& event) noexcept;
const cxx::optional<capro::IdString_t>& event,
const popo::MessagingPattern pattern) noexcept;

/// @brief Searches all services that match the provided service description and applies a function to each of them
/// @param[in] service service string to search for, a nullopt corresponds to a wildcard
Expand All @@ -58,7 +67,8 @@ class ServiceDiscovery
void findService(const cxx::optional<capro::IdString_t>& service,
const cxx::optional<capro::IdString_t>& instance,
const cxx::optional<capro::IdString_t>& event,
const cxx::function_ref<void(const capro::ServiceDescription&)>& callableForEach) noexcept;
const cxx::function_ref<void(const capro::ServiceDescription&)>& callableForEach,
const popo::MessagingPattern pattern) noexcept;

friend iox::popo::NotificationAttorney;

Expand Down
56 changes: 40 additions & 16 deletions iceoryx_posh/source/runtime/service_discovery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,22 @@ namespace runtime
{
ServiceContainer ServiceDiscovery::findService(const cxx::optional<capro::IdString_t>& service,
const cxx::optional<capro::IdString_t>& instance,
const cxx::optional<capro::IdString_t>& event) noexcept
const cxx::optional<capro::IdString_t>& event,
const popo::MessagingPattern pattern) noexcept
{
ServiceContainer searchResult;

auto lambda = [&](const capro::ServiceDescription& entry) { searchResult.emplace_back(entry); };
findService(service, instance, event, lambda);
findService(service, instance, event, lambda, pattern);

return searchResult;
}

void ServiceDiscovery::findService(
const cxx::optional<capro::IdString_t>& service,
const cxx::optional<capro::IdString_t>& instance,
const cxx::optional<capro::IdString_t>& event,
const cxx::function_ref<void(const capro::ServiceDescription&)>& callableForEach) noexcept
void ServiceDiscovery::findService(const cxx::optional<capro::IdString_t>& service,
const cxx::optional<capro::IdString_t>& instance,
const cxx::optional<capro::IdString_t>& event,
const cxx::function_ref<void(const capro::ServiceDescription&)>& callableForEach,
const popo::MessagingPattern pattern) noexcept
{
if (!callableForEach)
{
Expand All @@ -48,13 +49,36 @@ void ServiceDiscovery::findService(
m_serviceRegistry = *serviceRegistrySample;
});

m_serviceRegistry.find(
service, instance, event, [&](const roudi::ServiceRegistry::ServiceDescriptionEntry& serviceEntry) {
if (serviceEntry.publisherCount > 0)
{
callableForEach(serviceEntry.serviceDescription);
}
});
switch (pattern)
{
case popo::MessagingPattern::PUB_SUB:
{
m_serviceRegistry.find(
service, instance, event, [&](const roudi::ServiceRegistry::ServiceDescriptionEntry& serviceEntry) {
if (serviceEntry.publisherCount > 0)
{
callableForEach(serviceEntry.serviceDescription);
}
});
break;
}
case popo::MessagingPattern::REQ_RES:
{
m_serviceRegistry.find(
service, instance, event, [&](const roudi::ServiceRegistry::ServiceDescriptionEntry& serviceEntry) {
if (serviceEntry.serverCount > 0)
{
callableForEach(serviceEntry.serviceDescription);
}
});
break;
}
default:
{
LogWarn() << "ServiceDiscovery could not perform search due to unknown MessagingPattern!";
errorHandler(Error::kPOSH__SERVICE_DISCOVERY_UNKNOWN_MESSAGE_PATTERN_PROVIDED, nullptr, ErrorLevel::MODERATE);
}
}
}

void ServiceDiscovery::enableEvent(popo::TriggerHandle&& triggerHandle, const ServiceDiscoveryEvent event) noexcept
Expand All @@ -68,7 +92,7 @@ void ServiceDiscovery::enableEvent(popo::TriggerHandle&& triggerHandle, const Se
}
default:
{
LogWarn() << "ServiceDiscovery::enableEvent() called with unkown event!";
LogWarn() << "ServiceDiscovery::enableEvent() called with unknown event!";
errorHandler(Error::kPOSH__SERVICE_DISCOVERY_UNKNOWN_EVENT_PROVIDED, nullptr, ErrorLevel::MODERATE);
}
}
Expand All @@ -85,7 +109,7 @@ void ServiceDiscovery::disableEvent(const ServiceDiscoveryEvent event) noexcept
}
default:
{
LogWarn() << "ServiceDiscovery::disableEvent() called with unkown event!";
LogWarn() << "ServiceDiscovery::disableEvent() called with unknown event!";
errorHandler(Error::kPOSH__SERVICE_DISCOVERY_UNKNOWN_EVENT_PROVIDED, nullptr, ErrorLevel::MODERATE);
}
}
Expand Down
Loading

0 comments on commit e219b1f

Please sign in to comment.