The basic part of the application is written

The library nlohmann::json was removed (now QJson*), working class Network and idle handler.
This commit is contained in:
Denis Davydov 2018-06-21 15:47:21 +03:00
parent 89d1f77974
commit 7fa9deb1d4
9 changed files with 172 additions and 17379 deletions

View File

@ -27,38 +27,12 @@ SOURCES += \
main.cpp \
mainwindow.cpp \
kernel/network.cpp \
main.cpp \
mainwindow.cpp
kernel/handler.cpp
HEADERS += \
mainwindow.h \
kernel/network.hpp \
lib/json/detail/conversions/from_json.hpp \
lib/json/detail/conversions/to_chars.hpp \
lib/json/detail/conversions/to_json.hpp \
lib/json/detail/input/binary_reader.hpp \
lib/json/detail/input/input_adapters.hpp \
lib/json/detail/input/lexer.hpp \
lib/json/detail/input/parser.hpp \
lib/json/detail/iterators/internal_iterator.hpp \
lib/json/detail/iterators/iter_impl.hpp \
lib/json/detail/iterators/iteration_proxy.hpp \
lib/json/detail/iterators/json_reverse_iterator.hpp \
lib/json/detail/iterators/primitive_iterator.hpp \
lib/json/detail/output/binary_writer.hpp \
lib/json/detail/output/output_adapters.hpp \
lib/json/detail/output/serializer.hpp \
lib/json/detail/exceptions.hpp \
lib/json/detail/json_pointer.hpp \
lib/json/detail/json_ref.hpp \
lib/json/detail/macro_scope.hpp \
lib/json/detail/macro_unscope.hpp \
lib/json/detail/meta.hpp \
lib/json/detail/value_t.hpp \
lib/json/adl_serializer.hpp \
lib/json/json.hpp \
lib/json/json_fwd.hpp \
mainwindow.h
mainwindow.h \
kernel/handler.hpp
FORMS += \
mainwindow.ui

25
src/kernel/handler.cpp Normal file
View File

@ -0,0 +1,25 @@
#include "handler.hpp"
Handler::Handler()
{
network = new Network();
connect(network, &Network::json_received, this, &Handler::handle);
}
void Handler::handle(QJsonObject jsonReceived)
{
QJsonObject jsonSend;
if(jsonReceived["action"] == "createSession" && !jsonReceived.contains("status"))
{
jsonSend["peerID"] = QHostAddress("::1").toString();
jsonSend["action"] = "createSession";
jsonSend["status"] = true;
QString peerReceiver = jsonReceived["peerID"].toString();
network->sendDatagram(jsonSend, peerReceiver);
}
else
{
emit createSessionSuccess();
}
}

18
src/kernel/handler.hpp Normal file
View File

@ -0,0 +1,18 @@
#pragma once
#include "network.hpp"
#include <QJsonObject>
class Handler : public QObject
{
Q_OBJECT
public:
Handler();
signals:
createSessionSuccess();
private:
Network *network;
private slots:
void handle(QJsonObject jsonReceived);
};

View File

@ -1,25 +1,23 @@
#include "network.hpp"
Network::Network()
{
udpSocket = new QUdpSocket(this);
udpSocket->bind(QHostAddress::AnyIPv6, 6552);
connect(udpSocket, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams()));
connect(udpSocket, SIGNAL(readyRead()), this, SLOT(processTheDatagram()));
}
void Network::readPendingDatagrams()
{
while (udpSocket->hasPendingDatagrams()) {
QNetworkDatagram datagram = udpSocket->receiveDatagram();
processTheDatagram(datagram);
}
}
void Network::sendDatagram(json *j, QHostAddress ip)
void Network::sendDatagram(QJsonObject j, QString s)
{
QHostAddress ip = QHostAddress(s);
QByteArray baDatagram;
QDataStream out(&baDatagram, QIODevice::WriteOnly);
QString s = j.dump();
out << s;
udpSocket->writeDatagram(baDatagram, ip, 6552);
//QDataStream out(&baDatagram, QIODevice::WriteOnly);
QJsonDocument jbuff = QJsonDocument(j);
quint16 p = 6552;
baDatagram = jbuff.toJson(QJsonDocument::Compact);
udpSocket->writeDatagram(baDatagram, ip, p);
}
json Network::processTheDatagram()
void Network::processTheDatagram()
{
QByteArray baDatagram;
do {
@ -27,10 +25,9 @@ json Network::processTheDatagram()
udpSocket->readDatagram (baDatagram.data(), baDatagram.size()) ;
} while (udpSocket->hasPendingDatagrams()) ;
QString buff;
QDataStream in(&baDatagram, QIODevice::Readonly);
in >> buff;
json j = new json;
j = json::parse(buff);
return json;
QJsonDocument jbuff = QJsonDocument::fromJson(baDatagram);
QJsonObject j = QJsonObject(jbuff.object());
//QDataStream in(&baDatagram, QIODevice::ReadOnly);
//in >> jbuff.fromBinaryData(baDatagram);
emit json_received(j);
}

View File

@ -1,16 +1,21 @@
#include "../lib/json/json.hpp"
#include <QtNetwork>
#pragma once
class Network
#include <QtNetwork>
#include <QJsonObject>
#include <QJsonDocument>
class Network : public QObject
{
using json = nlohmann::json;
Q_OBJECT
private:
QUdpSocket* udpSocket;
public:
Network();
public slots:
void sendDatagram();
void sendDatagram(QJsonObject j, QString s);
signals:
void json_received(QJsonObject &jsonReceived);
private slots:
void readPendingDatagrams();
void processTheDatagram();
};

File diff suppressed because it is too large Load Diff

View File

@ -6,9 +6,41 @@ MainWindow::MainWindow(QWidget *parent) :
ui(new Ui::MainWindow)
{
ui->setupUi(this);
network = new Network();
handler = new Handler();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_connectToPeer_clicked()
{
QJsonObject j;
j["peerID"] = QHostAddress("::1").toString();
j["action"] = "createSession";
QString s = ui->peerID->text();
network->sendDatagram(j, s);
timer = new QTimer();
connect(handler, &Handler::createSessionSuccess, this, &MainWindow::peerReceiverConnected);
connect(timer, SIGNAL(timeout()), this, SLOT(slotTimerAlarm()));
timer->start(10000);
}
void MainWindow::slotTimerAlarm()
{
if(!receive)
{
int ret = QMessageBox::critical(this,QObject::tr("Error"),tr("Timeout Error"));
timer->stop();
delete timer;
}
}
void MainWindow::peerReceiverConnected()
{
receive = true;
int ret = QMessageBox::information(this,QObject::tr("Info"),tr("Peer available!"));
}

View File

@ -1,7 +1,12 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#pragma once
#include <QMainWindow>
#include "kernel/network.hpp"
#include "kernel/handler.hpp"
#include <QMessageBox>
#include <QJsonObject>
#include <QJsonDocument>
namespace Ui {
class MainWindow;
@ -14,9 +19,17 @@ class MainWindow : public QMainWindow
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
public slots:
void peerReceiverConnected();
private slots:
void on_connectToPeer_clicked();
void slotTimerAlarm();
private:
Ui::MainWindow *ui;
};
QTimer *timer;
Network *network;
Handler *handler;
#endif // MAINWINDOW_H
bool receive;
};

View File

@ -1,3 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
@ -5,20 +6,48 @@
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
<width>331</width>
<height>132</height>
</rect>
</property>
<property name="windowTitle" >
<string>MainWindow</string>
<property name="minimumSize">
<size>
<width>331</width>
<height>132</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>331</width>
<height>132</height>
</size>
</property>
<property name="cursor">
<cursorShape>ArrowCursor</cursorShape>
</property>
<property name="windowTitle">
<string>Influence</string>
</property>
<widget class="QWidget" name="centralWidget">
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QLineEdit" name="peerID">
<property name="text">
<string/>
</property>
<widget class="QMenuBar" name="menuBar" />
<widget class="QToolBar" name="mainToolBar" />
<widget class="QWidget" name="centralWidget" />
<widget class="QStatusBar" name="statusBar" />
</widget>
<layoutDefault spacing="6" margin="11" />
<pixmapfunction></pixmapfunction>
</item>
<item>
<widget class="QPushButton" name="connectToPeer">
<property name="text">
<string>Подключиться</string>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>