1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
/*
Aseba - an event-based framework for distributed robot control
Copyright (C) 2007--2016:
Stephane Magnenat <stephane at magnenat dot net>
(http://stephane.magnenat.net)
and other contributors, see authors.txt for details
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, version 3 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QSocketNotifier>
#include <QSignalMapper>
#include "../utils/FormatableString.h"
#include "zeroconf-qt.h"
#include "dns_sd.h"
using namespace std;
namespace Aseba
{
//! Destructor, clear all targets
QtZeroconf::~QtZeroconf()
{
// clear all targets
targets.clear();
// release browse serviceRef
releaseServiceRef(browseServiceRef);
}
//! Create a socket notifier for the provided service reference, and start watching
//! its file descriptor for activity.
void QtZeroconf::processServiceRef(DNSServiceRef serviceRef)
{
assert(serviceRef);
auto notifier = new ServiceRefSocketNotifier(serviceRef, this);
QObject::connect(notifier, SIGNAL(activated(int)), this, SLOT(doIncoming(int)));
zeroconfSockets.emplace(notifier->socket(), notifier);
}
//! Remove the socket notifier associated with the provided service reference,
//! and later deallocate it (through Qt's deleteLater mechanism).
void QtZeroconf::releaseServiceRef(DNSServiceRef serviceRef)
{
if (!serviceRef)
return;
// safety check
int socket = DNSServiceRefSockFD(serviceRef);
assert(socket != -1);
// remove the notifier and delete it later
auto socketNotifierIt(zeroconfSockets.find(socket));
if (socketNotifierIt != zeroconfSockets.end())
{
socketNotifierIt->second->deleteLater();
zeroconfSockets.erase(socketNotifierIt);
}
}
//! Emit signal when register completed.
//! If you override this method you take responsibility for emitting signals as you see fit.
void QtZeroconf::registerCompleted(const Aseba::Zeroconf::TargetInformation & target)
{
emit zeroconfRegisterCompleted(target);
}
//! Emit signal when update completed.
//! If you override this method you take responsibility for emitting signals as you see fit.
void QtZeroconf::updateCompleted(const Aseba::Zeroconf::TargetInformation & target)
{
emit zeroconfUpdateCompleted(target);
}
//! Emit signal when resolve completed.
//! If you override this method you take responsibility for emitting signals as you see fit.
void QtZeroconf::targetFound(const Aseba::Zeroconf::TargetInformation & target)
{
emit zeroconfTargetFound(target);
}
//! Emit signal when target is removed.
//! If you override this method you take responsibility for emitting signals as you see fit.
void QtZeroconf::targetRemoved(const Aseba::Zeroconf::TargetInformation & target)
{
emit zeroconfTargetRemoved(target);
}
//! Data are available on a zeroconf socket
void QtZeroconf::doIncoming(int socket)
{
auto streamIt(zeroconfSockets.find(socket));
if (streamIt != zeroconfSockets.end())
incomingData(streamIt->second->serviceRef);
}
//! Process incoming data associated to a service reference
void QtZeroconf::incomingData(DNSServiceRef serviceRef)
{
DNSServiceErrorType err = DNSServiceProcessResult(serviceRef);
if (err != kDNSServiceErr_NoError)
throw Zeroconf::Error(FormatableString("DNSServiceProcessResult (service ref %1): error %0").arg(err).arg(serviceRef));
}
//! Create a Qt socket notifier in read mode for the file descriptor of the provided service reference
QtZeroconf::ServiceRefSocketNotifier::ServiceRefSocketNotifier(DNSServiceRef serviceRef, QObject *parent):
QSocketNotifier(DNSServiceRefSockFD(serviceRef), QSocketNotifier::Read, parent),
serviceRef(serviceRef)
{ }
//! Deallocate the service reference
QtZeroconf::ServiceRefSocketNotifier::~ServiceRefSocketNotifier()
{
DNSServiceRefDeallocate(serviceRef);
}
} // namespace Aseba
|