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