-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
224 lines (190 loc) · 6.19 KB
/
Copy pathmain.cpp
File metadata and controls
224 lines (190 loc) · 6.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include <QApplication>
#include <QSplashScreen>
#include <QPixmap>
#include <QProcess>
#include <QDir>
#include <QThread>
#include <QFileInfo>
#include <QDebug>
#include <cstdlib>
#include <iostream>
#include "gui/MainWindow.h"
#include "utils/PathUtil.h"
#include "services/DataManager.h"
#include "services/TimetableEngine.h"
#include "timetable/Timetable.h"
static QProcess *flaskProcess = nullptr;
static int chosenPort = -1;
static QString findProjectRoot()
{
QString dir = QCoreApplication::applicationDirPath();
while (true) {
if (QFileInfo::exists(dir + "/web/app.py"))
return dir;
QDir up(dir);
up.cdUp();
if (up.absolutePath() == dir)
break;
dir = up.absolutePath();
}
return QDir::currentPath();
}
static void killPort(int port)
{
QProcess k;
QString cmd = QString(
"PID=$(lsof -ti:%1 2>/dev/null) && "
"if [ -n \"$PID\" ]; then "
" CMD=$(ps -p $PID -o comm= 2>/dev/null); "
" if echo \"$CMD\" | grep -qiE 'python|flask'; then "
" kill $PID 2>/dev/null; "
" fi; "
"fi; true"
).arg(port);
k.start("bash", {"-c", cmd});
k.waitForFinished(2000);
}
static int startFlaskWebServer(const QString &projectRoot)
{
killPort(5000);
for (int port = 5000; port <= 5009; ++port) {
killPort(port);
QThread::msleep(200);
QProcess *flask = new QProcess();
flask->setProcessChannelMode(QProcess::ForwardedChannels);
flask->setWorkingDirectory(projectRoot);
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
env.insert("FLASK_PORT", QString::number(port));
env.insert("FLASK_SILENT_LOGS", "1");
flask->setProcessEnvironment(env);
flask->start("python3", {"web/app.py"});
if (!flask->waitForStarted(5000)) {
qWarning() << "[WebServer] Failed to start Flask on port" << port << ":" << flask->errorString();
delete flask;
continue;
}
QThread::msleep(1500);
bool ready = false;
for (int attempt = 0; attempt < 10; ++attempt) {
if (flask->state() != QProcess::Running)
break;
QProcess probe;
probe.start("curl", {"-sf", "--max-time", "1",
QString("http://127.0.0.1:%1/").arg(port)});
probe.waitForFinished(2000);
if (probe.exitCode() == 0) {
ready = true;
break;
}
QThread::msleep(300);
}
if (ready) {
flaskProcess = flask;
chosenPort = port;
qDebug().noquote() << QString("[WebServer] Flask ready on http://127.0.0.1:%1").arg(port);
return port;
}
qWarning() << "[WebServer] Flask on port" << port << "did not become ready, trying next...";
flask->terminate();
flask->waitForFinished(2000);
delete flask;
}
qWarning() << "[WebServer] Could not start Flask on any port (5000-5009).";
return -1;
}
static void stopFlaskWebServer()
{
if (!flaskProcess)
return;
qDebug() << "[WebServer] Shutting down Flask...";
flaskProcess->terminate();
if (!flaskProcess->waitForFinished(5000)) {
flaskProcess->kill();
flaskProcess->waitForFinished(3000);
}
delete flaskProcess;
flaskProcess = nullptr;
chosenPort = -1;
}
static int solveFromDb(const QString &projectRoot)
{
Q_UNUSED(projectRoot);
DataManager dm;
if (!dm.loadFromDB()) {
std::cerr << "{\"error\":\"Failed to open database\"}\n";
return 1;
}
if (dm.lessons.empty()) {
std::cout << "{\"score\":0,\"slots\":0,\"unscheduled\":0,\"message\":\"No lessons in database\"}\n";
return 0;
}
auto old_buf = std::cout.rdbuf();
std::cout.rdbuf(std::cerr.rdbuf());
TimetableEngine engine;
Timetable result = engine.generate(dm);
std::cout.rdbuf(old_buf);
dm.sql.clearTimetableSlots();
int slotsWritten = 0;
for (auto it = result.schedules.begin(); it != result.schedules.end(); ++it) {
int classId = it->first;
const auto &schedule = it->second;
for (int dayIdx = 0; dayIdx < (int)schedule.size(); ++dayIdx) {
const auto &dayRow = schedule[dayIdx];
for (int perIdx = 0; perIdx < (int)dayRow.size(); ++perIdx) {
const TimetableCell &cell = dayRow[perIdx];
if (!cell.isEmpty()) {
dm.sql.addTimetableSlot(classId, dayIdx + 1, perIdx + 1,
cell.subjectId, cell.teacherId, cell.roomId);
slotsWritten++;
}
}
}
}
std::cout << "{\"score\":" << result.score
<< ",\"slots\":" << slotsWritten
<< ",\"unscheduled\":" << result.unscheduledLessons.size()
<< "}\n";
return 0;
}
static void printWebUrl()
{
if (chosenPort > 0) {
std::cout << "\n Web interface: http://127.0.0.1:" << chosenPort << "\n";
}
}
int main(int argc, char *argv[])
{
bool solveDbMode = false;
for (int i = 1; i < argc; ++i) {
std::string arg = argv[i];
if (arg == "--solve-from-db") {
solveDbMode = true;
}
}
// Headless solver mode (invoked by Flask /api/solve)
if (solveDbMode) {
QCoreApplication app(argc, argv);
QString projectRoot = findProjectRoot();
return solveFromDb(projectRoot);
}
// GUI mode: start Flask then launch the Qt application window
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QApplication app(argc, argv);
QString projectRoot = findProjectRoot();
qDebug().noquote() << "[Project Root]" << projectRoot;
if (startFlaskWebServer(projectRoot) > 0) {
printWebUrl();
} else {
qWarning() << "[WebServer] Could not start web server. GUI works standalone.";
}
QPixmap splashPixmap(PathUtil::resolvePath("gui/splash.png"));
QSplashScreen splash(splashPixmap);
splash.show();
app.processEvents();
MainWindow w;
w.show();
splash.finish(&w);
int ret = app.exec();
stopFlaskWebServer();
return ret;
}