Repository navigation
Expand file tree
/
Copy pathServer.cpp
More file actions
106 lines (93 loc) · 3.08 KB
/
Copy pathServer.cpp
File metadata and controls
106 lines (93 loc) · 3.08 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
#include "Server.h"
Server::Server(QObject *parent) : QTcpServer(parent) {
qDebug() << this << "created on" << QThread::currentThread();
if (!listen(QHostAddress::Any, 5000)) {
qDebug() << "could not start server";
}
int ret;
if ((ret = rsa.initialize()) != 0) {
qDebug() << "initialization failed: " << ret;
return;
}
if ((ret = rsa.setMyKeyFromFile(serverKeysPath)) != 0) {
qDebug() << "could not set key from file: " << ret;
}
}
QList<QString> Server::getRegisteredClients() {
QList<QString> clients;
for (auto con : activeConnections) {
if (con->isReady())
clients.push_back(con->getName());
}
return clients;
}
QJsonObject Server::getRegisteredClientsInJson() {
QJsonObject data;
QJsonArray names;
for (auto &it : getRegisteredClients()) {
qDebug() << it;
names.append(it);
}
data["names"] = names;
return data;
}
void Server::sendChannelRequest(QString destName, QJsonObject json) {
qDebug() << "sending connection request to: " << destName;
auto con = findConnection(destName);
if (con == nullptr) {
qDebug() << "requested user does not exist";
return;
}
con->sendChannelRequest(json);
}
void Server::sendChannelReply(QString destName, QJsonObject json) {
qDebug() << "sending channel reply to client: " << destName;
auto con = findConnection(destName);
if (con == nullptr) {
qDebug() << "requested user does not exist";
return;
}
con->sendChannelReply(json);
}
bool Server::clientExists(QString clientName) {
auto cl = getRegisteredClients();
return cl.contains(clientName);
}
void Server::removeClient(Connection *connection) {
qDebug() << "remove client";
activeConnections.removeAll(connection);
}
void Server::incomingConnection(qintptr socketDescriptor) {
qDebug() << "new Incoming connection" << socketDescriptor;
Connection *connection = new Connection(socketDescriptor, rsa, this);
connect(connection, SIGNAL(onRegistrationRequest(ClientInfo)), this,
SLOT(processRegistrationRequest(ClientInfo)));
connect(connection, SIGNAL(onCreateChannelRequest(QString, QJsonObject)),
this, SLOT(sendChannelRequest(QString, QJsonObject)));
connect(connection, SIGNAL(onCreateChannelReply(QString, QJsonObject)), this,
SLOT(sendChannelReply(QString, QJsonObject)));
connect(connection, SIGNAL(onQuit(Connection *)), this,
SLOT(removeClient(Connection *)));
activeConnections.push_back(connection);
}
Connection *Server::findConnection(QString name) {
for (auto it : activeConnections) {
if (it->isRegistered()) {
if (it->getName() == name)
return &(*it);
}
}
return nullptr;
}
void Server::processRegistrationRequest(ClientInfo clInfo) {
// qDebug() << "process registration request on server was called name: " <<
// name;
Connection *con = static_cast<Connection *>(QObject::sender());
if (findConnection(clInfo.name) == nullptr) {
con->processRegistrationRequest(clInfo, true);
qDebug() << "registration accepted ";
} else {
con->processRegistrationRequest(clInfo, false);
qDebug() << "registration rejected ";
}
}