ajusted avahi implementation for shared data

updated the example for shared data
This commit is contained in:
Matthias Kollmann
2017-10-06 17:17:19 +02:00
parent 5895c408be
commit 3be8677b02
3 changed files with 29 additions and 32 deletions

View File

@@ -105,6 +105,8 @@ public:
QString key = name + QString::number(interface);
QZeroConfPrivate *ref = static_cast<QZeroConfPrivate *>(userdata);
QZeroConfService zcs;
switch (event) {
case AVAHI_BROWSER_FAILURE:
ref->broswerCleanUp();
@@ -122,13 +124,12 @@ public:
if (!ref->pub->services.contains(key))
return;
QZeroConfService *zcs;
zcs = ref->pub->services[key];
ref->pub->services.remove(key);
emit ref->pub->serviceRemoved(zcs);
delete zcs;
break;
case AVAHI_BROWSER_ALL_FOR_NOW:
case AVAHI_BROWSER_ALL_FOR_NOW:
case AVAHI_BROWSER_CACHE_EXHAUSTED:
break;
}
@@ -150,7 +151,7 @@ public:
AVAHI_GCC_UNUSED void* userdata)
{
bool newRecord = 0;
QZeroConfService *zcs;
QZeroConfService zcs;
QZeroConfPrivate *ref = static_cast<QZeroConfPrivate *>(userdata);
QString key = name + QString::number(interface);
@@ -158,22 +159,21 @@ public:
if (ref->pub->services.contains(key))
zcs = ref->pub->services[key];
else {
newRecord = 1;
zcs = new QZeroConfService;
zcs->name = name;
zcs->type = type;
zcs->domain = domain;
zcs->host = host_name;
zcs->interfaceIndex = interface;
zcs->port = port;
newRecord = 1;
zcs.setName(name);
zcs.setType(type);
zcs.setDomain( domain);
zcs.setHost( host_name);
zcs.setInterfaceIndex(interface);
zcs.setPort(port);
while (txt) // get txt records
{
QByteArray avahiText((const char *)txt->text, txt->size);
QList<QByteArray> pair = avahiText.split('=');
if (pair.size() == 2)
zcs->txt[pair.at(0)] = pair.at(1);
zcs.appendTxt(pair.at(0), pair.at(1));
else
zcs->txt[pair.at(0)] = "";
zcs.appendTxt(pair.at(0));
txt = txt->next;
}
ref->pub->services.insert(key, zcs);
@@ -181,10 +181,11 @@ public:
char a[AVAHI_ADDRESS_STR_MAX];
avahi_address_snprint(a, sizeof(a), address);
QHostAddress addr(a);
if (protocol == AVAHI_PROTO_INET6)
zcs->ipv6 = QHostAddress(a);
zcs.setIpv6(addr);
else if (protocol == AVAHI_PROTO_INET)
zcs->ip = QHostAddress (a);
zcs.setIp(addr);
if (newRecord)
emit ref->pub->serviceAdded(zcs);
@@ -195,8 +196,7 @@ public:
zcs = ref->pub->services[key];
ref->pub->services.remove(key);
emit ref->pub->serviceRemoved(zcs);
delete zcs;
// don't delete the resolver here...we need to keep it around so Avahi will keep updating....might be able to resolve the service in the future
// don't delete the resolver here...we need to keep it around so Avahi will keep updating....might be able to resolve the service in the future
}
}
@@ -207,9 +207,6 @@ public:
avahi_service_browser_free(browser);
browser = NULL;
QMap<QString, QZeroConfService *>::iterator i;
for (i = pub->services.begin(); i != pub->services.end(); i++)
delete *i;
pub->services.clear();
QMap<QString, AvahiServiceResolver *>::iterator r;
@@ -230,7 +227,7 @@ public:
};
QZeroConf::QZeroConf()
QZeroConf::QZeroConf(QObject *parent) : QObject(parent)
{
pri = new QZeroConfPrivate(this);
}

View File

@@ -52,8 +52,8 @@ mainWindow::mainWindow()
publishEnabled = 0;
buildGUI();
connect(&zeroConf, SIGNAL(serviceAdded(QZeroConfService *)), this, SLOT(addService(QZeroConfService *)));
connect(&zeroConf, SIGNAL(serviceRemoved(QZeroConfService *)), this, SLOT(removeService(QZeroConfService *)));
connect(&zeroConf, SIGNAL(serviceAdded(QZeroConfService )), this, SLOT(addService(QZeroConfService )));
connect(&zeroConf, SIGNAL(serviceRemoved(QZeroConfService )), this, SLOT(removeService(QZeroConfService )));
zeroConf.startBrowser("_qtzeroconf_test._tcp");
startPublish();
@@ -126,16 +126,16 @@ void mainWindow::stopPublish()
// ---------- Discovery Callbacks ----------
void mainWindow::addService(QZeroConfService *zcs)
void mainWindow::addService(QZeroConfService zcs)
{
qint32 row;
QTableWidgetItem *cell;
row = table.rowCount();
table.insertRow(row);
cell = new QTableWidgetItem(zcs->name);
cell = new QTableWidgetItem(zcs.name());
table.setItem(row, 0, cell);
cell = new QTableWidgetItem(zcs->ip.toString());
cell = new QTableWidgetItem(zcs.ip().toString());
table.setItem(row, 1, cell);
table.resizeColumnsToContents();
#if !(defined(Q_OS_IOS) || defined(Q_OS_ANDROID))
@@ -143,17 +143,17 @@ void mainWindow::addService(QZeroConfService *zcs)
#endif
}
void mainWindow::removeService(QZeroConfService *zcs)
void mainWindow::removeService(QZeroConfService zcs)
{
qint32 i, row;
QTableWidgetItem *nameItem, *ipItem;
QList <QTableWidgetItem*> search = table.findItems(zcs->name, Qt::MatchExactly);
QList <QTableWidgetItem*> search = table.findItems(zcs.name(), Qt::MatchExactly);
for (i=0; i<search.size(); i++) {
nameItem = search.at(i);
row = nameItem->row();
ipItem = table.item(row, 1);
if (table.item(row, 1)->text() == zcs->ip.toString()) { // match ip address in case of dual homed systems
if (table.item(row, 1)->text() == zcs.ip().toString()) { // match ip address in case of dual homed systems
delete nameItem;
delete ipItem;
table.removeRow(row);

View File

@@ -48,8 +48,8 @@ private:
private slots:
void startPublish();
void stopPublish();
void addService(QZeroConfService *item);
void removeService(QZeroConfService *item);
void addService(QZeroConfService item);
void removeService(QZeroConfService item);
};
#endif /* WINDOW_H_ */