starting http server with error handling

This commit is contained in:
leca 2025-03-14 18:24:42 +03:00
parent 453f907bfa
commit f507ec8d67
5 changed files with 88 additions and 30 deletions

View File

@ -33,6 +33,7 @@ static QWidget *loadUI(QWidget *parent, std::string filename) {
}
int main(int argc, char *argv[]) {
srand(time(0));
QApplication app(argc, argv);
@ -92,10 +93,6 @@ int main(int argc, char *argv[]) {
OCRScene *ocrscene = new OCRScene();
OFDScene *ofdscene = new OFDScene();
// ofdscene->startHttpServer();
// get_local_ip_address();
sceneLayout->addWidget(mainwindowscene);
sceneLayout->addWidget(emailTextScene);
sceneLayout->addWidget(ocrscene);

View File

@ -17,6 +17,8 @@ OFDScene::OFDScene(QWidget *parent)
: QWidget(parent)
, ui(new Ui::OFDScene) {
ui->setupUi(this);
QObject::connect(this, &OFDScene::httpErrorOccured, this, &OFDScene::notifyHttpServerFailure);
}
OFDScene::~OFDScene() {
@ -25,6 +27,7 @@ OFDScene::~OFDScene() {
void OFDScene::startHttpServer() {
std::string localIp = "";
try {
localIp = get_local_ip_address();
} catch(std::exception e) {
@ -32,13 +35,29 @@ void OFDScene::startHttpServer() {
return;
}
httplib::Server svr;
//TODO: generate random port from 1024 to 65535 and check if its used.
svr.Get("/", [&](const httplib::Request &, httplib::Response &res){
res.set_redirect("http://"+ localIp +":8080/", 301);
});
unsigned short number_of_retries = 0;
svr.listen("0.0.0.0", 8080);
do {
if (number_of_retries == 10) {
emit httpErrorOccured();
return;
}
this->port = rand() % (65535 - 1024) + 1024;
httplib::Server svr;
svr.Get("/", [&](const httplib::Request &, httplib::Response &res){
res.set_redirect("http://"+ localIp +":"+ std::to_string(port) +"/", 301);
});
number_of_retries ++;
std::cerr << "Listening on port: " << this->port << std::endl;
if (!svr.listen("0.0.0.0", this->port)) {
std::cerr << "Random port seems to be occupied. Trying to generate another one" << std::endl;
number_of_retries ++;
continue;
}
} while(true);
}
void OFDScene::on_choose_image_button_clicked() {
@ -53,7 +72,6 @@ void OFDScene::on_choose_image_button_clicked() {
return;
}
ui->info_label->setText(tr("Selected image: ") + filename);
AdjustPictureDialog dialog = AdjustPictureDialog(this, filename.toStdString());
@ -145,6 +163,19 @@ void OFDScene::on_parse_button_clicked() {
void OFDScene::on_binary_eye_button_clicked() {
http_thread = new std::thread(&OFDScene::startHttpServer, this);
}
void OFDScene::notifyHttpServerFailure() {
QMessageBox *infoDialog = new QMessageBox();
infoDialog->setText(tr("Could not start http server. 10 times in a row random port was occupied. Either you should run for a lottery ticket, or the problem is in the program. If the lottery ticket wasn't lucky, please, contact the developer."));
infoDialog->setIcon(QMessageBox::Warning);
infoDialog->setWindowTitle(tr("Could not start http server."));
infoDialog->exec();
delete infoDialog;
}
unsigned int OFDScene::getPort() {
return port;
}

View File

@ -2,6 +2,7 @@
#define OFDSCENE_H
#include <QWidget>
#include <thread>
namespace Ui {
class OFDScene;
@ -15,6 +16,8 @@ public:
explicit OFDScene(QWidget *parent = nullptr);
~OFDScene();
void startHttpServer();
unsigned int getPort();
private slots:
void on_choose_image_button_clicked();
void onDataDecode(std::string data);
@ -22,9 +25,16 @@ private slots:
void on_parse_button_clicked();
void on_binary_eye_button_clicked();
void notifyHttpServerFailure();
signals:
void httpErrorOccured();
private:
Ui::OFDScene *ui;
std::thread *http_thread;
unsigned int port;
};
#endif // OFDSCENE_H

View File

@ -367,47 +367,57 @@
<translation>Parse</translation>
</message>
<message>
<location filename="../ofdscene.cpp" line="49"/>
<location filename="../ofdscene.cpp" line="178"/>
<source>Could not start http server. 10 times in a row random port was occupied. Either you should run for a lottery ticket, or the problem is in the program. If the lottery ticket wasn&apos;t lucky, please, contact the developer.</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../ofdscene.cpp" line="180"/>
<source>Could not start http server.</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../ofdscene.cpp" line="72"/>
<source>Please, select a picture where QR code that contains info about check is present</source>
<translation>Please, select a picture where QR code that contains info about check is present</translation>
</message>
<message>
<location filename="../ofdscene.cpp" line="51"/>
<location filename="../ofdscene.cpp" line="74"/>
<source>Picture was not selected</source>
<translation>Picture was not selected</translation>
</message>
<message>
<location filename="../ofdscene.cpp" line="57"/>
<location filename="../ofdscene.cpp" line="80"/>
<source>Selected image: </source>
<translation>Selected image: </translation>
</message>
<message>
<location filename="../ofdscene.cpp" line="116"/>
<location filename="../ofdscene.cpp" line="139"/>
<source>Captcha was not solved correctly!</source>
<translation>Captcha was not solved correctly!</translation>
</message>
<message>
<location filename="../ofdscene.cpp" line="118"/>
<location filename="../ofdscene.cpp" line="141"/>
<source>Captcha is incorrect</source>
<translation>Captcha is incorrect</translation>
</message>
<message>
<location filename="../ofdscene.cpp" line="123"/>
<location filename="../ofdscene.cpp" line="146"/>
<source>Internal server error. Please, try again later.</source>
<translation>Internal server error. Please, try again later.</translation>
</message>
<message>
<location filename="../ofdscene.cpp" line="125"/>
<location filename="../ofdscene.cpp" line="148"/>
<source>Internal server error</source>
<translation>Internal server error</translation>
</message>
<message>
<location filename="../ofdscene.cpp" line="130"/>
<location filename="../ofdscene.cpp" line="153"/>
<source>Check not found. Please, ensure correctness of entered data.</source>
<translation>Check not found. Please, ensure correctness of entered data.</translation>
</message>
<message>
<location filename="../ofdscene.cpp" line="132"/>
<location filename="../ofdscene.cpp" line="155"/>
<source>Check was not found</source>
<translation>Check was not found</translation>
</message>

View File

@ -367,47 +367,57 @@
<translation>Парсить</translation>
</message>
<message>
<location filename="../ofdscene.cpp" line="49"/>
<location filename="../ofdscene.cpp" line="178"/>
<source>Could not start http server. 10 times in a row random port was occupied. Either you should run for a lottery ticket, or the problem is in the program. If the lottery ticket wasn&apos;t lucky, please, contact the developer.</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../ofdscene.cpp" line="180"/>
<source>Could not start http server.</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../ofdscene.cpp" line="72"/>
<source>Please, select a picture where QR code that contains info about check is present</source>
<translation>Пожалуйста, выберете изображение, содержащее QR код с информацией о чеке</translation>
</message>
<message>
<location filename="../ofdscene.cpp" line="51"/>
<location filename="../ofdscene.cpp" line="74"/>
<source>Picture was not selected</source>
<translation>Изображение не было выбрано</translation>
</message>
<message>
<location filename="../ofdscene.cpp" line="57"/>
<location filename="../ofdscene.cpp" line="80"/>
<source>Selected image: </source>
<translation>Выбранное изображение: </translation>
</message>
<message>
<location filename="../ofdscene.cpp" line="116"/>
<location filename="../ofdscene.cpp" line="139"/>
<source>Captcha was not solved correctly!</source>
<translation>Капча была решена неверно!</translation>
</message>
<message>
<location filename="../ofdscene.cpp" line="118"/>
<location filename="../ofdscene.cpp" line="141"/>
<source>Captcha is incorrect</source>
<translation>Капча введена неверно</translation>
</message>
<message>
<location filename="../ofdscene.cpp" line="123"/>
<location filename="../ofdscene.cpp" line="146"/>
<source>Internal server error. Please, try again later.</source>
<translation>Внутренняя ошибка сервера. Пожалуйста, попробуйте снова позже.</translation>
</message>
<message>
<location filename="../ofdscene.cpp" line="125"/>
<location filename="../ofdscene.cpp" line="148"/>
<source>Internal server error</source>
<translation>Внутренняя ошибка сервера</translation>
</message>
<message>
<location filename="../ofdscene.cpp" line="130"/>
<location filename="../ofdscene.cpp" line="153"/>
<source>Check not found. Please, ensure correctness of entered data.</source>
<translation>Чек не найден. Пожалуйста, убедитесь в правильности введённых данных.</translation>
</message>
<message>
<location filename="../ofdscene.cpp" line="132"/>
<location filename="../ofdscene.cpp" line="155"/>
<source>Check was not found</source>
<translation>Чек не найден</translation>
</message>