Skip to content

Commit

Permalink
Expose servus_host and servus_port keys
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Eilemann authored and Stefan Eilemann committed Feb 22, 2018
1 parent 2e9a23a commit 8117ff0
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 8 deletions.
2 changes: 2 additions & 0 deletions doc/Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

# git master

* [82](https://github.com/HBPVis/Servus/pull/20):
Expose servus_host and servus_port keys.
* [80](https://github.com/HBPVis/Servus/pull/80):
Failsafe when Servus implementation can't be created and fallback to dummy.
* [77](https://github.com/HBPVis/Servus/pull/77):
Expand Down
12 changes: 7 additions & 5 deletions servus/avahi/servus.h
Original file line number Diff line number Diff line change
Expand Up @@ -313,16 +313,17 @@ 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,
AvahiStringList* txt, AvahiLookupResultFlags flags,
void* servus)
const char* host, const AvahiAddress*,
const 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, const uint16_t port, AvahiStringList* txt,
const AvahiLookupResultFlags flags)
{
// If browsing through the local interface, consider only the local
Expand All @@ -342,6 +343,7 @@ class Servus : public servus::Servus::Impl
{
ValueMap& values = _instanceMap[name];
values["servus_host"] = host;
values["servus_port"] = std::to_string(port);
for (; txt; txt = txt->next)
{
const std::string entry(reinterpret_cast<const char*>(
Expand Down
8 changes: 5 additions & 3 deletions servus/dnssd/servus.h
Original file line number Diff line number Diff line change
Expand Up @@ -294,19 +294,21 @@ class Servus : public servus::Servus::Impl
static void resolveCBS_(DNSServiceRef, DNSServiceFlags,
uint32_t /*interfaceIdx*/,
DNSServiceErrorType error, const char* /*name*/,
const char* host, uint16_t /*port*/,
const char* host, const 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, const uint16_t port, uint16_t txtLen,
const unsigned char* txt)
{
ValueMap& values = _instanceMap[_browsedName];
values["servus_host"] = host;
values["servus_port"] = std::to_string(port);

char key[256] = {0};
const char* value = 0;
Expand Down
3 changes: 3 additions & 0 deletions servus/servus.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ static const std::string TEST_DRIVER{"_servus._test"};
* to browse a ZeroConf service. If the Servus library is compiled without
* zeroconf support (@sa isAvailable()), this class does not do anything useful.
*
* The keys 'servus_host' and 'servus_port' are set for discovered instances to
* the values of the implementation-specific dnssd hostname and port.
*
* Example: @include tests/servus.cpp
*/
class Servus
Expand Down
1 change: 1 addition & 0 deletions servus/test/servus.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ class Servus : public servus::Servus::Impl
ValueMap& values = _instanceMap[i->_instance];
values.clear();
values["servus_host"] = "localhost";
values["servus_port"] = std::to_string(_port);

for (const auto& j : i->_data)
values[j.first] = j.second;
Expand Down
3 changes: 3 additions & 0 deletions tests/servus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ void test(const std::string& serviceName)
BOOST_REQUIRE_EQUAL(hosts.size(), 1);
BOOST_CHECK_EQUAL(hosts.front(), std::to_string(port));
BOOST_CHECK(service.containsKey(hosts.front(), "foo"));
BOOST_CHECK(service.containsKey(hosts.front(), "servus_host"));
BOOST_CHECK_EQUAL(service.get(hosts.front(), "servus_port"),
std::to_string(port));
BOOST_CHECK_EQUAL(service.get(hosts.front(), "foo"), "bar");
BOOST_CHECK_EQUAL(service.get("bar", "foo"), std::string());
BOOST_CHECK_EQUAL(service.get(hosts.front(), "foobar"), std::string());
Expand Down

0 comments on commit 8117ff0

Please sign in to comment.