Skip to content

Commit de98450

Browse files
committed
add Servus::getPort()
the port information is stored in 'servus_port' (as a string) Closes: #109
1 parent 1f3514e commit de98450

5 files changed

Lines changed: 27 additions & 7 deletions

File tree

servus/avahi/servus.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -313,16 +313,16 @@ class Servus : public servus::Servus::Impl
313313
static void _resolveCBS(AvahiServiceResolver* resolver, AvahiIfIndex,
314314
AvahiProtocol, AvahiResolverEvent event,
315315
const char* name, const char*, const char*,
316-
const char* host, const AvahiAddress*, uint16_t,
316+
const char* host, const AvahiAddress*, uint16_t port,
317317
AvahiStringList* txt, AvahiLookupResultFlags flags,
318318
void* servus)
319319
{
320-
((Servus*)servus)->_resolveCB(resolver, event, name, host, txt, flags);
320+
((Servus*)servus)->_resolveCB(resolver, event, name, host, port, txt, flags);
321321
}
322322

323323
void _resolveCB(AvahiServiceResolver* resolver,
324324
const AvahiResolverEvent event, const char* name,
325-
const char* host, AvahiStringList* txt,
325+
const char* host, uint16_t port, AvahiStringList* txt,
326326
const AvahiLookupResultFlags flags)
327327
{
328328
// If browsing through the local interface, consider only the local
@@ -342,6 +342,7 @@ class Servus : public servus::Servus::Impl
342342
{
343343
ValueMap& values = _instanceMap[name];
344344
values["servus_host"] = host;
345+
values["servus_port"] = std::to_string(static_cast<int>(port));
345346
for (; txt; txt = txt->next)
346347
{
347348
const std::string entry(reinterpret_cast<const char*>(

servus/dnssd/servus.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -294,19 +294,20 @@ class Servus : public servus::Servus::Impl
294294
static void DNSSD_API resolveCBS_(DNSServiceRef, DNSServiceFlags,
295295
uint32_t /*interfaceIdx*/,
296296
DNSServiceErrorType error, const char* /*name*/,
297-
const char* host, uint16_t /*port*/,
297+
const char* host, uint16_t port,
298298
uint16_t txtLen, const unsigned char* txt,
299299
Servus* servus)
300300
{
301301
if (error == kDNSServiceErr_NoError)
302-
servus->resolveCB_(host, txtLen, txt);
302+
servus->resolveCB_(host, port, txtLen, txt);
303303
servus->_result = error;
304304
}
305305

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

311312
char key[256] = {0};
312313
const char* value = 0;

servus/servus.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,19 @@ const std::string& Servus::getHost(const std::string& instance) const
369369
return get(instance, "servus_host");
370370
}
371371

372+
uint16_t Servus::getPort(const std::string& instance) const
373+
{
374+
std::string sport = get(instance, "servus_port");
375+
try {
376+
const int port = std::stoi (sport);
377+
if (port < 0 || port > 0xFFFF)
378+
return 0;
379+
return static_cast<uint16_t>(port);
380+
} catch (const std::exception&) {
381+
return 0;
382+
}
383+
}
384+
372385
bool Servus::containsKey(const std::string& instance,
373386
const std::string& key) const
374387
{

servus/servus.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,9 @@ class Servus
178178
/** @return the host corresponding to the given instance. @version 1.3 */
179179
SERVUS_API const std::string& getHost(const std::string& instance) const;
180180

181+
/** @return the port corresponding to the given instance. @version 1.6 */
182+
SERVUS_API uint16_t getPort(const std::string& instance) const;
183+
181184
/** @return true if the given key was discovered. @version 1.1 */
182185
SERVUS_API bool containsKey(const std::string& instance,
183186
const std::string& key) const;

tests/itemModel.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,15 +178,17 @@ BOOST_AUTO_TEST_CASE(servusItemModel)
178178
model.data(instanceIndex, Qt::UserRole).toString().toStdString(),
179179
service.get(TEST_INSTANCE, "servus_host"));
180180
BOOST_CHECK(model.data(instanceIndex, Qt::EditRole) == QVariant());
181-
BOOST_REQUIRE_EQUAL(model.rowCount(instanceIndex), 2);
181+
BOOST_REQUIRE_EQUAL(model.rowCount(instanceIndex), 3);
182182
const QModelIndex kv1Index = model.index(0, 0, instanceIndex);
183183
BOOST_CHECK(model.parent(kv1Index) == instanceIndex);
184184
BOOST_CHECK(model.data(kv1Index, Qt::UserRole) == QVariant());
185185
const QVariant kv1 = model.data(kv1Index);
186186
const QVariant kv2 = model.data(model.index(1, 0, instanceIndex));
187+
const QVariant kv3 = model.data(model.index(2, 0, instanceIndex));
187188
BOOST_REQUIRE_EQUAL(model.rowCount(kv1Index), 0);
188189
BOOST_CHECK_EQUAL(kv1.toString().toStdString(), "foo = bar");
189190
BOOST_CHECK(kv2.toString().startsWith("servus_host = "));
191+
BOOST_CHECK(kv3.toString().startsWith("servus_port = "));
190192

191193
WatchRemove watchRemove;
192194
service.addListener(&watchRemove);

0 commit comments

Comments
 (0)