-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.cpp
79 lines (67 loc) · 2.04 KB
/
handler.cpp
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
#include "handler.h"
#include <v2server.h>
#include <algorithm>
#include <QDebug>
#include <QJsonDocument>
#include <QFile>
#include <QProcess>
#include <QTimer>
#include <QTextStream>
#include <v2ui.h>
Handler::Handler(QObject *parent, v2Ui * ui) : QObject(parent)
{
qInfo()<<"handler started";
isConnected = false;
Ui = ui;
process = new QProcess;
}
void Handler::addServer(v2Server * server)
{
serverList.append(server);
emit serverAdded(server);
}
void Handler::removeServer(v2Server * server)
{
// finding index number of the v2Server object
QList<v2Server*>::iterator index_iter = std::find(serverList.begin(), serverList.end(), server);
int index_number = 0;
if (serverList.end() != index_iter)
index_number = std::distance(serverList.begin(), index_iter);
else
throw std::runtime_error("the server doesn't exist in the list");
serverList.removeAt(index_number);
emit serverRemoved(server);
}
void Handler::setActiveServer(v2Server * server)
{
activeServer = server;
}
void Handler::connectToServer(v2Server * serverToConnect)
{
QJsonDocument * json = serverToConnect->serverJson;
QString jsonString = QString(json->toJson());
QFile config("/home/arad/proj/v2rayParsa/v2rayRunner/config.json");
config.open(QFile::ReadWrite);
config.resize(0);
config.write(jsonString.toUtf8());
config.flush();
connect(process , &QProcess::readyReadStandardOutput , this , &Handler::readyReadStandardOutput);
process->setWorkingDirectory("/home/arad/proj/v2rayParsa/v2rayRunner");
process->start("/home/arad/proj/v2rayParsa/v2rayRunner/v2ray" , QStringList() << "run");
process->waitForStarted();
isConnected = true;
qInfo()<<"finished";
}
void Handler::disconnect()
{
qInfo()<<"killing process";
process->kill();
process->waitForFinished();
qInfo()<<"actually finished";
isConnected = false;
}
void Handler::readyReadStandardOutput()
{
qInfo()<<"readyReadStandardOutput";
Ui->setV2rayOutput(process->readAllStandardOutput());
}