Commit 0336ae75 authored by Albert Vaca Cintora's avatar Albert Vaca Cintora

Bugfixing

parent d340df1e
......@@ -69,7 +69,7 @@ static bool lessThan(DeviceLink* p1, DeviceLink* p2)
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*)));
......@@ -86,7 +86,6 @@ void Device::addLink(DeviceLink* link)
void Device::linkDestroyed(QObject* o)
{
qDebug() << "Link destroyed";
removeLink(static_cast<DeviceLink*>(o));
}
......
......@@ -53,6 +53,8 @@ void AvahiTcpLinkProvider::onStop()
}
void AvahiTcpLinkProvider::onNetworkChange(QNetworkSession::State state)
{
Q_UNUSED(state);
//Nothing to do, Avahi will handle it
}
......
......@@ -70,20 +70,20 @@ void BroadcastTcpLinkProvider::newUdpConnection()
mUdpServer->readDatagram(datagram.data(), datagram.size(), &sender, &senderPort);
NetworkPackage np("");
NetworkPackage::unserialize(datagram,&np);
NetworkPackage* np = new NetworkPackage("");
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::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!";
return;
}
const QString& id = np.get<QString>("deviceId");
const QString& id = np->get<QString>("deviceId");
if (links.contains(id)) {
//Delete old link if we already know it, probably it is down if this happens.
qDebug() << "Destroying old link";
......@@ -94,28 +94,68 @@ void BroadcastTcpLinkProvider::newUdpConnection()
QTcpSocket* socket = new QTcpSocket(this);
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->waitForConnected();
qDebug() << "Connected" << socket->isWritable();
TcpDeviceLink* dl = new TcpDeviceLink(id, this, socket);
connect(dl,SIGNAL(destroyed(QObject*)),this,SLOT(deviceLinkDestroyed(QObject*)));
} else {
delete np;
}
}
}
links[id] = dl;
bool success = dl->sendPackage(np2);
if (!success) { //FIXME: Why is this happening?
qDebug() << "Fallback, try reverse connection";
QUdpSocket().writeDatagram(np2.serialize(),sender, port);
}
void BroadcastTcpLinkProvider::connectError()
{
QTcpSocket* socket = (QTcpSocket*)sender();
qDebug() << "Handshaking done (i'm the existing device)";
disconnect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(connectError()));
disconnect(socket, SIGNAL(connected()), this, SLOT(connected()));
emit onNewDeviceLink(np, dl);
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();
TcpDeviceLink* dl = new TcpDeviceLink(id, this, socket);
connect(dl,SIGNAL(destroyed(QObject*)),this,SLOT(deviceLinkDestroyed(QObject*)));
links[id] = dl;
NetworkPackage np2("");
NetworkPackage::createIdentityPackage(&np2);
bool success = dl->sendPackage(np2);
if (success) {
qDebug() << "Handshaking done (i'm the existing device)";
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)
......
......@@ -45,6 +45,8 @@ public Q_SLOTS:
virtual void onNetworkChange(QNetworkSession::State state);
virtual void onStart();
virtual void onStop();
void connected();
void connectError();
private Q_SLOTS:
void newUdpConnection();
......@@ -58,6 +60,7 @@ private:
const static quint16 port = 1714;
QMap<QString, DeviceLink*> links;
QMap<QTcpSocket*, NetworkPackage*> receivedIdentityPackages;
};
......
......@@ -33,7 +33,7 @@
const static int CURRENT_PACKAGE_VERSION = 1;
NetworkPackage::NetworkPackage(QString type)
NetworkPackage::NetworkPackage(const QString& type)
{
mId = time(NULL);
mType = type;
......@@ -63,7 +63,7 @@ QByteArray NetworkPackage::serialize() const
return json;
}
void NetworkPackage::unserialize(QByteArray a, NetworkPackage* np)
void NetworkPackage::unserialize(const QByteArray& a, NetworkPackage* np)
{
qDebug() << "Unserialize:" << a;
......
......@@ -39,9 +39,9 @@ class NetworkPackage : public QObject
public:
NetworkPackage(QString type);
NetworkPackage(const QString& type);
static void unserialize(QByteArray, NetworkPackage*);
static void unserialize(const QByteArray&, NetworkPackage*);
QByteArray serialize() const;
static void createIdentityPackage(NetworkPackage*);
......
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