Repository navigation
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
349 lines (301 loc) · 7.31 KB
/
Copy pathmainwindow.cpp
File metadata and controls
349 lines (301 loc) · 7.31 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent), ui(new Ui::MainWindow)
{
message = NULL;
c = NULL;
s = NULL;
ui->setupUi(this);
QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
tray = new QSystemTrayIcon(QIcon(":/new/logo.png"));
tray->show();
is_visible = true;
connect(tray, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this,
SLOT(show_hide(QSystemTrayIcon::ActivationReason)));
connect(ui->zakoncz, SIGNAL(clicked()), this, SLOT(stop()));
connect(MessageFlow::getInstance(), SIGNAL(message(MessageObject*)), this,
SLOT(showInfo(MessageObject*)));
setWindowTitle("Monitor sieci");
MessageFlow::getInstance()->start();
ui->tableWidget->setColumnCount(2);
ui->tableWidget->setRowCount(0);
ui->tableWidget->setHorizontalHeaderLabels(
QStringList() << "Nazwa hosta" << "Adres");
ui->tableWidget->horizontalHeader()->setStretchLastSection(true);
}
MainWindow::~MainWindow()
{
delete ui;
stop();
}
void MainWindow::show_hide(QSystemTrayIcon::ActivationReason dana)
{
if (dana == QSystemTrayIcon::DoubleClick)
{
if (is_visible)
{
hide();
is_visible = false;
}
else
{
show();
is_visible = true;
}
}
}
void MainWindow::ustaw(QString adres, int i)
{
int ob = adresy_obecnosc[adres];
if (ob != i)
{
if (i == 0)
{
QHostInfo info = QHostInfo::fromName(adres);
tray->showMessage("",
"Podłączono do " + adres + " " + info.hostName(),
QSystemTrayIcon::NoIcon, 2000);
adresy_obecnosc[adres] = i;
ui->tableWidget->setRowCount(ui->tableWidget->rowCount() + 1);
int row = ui->tableWidget->rowCount() - 1;
hosty[adres] = new Host(ui->tableWidget, row, adres);
//std::cout<<"ROW: "<<row<<std::endl;
ui->tableWidget->setItem(row, 0,
new QTableWidgetItem(info.hostName()));
ui->tableWidget->setItem(row, 1,
new QTableWidgetItem(tr(adres.toLatin1().data())));
/*startowanie watku sprawdzającego hosta*/
hosty[adres]->start();
}
else
{
tray->showMessage("", "Odłączono " + adres,
QSystemTrayIcon::Information, 2000);
adresy_obecnosc[adres] = i;
QList<QTableWidgetItem *> lista = ui->tableWidget->findItems(adres,
Qt::MatchContains);
if (lista.size() > 0) {
int row = ui->tableWidget->row(lista[0]);
ui->tableWidget->removeRow(row);
/*konczenie watku sprawdzającego hosta*/
hosty[adres]->quit();
}
}
}
else if (i == 0)
{
QList<QTableWidgetItem *> lista = ui->tableWidget->findItems(adres,
Qt::MatchContains);
qDebug()<<"lista->size(): "<<QString::number(lista.size())<<"\n";
if (lista.size() > 0) {
int row = ui->tableWidget->row(lista[0]);
QTableWidgetItem *item = ui->tableWidget->item(row, 0);
std::string str = item->text().toStdString();
qDebug()<<"Tekst: "<<item->text()<<"\nAdres: "<<adres<<"\n";
if (item->text() == adres) {
QHostInfo info = QHostInfo::fromName(adres);
qDebug()<<"Host name: "<<info.hostName()<<"\n";
if (info.hostName() != adres) {
item->setText(info.hostName());
}
}
}
}
}
void MainWindow::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type())
{
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
void MainWindow::on_pushButton_clicked()
{
closeAllThreads(false);
if (s != NULL)
{
s->terminate();
while (!s->isFinished())
{
}
delete s;
}
Ping::setStarted(true);
MessageFlow *message = MessageFlow::getInstance();
message->sendMessage(new MessageObject(0, "Startowanie wątków", true));
s = new Start(&list, &adresy_obecnosc, ui->tableWidget,
ui->lineEdit->text(), ui->lineEdit_2->text(), this);
s->start();
message->sendMessage(new MessageObject(0, "Wątki wystartowane", false));
}
void MainWindow::closeAllThreads(bool notify)
{
if (c != NULL)
{
if (!c->isFinished())
{
c->terminate();
for (int retries = 0; retries < 100 && !c->isFinished(); retries++)
{
usleep(rand() % 100);
}
if (!c->isRunning())
qDebug() << "thread is stopped";
else
qDebug() << "thread is not stopped";
}
}
c = new Clean(&list, notify);
c->start();
}
void MainWindow::stop()
{
closeAllThreads(true);
}
void MainWindow::showInfo(MessageObject* m) {
if (m->getIcon() == 0) {
if (message != NULL) {
message->close();
delete (message);
}
message = new QMessageBox(QMessageBox::Information, "Informacja",
m->getMessage(), QMessageBox::Ok, this, Qt::Dialog);
message->setWindowModality(Qt::ApplicationModal);
message->show();
blockButtons(m->isBlocking());
}
}
void MainWindow::blockButtons(bool block)
{
ui->pushButton->setEnabled(!block);
ui->zakoncz->setEnabled(!block);
}
Clean::Clean(QList<Ping*> *l, bool isNotify)
{
list = l;
this->isNotify = isNotify;
}
void Clean::run()
{
if(isNotify)
MessageFlow::getInstance()->sendMessage(
new MessageObject(0, "Kończenie pracy wątków", true));
Ping::setStarted(false);
while (!Mutex::tryLock())
{
}
for (int i = 0; i < list->size(); i++)
{
Ping *p = list->at(i);
qDebug() << "try to stop thread";
p->terminate();
srand( time(NULL));
for (int retries = 0; retries < 100 && !p->isFinished(); retries++)
{
usleep(rand() % 100);
}
if (!p->isRunning())
qDebug() << "thread is stopped";
else
qDebug() << "thread is not stopped";
//delete(p);
}
qDebug()<<"#################";
for (int i = 0; i < list->size(); i++)
{
qDebug() << i << " "<< list->at(i)->isFinished();
}
qDebug()<<"#################";
list->clear();
qDebug()<<"list->size() "<< list->size();
Mutex::unlock();
if(isNotify)
MessageFlow::getInstance()->sendMessage(
new MessageObject(0, "Zamykanie wątków udane", false));
}
Start::Start(QList<Ping*> *list, QMap<QString, int> *adresy_obecnosc,
QTableWidget *table, QString poczatek, QString koniec,
MainWindow *mainW)
{
this->list = list;
this->adresy_obecnosc = adresy_obecnosc;
this->poczatek = poczatek;
this->koniec = koniec;
this->table = table;
this->mainW = mainW;
}
void Start::startThread()
{
start();
}
void Start::run()
{
while (!Mutex::tryLock())
{
}
for (int i = table->rowCount() - 1; i >= 0; --i)
{
table->removeRow(i);
}
Ping::setStarted(true);
QString tmp;
int kropki = 0;
for (int i = 0; i < poczatek.length(); i++)
{
if (kropki == 3)
{
tmp += poczatek[i];
}
if (poczatek[i] == '.')
kropki++;
}
int start = tmp.toInt();
std::cout << start << std::endl;
tmp = "";
kropki = 0;
for (int i = 0; i < koniec.length(); i++)
{
if (kropki == 3)
{
tmp += koniec[i];
}
if (koniec[i] == '.')
kropki++;
}
int stop = tmp.toInt();
std::cout << stop << std::endl;
if (stop - start <= 0)
{
MessageFlow::getInstance()->sendMessage(
new MessageObject(0, "Błąd zakresu", false));
}
else
{
QString podstawa;
kropki = 0;
for (int i = 0; kropki < 3; i++)
{
if (poczatek[i] == '.')
kropki++;
podstawa += poczatek[i];
}
std::cout << podstawa.toStdString() << std::endl;
std::cout << "List.size:" << list->size() << std::endl;
for (int i = 0; i < stop - start; i++) {
Ping *p = new Ping(5, podstawa + QString::number((start + i)));
list->push_back(p);
connect(p, SIGNAL(dane(QString,int)), mainW,
SLOT(ustaw(QString,int)));
msleep(100);
p->start();
(*adresy_obecnosc)[podstawa + QString::number((start + i))] = 256;
}
}
Mutex::unlock();
}