Bugfixing

This commit is contained in:
Albert Vaca 2013-08-07 13:40:39 +02:00
parent d340df1e6e
commit 0336ae75de
6 changed files with 71 additions and 27 deletions

View file

@ -69,7 +69,7 @@ static bool lessThan(DeviceLink* p1, DeviceLink* p2)
void Device::addLink(DeviceLink* link) void Device::addLink(DeviceLink* link)
{ {
qDebug() << "Adding link to " << id() << "via" << link->provider(); qDebug() << "Adding link to" << id() << "via" << link->provider();
connect(link,SIGNAL(destroyed(QObject*)),this,SLOT(linkDestroyed(QObject*))); connect(link,SIGNAL(destroyed(QObject*)),this,SLOT(linkDestroyed(QObject*)));
@ -86,7 +86,6 @@ void Device::addLink(DeviceLink* link)
void Device::linkDestroyed(QObject* o) void Device::linkDestroyed(QObject* o)
{ {
qDebug() << "Link destroyed";
removeLink(static_cast<DeviceLink*>(o)); removeLink(static_cast<DeviceLink*>(o));
} }

View file

@ -53,6 +53,8 @@ void AvahiTcpLinkProvider::onStop()
} }
void AvahiTcpLinkProvider::onNetworkChange(QNetworkSession::State state) void AvahiTcpLinkProvider::onNetworkChange(QNetworkSession::State state)
{ {
Q_UNUSED(state);
//Nothing to do, Avahi will handle it //Nothing to do, Avahi will handle it
} }

View file

@ -70,20 +70,20 @@ void BroadcastTcpLinkProvider::newUdpConnection()
mUdpServer->readDatagram(datagram.data(), datagram.size(), &sender, &senderPort); mUdpServer->readDatagram(datagram.data(), datagram.size(), &sender, &senderPort);
NetworkPackage np(""); NetworkPackage* np = new NetworkPackage("");
NetworkPackage::unserialize(datagram,&np); NetworkPackage::unserialize(datagram,np);
if (np.version() > 0 && np.type() == PACKAGE_TYPE_IDENTITY) { if (np->version() > 0 && np->type() == PACKAGE_TYPE_IDENTITY) {
NetworkPackage np2(""); NetworkPackage np2("");
NetworkPackage::createIdentityPackage(&np2); NetworkPackage::createIdentityPackage(&np2);
if (np.get<QString>("deviceId") == np2.get<QString>("deviceId")) { if (np->get<QString>("deviceId") == np2.get<QString>("deviceId")) {
qDebug() << "I can't fuck myself!"; qDebug() << "I can't fuck myself!";
return; return;
} }
const QString& id = np.get<QString>("deviceId"); const QString& id = np->get<QString>("deviceId");
if (links.contains(id)) { if (links.contains(id)) {
//Delete old link if we already know it, probably it is down if this happens. //Delete old link if we already know it, probably it is down if this happens.
qDebug() << "Destroying old link"; qDebug() << "Destroying old link";
@ -94,8 +94,42 @@ void BroadcastTcpLinkProvider::newUdpConnection()
QTcpSocket* socket = new QTcpSocket(this); QTcpSocket* socket = new QTcpSocket(this);
qDebug() << "Received Udp presentation from" << sender << "asking for a tcp connection..."; qDebug() << "Received Udp presentation from" << sender << "asking for a tcp connection...";
receivedIdentityPackages[socket] = np;
connect(socket, SIGNAL(connected()), this, SLOT(connected()));
connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(connectError()));
socket->connectToHost(sender, port); socket->connectToHost(sender, port);
socket->waitForConnected();
} else {
delete np;
}
}
}
void BroadcastTcpLinkProvider::connectError()
{
QTcpSocket* socket = (QTcpSocket*)sender();
disconnect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(connectError()));
disconnect(socket, SIGNAL(connected()), this, SLOT(connected()));
qDebug() << "Fallback (1), try reverse connection";
onNetworkChange(QNetworkSession::Connected);
}
void BroadcastTcpLinkProvider::connected()
{
QTcpSocket* socket = (QTcpSocket*)sender();
disconnect(socket, SIGNAL(connected()), this, SLOT(connected()));
disconnect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(connectError()));
NetworkPackage* np = receivedIdentityPackages[socket];
const QString& id = np->get<QString>("deviceId");
qDebug() << "Connected" << socket->isWritable(); qDebug() << "Connected" << socket->isWritable();
TcpDeviceLink* dl = new TcpDeviceLink(id, this, socket); TcpDeviceLink* dl = new TcpDeviceLink(id, this, socket);
@ -103,19 +137,25 @@ void BroadcastTcpLinkProvider::newUdpConnection()
connect(dl,SIGNAL(destroyed(QObject*)),this,SLOT(deviceLinkDestroyed(QObject*))); connect(dl,SIGNAL(destroyed(QObject*)),this,SLOT(deviceLinkDestroyed(QObject*)));
links[id] = dl; links[id] = dl;
NetworkPackage np2("");
NetworkPackage::createIdentityPackage(&np2);
bool success = dl->sendPackage(np2); bool success = dl->sendPackage(np2);
if (!success) { //FIXME: Why is this happening?
qDebug() << "Fallback, try reverse connection";
QUdpSocket().writeDatagram(np2.serialize(),sender, port);
}
if (success) {
qDebug() << "Handshaking done (i'm the existing device)"; qDebug() << "Handshaking done (i'm the existing device)";
emit onNewDeviceLink(*np, dl);
emit onNewDeviceLink(np, dl); } else {
//I think this will never happen
} qDebug() << "Fallback (2), try reverse connection";
onNetworkChange(QNetworkSession::Connected);
delete dl;
} }
receivedIdentityPackages.remove(socket);
delete np;
} }
//I'm the new device and this is the answer to my UDP introduction (no data received yet) //I'm the new device and this is the answer to my UDP introduction (no data received yet)

View file

@ -45,6 +45,8 @@ public Q_SLOTS:
virtual void onNetworkChange(QNetworkSession::State state); virtual void onNetworkChange(QNetworkSession::State state);
virtual void onStart(); virtual void onStart();
virtual void onStop(); virtual void onStop();
void connected();
void connectError();
private Q_SLOTS: private Q_SLOTS:
void newUdpConnection(); void newUdpConnection();
@ -58,6 +60,7 @@ private:
const static quint16 port = 1714; const static quint16 port = 1714;
QMap<QString, DeviceLink*> links; QMap<QString, DeviceLink*> links;
QMap<QTcpSocket*, NetworkPackage*> receivedIdentityPackages;
}; };

View file

@ -33,7 +33,7 @@
const static int CURRENT_PACKAGE_VERSION = 1; const static int CURRENT_PACKAGE_VERSION = 1;
NetworkPackage::NetworkPackage(QString type) NetworkPackage::NetworkPackage(const QString& type)
{ {
mId = time(NULL); mId = time(NULL);
mType = type; mType = type;
@ -63,7 +63,7 @@ QByteArray NetworkPackage::serialize() const
return json; return json;
} }
void NetworkPackage::unserialize(QByteArray a, NetworkPackage* np) void NetworkPackage::unserialize(const QByteArray& a, NetworkPackage* np)
{ {
qDebug() << "Unserialize:" << a; qDebug() << "Unserialize:" << a;

View file

@ -39,9 +39,9 @@ class NetworkPackage : public QObject
public: public:
NetworkPackage(QString type); NetworkPackage(const QString& type);
static void unserialize(QByteArray, NetworkPackage*); static void unserialize(const QByteArray&, NetworkPackage*);
QByteArray serialize() const; QByteArray serialize() const;
static void createIdentityPackage(NetworkPackage*); static void createIdentityPackage(NetworkPackage*);