2011-01-11 21:36:16 +01:00
|
|
|
#include "avahi.h"
|
|
|
|
|
|
|
|
#include <QDBusConnection>
|
|
|
|
#include <QHostInfo>
|
|
|
|
|
|
|
|
#include <QtDebug>
|
|
|
|
|
|
|
|
#include "dbus/avahientrygroup.h"
|
|
|
|
#include "dbus/avahiserver.h"
|
|
|
|
|
2011-01-22 13:47:19 +01:00
|
|
|
Avahi::Avahi()
|
|
|
|
: server_(NULL),
|
|
|
|
entry_group_(NULL)
|
|
|
|
{
|
2011-01-11 21:36:16 +01:00
|
|
|
}
|
|
|
|
|
2011-01-22 13:47:19 +01:00
|
|
|
void Avahi::Publish(const QString& domain,
|
|
|
|
const QString& type,
|
|
|
|
const QString& name,
|
|
|
|
quint16 port) {
|
|
|
|
if (server_) {
|
|
|
|
// Already published
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
domain_ = domain;
|
|
|
|
type_ = type;
|
|
|
|
name_ = name;
|
|
|
|
port_ = port;
|
|
|
|
|
|
|
|
server_ = new OrgFreedesktopAvahiServerInterface(
|
2011-01-11 21:36:16 +01:00
|
|
|
"org.freedesktop.Avahi",
|
|
|
|
"/",
|
2011-01-22 13:47:19 +01:00
|
|
|
QDBusConnection::systemBus(),
|
|
|
|
this);
|
2011-01-11 21:36:16 +01:00
|
|
|
|
2011-01-22 13:47:19 +01:00
|
|
|
QDBusPendingReply<QDBusObjectPath> reply = server_->EntryGroupNew();
|
|
|
|
QDBusPendingCallWatcher* watcher = new QDBusPendingCallWatcher(reply);
|
|
|
|
connect(watcher, SIGNAL(finished(QDBusPendingCallWatcher*)),
|
|
|
|
SLOT(EntryGroupNewFinished(QDBusPendingCallWatcher*)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Avahi::EntryGroupNewFinished(QDBusPendingCallWatcher* call) {
|
|
|
|
call->deleteLater();
|
|
|
|
QDBusPendingReply<QDBusObjectPath> reply = *call;
|
|
|
|
|
|
|
|
if (reply.isError()) {
|
2011-01-24 13:44:19 +01:00
|
|
|
qWarning() << "Failed to create new Avahi entry group:" << call->error().message();
|
2011-01-22 13:47:19 +01:00
|
|
|
return;
|
|
|
|
}
|
2011-01-11 21:36:16 +01:00
|
|
|
|
2011-01-22 13:47:19 +01:00
|
|
|
entry_group_ = new OrgFreedesktopAvahiEntryGroupInterface(
|
2011-01-11 21:36:16 +01:00
|
|
|
"org.freedesktop.Avahi",
|
|
|
|
reply.value().path(),
|
2011-01-22 13:47:19 +01:00
|
|
|
QDBusConnection::systemBus(),
|
|
|
|
this);
|
2011-01-11 21:36:16 +01:00
|
|
|
|
2011-01-22 13:47:19 +01:00
|
|
|
QDBusPendingReply<> add_reply = entry_group_->AddService(
|
2011-01-11 21:36:16 +01:00
|
|
|
-1, // Interface (Unspecified, ie. all interfaces)
|
|
|
|
-1, // Protocol (Unspecified, ie. IPv4 & IPv6)
|
|
|
|
0, // Flags
|
2011-01-22 13:47:19 +01:00
|
|
|
name_, // Service name
|
|
|
|
type_, // Service type
|
|
|
|
domain_, // Domain, ie. local
|
2011-01-11 21:36:16 +01:00
|
|
|
QString::null, // Hostname (Avahi fills it if it's null)
|
2011-01-22 13:47:19 +01:00
|
|
|
port_, // Port
|
2011-01-11 21:36:16 +01:00
|
|
|
QList<QByteArray>()); // TXT record
|
2011-01-22 13:47:19 +01:00
|
|
|
QDBusPendingCallWatcher* watcher = new QDBusPendingCallWatcher(add_reply);
|
|
|
|
connect(watcher, SIGNAL(finished(QDBusPendingCallWatcher*)),
|
|
|
|
SLOT(AddServiceFinished(QDBusPendingCallWatcher*)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Avahi::AddServiceFinished(QDBusPendingCallWatcher* call) {
|
|
|
|
call->deleteLater();
|
|
|
|
|
|
|
|
if (call->isError()) {
|
2011-01-24 13:44:19 +01:00
|
|
|
qWarning() << "Failed to add Avahi service:" << call->error().message();
|
2011-01-22 13:47:19 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QDBusPendingReply<> commit_reply = entry_group_->Commit();
|
|
|
|
QDBusPendingCallWatcher* watcher = new QDBusPendingCallWatcher(commit_reply);
|
|
|
|
connect(watcher, SIGNAL(finished(QDBusPendingCallWatcher*)),
|
|
|
|
SLOT(CommitFinished(QDBusPendingCallWatcher*)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Avahi::CommitFinished(QDBusPendingCallWatcher* call) {
|
|
|
|
call->deleteLater();
|
2011-01-11 21:36:16 +01:00
|
|
|
|
2011-01-22 13:47:19 +01:00
|
|
|
if (call->isError()) {
|
2011-01-24 13:44:19 +01:00
|
|
|
qWarning() << "Failed to commit Avahi changes:" << call->error().message();
|
2011-01-24 21:45:15 +01:00
|
|
|
} else {
|
|
|
|
qDebug() << "Remote interface published on Avahi";
|
2011-01-11 21:36:16 +01:00
|
|
|
}
|
|
|
|
}
|