forked from hbdgr/simple_gui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
329 lines (266 loc) · 8.29 KB
/
mainwindow.cpp
File metadata and controls
329 lines (266 loc) · 8.29 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
#include <QDebug>
#include <QProcess>
#include <QTcpSocket>
#include <regex>
#include "mainwindow.hpp"
#include "ui_mainwindow.h"
#include "addressdialog.hpp"
#include "paramscontainer.hpp"
#include "dataeater.hpp"
#include "debugdialog.hpp"
#include "trivialserialize.hpp"
#include <boost/asio.hpp>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
m_parser(*this),
ui(new Ui::MainWindow),
m_tunserver_process(nullptr),
m_dlg(nullptr),
m_socket(nullptr),
th_peerlist(nullptr)
{
ui->setupUi(this);
/*
m_msg_server = new QTcpServer(this);
m_msg_server->setMaxPendingConnections(1);
m_msg_server->listen(QHostAddress::LocalHost,9933);
*/
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();
*/
}
MainWindow::~MainWindow()
{
delete ui;
delete m_tunserver_process;
delete m_dlg;
delete m_socket;
}
void MainWindow::on_connectButton_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);
}
/*
for (int i= 0 ;i<ui->peerListWidget->count();i++) {
l_peer_list.push_back(ui->peerListWidget->item(i)->text());
//QString peer_string =" --peer "+my_ip +":9042-"+it;
}
*/
m_pr_call = false;
if (th_peerlist != nullptr && th_peerlist->joinable()) {
th_peerlist->join();
th_peerlist.reset(nullptr);
}
if (check_connection()) {
m_socket->disconnectFromHost();
}
startProgram(l_peer_list);
startConnection();
}
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::addAddress(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::startProgram(QStringList & l_peer_list)
{
QString command = "./../../../galaxy42/tunserver.elf";
QStringList params_list;
foreach (auto it, l_peer_list) {
QString peer_string =" --peer "+it;
params_list.push_back(peer_string);
}
qDebug()<<params_list;
QProcess *m_tunserver_process = new QProcess(this);
//connect (m_tunserver_process,SIGNAL(readyReadStandardOutput()),this,SLOT(onProcessInfo()));
//connect (m_tunserver_process,SIGNAL(readyReadStandardError()),this,SLOT(onProcessError()));
m_tunserver_process->start(command , params_list);
}
void MainWindow::onReciveTcp()
{
QByteArray data_array = m_socket->readAll();
std::string arr(data_array.data(), static_cast<size_t>(data_array.size()));
m_data_eater.eat(arr);
m_data_eater.process();
std::string last_cmd = m_data_eater.getLastCommand();
std::cout << "Last cmd: " << last_cmd << '\n';
m_parser.parseMsg(last_cmd);
//m_packet_eater.eat_packet(arr);
//std::string msg = m_packet_eater.pop_last_message();
//qDebug() << "Arr: " << arr.c_str() << " msg: " << msg.c_str();
//m_parser.parseMsg(msg);
execNextOrder();
}
void MainWindow::peerlist_request_slot() {
if (check_connection())
send_request( {
{"cmd","peer_list"}
} );
}
bool MainWindow::check_connection() {
std::lock_guard<std::mutex> socekt_lock(m_mutex);
if(m_socket == nullptr) {
qDebug()<<"Socket is not defined (nullptr)";
return false;
} else if (m_socket->state() != QAbstractSocket::ConnectedState) {
qDebug()<<"Socket is not connected";
return false;
} else {
return true;
}
}
void MainWindow::sendReciveTcp(QString &msg) {
}
void MainWindow::showDebugPage(QByteArray &pageCode) {
}
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(addAddress(QString)),this,SLOT(addAddress(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::SavePeers(QString file_name)
{
ParamsContainer container;
container.setPeerList(m_peer_lst);
container.writeParams(file_name);
}
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::call_peerlist_requests(const std::chrono::seconds &time_interval) {
while(m_pr_call == true) {
std::this_thread::yield();
std::this_thread::sleep_for(time_interval);
qDebug() << "pr_call:" << m_pr_call << '\n';
emit ask_for_peerlist();
}
qDebug() << "pr_call:" << m_pr_call << '\n';
}
void MainWindow::startConnection()
{
m_socket = new QTcpSocket(this);
m_socket->connectToHost("localhost", 42000);
connect(m_socket, SIGNAL(readyRead()),this, SLOT(onReciveTcp()));
m_pr_call = true;
connect(this, SIGNAL(ask_for_peerlist()),this, SLOT(peerlist_request_slot()));
// we need to wait...
if(!m_socket->waitForConnected(5000))
{
qDebug() << "Error: " << m_socket->errorString();
}
th_peerlist = std::make_unique<std::thread>([this]() { call_peerlist_requests(); });
}
void MainWindow::on_actionDebug_triggered()
{
qDebug()<< "show dlg";
DebugDialog dialog;
dialog.exec();
dialog.show();
}
void MainWindow::send_request(const json &request) {
std::string msg = request.dump();
qDebug() << "json msg to send: [" << msg.c_str() << ']';
std::vector<uint8_t> packet = simple_packet_eater::serialize_msg(msg);
size_t written = m_socket->write(QByteArray(reinterpret_cast<const char*>(packet.data()), packet.size()));
if(written != packet.size())
throw std::runtime_error("Some errors occurred while writing data to m_socket");
}
void MainWindow::on_ping_clicked() {
if (check_connection())
send_request( {
{"cmd","ping"},
{"msg","ping"}
} );
}