Skip to content

Commit 847c89b

Browse files
committed
server-starter class
1 parent 6a2497f commit 847c89b

File tree

8 files changed

+249
-179
lines changed

8 files changed

+249
-179
lines changed

src/main.cpp

Lines changed: 11 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#include <QTextCodec>
2020
#include <QtGlobal>
2121

22-
#include "port-scanner.h"
22+
#include "server-starter.h"
2323

2424
#if QT_VERSION < QT_VERSION_CHECK(5, 6, 0)
2525
#include "webkit-main-window.h"
@@ -45,7 +45,7 @@ int main(int argc, char **argv)
4545
// ==============================
4646
// Application version:
4747
// ==============================
48-
application.setApplicationVersion("0.9.1");
48+
application.setApplicationVersion("1.0.0");
4949

5050
// ==============================
5151
// UTF-8 encoding application-wide:
@@ -215,7 +215,6 @@ int main(int argc, char **argv)
215215
bool startFileFound = false;
216216

217217
QString startPageFilePath = applicationDirName + "/index.html";
218-
219218
QString localServerSettingsFilePath =
220219
applicationDirName + "/local-server.json";
221220

@@ -233,123 +232,22 @@ int main(int argc, char **argv)
233232
// Local server:
234233
if ((!startPageFile.exists()) and localServerSettingsFile.exists()) {
235234
startFileFound = true;
236-
bool localServerSettingsCorrect = false;
237-
238-
QString localServerFullPath;
239-
QString port;
240-
QStringList localServerCommandLine;
241-
242-
QFileReader *localServerSettingsReader =
243-
new QFileReader(localServerSettingsFilePath);
244-
QString localServerSettings =
245-
localServerSettingsReader->fileContents;
246-
247-
QJsonDocument localServerJsonDocument =
248-
QJsonDocument::fromJson(localServerSettings.toUtf8());
249-
250-
QJsonObject localServerJson;
251235

252-
if (!localServerJsonDocument.isNull()) {
253-
localServerJson = localServerJsonDocument.object();
254-
if (!localServerJson.isEmpty()) {
255-
localServerSettingsCorrect = true;
256-
}
257-
}
236+
QServerStarter *serverStarter =
237+
new QServerStarter(localServerSettingsFilePath);
258238

259-
if (localServerSettingsCorrect == true) {
260-
// Local server full path:
261-
QString localServerFullPathSetting =
262-
applicationDirName + "/" +
263-
localServerJson["file"].toString();
264-
265-
QFile localServerFile(localServerFullPathSetting);
266-
267-
if (localServerFile.exists()) {
268-
localServerFullPath = localServerFullPathSetting;
269-
localServerCommandLine.append(localServerFullPath);
270-
} else {
271-
mainWindow.qDisplayError(
272-
QString("Local server file is not found."));
273-
}
274-
275-
// Local server port:
276-
QJsonArray ports = localServerJson["ports"].toArray();
277-
278-
qint16 firstPort = ports[0].toInt();
279-
qint16 lastPort = ports[1].toInt();
280-
281-
if (firstPort == 0) {
282-
localServerSettingsCorrect = false;
283-
mainWindow.qDisplayError(
284-
QString("No local server port is set."));
285-
}
286-
287-
if (firstPort > 0) {
288-
if (lastPort == 0) {
289-
lastPort = firstPort;
290-
}
291-
292-
QPortScanner *portScanner =
293-
new QPortScanner(firstPort, lastPort);
294-
295-
if (portScanner->portScannerError.length() == 0) {
296-
port = QString::number(portScanner->port);
297-
application.setProperty("port", port);
298-
}
299-
300-
if (portScanner->portScannerError.length() > 0) {
301-
mainWindow.qDisplayError(
302-
portScanner->portScannerError);
303-
}
304-
}
305-
306-
// Local server command line arguments.
307-
// Local server port must be defined at this point!
308-
QJsonArray commandLineArgumentsArray =
309-
localServerJson["command-line-arguments"].toArray();
310-
foreach (QVariant argument, commandLineArgumentsArray) {
311-
QString argumentString = argument.toString();
312-
argumentString.replace("#PORT#", port);
313-
localServerCommandLine.append(argumentString);
314-
}
315-
316-
// Local server shutdown command:
317-
QString shutdownCommand =
318-
localServerJson["shutdown_command"].toString();
319-
if (shutdownCommand.length() > 0) {;
320-
application.setProperty("shutdown_command", shutdownCommand);
321-
}
322-
}
239+
// Signal and slot for loading of the local server URL:
240+
QObject::connect(serverStarter, SIGNAL(loadUrlSignal(QUrl)),
241+
&mainWindow, SLOT(qLoadUrlSlot(QUrl)));
323242

324-
if (localServerSettingsCorrect == false) {
325-
mainWindow.qDisplayError(
326-
localServerSettingsFilePath + "<br>" +
327-
"is empty or malformed.");
328-
}
329-
330-
// Local server has to be started as
331-
// a detached process for its proper operation:
332-
if (localServerSettingsCorrect == true) {
333-
QProcess localServer;
334-
localServer.startDetached (
335-
qApp->property("perlInterpreter").toString(),
336-
localServerCommandLine);
337-
338-
// Local server is pinged every second until ready,
339-
// but after 5 seconds of being unaivailable,
340-
// a timeout message is displayed.
341-
// If local server is up and running for less than 5 seconds,
342-
// its index page is loaded.
343-
mainWindow.localServerTester = new QTimer();
344-
QObject::connect(mainWindow.localServerTester, SIGNAL (timeout()),
345-
&mainWindow, SLOT(qLocalServerPingSlot()));
346-
mainWindow.localServerTester->start(1000);
347-
}
243+
// Signal and slot for displaying local server configuration errors:
244+
QObject::connect(serverStarter, SIGNAL(displayErrorSignal(QString)),
245+
&mainWindow, SLOT(qDisplayErrorSlot(QString)));
348246
}
349247

350248
// No start file:
351249
if (startFileFound == false) {
352-
mainWindow.qDisplayError(
250+
mainWindow.qDisplayErrorSlot(
353251
QString("No start page or local server is found."));
354252
}
355253

src/peb.pro

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ greaterThan (QT_MAJOR_VERSION, 4) {
120120
file-reader.cpp \
121121
main-window.cpp \
122122
port-scanner.cpp \
123+
server-starter.cpp \
123124
script-handler.cpp \
124125
webkit-page.cpp \
125126
webkit-view.cpp
@@ -128,6 +129,7 @@ greaterThan (QT_MAJOR_VERSION, 4) {
128129
HEADERS += \
129130
file-reader.h \
130131
port-scanner.h \
132+
server-starter.h \
131133
script-handler.h \
132134
webkit-main-window.h \
133135
webkit-page.h \
@@ -142,6 +144,7 @@ greaterThan (QT_MAJOR_VERSION, 4) {
142144
file-reader.cpp \
143145
main-window.cpp \
144146
port-scanner.cpp \
147+
server-starter.cpp \
145148
script-handler.cpp \
146149
webengine-page.cpp \
147150
webengine-view.cpp
@@ -150,6 +153,7 @@ greaterThan (QT_MAJOR_VERSION, 4) {
150153
HEADERS += \
151154
file-reader.h \
152155
port-scanner.h \
156+
server-starter.h \
153157
script-handler.h \
154158
webengine-main-window.h \
155159
webengine-page.h \
@@ -163,6 +167,7 @@ greaterThan (QT_MAJOR_VERSION, 4) {
163167
file-reader.cpp \
164168
main-window.cpp \
165169
port-scanner.cpp \
170+
server-starter.cpp \
166171
script-handler.cpp \
167172
webkit-page.cpp \
168173
webkit-view.cpp
@@ -171,6 +176,7 @@ greaterThan (QT_MAJOR_VERSION, 4) {
171176
HEADERS += \
172177
file-reader.h \
173178
port-scanner.h \
179+
server-starter.h \
174180
script-handler.h \
175181
webkit-main-window.h \
176182
webkit-page.h \

src/port-scanner.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,12 @@
1515
https://github.com/ddmitov/perl-executing-browser
1616
*/
1717

18+
#include <QObject>
1819
#include <QTcpSocket>
1920

2021
#ifndef PORTSCANNER_H
2122
#define PORTSCANNER_H
2223

23-
#include <QObject>
24-
2524
// ==============================
2625
// PORT SCANNER CLASS DEFINITION:
2726
// ==============================

src/resources/peb.rc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ IDI_ICON1 ICON DISCARDABLE "icon/camel.ico"
33
# include <winver.h>
44

55
VS_VERSION_INFO VERSIONINFO
6-
FILEVERSION 0,9,0,0
7-
PRODUCTVERSION 0,9,0,0
6+
FILEVERSION 1,0,0,0
7+
PRODUCTVERSION 1,0,0,0
88
FILEFLAGSMASK 0x3fL
99
#ifdef _DEBUG
1010
FILEFLAGS VS_FF_DEBUG
@@ -21,7 +21,7 @@ VS_VERSION_INFO VERSIONINFO
2121
BEGIN
2222
VALUE "CompanyName", "PEB Dev Team\0"
2323
VALUE "FileDescription", "Perl Executing Browser\0"
24-
VALUE "FileVersion", "0.9.0.0\0"
24+
VALUE "FileVersion", "1.0.0.0\0"
2525
VALUE "LegalCopyright", "LGPL v.3\0"
2626
VALUE "OriginalFilename", "peb.exe\0"
2727
VALUE "ProductName", "Perl Executing Browser\0"

src/server-starter.cpp

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
/*
2+
Perl Executing Browser
3+
4+
This program is free software;
5+
you can redistribute it and/or modify it under the terms of the
6+
GNU Lesser General Public License,
7+
as published by the Free Software Foundation;
8+
either version 3 of the License, or (at your option) any later version.
9+
This program is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY;
11+
without even the implied warranty of MERCHANTABILITY or
12+
FITNESS FOR A PARTICULAR PURPOSE.
13+
Dimitar D. Mitov, 2013 - 2018
14+
Valcho Nedelchev, 2014 - 2016
15+
https://github.com/ddmitov/perl-executing-browser
16+
*/
17+
18+
#include <QtWidgets/QApplication>
19+
#include <QFile>
20+
#include <QJsonArray>
21+
#include <QJsonDocument>
22+
#include <QJsonObject>
23+
#include <QProcess>
24+
25+
#include "file-reader.h"
26+
#include "port-scanner.h"
27+
#include "server-starter.h"
28+
29+
// ==============================
30+
// SERVER STARTER CONSTRUCTOR:
31+
// ==============================
32+
QServerStarter::QServerStarter(QString localServerSettingsFilePath)
33+
: QObject(0)
34+
{
35+
bool localServerSettingsCorrect = false;
36+
QString localServerFullPath;
37+
QString port;
38+
QStringList localServerCommandLine;
39+
40+
QFileReader *localServerSettingsReader =
41+
new QFileReader(localServerSettingsFilePath);
42+
QString localServerSettings =
43+
localServerSettingsReader->fileContents;
44+
45+
QJsonDocument localServerJsonDocument =
46+
QJsonDocument::fromJson(localServerSettings.toUtf8());
47+
48+
QJsonObject localServerJson;
49+
50+
if (!localServerJsonDocument.isNull()) {
51+
localServerJson = localServerJsonDocument.object();
52+
if (!localServerJson.isEmpty()) {
53+
localServerSettingsCorrect = true;
54+
}
55+
}
56+
57+
if (localServerSettingsCorrect == true) {
58+
// Local server full path:
59+
QString localServerFullPathSetting =
60+
qApp->property("application").toString() + "/" +
61+
localServerJson["file"].toString();
62+
63+
QFile localServerFile(localServerFullPathSetting);
64+
65+
if (localServerFile.exists()) {
66+
localServerFullPath = localServerFullPathSetting;
67+
localServerCommandLine.append(localServerFullPath);
68+
} else {
69+
displayErrorSignal(QString("Local server file is not found."));
70+
}
71+
72+
// Local server port:
73+
QJsonArray ports = localServerJson["ports"].toArray();
74+
75+
qint16 firstPort = ports[0].toInt();
76+
qint16 lastPort = ports[1].toInt();
77+
78+
if (firstPort == 0) {
79+
localServerSettingsCorrect = false;
80+
displayErrorSignal(QString("No local server port is set."));
81+
}
82+
83+
if (firstPort > 0) {
84+
if (lastPort == 0) {
85+
lastPort = firstPort;
86+
}
87+
88+
QPortScanner *portScanner = new QPortScanner(firstPort, lastPort);
89+
90+
if (portScanner->portScannerError.length() == 0) {
91+
port = QString::number(portScanner->port);
92+
qApp->setProperty("port", port);
93+
}
94+
95+
if (portScanner->portScannerError.length() > 0) {
96+
displayErrorSignal(portScanner->portScannerError);
97+
}
98+
}
99+
100+
// Local server command line arguments.
101+
// Local server port must be defined at this point!
102+
QJsonArray commandLineArgumentsArray =
103+
localServerJson["command-line-arguments"].toArray();
104+
foreach (QVariant argument, commandLineArgumentsArray) {
105+
QString argumentString = argument.toString();
106+
argumentString.replace("#PORT#", port);
107+
localServerCommandLine.append(argumentString);
108+
}
109+
110+
// Local server shutdown command:
111+
if (localServerJson["shutdown_command"].toString().length() > 0) {;
112+
qApp->setProperty(
113+
"shutdown_command",
114+
localServerJson["shutdown_command"].toString());
115+
}
116+
}
117+
118+
if (localServerSettingsCorrect == false) {
119+
displayErrorSignal(
120+
localServerSettingsFilePath + "<br>" +
121+
"is empty or malformed.");
122+
}
123+
124+
// Local server has to be started as
125+
// a detached process for its proper operation:
126+
if (localServerSettingsCorrect == true) {
127+
QProcess localServer;
128+
localServer.startDetached (
129+
qApp->property("perlInterpreter").toString(),
130+
localServerCommandLine);
131+
132+
// Local server is pinged every second until ready,
133+
// but after 5 seconds of being unaivailable,
134+
// a timeout message is displayed.
135+
// If local server is up and running for less than 5 seconds,
136+
// its index page is loaded.
137+
localServerTester = new QTimer();
138+
139+
QObject::connect(localServerTester, SIGNAL (timeout()),
140+
this, SLOT(qLocalServerPingSlot()));
141+
localServerTester->start(1000);
142+
}
143+
}

0 commit comments

Comments
 (0)