forked from CPP-Final-Project/Chat_Server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.cpp
More file actions
124 lines (106 loc) · 3.62 KB
/
server.cpp
File metadata and controls
124 lines (106 loc) · 3.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
#include "server.h"
Server::Server(QObject* parent_) : QTcpServer(parent_)
{
connect(this, &Server::incomingConnection, UserController::instance().get(), &UserController::addConnection);
}
Server::~Server(){}
void Server::startServer()
{
loadConfig(CONFIG_FILE_PATH);
openConnection();
DBService::DBConnection_Service::init().then(RoomStorage_Service::init).waitForFinished();
PLOGI << "Server initialized";
}
void Server::stopServer()
{
if (isListening()) {
close();
PLOGI << "Server stop - OK";
emit serverStopped();
}
else
{
PLOGD << "Server already stopped";
}
}
void Server::openConnection()
{
if (!this->isListening()) {
if (this->listen(QHostAddress::Any, server_port)) //QHostAddress::Any, 5555
{
PLOGD << "Server start - OK";
emit serverStarted();
}
else
{
PLOGE << "Sever start - Error";
}
}
else
{
PLOGD << "Server is already listening";
}
}
void Server::loadConfig(const QString& path_)
{
QFile config_file;
QJsonParseError json_error;
config_file.setFileName(path_);
if (config_file.open(QIODevice::ReadOnly | QFile::Text))
{
const QJsonDocument config_file_doc = QJsonDocument::fromJson(QByteArray(config_file.readAll()), &json_error);
config_file.close();
if (json_error.error == QJsonParseError::NoError)
{
QJsonObject config_json = config_file_doc.object();
if (const QJsonValue v = config_json["ServerAddress"]; v.isString())
server_address = v.toString();
else
qWarning() << "Error ServerAddress reading";
if (const QJsonValue v = config_json["ServerPort"]; v.isDouble())
server_port = v.toInt();
else
qWarning() << "Error ServerPort reading";
if (const QJsonValue v = config_json["FloodLimit"]; v.isDouble())
flood_limit = v.toInt();
else
qWarning() << "Error FloodLimit reading";
if (const QJsonValue v = config_json["BlackListPath"]; v.isString())
black_list_path = v.toString();
else
qWarning() << "Error BlackListPath reading";
//if (const QJsonValue v = config_file_doc["MessagesHistorySettings"]["Path"]; v.isString())
// msg_history_path = v.toString();
//else
// qWarning() << "Error BlackListPath reading";
}
else
{
qWarning() << "Error config file read: " << json_error.error;
}
}
else
{
qWarning("Couldn't open config file.");
}
}
//void Server::loadRooms()
//{
// //Load rooms from repository to vector<Room> ------------------!!!!!-----------------------
// //run loop to init all rooms
//
// //QVector<DBEntity::DBRoom> dbRooms =
// //{
// // DBEntity::DBRoom{1, "room1", "a_description", 11, false, "a_password", false },
// // DBEntity::DBRoom{2, "room2", "b_description", 12, true, "b_password", false },
// // DBEntity::DBRoom{3, "room3", "c_description", 13, false, "c_password", true }
// //};
//
// //for(const DBEntity::DBRoom& db_room: dbRooms)
// //{
// // auto* room = new SrvRoom(db_room.getId(), db_room.getName(), db_room.getDescription(), db_room.getTopicId(), db_room.isPrivate(), db_room.getPassword(), db_room.isDeleted(), this);
// // //connect();
// // rooms.append(room);
// // PLOGI << "New room created! Now rooms: " + QString::number(rooms.size());
// //}
//}