Skip to content

Commit

Permalink
add Servus::getPort()
Browse files Browse the repository at this point in the history
the port information is stored in 'servus_port' (as a string)

Closes: HBPVIS#109
  • Loading branch information
umlaeute committed Mar 7, 2023
1 parent 1f3514e commit d6e4aa1
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 7 deletions.
7 changes: 4 additions & 3 deletions servus/avahi/servus.h
Original file line number Diff line number Diff line change
Expand Up @@ -313,16 +313,16 @@ class Servus : public servus::Servus::Impl
static void _resolveCBS(AvahiServiceResolver* resolver, AvahiIfIndex,
AvahiProtocol, AvahiResolverEvent event,
const char* name, const char*, const char*,
const char* host, const AvahiAddress*, uint16_t,
const char* host, const AvahiAddress*, uint16_t port,
AvahiStringList* txt, AvahiLookupResultFlags flags,
void* servus)
{
((Servus*)servus)->_resolveCB(resolver, event, name, host, txt, flags);
((Servus*)servus)->_resolveCB(resolver, event, name, host, port, txt, flags);
}

void _resolveCB(AvahiServiceResolver* resolver,
const AvahiResolverEvent event, const char* name,
const char* host, AvahiStringList* txt,
const char* host, uint16_t port, AvahiStringList* txt,
const AvahiLookupResultFlags flags)
{
// If browsing through the local interface, consider only the local
Expand All @@ -342,6 +342,7 @@ class Servus : public servus::Servus::Impl
{
ValueMap& values = _instanceMap[name];
values["servus_host"] = host;
values["servus_port"] = std::to_string(static_cast<int>(port));
for (; txt; txt = txt->next)
{
const std::string entry(reinterpret_cast<const char*>(
Expand Down
7 changes: 4 additions & 3 deletions servus/dnssd/servus.h
Original file line number Diff line number Diff line change
Expand Up @@ -294,19 +294,20 @@ class Servus : public servus::Servus::Impl
static void DNSSD_API resolveCBS_(DNSServiceRef, DNSServiceFlags,
uint32_t /*interfaceIdx*/,
DNSServiceErrorType error, const char* /*name*/,
const char* host, uint16_t /*port*/,
const char* host, uint16_t port,
uint16_t txtLen, const unsigned char* txt,
Servus* servus)
{
if (error == kDNSServiceErr_NoError)
servus->resolveCB_(host, txtLen, txt);
servus->resolveCB_(host, port, txtLen, txt);
servus->_result = error;
}

void resolveCB_(const char* host, uint16_t txtLen, const unsigned char* txt)
void resolveCB_(const char* host, uint16_t port, uint16_t txtLen, const unsigned char* txt)
{
ValueMap& values = _instanceMap[_browsedName];
values["servus_host"] = host;
values["servus_port"] = std::to_string(static_cast<int>(port));

char key[256] = {0};
const char* value = 0;
Expand Down
13 changes: 13 additions & 0 deletions servus/servus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,19 @@ const std::string& Servus::getHost(const std::string& instance) const
return get(instance, "servus_host");
}

uint16_t Servus::getPort(const std::string& instance) const
{
std::string sport = get(instance, "servus_port");
try {
const int port = std::stoi (sport);
if (port < 0 || port > 0xFFFF)
return 0;
return static_cast<uint16_t>(port);
} catch (const std::exception&) {
return 0;
}
}

bool Servus::containsKey(const std::string& instance,
const std::string& key) const
{
Expand Down
3 changes: 3 additions & 0 deletions servus/servus.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@ class Servus
/** @return the host corresponding to the given instance. @version 1.3 */
SERVUS_API const std::string& getHost(const std::string& instance) const;

/** @return the port corresponding to the given instance. @version 1.5 */
SERVUS_API uint16_t getPort(const std::string& instance) const;

/** @return true if the given key was discovered. @version 1.1 */
SERVUS_API bool containsKey(const std::string& instance,
const std::string& key) const;
Expand Down
4 changes: 3 additions & 1 deletion tests/itemModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,15 +178,17 @@ BOOST_AUTO_TEST_CASE(servusItemModel)
model.data(instanceIndex, Qt::UserRole).toString().toStdString(),
service.get(TEST_INSTANCE, "servus_host"));
BOOST_CHECK(model.data(instanceIndex, Qt::EditRole) == QVariant());
BOOST_REQUIRE_EQUAL(model.rowCount(instanceIndex), 2);
BOOST_REQUIRE_EQUAL(model.rowCount(instanceIndex), 3);
const QModelIndex kv1Index = model.index(0, 0, instanceIndex);
BOOST_CHECK(model.parent(kv1Index) == instanceIndex);
BOOST_CHECK(model.data(kv1Index, Qt::UserRole) == QVariant());
const QVariant kv1 = model.data(kv1Index);
const QVariant kv2 = model.data(model.index(1, 0, instanceIndex));
const QVariant kv3 = model.data(model.index(2, 0, instanceIndex));
BOOST_REQUIRE_EQUAL(model.rowCount(kv1Index), 0);
BOOST_CHECK_EQUAL(kv1.toString().toStdString(), "foo = bar");
BOOST_CHECK(kv2.toString().startsWith("servus_host = "));
BOOST_CHECK(kv3.toString().startsWith("servus_port = "));

WatchRemove watchRemove;
service.addListener(&watchRemove);
Expand Down

0 comments on commit d6e4aa1

Please sign in to comment.