Skip to content

Commit 9ac78e7

Browse files
committed
Server: small refactor
1 parent 544442e commit 9ac78e7

File tree

14 files changed

+104
-100
lines changed

14 files changed

+104
-100
lines changed

server/CMakeLists.txt

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
cmake_minimum_required(VERSION 3.14)
66

77
project(iot-facerecognition-server LANGUAGES CXX)
8+
project(iot-facerecognition-server VERSION 1.5.0)
9+
add_compile_definitions(VERSION="${CMAKE_PROJECT_VERSION}")
810

911
set(CMAKE_INCLUDE_CURRENT_DIR OFF)
1012

@@ -15,19 +17,6 @@ set(CMAKE_AUTORCC ON)
1517
set(CMAKE_CXX_STANDARD 17)
1618
set(CMAKE_CXX_STANDARD_REQUIRED ON)
1719

18-
# QtCreator supports the following variables for Android, which are identical to qmake Android variables.
19-
# Check https://doc.qt.io/qt/deployment-android.html for more information.
20-
# They need to be set before the find_package(...) calls below.
21-
22-
#if(ANDROID)
23-
# set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
24-
# if (ANDROID_ABI STREQUAL "armeabi-v7a")
25-
# set(ANDROID_EXTRA_LIBS
26-
# ${CMAKE_CURRENT_SOURCE_DIR}/path/to/libcrypto.so
27-
# ${CMAKE_CURRENT_SOURCE_DIR}/path/to/libssl.so)
28-
# endif()
29-
#endif()
30-
3120
if (MSVC)
3221
add_definitions(/bigobj)
3322
add_definitions(/FS)
@@ -57,15 +46,9 @@ if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
5746
${PROJECT_SOURCES}
5847
)
5948
else()
60-
if(ANDROID)
61-
add_library(iot-facerecognition-server SHARED
62-
${PROJECT_SOURCES}
63-
)
64-
else()
65-
add_executable(iot-facerecognition-server
66-
${PROJECT_SOURCES}
67-
)
68-
endif()
49+
add_executable(iot-facerecognition-server
50+
${PROJECT_SOURCES}
51+
)
6952
endif()
7053

7154
# copy config file

server/source/Client/Client.cpp

Lines changed: 45 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,31 @@ Client::Client(QObject *parent, QWebSocket* _socket, QSettings* config)
5252
connect(dlibWorker, &DLIBWorker::log, this, &Client::log);
5353
dlibWorkerThread->start();
5454

55-
QObject::connect(socket, &QWebSocket::textMessageReceived, this, &Client::processTextMessage);
56-
QObject::connect(socket, &QWebSocket::binaryMessageReceived, this, &Client::processBinaryMessage);
55+
QObject::connect(socket, &QWebSocket::textMessageReceived, this, &Client::processTextMessage, Qt::QueuedConnection);
56+
QObject::connect(socket, &QWebSocket::binaryMessageReceived, this, &Client::processBinaryMessage, Qt::QueuedConnection);
57+
58+
QObject::connect(socket, QOverload<QAbstractSocket::SocketError>::of(&QWebSocket::error), [this]() {
59+
assert(false);
60+
log(QString("Socket error: %1").arg(socket->errorString()));
61+
});
62+
63+
QObject::connect(socket, &QWebSocket::sslErrors, [this](const QList<QSslError> &errors) {
64+
assert(false);
65+
for (const auto& it : errors)
66+
{
67+
log(QString("Socket SSL Error: %1").arg(it.errorString()));
68+
}
69+
});
70+
71+
QObject::connect(socket, &QWebSocket::disconnected, [this]() {
72+
log(QString("Disconnected. Close reason: %1").arg(socket->closeReason()));
73+
});
5774

5875
clearSecondaryDisplayTimer = new QTimer(this);
5976
clearSecondaryDisplayTimer->setInterval(1000);
6077
connect(clearSecondaryDisplayTimer, &QTimer::timeout, [this](){
6178
secondaryDisplay->setPixmap(QPixmap());
6279
});
63-
64-
log("Started processing incoming images. (if any)");
6580
}
6681

6782
Client::~Client()
@@ -77,26 +92,24 @@ Client::~Client()
7792
}
7893

7994
socket->flush();
80-
socket->close();
8195
}
8296

8397
void Client::sendCommand(Client::Command cmd, const QVariant &ctx)
8498
{
8599
QJsonObject obj;
86100

87-
obj["command"] = static_cast<int>(cmd);
101+
obj[keyCommand] = static_cast<int>(cmd);
88102
if (ctx.isValid())
89-
obj["context"] = QJsonValue::fromVariant(ctx);
103+
obj[keyContext] = QJsonValue::fromVariant(ctx);
90104

91105
QJsonDocument jDoc(obj);
92106

93107
sendTextMessage(QString(jDoc.toJson(QJsonDocument::JsonFormat::Compact)));
94108
}
95109

96-
void Client::throwException(const QString& str)
110+
void Client::throwException(const std::exception& e)
97111
{
98-
std::cout << "FATAL ERROR: " << str.toStdString() << std::endl;
99-
throw str;
112+
throw e;
100113
}
101114

102115
void Client::processTextMessage(const QString& string)
@@ -106,18 +119,17 @@ void Client::processTextMessage(const QString& string)
106119
if (jDoc.isNull() || jDoc.isEmpty() || !jDoc.isObject())
107120
return;
108121

109-
const unsigned int command = (unsigned)(jDoc["command"].toInt(-1));
122+
const unsigned int command = (unsigned)(jDoc[keyCommand].toInt(-1));
110123

111-
QJsonValue ctxVal = jDoc["context"];
124+
QJsonValue ctxVal = jDoc[keyContext];
112125
QVariant context;
113126

114127
if (ctxVal != QJsonValue::Undefined)
115128
context = ctxVal.toVariant();
116129

117-
switch(command)
130+
switch( static_cast<Command>(command) )
118131
{
119-
// Client wants to change its name:
120-
case (unsigned int)Command::SETTING_NAME:
132+
case Command::SETTING_NAME:
121133
{
122134
// ClientHandler* cHandler = qobject_cast<ClientHandler *>(parent());
123135
QString _name = context.toString();
@@ -143,44 +155,49 @@ void Client::processTextMessage(const QString& string)
143155
break;
144156
}
145157

146-
// Client wants to send a message:
147-
case (unsigned int)Command::MESSAGE:
158+
case Command::MESSAGE:
148159
{
149160
emit log(" says: " + context.toString());
150161
break;
151162
}
152163

153-
case (unsigned int)Command::SETTING_LABELCOUNT:
164+
case Command::SETTING_LABELCOUNT:
154165
{
155166
settings.labelCount = context.toULongLong();
156167

157168
sendCommand(Command::SETTING_LABELCOUNT, settings.labelCount.load());
158169
break;
159170
}
160171

161-
case (unsigned int)Command::SETTING_OBJDETECTIONENABLED:
172+
case Command::SETTING_OBJDETECTIONENABLED:
162173
{
163174
settings.objectDetectionEnabled = context.toBool();
164175

165-
sendCommand(Command::SETTING_LABELCOUNT, settings.objectDetectionEnabled.load());
176+
sendCommand(Command::SETTING_OBJDETECTIONENABLED, settings.objectDetectionEnabled.load());
166177
break;
167178
}
168179

169-
case (unsigned int)Command::SETTING_DETERMINISTICOBJECTDETECTION:
180+
case Command::SETTING_DETERMINISTICOBJECTDETECTION:
170181
{
171182
settings.deterministicObjectDetection = context.toBool();
172183

173184
sendCommand(Command::SETTING_DETERMINISTICOBJECTDETECTION, settings.deterministicObjectDetection.load());
174185
break;
175186
}
176187

177-
case (unsigned int)Command::SETTING_FACERECOGNITIONENABLED:
188+
case Command::SETTING_FACERECOGNITIONENABLED:
178189
{
179190
settings.faceRecognitionEnabled = context.toBool();
180191

181192
sendCommand(Command::SETTING_FACERECOGNITIONENABLED, settings.faceRecognitionEnabled.load());
182193
break;
183194
}
195+
196+
default:
197+
{
198+
emit log(QString("Received unrecognized command! %1").arg(command));
199+
break;
200+
}
184201
// implement later ...
185202
}
186203
}
@@ -213,6 +230,10 @@ void Client::processBinaryMessage(const QByteArray& data)
213230
// log("Received image frame with size: " + QString::number(data.size()) + " bytes. Processing it...");
214231
emit process(data);
215232
}
233+
else
234+
{
235+
log("Frame dropped!");
236+
}
216237

217238
if(dialog)
218239
{
@@ -347,12 +368,12 @@ void Client::sendBinaryMessage(const QByteArray &data)
347368
socket->sendBinaryMessage(data);
348369
}
349370

350-
QListWidgetItem* Client::getListWidgetItem()
371+
QListWidgetItem* Client::getListWidgetItem() const
351372
{
352373
return listItem;
353374
}
354375

355-
ClientDialog* Client::getDialog()
376+
ClientDialog* Client::getDialog() const
356377
{
357378
return dialog;
358379
}

server/source/Client/Client.h

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,22 @@ struct Settings
3131
std::atomic<bool> faceRecognitionEnabled = true;
3232
};
3333

34+
class QWebSocket;
35+
class QGraphicsPixmapItem;
36+
class DLIBWorker;
37+
class QTimer;
38+
class QListWidgetItem;
39+
class ClientDialog;
40+
class QThread;
41+
class QSettings;
42+
3443
class Client : public QObject
3544
{
3645
Q_OBJECT
3746

38-
private:
47+
inline static const char * keyCommand = "command";
48+
inline static const char * keyContext = "context";
49+
3950
enum class Command : unsigned int
4051
{
4152
SETTING_NAME = 1,
@@ -50,27 +61,27 @@ class Client : public QObject
5061

5162
Settings settings;
5263

53-
class QWebSocket* socket;
64+
QWebSocket* socket;
5465

5566
QString name {"?"};
5667

57-
class QGraphicsPixmapItem* primaryDisplay;
58-
class QGraphicsPixmapItem* secondaryDisplay;
59-
class QGraphicsPixmapItem* tertiaryDisplay;
68+
QGraphicsPixmapItem* primaryDisplay;
69+
QGraphicsPixmapItem* secondaryDisplay;
70+
QGraphicsPixmapItem* tertiaryDisplay;
6071

61-
class DLIBWorker *dlibWorker;
72+
DLIBWorker *dlibWorker;
6273

63-
class QTimer* clearSecondaryDisplayTimer;
74+
QTimer* clearSecondaryDisplayTimer;
6475

65-
class QListWidgetItem* listItem = nullptr;
66-
class ClientDialog* dialog = nullptr;
76+
QListWidgetItem* listItem = nullptr;
77+
ClientDialog* dialog = nullptr;
6778

68-
class QThread* dlibWorkerThread;
79+
QThread* dlibWorkerThread;
6980

7081
void sendCommand(Command cmd, const QVariant& ctx = QVariant());
7182

7283
public:
73-
explicit Client(QObject *parent, QWebSocket* _socket, class QSettings* config);
84+
explicit Client(QObject *parent, QWebSocket* _socket, QSettings* config);
7485
~Client();
7586

7687
void sendTextMessage(const QString& string);
@@ -82,8 +93,8 @@ class Client : public QObject
8293

8394
QString getName() const;
8495

85-
QListWidgetItem* getListWidgetItem();
86-
ClientDialog* getDialog();
96+
QListWidgetItem* getListWidgetItem() const;
97+
ClientDialog* getDialog() const;
8798

8899
void setListWidgetItem(QListWidgetItem* _listItem);
89100
void setDialog(ClientDialog* _dialog);
@@ -96,13 +107,12 @@ private slots:
96107
void processDlibWorkerFaceResults(const QVector<QPair<QRect, QString>>& results);
97108
void processDlibWorkerObjectResults(const QStringList& results);
98109

99-
void throwException(const QString& str);
110+
void throwException(const std::exception &e);
100111

101112
public:
102113
signals:
103114
void clientNameChanged(const QString& name);
104115
void log(const QString& str);
105-
106116
void process(const QByteArray& buffer);
107117

108118
};

server/source/ClientHandler/ClientHandler.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ void ClientHandler::removeClient(QWebSocket *socket)
5656
if(client)
5757
{
5858
delete client;
59+
client = nullptr;
5960
clients.erase(it);
6061
}
6162
}

server/source/ClientHandler/ClientHandler.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,25 @@
2525

2626
#include "UIInterface/UIInterface.h"
2727

28+
class QSettings;
29+
class QWebSocket;
30+
2831
class ClientHandler : public QObject
2932
{
3033
Q_OBJECT
3134

3235
private:
33-
class QSettings* config;
36+
QSettings* config;
3437
UIInterface*& uiInterface;
3538

36-
class QHash<class QWebSocket*, class Client *> clients;
39+
QHash<class QWebSocket*, class Client *> clients;
3740

3841
public:
3942
explicit ClientHandler(QObject *parent, UIInterface*& _uiInterface, QSettings* config);
4043
~ClientHandler();
4144

42-
void newClient(class QWebSocket *socket);
43-
void removeClient(class QWebSocket *socket);
45+
void newClient(QWebSocket *socket);
46+
void removeClient(QWebSocket *socket);
4447
void removeAllClients(void);
4548

4649
bool isClientPresent(const QString& name);

server/source/DLIBWorker/DLIBWorker.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ DLIBWorker::DLIBWorker(class QSettings* config, Settings *settings)
5959
QFile refFile(_refFile);
6060
if(!refFile.open(QIODevice::ReadOnly))
6161
{
62-
throw "Reference photo list file can not be opened!";
62+
throw std::exception("Reference photo list file can not be opened!");
6363
}
6464
else
6565
{
@@ -251,7 +251,7 @@ void DLIBWorker::process(const QByteArray& buffer)
251251
{
252252
if(m_refPhotoFileList.size() == 0)
253253
{
254-
throwException("Reference face list is empty!");
254+
throwException(std::exception("Reference face list is empty!"));
255255
}
256256
else
257257
{
@@ -329,7 +329,7 @@ void DLIBWorker::process(const QByteArray& buffer)
329329
}
330330
catch(const std::exception& e)
331331
{
332-
emit throwException(e.what());
332+
emit throwException(e);
333333
}
334334
}
335335

server/source/DLIBWorker/DLIBWorker.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@ public slots:
131131
std::vector<Face> findFaces(const dlib::array2d<dlib::rgb_pixel>& img);
132132
std::vector<Face> findFaces(const QString& fileName);
133133

134-
// util functions taken from dnn_imagenet_ex.cpp:
135134
static dlib::rectangle make_cropping_rect_resnet(const dlib::matrix<dlib::rgb_pixel>& img, dlib::rand* rnd);
136135
static void crop_images(const dlib::matrix<dlib::rgb_pixel>& img, dlib::array<dlib::matrix<dlib::rgb_pixel>>& crops, dlib::rand* rnd, long num_crops);
137136

@@ -140,7 +139,7 @@ public slots:
140139
void doneFace(const QVector<QPair<QRect, QString>>& results);
141140
void doneObject(const QStringList& results);
142141

143-
void throwException(const QString& str);
142+
void throwException(const std::exception& e);
144143
void log(const QString& str);
145144

146145
};

server/source/MainWindow/MainWindow.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ class MainWindow : public QMainWindow
4141

4242
private slots:
4343
void on_quitButton_clicked();
44-
4544
void on_aboutButton_clicked();
4645

4746
private:

0 commit comments

Comments
 (0)