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 \ main.cpp \
mainwindow.cpp \ mainwindow.cpp \
kernel/network.cpp \ kernel/network.cpp \
main.cpp \ kernel/handler.cpp
mainwindow.cpp
HEADERS += \ HEADERS += \
mainwindow.h \
kernel/network.hpp \ kernel/network.hpp \
lib/json/detail/conversions/from_json.hpp \ mainwindow.h \
lib/json/detail/conversions/to_chars.hpp \ kernel/handler.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
FORMS += \ FORMS += \
mainwindow.ui 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() Network::Network()
{ {
udpSocket = new QUdpSocket(this); udpSocket = new QUdpSocket(this);
udpSocket->bind(QHostAddress::AnyIPv6, 6552); udpSocket->bind(QHostAddress::AnyIPv6, 6552);
connect(udpSocket, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams())); connect(udpSocket, SIGNAL(readyRead()), this, SLOT(processTheDatagram()));
} }
void Network::readPendingDatagrams()
{ void Network::sendDatagram(QJsonObject j, QString s)
while (udpSocket->hasPendingDatagrams()) {
QNetworkDatagram datagram = udpSocket->receiveDatagram();
processTheDatagram(datagram);
}
}
void Network::sendDatagram(json *j, QHostAddress ip)
{ {
QHostAddress ip = QHostAddress(s);
QByteArray baDatagram; QByteArray baDatagram;
QDataStream out(&baDatagram, QIODevice::WriteOnly); //QDataStream out(&baDatagram, QIODevice::WriteOnly);
QString s = j.dump(); QJsonDocument jbuff = QJsonDocument(j);
out << s; quint16 p = 6552;
udpSocket->writeDatagram(baDatagram, ip, 6552); baDatagram = jbuff.toJson(QJsonDocument::Compact);
udpSocket->writeDatagram(baDatagram, ip, p);
} }
json Network::processTheDatagram()
void Network::processTheDatagram()
{ {
QByteArray baDatagram; QByteArray baDatagram;
do { do {
@ -27,10 +25,9 @@ json Network::processTheDatagram()
udpSocket->readDatagram (baDatagram.data(), baDatagram.size()) ; udpSocket->readDatagram (baDatagram.data(), baDatagram.size()) ;
} while (udpSocket->hasPendingDatagrams()) ; } while (udpSocket->hasPendingDatagrams()) ;
QString buff; QJsonDocument jbuff = QJsonDocument::fromJson(baDatagram);
QDataStream in(&baDatagram, QIODevice::Readonly); QJsonObject j = QJsonObject(jbuff.object());
in >> buff; //QDataStream in(&baDatagram, QIODevice::ReadOnly);
json j = new json; //in >> jbuff.fromBinaryData(baDatagram);
j = json::parse(buff); emit json_received(j);
return json;
} }

View File

@ -1,16 +1,21 @@
#include "../lib/json/json.hpp" #pragma once
#include <QtNetwork>
class Network #include <QtNetwork>
#include <QJsonObject>
#include <QJsonDocument>
class Network : public QObject
{ {
using json = nlohmann::json; Q_OBJECT
private: private:
QUdpSocket* udpSocket; QUdpSocket* udpSocket;
public: public:
Network(); Network();
public slots: public slots:
void sendDatagram(); void sendDatagram(QJsonObject j, QString s);
signals:
void json_received(QJsonObject &jsonReceived);
private slots: 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(new Ui::MainWindow)
{ {
ui->setupUi(this); ui->setupUi(this);
network = new Network();
handler = new Handler();
} }
MainWindow::~MainWindow() MainWindow::~MainWindow()
{ {
delete ui; 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,22 +1,35 @@
#ifndef MAINWINDOW_H #pragma once
#define MAINWINDOW_H
#include <QMainWindow> #include <QMainWindow>
#include "kernel/network.hpp"
#include "kernel/handler.hpp"
#include <QMessageBox>
#include <QJsonObject>
#include <QJsonDocument>
namespace Ui { namespace Ui {
class MainWindow; class MainWindow;
} }
class MainWindow : public QMainWindow class MainWindow : public QMainWindow
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit MainWindow(QWidget *parent = 0); explicit MainWindow(QWidget *parent = 0);
~MainWindow(); ~MainWindow();
public slots:
void peerReceiverConnected();
private slots:
void on_connectToPeer_clicked();
void slotTimerAlarm();
private: private:
Ui::MainWindow *ui; Ui::MainWindow *ui;
}; QTimer *timer;
Network *network;
Handler *handler;
#endif // MAINWINDOW_H bool receive;
};

View File

@ -1,24 +1,53 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0"> <ui version="4.0">
<class>MainWindow</class> <class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow" > <widget class="QMainWindow" name="MainWindow">
<property name="geometry" > <property name="geometry">
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>400</width> <width>331</width>
<height>300</height> <height>132</height>
</rect> </rect>
</property> </property>
<property name="windowTitle" > <property name="minimumSize">
<string>MainWindow</string> <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> </property>
<widget class="QMenuBar" name="menuBar" />
<widget class="QToolBar" name="mainToolBar" />
<widget class="QWidget" name="centralWidget" />
<widget class="QStatusBar" name="statusBar" />
</widget> </widget>
<layoutDefault spacing="6" margin="11" /> </item>
<pixmapfunction></pixmapfunction> <item>
<widget class="QPushButton" name="connectToPeer">
<property name="text">
<string>Подключиться</string>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/> <resources/>
<connections/> <connections/>
</ui> </ui>