-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.cpp
More file actions
136 lines (123 loc) · 3.59 KB
/
Server.cpp
File metadata and controls
136 lines (123 loc) · 3.59 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
* Copyright (C) 2021 Ilya Entin
*/
#include "Server.h"
#include <filesystem>
#include <boost/interprocess/sync/named_mutex.hpp>
#include "Ad.h"
#include "EchoPolicy.h"
#include "FifoAcceptor.h"
#include "FifoSession.h"
#include "NoSortInputPolicy.h"
#include "ServerOptions.h"
#include "SortInputPolicy.h"
#include "TaskController.h"
#include "TcpAcceptor.h"
#include "TcpSession.h"
#include "Utility.h"
Server::Server() :
_chronometer(ServerOptions::_timing),
_threadPoolSession(ServerOptions::_maxTotalSessions) {
try {
// create if was removed
std::string_view fifoDirectoryNameV(Options::_fifoDirectoryName.data(), Options::_fifoDirectoryName.size());
std::filesystem::create_directory(fifoDirectoryNameV);
// Unlock computer in case the app crashed during debugging
// leaving mutex locked, run serverX or testbin in this case.
removeNamedMutex();
}
catch (const std::exception& e) {
LogError << e.what() << '\n';
}
}
Server::~Server() {
try {
Ad::clear();
utility::removeAccess();
}
catch (const std::exception& e) {
Warn << e.what() << '\n';
}
}
void Server::setPolicy() {
switch (ServerOptions::_policyEnum) {
case POLICYENUM::NOSORTINPUT:
Ad::readAds(ServerOptions::_adsFileName);
_policy = std::make_unique<NoSortInputPolicy>();
break;
case POLICYENUM::SORTINPUT:
Ad::readAds(ServerOptions::_adsFileName);
_policy = std::make_unique<SortInputPolicy>();
break;
case POLICYENUM::ECHOPOLICY:
_policy = std::make_unique<EchoPolicy>();
break;
default:
throw std::runtime_error("non-existent policy");
break;
}
}
bool Server::start() {
setPolicy();
if (!TaskController::create())
return false;
_tcpAcceptor = std::make_shared<tcp::TcpAcceptor>(weak_from_this());
if (!_tcpAcceptor->start())
return false;
_threadPoolAcceptor.push(_tcpAcceptor);
_fifoAcceptor = std::make_shared<fifo::FifoAcceptor>(weak_from_this());
if (!_fifoAcceptor->start())
return false;
_threadPoolAcceptor.push(_fifoAcceptor);
return true;
}
void Server::stop() {
stopSessions();
if (_tcpAcceptor)
_tcpAcceptor->stop();
if (_fifoAcceptor)
_fifoAcceptor->stop();
_threadPoolAcceptor.stop();
_threadPoolSession.stop();
TaskController::destroy();
}
void Server::createFifoSession(std::string_view primarySignatureWithKey,
std::string_view primaryPubKeyAes,
std::string_view secondarySignatureWithKey,
std::string_view secondaryPubKeyAes) {
std::lock_guard lock(_mutex);
auto session =
std::make_shared<fifo::FifoSession>(weak_from_this(),
primarySignatureWithKey,
primaryPubKeyAes,
secondarySignatureWithKey,
secondaryPubKeyAes);
startSession(session);
}
void Server::createTcpSession(tcp::ConnectionPtr connection,
std::string_view primarySignatureWithKey,
std::string_view primaryPubKeyAes,
std::string_view secondarySignatureWithKey,
std::string_view secondaryPubKeyAes) {
std::lock_guard lock(_mutex);
auto session =
std::make_shared<tcp::TcpSession>(weak_from_this(),
connection,
primarySignatureWithKey,
primaryPubKeyAes,
secondarySignatureWithKey,
secondaryPubKeyAes);
startSession(session);
}
void Server::stopSessions() {
std::lock_guard lock(_mutex);
for (auto& pr : _sessions)
if (auto session = pr.second.lock())
session->_stopped.store(true);
for (auto& pr : _sessions)
if (auto session = pr.second.lock())
session->stop();
}
void Server::removeNamedMutex() {
boost::interprocess::named_mutex::remove(FIFO_NAMED_MUTEX);
}