Skip to content

Commit

Permalink
Bug fixes and general improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandruIstrate committed Jul 25, 2018
1 parent 0475f14 commit 3b88223
Show file tree
Hide file tree
Showing 12 changed files with 204 additions and 101 deletions.
Binary file removed ExeQt/ExeQtSource.tar.xz
Binary file not shown.
6 changes: 4 additions & 2 deletions ExeQt/TrayIcon.pro
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ SOURCES += \
actionreference.cpp \
clientinfodialog.cpp \
settingsregistry.cpp \
common.cpp
common.cpp \
socket.cpp

HEADERS += \
mainwidget.h \
Expand Down Expand Up @@ -101,7 +102,8 @@ HEADERS += \
networkmessage.h \
actionreference.h \
clientinfodialog.h \
settingsregistry.h
settingsregistry.h \
socket.h

FORMS += \
mainwidget.ui \
Expand Down
7 changes: 4 additions & 3 deletions ExeQt/applicationaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <QDebug>
#include <QProcess>
#include <QFileDialog>
#include <QDir>
#include <QMessageBox>

#define APP_PATH_PROPERTY "appPath"
Expand Down Expand Up @@ -91,10 +92,10 @@ void ApplicationAction::initBundle()

void ApplicationAction::execute()
{
// TODO: Member QProcess and error handling using the return value of startDetached
QProcess* process = new QProcess(this);
// connect(process, &QProcess::finished, this, &ApplicationAction::onProcessFinished);
connect(process, SIGNAL(errorOccurred(QProcess::ProcessError)), this, SLOT(onProcessError(QProcess::ProcessError)));
process->startDetached(m_AppPath);
process->startDetached(QString("\"%1\"").arg(m_AppPath));
}

bool ApplicationAction::validate()
Expand Down Expand Up @@ -129,7 +130,7 @@ void ApplicationAction::onBrowse()
if (dialog.exec() == QDialog::DialogCode::Rejected)
return;

ui->edtPath->setText(dialog.selectedFiles()[0]);
ui->edtPath->setText(QDir::toNativeSeparators(dialog.selectedFiles().at(0)));
}

void ApplicationAction::onProcessError(QProcess::ProcessError)
Expand Down
27 changes: 23 additions & 4 deletions ExeQt/authconnecttab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@

#include "clientinfodialog.h"

#define MSG_BROADCASTING tr("Check your other devices. This device should show up in the \"Connect\" tab.")
#define MSG_IDLE tr("Press the button to become discoverable.")
#define MSG_BROADCASTING tr("Check your other devices. This device should show up in the \"Connect\" tab.")
#define MSG_IDLE tr("Press the button to become discoverable.")

#define BTN_BROADCASTING tr("Stop Broadcasting")
#define BTN_IDLE tr("Broadcast")
#define BTN_BROADCASTING tr("Stop Broadcasting")
#define BTN_IDLE tr("Broadcast")

#define AUTO_REFRESH_INTERVAL 3000

AuthConnectTab::AuthConnectTab(QWidget* parent) :
QWidget(parent),
Expand All @@ -28,6 +30,8 @@ AuthConnectTab::AuthConnectTab(QWidget* parent) :

initUI();
setupSignalsAndSlots();

initTimer();
}

AuthConnectTab::~AuthConnectTab()
Expand All @@ -44,13 +48,21 @@ void AuthConnectTab::setupSignalsAndSlots()
connect(ui->btnInfo, &QPushButton::clicked, this, &AuthConnectTab::onGetInfo);

connect(ui->lstDevices, &QListWidget::doubleClicked, this, &AuthConnectTab::onDoubleClick);

connect(&m_RefreshTimer, &QTimer::timeout, this, &AuthConnectTab::onAutoRefresh);
}

void AuthConnectTab::initUI()
{
setUIState(NetworkManager::instance()->getState());
}

void AuthConnectTab::initTimer()
{
m_RefreshTimer.setInterval(AUTO_REFRESH_INTERVAL);
m_RefreshTimer.setSingleShot(false);
}

void AuthConnectTab::setButtonText(const QString& text)
{
ui->btnBroadcast->setText(text);
Expand Down Expand Up @@ -138,6 +150,8 @@ void AuthConnectTab::onBroadcast()

void AuthConnectTab::onClientAvailable(Client client)
{
m_RefreshTimer.start(); // Start or restart the timer

// If the client is not the current device
if (NetworkManager::instance()->getThisClient()->getLocalizedName() == client.getLocalizedName())
return;
Expand Down Expand Up @@ -176,6 +190,11 @@ void AuthConnectTab::onGetInfo()
infoDialog.exec();
}

void AuthConnectTab::onAutoRefresh()
{
clearClients();
}

void AuthConnectTab::onDoubleClick(const QModelIndex&)
{
onConnect();
Expand Down
5 changes: 5 additions & 0 deletions ExeQt/authconnecttab.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#define AUTHCONNECTTAB_H

#include <QWidget>
#include <QTimer>

#include "networkmanager.h"

Expand All @@ -24,6 +25,7 @@ class AuthConnectTab : public QWidget

private:
Ui::AuthConnectTab* ui;
QTimer m_RefreshTimer;

QList<Client> m_Clients;

Expand All @@ -34,6 +36,7 @@ class AuthConnectTab : public QWidget
private:
void setupSignalsAndSlots();
void initUI();
void initTimer();

void setButtonText(const QString& text);
void setInfo(const QString& info);
Expand All @@ -57,6 +60,8 @@ private slots:
void onConnect();
void onGetInfo();

void onAutoRefresh();

void onDoubleClick(const QModelIndex&);
};

Expand Down
102 changes: 37 additions & 65 deletions ExeQt/networkmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,20 @@ Client::Client(const QString& name, const QString& id, const QString& address, Q
m_Name { name }, m_ID { id }, m_Address { address }, m_ConnectSocket { socket }
{
if (m_ConnectSocket)
connect(m_ConnectSocket, &QTcpSocket::readyRead, this, &Client::onReadyRead);
{
connect(&m_BufferedSocket, &BufferedSocket::transferFinished, this, &Client::onActionsReceived);
m_BufferedSocket.setBufferSocket(m_ConnectSocket);
}
}

Client::Client(const Client& other)
: QObject {}, m_Name { other.m_Name }, m_ID { other.m_ID }, m_Address { other.m_Address }, m_ConnectSocket { other.m_ConnectSocket }
{
if (m_ConnectSocket)
connect(m_ConnectSocket, &QTcpSocket::readyRead, this, &Client::onReadyRead);
{
connect(&m_BufferedSocket, &BufferedSocket::transferFinished, this, &Client::onActionsReceived);
m_BufferedSocket.setBufferSocket(m_ConnectSocket);
}
}

Client::~Client()
Expand All @@ -69,7 +75,9 @@ Client::~Client()
void Client::setSocket(QTcpSocket* socket)
{
m_ConnectSocket = socket;
connect(m_ConnectSocket, &QTcpSocket::readyRead, this, &Client::onReadyRead);

connect(&m_BufferedSocket, &BufferedSocket::transferFinished, this, &Client::onActionsReceived);
m_BufferedSocket.setBufferSocket(m_ConnectSocket);
}

QString Client::getLocalizedName() const
Expand Down Expand Up @@ -180,21 +188,16 @@ QString Client::generateID()
return QString(QCryptographicHash::hash(getHardwareID().toUtf8(), QCryptographicHash::Md5).toHex());
}

void Client::onReadyRead()
void Client::onActionsReceived(const QByteArray& actionsData)
{
QTcpSocket* socket = static_cast<QTcpSocket*>(QObject::sender());

NetworkMessage message(JSON_APP_IDENTIFIER, socket->readAll());
NetworkMessage message(JSON_APP_IDENTIFIER, actionsData);

if (message.hasError())
return;

if (message.hasProperty(JSON_KEY_ACTIONS))
{
QString bundle = message.getProperty(JSON_KEY_ACTIONS);

qDebug() << "Actions recieved:" << bundle;

if (bundle.isEmpty())
return;

Expand All @@ -221,55 +224,35 @@ void Client::onReadyRead()
}
}

// WritableSocket

WritableSocket::WritableSocket(const QString& address, int port) : WritableSocket()
{
connectToHost(address, port);
}
// NetworkManager

WritableSocket::WritableSocket()
{
m_Socket = new QTcpSocket();
connect(m_Socket, &QTcpSocket::connected, this, &WritableSocket::onConnect);
connect(m_Socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(onError(QAbstractSocket::SocketError)));
}
NetworkManager* NetworkManager::s_Instance = nullptr;

void WritableSocket::connectToHost(const QString& address, int port)
NetworkManager::NetworkManager() : m_WritableSocket { }, m_State { State::IDLE }
{
m_Socket->connectToHost(address, port);
}
m_ThisClient = new Client();

void WritableSocket::write(const QByteArray& data)
{
m_Data = data;
initNetwork();
initTimer();
}

void WritableSocket::onConnect()
NetworkManager::~NetworkManager()
{
qint64 size = m_Socket->write(m_Data);
if (size != m_Data.size())
QMessageBox::critical(nullptr, tr("Network Error"), tr("A network communication error has ocurred!"));
delete m_ThisClient;
}

void WritableSocket::onError(QAbstractSocket::SocketError error)
void NetworkManager::init()
{
qDebug() << "Socket Error:" << error;
s_Instance = new NetworkManager();
}

// NetworkManager

NetworkManager* NetworkManager::s_Instance = nullptr;

NetworkManager::NetworkManager() : m_WritableSocket { }, m_State { State::IDLE }
void NetworkManager::terminate()
{
m_ThisClient = new Client();

initNetwork();
initTimer();
s_Instance->closeAllConnections();
delete s_Instance;
}

NetworkManager::~NetworkManager()
void NetworkManager::closeAllConnections()
{
while (m_ConnectedTo.size() > 0)
{
Expand All @@ -286,18 +269,6 @@ NetworkManager::~NetworkManager()
m_ConnectedFrom[index].disconnectFromHost();
m_ConnectedFrom.removeAt(index);
}

delete m_ThisClient;
}

void NetworkManager::init()
{
s_Instance = new NetworkManager();
}

void NetworkManager::terminate()
{
delete s_Instance;
}

void NetworkManager::requestActionUpdate()
Expand Down Expand Up @@ -540,19 +511,20 @@ void ActionServer::setupSignalsAndSlots()

void ActionServer::parseRequest(QTcpSocket* socket)
{
QString response = socket->readAll();
qDebug() << response;
QString messageStr = socket->readAll();
qDebug() << messageStr;

QJsonDocument doc = QJsonDocument::fromJson(response.toUtf8());
QJsonObject object = doc.object();
NetworkMessage message(JSON_APP_IDENTIFIER, messageStr);

// Check that this request is a valid ExeQt request
if (object.value(JSON_KEY_IDENTIFIER).toString() != JSON_APP_IDENTIFIER)
if (message.hasError())
{
qDebug() << "Auth message has an error";
return;
}

Client client(object.value(JSON_KEY_NAME).toString(),
object.value(JSON_KEY_ID).toString(),
stripAddress(socket->localAddress().toString()),
Client client(message.getProperty(JSON_KEY_NAME),
message.getProperty(JSON_KEY_ID),
stripAddress(socket->peerAddress().toString()),
socket);

QMessageBox::StandardButton reply =
Expand Down
29 changes: 6 additions & 23 deletions ExeQt/networkmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <QTcpServer>

#include "saveable.h"
#include "socket.h"

class ActionReference;
class SettingsRegistry;
Expand All @@ -34,6 +35,7 @@ class Client : public QObject
QString m_Address; // TODO: Remove this field and use the socket for getting the IP address

QTcpSocket* m_ConnectSocket;
BufferedSocket m_BufferedSocket;

public:
Client();
Expand Down Expand Up @@ -77,29 +79,8 @@ class Client : public QObject
void actionsUpdated(Bundle bundle);

private slots:
void onReadyRead();
};

class WritableSocket : public QObject
{
Q_OBJECT

private:
QTcpSocket* m_Socket;
QByteArray m_Data;

public:
WritableSocket(const QString& address, int port);
WritableSocket();

inline QTcpSocket* getSocket() { return m_Socket; }

void connectToHost(const QString& address, int port);
void write(const QByteArray& data);

private slots:
void onConnect();
void onError(QAbstractSocket::SocketError);
// void onReadyRead();
void onActionsReceived(const QByteArray& actionsData);
};

class NetworkManager : public QObject
Expand Down Expand Up @@ -151,6 +132,8 @@ class NetworkManager : public QObject
static void init();
static void terminate();

void closeAllConnections();

void requestActionUpdate();
void updateActions(const Bundle&);

Expand Down
Loading

0 comments on commit 3b88223

Please sign in to comment.