Commit 31e08113 authored by Camilo Higuita's avatar Camilo Higuita

more work on linking sockets

parent e3f22376
......@@ -68,6 +68,7 @@ Babe::Babe(QObject *parent) : CollectionDB(parent)
});
connect(&link, &Linking::parseAsk, this, &Babe::linkDecoder);
#if (defined (Q_OS_LINUX) && !defined (Q_OS_ANDROID))
this->nof = new Notify(this);
......@@ -228,6 +229,36 @@ void Babe::fetchTrackLyrics(DB &song)
pulpo.feed(song, PULPO::RECURSIVE::OFF);
}
void Babe::linkDecoder(QString json)
{
qDebug()<<"DECODING LINKER MSG"<<json;
auto ask = link.decode(json);
auto code = ask[BAE::SLANG[BAE::W::CODE]].toInt();
auto msg = ask[BAE::SLANG[BAE::W::MSG]].toString();
switch(code)
{
case LINK::CODE::CONNECTED:
{
this->link.deviceName = msg;
emit this->link.serverConReady(msg);
break;
}
case LINK::CODE::PLAYLISTS:
{
auto playlists = this->getPlaylists();
QVariantMap map;
map.insert(LINK::DECODE[LINK::CODE::PLAYLISTS], playlists);
link.sendToClient(map);
break;
}
default: break;
}
}
QString Babe::albumWiki(const QString &album, const QString &artist)
{
auto queryStr = QString("SELECT %1 FROM %2 WHERE %3 = \"%4\" AND %5 = \"%6\"").arg(KEYMAP[KEY::WIKI],
......
......@@ -142,6 +142,7 @@ class Babe : public CollectionDB
static QVariantList transformData(const DB_LIST &dbList);
void fetchTrackLyrics(DB &song);
void linkDecoder(QString json);
signals:
void refreshTables(int size);
......
......@@ -127,6 +127,9 @@ Kirigami.ApplicationWindow
property string infoMsg : ""
property bool infoLabels : bae.loadSetting("PLAYBACKINFO", "BABE", false) == "true" ? true : false
property bool isLinked : false
property bool isServing : false
/*SIGNALS*/
signal missingAlert(var track)
......@@ -876,7 +879,37 @@ Kirigami.ApplicationWindow
Connections
{
target: link
onServerConReady: H.notify(deviceName, "You're now linked!")
onClientConError: H.notify("Error connecting to server")
onServerConReady:
{
isServing = true
H.notify(deviceName, "You're now linked!")
}
onClientConError:
{
isLinked = false
H.notify("Linking error", "error connecting to server")
}
onDevicesLinked:
{
isLinked = true
H.notify("Linked!", "The link is ready")
H.addPlaylist({ playlist: "Linked", playlistIcon: "link"})
}
onClientConDisconnected:
{
isLinked = false;
H.notify("Unlinked!", "The client is disconnected")
}
onServerConDisconnected:
{
isServing = false;
H.notify("Unlinked!", "The server is disconnected")
}
}
}
......@@ -3,8 +3,10 @@ import QtQuick.Controls 2.2
import QtQuick.Layouts 1.3
import "../../view_models/BabeDialog"
import "../../utils/Help.js" as H
BabeDialog
{
id: linkingDialogRoot
title: "Add "+ tracks.length +" tracks to..."
standardButtons: Dialog.Save | Dialog.Cancel
......@@ -18,95 +20,116 @@ BabeDialog
H.notify("Error", "Please provide a different IP address")
else
{
if(nameField.text.length<1)
nameField.text = "Device1"
bae.saveSetting("LINKINGIP", ipField.text, "BABE")
link.connectTo(ipField.text, link.getPort())
}
}
ColumnLayout
ScrollView
{
anchors.fill: parent
anchors.centerIn: parent
width: parent.width*0.8
height: parent.height*0.9
spacing: c
Item
{
Layout.fillWidth: true
Layout.fillHeight: true
}
Label
{
text:qsTr("Linking allows to connect two devices on the same network. Just provide the device IP address to which you want to connect")
verticalAlignment: Qt.AlignVCenter
elide: Text.ElideRight
font.pointSize: fontSizes.medium
wrapMode: Text.Wrap
Layout.fillWidth: true
}
Item
{
Layout.fillWidth: true
Layout.fillHeight: true
}
Label
{
text: qsTr("IP Address")
verticalAlignment: Qt.AlignVCenter
elide: Text.ElideRight
font.pointSize: fontSizes.medium
Layout.fillWidth: true
}
clip: true
TextField
{
id: ipField
Layout.fillWidth: true
text: bae.loadSetting("LINKINGIP", "BABE", link.getIp())
}
Label
{
text: qsTr("Device Name")
verticalAlignment: Qt.AlignVCenter
elide: Text.ElideRight
font.pointSize: fontSizes.medium
Layout.fillWidth: true
}
TextField
{
id: nameField
Layout.fillWidth: true
text: bae.loadSetting("LINKINGIP", "BABE", "").name
}
contentWidth: parent.width
contentHeight: contentLayout.implicitHeight
Label
ColumnLayout
{
text:qsTr("Device IP address: \n") +link.deviceIp()
verticalAlignment: Qt.AlignVCenter
elide: Text.ElideRight
font.pointSize: fontSizes.medium
id: contentLayout
anchors.centerIn: parent
width: linkingDialogRoot.width*0.8
height: linkingDialogRoot.height*0.9
Item
{
Layout.fillWidth: true
Layout.fillHeight: true
}
Label
{
text:qsTr("Linking allows to connect two devices on the same network. Just provide the device IP address to which you want to connect")
verticalAlignment: Qt.AlignVCenter
elide: Text.ElideRight
font.pointSize: fontSizes.medium
wrapMode: Text.Wrap
Layout.fillWidth: true
}
Item
{
Layout.fillWidth: true
Layout.fillHeight: true
}
Label
{
text: qsTr("IP Address")
verticalAlignment: Qt.AlignVCenter
elide: Text.ElideRight
font.pointSize: fontSizes.medium
Layout.fillWidth: true
}
TextField
{
id: ipField
Layout.fillWidth: true
text: bae.loadSetting("LINKINGIP", "BABE", link.getIp())
}
CheckBox
{
id: autoLink
checked: bae.loadSetting("AUTOLINKING", "BABE", false)
text: qsTr("Autolink to IP address")
onCheckedChanged:
{
bae.saveSetting("AUTOLINKING", checked, "BABE")
}
}
CheckBox
{
id: linkState
checked: isLinked
text: isLinked ? qsTr("Linked to ")+ link.getIp(): "You're not linked"
enabled: false
}
CheckBox
{
id: servingState
checked: isServing
text: isServing ? qsTr("Serving to ")+ link.getDeviceName() : "You're not serving"
enabled: false
}
Label
{
text:qsTr("This Device IP address: \n") +link.deviceIp()
verticalAlignment: Qt.AlignVCenter
elide: Text.ElideRight
font.pointSize: fontSizes.small
Layout.fillWidth: true
}
Item
{
Layout.fillWidth: true
Layout.fillHeight: true
}
Layout.fillWidth: true
}
Item
{
Layout.fillWidth: true
Layout.fillHeight: true
}
}
}
}
......@@ -4,36 +4,44 @@
#include <QNetworkInterface>
#include "../../utils/babeconsole.h"
#include <QSysInfo>
#include <QAbstractSocket>
QString Linking::stringify(const QVariantMap &map)
{
if(map.isEmpty()) return "{}";
auto JSON = QString("{ \"%1\" : \"%2\", \"%3\" : \"%4\" }").arg(BAE::SLANG[BAE::W::CODE],
QString::number(map[BAE::SLANG[BAE::W::CODE]].toInt()),
BAE::SLANG[BAE::W::MSG],
map[BAE::SLANG[BAE::W::MSG]].toString());
return JSON;
}
Linking::Linking(QObject *parent) : QObject(parent)
{
this->server = new Socket(BAE::LinkPort.toUInt(), this);
connect(this->server, &Socket::connected, this, &Linking::init);
connect(this->server, &Socket::message, [this](QString msg)
connect(this->server, &Socket::message, this, &Linking::parseAsk);
connect(this->server, &Socket::disconnected, [this](QString id)
{
qDebug()<<"Reciving message in server:" << msg;
this->decode(msg);
emit this->serverConDisconnected(id);
});
connect(&client, &QWebSocket::connected, this, &Linking::onConnected);
// connect(&client, &QWebSocket::error, [this](QAbstractSocket::SocketError error)
// {
// emit this->clientConError(error);
// });
connect(&client, &QWebSocket::disconnected, this, &Linking::closed);
connect(&client, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(handleError(QAbstractSocket::SocketError)));
connect(&client, &QWebSocket::disconnected, this, &Linking::clientConDisconnected);
connect(&client, &QWebSocket::textMessageReceived, [this](QString msg)
{
qDebug()<<msg;
emit this->responseReady(decode(msg));
});
}
void Linking::init(const int &index)
{
qDebug()<<"Got connected with index"<<index;
emit this->devicesLinked();
}
void Linking::setIp(const QString &ip)
......@@ -60,36 +68,43 @@ QString Linking::getPort()
return BAE::LinkPort;
}
void Linking::ask(LINK::CODE code, QString msg)
QString Linking::getDeviceName()
{
return this->deviceName;
}
void Linking::ask(int code, QString msg)
{
auto JSON = QString("{ %1 : %2, %3 : \"%4\" }").arg(BAE::SLANG[BAE::W::CODE],
LINK::DECODE[code],
auto JSON = QString("{ \"%1\" : \"%2\", \"%3\" : \"%4\" }").arg(BAE::SLANG[BAE::W::CODE],
QString::number(code),
BAE::SLANG[BAE::W::MSG],
msg);
client.sendTextMessage(JSON);
qDebug()<<"msg sent as json to server";
}
void Linking::decode(const QString &json)
QVariantMap Linking::decode(const QString &json)
{
qDebug()<<"trying to decode msg";
QJsonParseError jsonParseError;
auto jsonResponse = QJsonDocument::fromJson(json.toUtf8(), &jsonParseError);
if (jsonParseError.error != QJsonParseError::NoError) return;
if (!jsonResponse.isObject()) return;
if (jsonParseError.error != QJsonParseError::NoError) return QVariantMap ();
if (!jsonResponse.isObject()) return QVariantMap ();
qDebug()<<"trying to decode msg2";
QJsonObject mainJsonObject(jsonResponse.object());
auto data = mainJsonObject.toVariantMap();
auto code = data.value(BAE::SLANG[BAE::W::CODE]).toInt();
auto msg = data.value(BAE::SLANG[BAE::W::MSG]).toString();
qDebug()<<code<<msg<<data;
return data;
}
void Linking::onConnected()
{
qDebug()<<"Got connected to server";
emit this->devicesLinked();
this->ask(LINK::CODE::CONNECTED, QSysInfo::prettyProductName());
}
......@@ -119,7 +134,14 @@ void Linking::connectTo(QString ip, QString port)
qDebug()<<url<<ip<<port;
}
void Linking::sendToClient(QVariantMap map)
{
auto json = stringify(map);
server->sendMessageTo(0, json);
}
void Linking::handleError(QAbstractSocket::SocketError error)
{
qDebug()<<error;
emit this->clientConError("An error happened connecting to server");
}
......@@ -12,22 +12,27 @@ namespace LINK
{
Q_NAMESPACE
enum CODE
enum CODE
{
CONNECTED = 1,
ERROR = 2,
DISCONNECTED = 3,
SEARCHFOR = 4,
PLAYLISTS = 5,
GETQUERY = 6,
};
Q_ENUM_NS(CODE);
static QMap<CODE, QString> DECODE =
static QMap<CODE, QString> DECODE =
{
{CODE::CONNECTED, "CONNECTED"},
{CODE::ERROR, "ERROR"},
{CODE::DISCONNECTED, "DISCONNECTED"},
{CODE::SEARCHFOR, "SEARCHFOR"}
{CODE::SEARCHFOR, "SEARCHFOR"},
{CODE::PLAYLISTS, "PLAYLISTS"},
{CODE::GETQUERY, "GETQUERY"}
};
}
......@@ -39,29 +44,37 @@ class Linking : public QObject
private:
Socket *server;
QWebSocket client;
QString IP;
QString stringify(const QVariantMap &map);
public:
explicit Linking(QObject *parent = nullptr);
QString deviceName;
void init(const int &index);
Q_INVOKABLE void setIp(const QString &ip);
Q_INVOKABLE QString getIp();
Q_INVOKABLE QString deviceIp();
Q_INVOKABLE QString getPort();
Q_INVOKABLE void ask(LINK::CODE code, QString msg);
void decode(const QString &json);
Q_INVOKABLE QString getDeviceName();
Q_INVOKABLE void ask(int code, QString msg);
QVariantMap decode(const QString &json);
void onConnected();
QStringList checkAddresses();
Q_INVOKABLE void connectTo(QString ip, QString port);
Q_INVOKABLE void sendToClient(QVariantMap map);
signals:
void closed();
void devicesLinked();
void serverConReady(const QString deviceName);
void serverConReady(QString deviceName);
void serverConDisconnected(QString index);
void clientConDisconnected();
void clientConError(QString message);
void parseAsk(QString json);
void clientConError(const QString &message);
void responseReady(QVariantMap map);
public slots:
void handleError(QAbstractSocket::SocketError error);
......
......@@ -72,7 +72,13 @@ function refreshArtists()
function notify(title, body)
{
if(isMobile)
babeNotify(title+"\n"+body)
babeNotify.notify(title+"\n"+body)
else
bae.notify(title, body)
}
function addPlaylist(playlist)
{
playlistsView.playlistViewModel.model.insert(0, playlist)
}
......@@ -22,7 +22,7 @@ BabePopup
clip: true
contentWidth: parent.width
contentHeight: loginLayout.implicitHeight
contentHeight: loginLayout.implicitHeight+contentMargins
ColumnLayout
{
......
......@@ -7,6 +7,8 @@ import "../../utils"
import "../../view_models"
import "../../db/Queries.js" as Q
import "../../utils/Help.js" as H
import Link.Codes 1.0
BabeList
{
......@@ -115,13 +117,16 @@ BabeList
populateExtra(Q.GET.genres, "Genres")
break;
case "Linked":
link.ask(5, "")
break;
default:
playlistViewRoot.populate(Q.GET.playlistTracks_.arg(playlist));
break;
}
}
}
onPlaySync: playlistListRoot.playSync(index)
......
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