Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Messenger/Messenger.pro.user
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 4.14.2, 2021-05-31T04:56:11. -->
<!-- Written by QtCreator 4.14.2, 2021-05-31T23:27:45. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>
Expand Down Expand Up @@ -286,8 +286,9 @@
<valuelist type="QVariantList" key="CustomOutputParsers"/>
<value type="int" key="PE.EnvironmentAspect.Base">2</value>
<valuelist type="QVariantList" key="PE.EnvironmentAspect.Changes"/>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.Qt4RunConfiguration:C:/Users/vladi/OneDrive/Documents/Messenger/Messenger.pro</value>
<value type="QString" key="ProjectExplorer.RunConfiguration.BuildKey">C:/Users/vladi/OneDrive/Documents/Messenger/Messenger.pro</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Messenger2</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.Qt4RunConfiguration:D:/repositories/Messenger/Messenger/Messenger.pro</value>
<value type="QString" key="ProjectExplorer.RunConfiguration.BuildKey">D:/repositories/Messenger/Messenger/Messenger.pro</value>
<value type="bool" key="RunConfiguration.UseCppDebugger">false</value>
<value type="bool" key="RunConfiguration.UseCppDebuggerAuto">true</value>
<value type="bool" key="RunConfiguration.UseLibrarySearchPath">true</value>
Expand Down
12 changes: 6 additions & 6 deletions Messenger/authandregistration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,8 @@ void authAndRegistration::receivedSomething(QString msg)
void authAndRegistration::on_loginBtn_2_clicked()
{
QMessageBox msgBox;
if(GetCode() == 200)
{
//if(GetCode() == 200)
//{
QString username = ui->lineEdit_4->text();
QString password = ui->lineEdit_5->text();
client->connect2host();
Expand All @@ -267,10 +267,10 @@ void authAndRegistration::on_loginBtn_2_clicked()

msgBox.setText("User created!");
msgBox.exec();
}
/*}
else if(GetCode() == 404)
{
msgBox.setText("Incorrect parameters!");
{*/
/*msgBox.setText("Incorrect parameters!");
msgBox.exec();
}
else if(GetCode() == 403)
Expand All @@ -282,7 +282,7 @@ void authAndRegistration::on_loginBtn_2_clicked()
{
msgBox.setText("User not created!");
msgBox.exec();
}
}*/
/*QMessageBox msg;
if(ui->lineEdit_3->text() == "" || ui->lineEdit_4->text() == "" || ui->lineEdit_5->text() == "" || ui->label_6->text() == "")
{
Expand Down
29 changes: 21 additions & 8 deletions Messenger/businesslogic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,23 @@ BusinessLogic::BusinessLogic(ClientStuff *client, QObject *parent) : QObject(par

void BusinessLogic::CreateNewUser(const QString &username, const QString &password)
{
QDir().mkdir("Users");
/*QDir().mkdir("Users");

QFile saveFile("Users/" + username + ".json");
if (!saveFile.open(QIODevice::WriteOnly))
{
qWarning("Couldn't open save file.");
}
}*/

QJsonObject json;

json["login"] = username;
json["password"] = password;
json["cmd"] = 0;

QJsonDocument saveDoc(json);
saveFile.write(saveDoc.toJson());
saveFile.close();

client->Send(username, "new_user");
client->Send(saveDoc.toJson());
//1. Create parameters map json["mmmm"] for command new_user
//2. Serialize to JSON into dto
//QString dto; //...
Expand All @@ -47,9 +46,23 @@ void BusinessLogic::SendMessage(const QString &to, const QString &body)
//client->Send(dto);
}

void BusinessLogic::IncomingPacket(QString msg)
void BusinessLogic::IncomingPacket(QByteArray msg)
{
authAndRegistration auth;
QByteArray arrayPackage = msg;//decrypt arrayBlock to QByteArray packageArray

QJsonDocument doc = QJsonDocument::fromJson(arrayPackage);
QJsonObject package = doc.object();
int code = package["code"].toInt();
switch(code)
{
case 200:
{
qDebug() << "Code: " << code;
}
break;
}

/*authAndRegistration auth;

if(msg == "200")
{
Expand All @@ -66,6 +79,6 @@ void BusinessLogic::IncomingPacket(QString msg)
if(msg == "500")
{
auth.SetCode(500);
}
}*/
}

2 changes: 1 addition & 1 deletion Messenger/businesslogic.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class BusinessLogic : public QObject
ClientStuff *client;

public slots:
void IncomingPacket(QString msg);
void IncomingPacket(QByteArray msg);
};

#endif // BUSINESSLOGIC_H
25 changes: 19 additions & 6 deletions Messenger/clientStuff.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,33 @@ void ClientStuff::connected()

bool ClientStuff::getStatus() {return status;}

void ClientStuff::Send(const QString &username, const QString &command)
void ClientStuff::Send(QByteArray package)
{
QByteArray arrBlock;
QDataStream out(&arrBlock, QIODevice::WriteOnly);
//out.setVersion(QDataStream::Qt_5_10);
out << quint16(0) << username << command;
//out << quint16(1) << command;

out << package.length() << package;

tcpSocket->write(arrBlock);
}

void ClientStuff::readyRead()
{
QDataStream in(tcpSocket);
QDataStream in(tcpSocket);
if(tcpSocket->bytesAvailable() < sizeof(int))
{
return;
}
int blockSize ;
in >> blockSize;
if(tcpSocket->bytesAvailable() < blockSize)
{
return;
}
QByteArray arrayBlock;
in >> arrayBlock;
emit hasReadSome(arrayBlock);
/*QDataStream in(tcpSocket);
for (;;)
{
if (!m_nNextBlockSize)
Expand All @@ -82,7 +95,7 @@ void ClientStuff::readyRead()

emit hasReadSome(str);
m_nNextBlockSize = 0;
}
}*/
}

/*void ClientStuff::gotDisconnection()
Expand Down
4 changes: 2 additions & 2 deletions Messenger/clientStuff.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ class ClientStuff : public QObject
QTcpSocket *tcpSocket;
bool getStatus();
//void SendCodeAndLoginToServer(const QString& login, const QString& command);
void Send(const QString& username, const QString& command);
void Send(QByteArray package);

public slots:
void closeConnection();
void connect2host();

signals:
void statusChanged(bool);
void hasReadSome(QString msg);
void hasReadSome(QByteArray msg);

private slots:
void readyRead();
Expand Down
8 changes: 4 additions & 4 deletions ServerForMessenger/ServerForMessenger.pro.user
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 4.14.2, 2021-05-31T04:56:24. -->
<!-- Written by QtCreator 4.14.2, 2021-05-31T23:27:44. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>
Expand Down Expand Up @@ -289,8 +289,9 @@
<valuelist type="QVariantList" key="CustomOutputParsers"/>
<value type="int" key="PE.EnvironmentAspect.Base">2</value>
<valuelist type="QVariantList" key="PE.EnvironmentAspect.Changes"/>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.Qt4RunConfiguration:C:/Users/vladi/OneDrive/Documents/ServerForMessenger/ServerForMessenger.pro</value>
<value type="QString" key="ProjectExplorer.RunConfiguration.BuildKey">C:/Users/vladi/OneDrive/Documents/ServerForMessenger/ServerForMessenger.pro</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">ServerForMessenger2</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.Qt4RunConfiguration:D:/repositories/Messenger/ServerForMessenger/ServerForMessenger.pro</value>
<value type="QString" key="ProjectExplorer.RunConfiguration.BuildKey">D:/repositories/Messenger/ServerForMessenger/ServerForMessenger.pro</value>
<value type="bool" key="RunConfiguration.UseCppDebugger">false</value>
<value type="bool" key="RunConfiguration.UseCppDebuggerAuto">true</value>
<value type="bool" key="RunConfiguration.UseLibrarySearchPath">true</value>
Expand Down Expand Up @@ -518,7 +519,6 @@
<value type="bool" key="RunConfiguration.UseLibrarySearchPath">true</value>
<value type="bool" key="RunConfiguration.UseQmlDebugger">false</value>
<value type="bool" key="RunConfiguration.UseQmlDebuggerAuto">true</value>
<value type="QString" key="RunConfiguration.WorkingDirectory.default">C:/Users/vladi/OneDrive/Documents/build-ServerForMessenger-Desktop_Qt_5_12_7_MinGW_32_bit-Debug</value>
</valuemap>
<value type="int" key="ProjectExplorer.Target.RunConfigurationCount">1</value>
</valuemap>
Expand Down
68 changes: 20 additions & 48 deletions ServerForMessenger/businesslogic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,73 +23,45 @@ void BusinessLogic::LoginUser(const QString &username, const QString &password)
// Check user in database
// Take him datas
// Send datas to client
//server->Send(socket, username, password);
//server->Send(socket, code);
}

void BusinessLogic::FindUser(const QString &username)
{
//server->Send(socket, username);
}

void BusinessLogic::gotMessageHandler(const QString& username, const int& code)
void BusinessLogic::gotMessageHandler(QTcpSocket* socket, QByteArray arrayBlock)
{
QFile file_obj("Users/" + username + ".json");
if(!file_obj.open(QIODevice::ReadOnly)){
qDebug()<<"Failed to open "<< username << ".json";
}

QTextStream file_text(&file_obj);
QString json_string;
json_string = file_text.readAll();
file_obj.close();
QByteArray json_bytes = json_string.toLocal8Bit();

auto json_doc = QJsonDocument::fromJson(json_bytes);

if(json_doc.isNull()){
qDebug()<<"Failed to create JSON doc.";
}
if(!json_doc.isObject()){
qDebug()<<"JSON is not an object.";
}

QJsonObject json_obj = json_doc.object();

if(json_obj.isEmpty()){
qDebug()<<"JSON object is empty.";
}

QVariantMap json_map = json_obj.toVariantMap();
qDebug() << arrayBlock;

qDebug() << json_map["login"].toString();
qDebug() << json_map["password"].toString();
QByteArray arrayPackage = arrayBlock;//decrypt arrayBlock to QByteArray packageArray

QString login = json_map["login"].toString();
QString password = json_map["password"].toString();
QJsonDocument doc = QJsonDocument::fromJson(arrayPackage);
QJsonObject package = doc.object();
int code = package["cmd"].toInt();
switch(code)
{
case 0:
{
QMessageBox msg;
QString login = package["login"].toString();
QString password = package["password"].toString();
msg.setText("login: " + login + "\n password: " + password);
msg.exec();

emit CreateUser(username, password);
}
break;
case 1:
{
emit LoginUser(username, password);
}
break;
case 2:
{
emit FindUser(username);
CreateUser(login, password);
QJsonObject json;

json["code"] = 200;

QJsonDocument saveDoc(json);
QByteArray rawPacket = saveDoc.toJson();

QByteArray chiperPackage = rawPacket;//crypt rawPacket to QByteArray
server->Send(socket, chiperPackage);
}
break;
default:
/* Failure code! */
break;
}

//1. Deserialize from JSON
Expand All @@ -114,5 +86,5 @@ void BusinessLogic::gotMessageHandler(const QString& username, const int& code)
//Serialize into JSON
//and send to client:
//QString dto;
//server->Send(dto);
//server->Send(dto);
}
4 changes: 1 addition & 3 deletions ServerForMessenger/businesslogic.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ class BusinessLogic : public QObject
Q_OBJECT
public:
explicit BusinessLogic(ServerStuff *server, QObject *parent = nullptr);

public slots:
void CreateUser(const QString& username, const QString& password);
void NewMsg(QString msg);
void LoginUser(const QString& username, const QString& password);
Expand All @@ -29,7 +27,7 @@ public slots:
ServerStuff *server;

private slots:
void gotMessageHandler(const QString& msg, const int &code);
void gotMessageHandler(QTcpSocket* socket, QByteArray);

};

Expand Down
5 changes: 3 additions & 2 deletions ServerForMessenger/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ MainWindow::MainWindow(QWidget *parent)
ui->checkConnection->setEnabled(false);

server = new ServerStuff(this);
connect(server, &ServerStuff::gotNewMessage,
this, &MainWindow::gotNewMessage);
bl = new BusinessLogic(server);
//connect(server, &ServerStuff::gotNewMessage,
// this, &MainWindow::gotNewMessage);
connect(server->tcpServer, &QTcpServer::newConnection,
this, &MainWindow::smbConnectedToServer);
connect(server, &ServerStuff::smbDisconnected,
Expand Down
6 changes: 4 additions & 2 deletions ServerForMessenger/mainwindow.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H


#include "businesslogic.h"

#include <QMainWindow>
#include "serverStuff.h"
Expand Down Expand Up @@ -31,8 +31,10 @@ private slots:

private:
Ui::MainWindow *ui;
ServerStuff *server = nullptr;
//ServerStuff *server = nullptr;
void SendBroadcastShutdownToClients();
BusinessLogic *bl;
ServerStuff* server;

};
#endif // MAINWINDOW_H
Loading