-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
294 lines (238 loc) · 7.62 KB
/
mainwindow.cpp
File metadata and controls
294 lines (238 loc) · 7.62 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
#include <QDebug>
#include <QProcess>
#include <QTcpSocket>
#include <QHostAddress>
#include <QFileDialog>
#include <QFormLayout>
#include <regex>
#include "mainwindow.hpp"
#include "ui_mainwindow.h"
#include "addressdialog.hpp"
#include "paramscontainer.hpp"
#include "dataeater.hpp"
#include "debugdialog.hpp"
#include "get_host_info.hpp"
#include "trivialserialize.hpp"
#include <boost/asio.hpp>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
m_tunserver_process(nullptr),
m_dlg(nullptr)
{
ui->setupUi(this);
ParamsContainer params;
params.readParams("peers.json");
m_peer_lst = params.getPeerList();
for(auto it : m_peer_lst){
QString peer_val = QString::fromStdString(it.m_ipv6);
ui->peerListWidget->addItem(peer_val);
}
std::vector <char> testData;
std::string tst_msg ("{\"id\" : 1,\"cmd\" : \"info\",\"topic\":\"info1\",\"msg\":{\"text\":\"newmessage\"} }");
testData.push_back(0xff);
testData.push_back(tst_msg.size()+3);
testData.push_back(0);
for (auto it :tst_msg) {
testData.push_back(it);
}
/*
dataeater eater;
eater.eat(testData);
eater.process();
std::string msg = eater.getLastCommand();
// std::string tst_msg ("{\"id\" : 1,\"cmd\" : \"info\",\"topic\":\"info1\",\"msg\":{\"text\":\"newmessage\"} }");
netParser parser(*this);
parser.parseMsg(msg);
execNextOrder();
*/
}
std::shared_ptr<MainWindow> MainWindow::create_shared_ptr() {
// TODO
std::shared_ptr<MainWindow> ret(new MainWindow);// = std::make_shared<MainWindow>();
ret->m_cmd_exec = commandExecutor::construct(ret);
return ret;
}
MainWindow::MainWindow(MainWindow &&other) {
if (this == &other)
return;
m_cmd_exec = std::move(other.m_cmd_exec);
ui = other.ui;
other.ui = nullptr;
m_tunserver_process = other.m_tunserver_process;
other.m_tunserver_process = nullptr;
m_dlg = other.m_dlg;
other.m_dlg = nullptr;
m_peer_lst = std::move(other.m_peer_lst);
}
MainWindow::~MainWindow()
{
delete ui;
delete m_tunserver_process;
delete m_dlg;
}
peer_reference peer_reference::get_validated_ref(std::string ref) {
std::string r_ipv4_port;
std::string r_ipv4;
std::string r_port;
std::string r_ipv6;
size_t pos1;
if ((pos1 = ref.find('-')) != std::string::npos) {
r_ipv4_port = ref.substr(0,pos1);
r_ipv6 = ref.substr(pos1+1);
std::regex pattern("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}:\\d{1,5}"); // i.e. 127.0.0.1:4562
std::smatch result;
qDebug()<<r_ipv4_port.c_str();
if (std::regex_search(r_ipv4_port, result, pattern)) {
size_t pos2 = r_ipv4_port.find(':');
r_ipv4 = r_ipv4_port.substr(0, pos2);
r_port = r_ipv4_port.substr(pos2+1);
} else
throw std::invalid_argument("bad format of input remote address and port");
boost::system::error_code ec;
// validate ipv4
boost::asio::ip::address_v4::from_string(r_ipv4, ec);
if (ec)
throw std::invalid_argument("bad format of input remote IPv4 address");
// validate ipv6
boost::asio::ip::address_v6::from_string(r_ipv6, ec);
if(ec)
throw std::invalid_argument("bad format of input remote IPv6 address");
} else
throw std::invalid_argument("bad format of input ref - missing '-'");
return {r_ipv4 ,stoi(r_port), r_ipv6};
}
void MainWindow::add_address(QString address)
{
qDebug()<< "add address [" << address << ']';
try {
peer_reference peer_ref = peer_reference::get_validated_ref(address.toStdString());
m_peer_lst.push_back(peer_ref);
ui->peerListWidget->addItem(address);
} catch (std::exception &er) {
qDebug()<< er.what();
} catch (...) {
qDebug()<< "fail to parse address - bad format";
}
}
void MainWindow::showDebugPage(QByteArray &pageCode) {
}
void MainWindow::start_tunserver(std::vector<peer_reference> &peer_list, const QString &tunserver_path) {
QStringList params_list;
for (const auto &peer : peer_list) {
std::string peer_string = " --peer ";
peer_string += peer.m_ipv4 + ":" + std::to_string(peer.m_port);
peer_string += "-" + peer.m_ipv6;
params_list.push_back(QString::fromStdString(peer_string));
}
qDebug()<<params_list;
QProcess *m_tunserver_process = new QProcess(this);
m_tunserver_process->start(tunserver_path , params_list);
if(m_tunserver_process->state() == QProcess::NotRunning)
throw std::runtime_error("Fail to run tunserver process");
}
void MainWindow::onProcessInfo() {
qDebug()<<m_tunserver_process->readAll();
}
void MainWindow::onProcessError() {
qDebug()<<m_tunserver_process->readAll();
}
void MainWindow::on_plusButton_clicked() {
m_dlg = new addressDialog(this);
connect (m_dlg,SIGNAL(add_address(QString)),this,SLOT(add_address(QString)));
m_dlg->show();
}
void MainWindow::on_minusButton_clicked() {
const auto &delete_list= ui->peerListWidget->selectedItems();
try{
const auto &it = std::find_if_not(m_peer_lst.begin(), m_peer_lst.end()
, [&delete_list](const auto &a){return delete_list[0]->text().toStdString().find(a.m_ipv6) == std::string::npos;});
if(it != m_peer_lst.end())
m_peer_lst.erase(it);
ui->peerListWidget->removeItemWidget(delete_list.at(0));
delete_list.at(0)->setText("");
ui->peerListWidget->sortItems(Qt::DescendingOrder);
} catch(...){
qDebug()<<"co mam niby usunac?!";
}
}
void MainWindow::on_run_tunserver_clicked() {
QStringList l_peer_list;
for (auto it :m_peer_lst) {
QString label = QString::fromStdString(it.m_ipv4+":"+std::to_string(it.m_port)+"-"+it.m_ipv6);
l_peer_list.push_back(label);
}
QString tunserver_location = QFileDialog::getOpenFileName(this,
tr("Open tunserver binary file (tunserver.elf)"));
try {
start_tunserver(m_peer_lst, tunserver_location);
} catch (const std::exception &err) {
qDebug() << err.what();
}
}
void MainWindow::add_host_info(QString host, uint16_t port) {
qDebug() << "Host: " << host << "Port:" << port << '\n';
if (host.isEmpty()) {
host = "127.0.0.1";
qDebug() << "use default 'localhost (127.0.0.1)' host";
}
if (port == 0) {
port = 42000;
qDebug() << "use default '42000' port";
}
m_cmd_exec->startConnect(QHostAddress(host), port);
}
void MainWindow::on_connectButton_clicked() {
hostDialog host_dialog;
connect (&host_dialog, SIGNAL(host_info(QString, uint16_t)),
this, SLOT(add_host_info(QString, uint16_t)));
if (host_dialog.exec() == QDialog::Accepted){
qDebug() << "host inforation accepted";
}
}
void MainWindow::on_ping_clicked() {
order ord(order::e_type::PING);
m_cmd_exec->sendNetRequest(ord);
}
void MainWindow::SavePeers(QString file_name)
{
ParamsContainer container;
container.setPeerList(m_peer_lst);
container.writeParams(file_name);
}
void MainWindow::add_to_debug_window(const std::string &message) {
ui->debugWidget->addItem(message.c_str());
ui->debugWidget->scrollToBottom();
}
void MainWindow::show_peers(const std::vector<std::string> &peers) {
ui->peerListWidget->clear();
for (const auto &element : peers) {
ui->peerListWidget->addItem(QString(element.c_str()));
}
}
/*void MainWindow::show_msg(const json &msg)
{
qDebug()<<"show new message \n";
//std::cout<<msg["topic"];
std::string tmp = msg["msg"];
//std::string text = tmp["text"];
ui->debugWidget->addItem(tmp.c_str());
// ui->debugWidget->addItem(text);
qDebug()<<tmp.c_str();
}
void MainWindow::show_peers(const nlohmann::json &msg) {
qDebug() << "show message as array\n";
std::vector<std::string> messages = msg["msg"];
ui->peerListWidget->clear();
for (const auto &element : messages) {
ui->debugWidget->addItem(element.c_str());
qDebug() << element.c_str();
ui->peerListWidget->addItem(QString(element.c_str()));
}
}*/
void MainWindow::on_actionDebug_triggered() {
qDebug()<< "show dialog";
DebugDialog dialog;
dialog.exec();
dialog.show();
}