Commit 95230394 authored by Albert Vaca Cintora's avatar Albert Vaca Cintora

Avahi discovering

parent a736beae
......@@ -6,3 +6,5 @@ include(KDE4Defaults)
include_directories(${KDE4_INCLUDES})
add_subdirectory(src)
add_subdirectory(test)
#!/bin/sh
#!/bin/bash
#Source bashrc to define kdebuild
#http://techbase.kde.org/Getting_Started/Build/Environment
......
set(kded_androidshine_SRCS fakedevicelocator.cpp echodevicelink.cpp udpdevicelink.cpp bonjourdevicelocator.cpp pausemusicpackagereceiver.cpp notificationpackagereceiver.cpp networkpackage.cpp
daemon.cpp devicelink.cpp devicelocator.cpp packagereceiver.cpp
set(kded_androidshine_SRCS
fakedevicelocator.cpp
echodevicelink.cpp
udpdevicelink.cpp
avahidevicelocator.cpp
pausemusicpackagereceiver.cpp
notificationpackagereceiver.cpp
networkpackage.cpp
daemon.cpp
devicelink.cpp
devicelocator.cpp
packagereceiver.cpp
)
kde4_add_plugin(kded_androidshine
${kded_androidshine_SRCS}
)
kde4_add_plugin(kded_androidshine ${kded_androidshine_SRCS})
target_link_libraries(kded_androidshine
${KDE4_KDECORE_LIBS}
${KDE4_KDEUI_LIBS}
kdnssd
${QT_QTNETWORK_LIBRARY}
)
install(TARGETS kded_androidshine DESTINATION ${PLUGIN_INSTALL_DIR})
install(FILES androidshine.desktop DESTINATION ${SERVICES_INSTALL_DIR}/kded)
install(FILES androidshine.notifyrc DESTINATION ${DATA_INSTALL_DIR}/androidshine)
\ No newline at end of file
install(FILES androidshine.notifyrc DESTINATION ${DATA_INSTALL_DIR}/androidshine)
......@@ -18,35 +18,95 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "bonjourdevicelocator.h"
#include "avahidevicelocator.h"
#include "udpdevicelink.h"
BonjourDeviceLocator::BonjourDeviceLocator()
{
/*
AvahiDeviceLocator::AvahiDeviceLocator()
{
//http://api.kde.org/4.x-api/kdelibs-apidocs/dnssd/html/index.html
DNSSD::ServiceBrowser* browser = new DNSSD::ServiceBrowser("_http._tcp");
connect(browser, SIGNAL(serviceAdded(RemoteService::Ptr)),
this, SLOT(addService(RemoteService::Ptr)));
connect(browser, SIGNAL(serviceRemoved(RemoteService::Ptr)),
this, SLOT(delService(RemoteService::Ptr)));
browser = new DNSSD::ServiceBrowser("_device._tcp");
connect(browser, SIGNAL(serviceAdded(DNSSD::RemoteService::Ptr)),this,SLOT(deviceDetected(DNSSD::RemoteService::Ptr)));
connect(browser, SIGNAL(serviceRemoved(DNSSD::RemoteService::Ptr)),this,SLOT(deviceLost(DNSSD::RemoteService::Ptr)));
browser->startBrowse();
In above example addService() will
*/
}
DeviceLink* BonjourDeviceLocator::link(Device* d) {
DeviceLink* dl = new UdpDeviceLink("192.168.1.48");
bool AvahiDeviceLocator::canLink(QString id) {
return visibleDevices.contains(id);
}
DeviceLink* AvahiDeviceLocator::link(QString id) {
if (!visibleDevices.contains(id)) return NULL;
Device* d = visibleDevices[id];
const DNSSD::RemoteService::Ptr& rs = deviceRoutes[d];
DeviceLink* dl = new UdpDeviceLink(QHostAddress(rs->hostName()),rs->port());
linkedDevices.append(dl); //Store the ref to be able to delete the memory later
return dl;
}
bool BonjourDeviceLocator::pair(Device* d) {
bool AvahiDeviceLocator::pair(Device* d)
{
//TODO: ?
d->pair();
return true;
}
QVector<Device*> BonjourDeviceLocator::discover() {
QVector<Device*> devices;
return devices;
void AvahiDeviceLocator::deviceDetected(DNSSD::RemoteService::Ptr s)
{
qDebug() << "DETECTED";
Device* d = getDeviceInfo(s);
if (!visibleDevices.contains(d->id())) {
visibleDevices[d->id()] = d;
deviceRoutes[d] = s;
} else {
qDebug() << "Warning: duplicate device id " + d->id();
}
}
void AvahiDeviceLocator::deviceLost(DNSSD::RemoteService::Ptr s)
{
qDebug() << "LOST";
Device* found = NULL;
for(QMap< Device*, DNSSD::RemoteService::Ptr >::iterator it = deviceRoutes.begin(), itEnd = deviceRoutes.end(); it != itEnd; it++) {
if (s == it.value()) {
found = it.key();
}
}
if (found != NULL) {
visibleDevices.remove(found->id());
deviceRoutes.remove(found);
delete found; //created at getDeviceInfo called from deviceDetected
} else {
//TODO: Add a TAG to the debug messages
qDebug() << "Warning: lost unknown device?";
}
}
Device* AvahiDeviceLocator::getDeviceInfo(DNSSD::RemoteService::Ptr s)
{
//TODO: I have no idea on how to get this info using only DNSSD, wihtout connecting to the device
Device* d = new Device("1234567890", "Unnamed"); //deleted at deviceLost
return d;
}
......@@ -18,27 +18,44 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef BONJOURDEVICELOCATOR_H
#define BONJOURDEVICELOCATOR_H
#ifndef AVAHIDEVICELOCATOR_H
#define AVAHIDEVICELOCATOR_H
#include <QObject>
#include <KDE/DNSSD/ServiceBrowser>
#include <KDE/DNSSD/RemoteService>
#include "devicelocator.h"
class BonjourDeviceLocator
class AvahiDeviceLocator
: public DeviceLocator
{
Q_OBJECT
public:
BonjourDeviceLocator();
AvahiDeviceLocator();
QString getName() { return "Avahi"; }
Priority getPriority() { return PRIORITY_HIGH; }
bool canLink(Device* d) { return true; }
DeviceLink* link(Device* d);
bool canLink(QString id);
DeviceLink* link(QString id);
bool pair(Device* d);
QVector<Device*> discover();
QList<Device*> discover() { return visibleDevices.values(); }
private slots:
void deviceDetected(DNSSD::RemoteService::Ptr s);
void deviceLost(DNSSD::RemoteService::Ptr s);
private:
DNSSD::ServiceBrowser* browser;
Device* getDeviceInfo(DNSSD::RemoteService::Ptr s);
QMap<QString, Device*> visibleDevices;
QMap<Device*, DNSSD::RemoteService::Ptr> deviceRoutes;
QList<DeviceLink*> linkedDevices;
};
#endif // BONJOURDEVICELOCATOR_H
#endif // AVAHIDEVICELOCATOR_H
......@@ -22,7 +22,7 @@
#include "networkpackage.h"
#include "notificationpackagereceiver.h"
#include "pausemusicpackagereceiver.h"
#include "bonjourdevicelocator.h"
#include "avahidevicelocator.h"
#include "fakedevicelocator.h"
#include <QtNetwork/QUdpSocket>
......@@ -30,12 +30,40 @@
#include <KDE/KIcon>
#include <sstream>
#include <iomanip>
#include <iostream>
K_PLUGIN_FACTORY(AndroidShineFactory,
registerPlugin<Daemon>();)
K_PLUGIN_FACTORY(AndroidShineFactory, registerPlugin<Daemon>();)
K_EXPORT_PLUGIN(AndroidShineFactory("androidshine", "androidshine"))
DeviceLink* Daemon::linkTo(QString id) {
DeviceLink* link = NULL;
Q_FOREACH (DeviceLocator* locator, deviceLocators) {
if (locator->canLink(id)) {
link = locator->link(id);
if (link != NULL) break;
}
}
if (link != NULL) {
linkedDevices.append(link);
Q_FOREACH (PackageReceiver* pr, packageReceivers) {
QObject::connect(link,SIGNAL(receivedPackage(const NetworkPackage&)),
pr,SLOT(receivePackage(const NetworkPackage&)));
}
} else {
qDebug() << "Could not link device id " + id;
}
return link;
}
Daemon::Daemon(QObject *parent, const QList<QVariant>&)
: KDEDModule(parent)
......@@ -43,45 +71,55 @@ Daemon::Daemon(QObject *parent, const QList<QVariant>&)
qDebug() << "GO GO GO!";
//TODO: Do not hardcode the load of the package receivers
packageReceivers.push_back(new NotificationPackageReceiver());
packageReceivers.push_back(new PauseMusicPackageReceiver());
//TODO: Do not hardcode the load of the device locators
deviceLocators.insert(new BonjourDeviceLocator());
deviceLocators.insert(new AvahiDeviceLocator());
deviceLocators.insert(new FakeDeviceLocator());
//TODO: Read paired devices from config
pairedDevices.push_back(new Device("MyAndroid","MyAndroid"));
//At boot time, try to link to all paired devices
Q_FOREACH (Device* device, pairedDevices) {
linkTo(device->id());
}
//Try to link to all paired devices
foreach (Device* device, pairedDevices) {
}
DeviceLink* link = NULL;
foreach (DeviceLocator* locator, deviceLocators) {
QString Daemon::listVisibleDevices()
{
if (locator->canLink(device)) {
link = locator->link(device);
if (link != NULL) break;
}
std::stringstream ret;
}
ret << std::setw(20) << "ID";
ret << std::setw(20) << "Name";
ret << std::setw(20) << "Source";
ret << std::endl;
if (link != NULL) linkedDevices.append(link);
else qDebug() << "Could not link " + device->name();
QList<Device*> visibleDevices;
}
foreach (DeviceLink* ld, linkedDevices) {
foreach (PackageReceiver* pr, packageReceivers) {
QObject::connect(ld,SIGNAL(receivedPackage(const NetworkPackage&)),
pr,SLOT(receivePackage(const NetworkPackage&)));
foreach (DeviceLocator* dl, deviceLocators) {
foreach (Device* d, dl->discover()) {
ret << std::setw(20) << d->id().toStdString();
ret << std::setw(20) << d->name().toStdString();
ret << std::setw(20) << dl->getName().toStdString();
ret << std::endl;
}
}
return QString::fromStdString(ret.str());
}
bool Daemon::linkDevice(QString id)
{
return linkTo(id);
}
Daemon::~Daemon()
......@@ -89,3 +127,4 @@ Daemon::~Daemon()
qDebug() << "SAYONARA BABY";
}
......@@ -52,8 +52,30 @@ public:
Daemon(QObject *parent, const QList<QVariant>&);
~Daemon();
//DBUS interface
public Q_SLOTS:
Q_SCRIPTABLE QString listVisibleDevices();
Q_SCRIPTABLE bool linkDevice(QString id);
/*
Q_SCRIPTABLE bool pairDevice(long id);
Q_SCRIPTABLE QString listPairedDevices(long id);
Q_SCRIPTABLE bool linkAllPairedDevices();
Q_SCRIPTABLE QString listLinkedDevices(long id);
*/
private:
//Get a DeviceLink through the best DeviceLocator available
DeviceLink* linkTo(QString d);
//All known devices (read from/stored to settings)
QVector<Device*> pairedDevices;
......
......@@ -26,15 +26,21 @@
class Device
{
public:
Device(QString id, QString name) {
Device(const QString& id, const QString& name)
{
mDeviceId = id;
mDeviceName = name;
}
QString id() { return mDeviceId; }
QString name() { return mDeviceName; }
bool paired() { return mPaired; }
void pair() { mPaired = true; }
QString id() const{ return mDeviceId; }
QString name() const { return mDeviceName; }
bool paired() const { return mPaired; }
void pair() {
mPaired = true;
//TODO: Actually do something
}
private:
bool mPaired;
......
......@@ -36,6 +36,8 @@ public:
DeviceLocator();
virtual ~DeviceLocator() { }
virtual QString getName() = 0;
enum Priority {
PRIORITY_LOW = 0, //ie: 3g
PRIORITY_MEDIUM = 50, //ie: internet
......@@ -43,10 +45,10 @@ public:
};
virtual Priority getPriority() = 0;
virtual bool canLink(Device* d) = 0;
virtual DeviceLink* link(Device* d) = 0;
virtual bool canLink(QString id) = 0;
virtual DeviceLink* link(QString id) = 0;
virtual bool pair(Device* d) = 0;
virtual QVector<Device*> discover() = 0;
virtual QList<Device*> discover() = 0;
};
......
......@@ -21,29 +21,29 @@
#include "fakedevicelocator.h"
#include "echodevicelink.h"
bool FakeDeviceLocator::canLink(Device* d) {
return d->name() == "fake";
FakeDeviceLocator::FakeDeviceLocator()
{
fakeDevice = new Device("fake","Fake device");
echoDeviceLink = new EchoDeviceLink();
}
DeviceLink* FakeDeviceLocator::link(Device* d) {
if (d->name() == "fake") {
return new EchoDeviceLink();
} else {
return NULL;
}
bool FakeDeviceLocator::canLink(QString id) {
return id == "fake";
}
DeviceLink* FakeDeviceLocator::link(QString id) {
if (!canLink(id)) return NULL;
return echoDeviceLink;
}
bool FakeDeviceLocator::pair(Device* d) {
if (d->name() != "fake") {
return false;
} else {
d->pair();
return true;
}
if (d != fakeDevice) return false;
d->pair();
return true;
}
QVector<Device*> FakeDeviceLocator::discover() {
QVector<Device*> devices;
devices.append(new Device("fake","Fake device"));
QList<Device*> FakeDeviceLocator::discover() {
QList<Device*> devices;
devices.append(fakeDevice);
return devices;
}
......@@ -26,12 +26,20 @@ class FakeDeviceLocator
: public DeviceLocator
{
public:
FakeDeviceLocator();
QString getName() { return "FakeLocator"; }
Priority getPriority() { return PRIORITY_LOW; }
bool canLink(Device* d);
DeviceLink* link(Device* d);
bool canLink(QString id);
DeviceLink* link(QString id);
bool pair(Device* d);
QVector<Device*> discover();
QList<Device*> discover();
private:
Device* fakeDevice;
DeviceLink* echoDeviceLink;
};
#endif // FAKEDEVICELOCATOR_H
......@@ -28,17 +28,17 @@
NetworkPackage NetworkPackage::fromString(QByteArray s)
{
NetworkPackage pp;
//FIXME: How can I do this using Qt?
std::string stds(std::string(s.data()));
std::cout << stds << std::endl;
std::stringstream ss(stds);
ss >> pp.mId;
std::cout << pp.mId << std::endl;
long id;
ss >> id;
qDebug() << "Receiving package with id: " << id;
NetworkPackage pp(id);
ss >> pp.mDeviceId;
qDebug() << pp.mDeviceId;
......@@ -64,3 +64,17 @@ NetworkPackage NetworkPackage::fromString(QByteArray s)
}
QByteArray NetworkPackage::toString() const
{
QByteArray s;
//TODO
s += "HOLA";
return s;
}
......@@ -29,13 +29,21 @@ class NetworkPackage
public:
NetworkPackage(long id) {
mId = id;
//TODO
}
static NetworkPackage fromString(QByteArray);
QByteArray toString() const;
QString type() const { return mType; }
QString body() const { return mBody; }
long id() const { return mId; }
bool isCancel() const { return mIsCancel; }
private:
long mId;
......
......@@ -21,25 +21,28 @@
#include "udpdevicelink.h"
UdpDeviceLink::UdpDeviceLink(QString ip)
UdpDeviceLink::UdpDeviceLink(QHostAddress ip, quint16 port)
{
m_udpSocket = new QUdpSocket();
m_udpSocket->bind(10600);
connect(m_udpSocket, SIGNAL(readyRead()), this, SLOT(readPendingNotifications()));
mIp = ip;
mPort = port;
mUdpSocket = new QUdpSocket();
mUdpSocket->bind(port);
connect(mUdpSocket, SIGNAL(readyRead()), this, SLOT(readPendingNotifications()));
}
void UdpDeviceLink::readPendingNotifications()
{
while (m_udpSocket->hasPendingDatagrams()) {
while (mUdpSocket->hasPendingDatagrams()) {
QByteArray datagram;
datagram.resize(m_udpSocket->pendingDatagramSize());
datagram.resize(mUdpSocket->pendingDatagramSize());
QHostAddress sender;
quint16 senderPort;
m_udpSocket->readDatagram(datagram.data(), datagram.size(), &sender, &senderPort);
mUdpSocket->readDatagram(datagram.data(), datagram.size(), &sender, &senderPort);
//log.write(datagram);
qDebug() << datagram;
......
......@@ -33,10 +33,11 @@ class UdpDeviceLink
Q_OBJECT
public:
UdpDeviceLink(QString ip);
UdpDeviceLink(QHostAddress ip, quint16 port);
void sendPackage(const NetworkPackage& np) {
//TODO: Not implemented
mUdpSocket->writeDatagram(np.toString(), mIp, mPort);
}
......@@ -45,7 +46,10 @@ private Q_SLOTS:
void readPendingNotifications();
private:
QUdpSocket* m_udpSocket;
QUdpSocket* mUdpSocket;
QHostAddress mIp;
quint16 mPort;
};
......
set(kded_androidshine_tests_SRCS
backendtests.cpp
)
kde4_add_unit_test(kded_androidshine_tests ${kded_androidshine_tests_SRCS})
target_link_libraries(kded_androidshine_tests
${KDE4_KDECORE_LIBS}
${KDE4_KDEUI_LIBS}
kdnssd
${QT_QTNETWORK_LIBRARY}
)
add_test(kded_androidshine_tests ${CMAKE_CURRENT_BINARY_DIR}/kded_androidshine_tests)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment