-
Notifications
You must be signed in to change notification settings - Fork 0
/
FifoSession.cpp
106 lines (92 loc) · 2.41 KB
/
FifoSession.cpp
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
/*
* Copyright (C) 2021 Ilya Entin
*/
#include "FifoSession.h"
#include <filesystem>
#include <sys/stat.h>
#include "Fifo.h"
#include "Server.h"
#include "ServerOptions.h"
namespace fifo {
FifoSession::FifoSession(ServerWeakPtr server,
const CryptoPP::SecByteBlock& pubB,
std::string_view signatureWithPubKey) :
RunnableT(ServerOptions::_maxFifoSessions),
Session(server, pubB, signatureWithPubKey) {}
FifoSession::~FifoSession() {
try {
std::filesystem::remove(_fifoName);
}
catch (const std::exception& e) {
Warn << e.what() << '\n';
}
Trace << '\n';
}
bool FifoSession::start() {
_fifoName = ServerOptions::_fifoDirectoryName + '/';
ioutility::toChars(_clientId, _fifoName);
if (mkfifo(_fifoName.data(), 0666) == -1 && errno != EEXIST) {
LogError << strerror(errno) << '-' << _fifoName << '\n';
return false;
}
return true;
}
void FifoSession::run() {
CountRunning countRunning;
if (!std::filesystem::exists(_fifoName) || _stopped)
return;
while (!_stopped) {
if (!receiveRequest())
break;
}
}
void FifoSession::stop() {
_stopped = true;
Fifo::onExit(_fifoName);
}
bool FifoSession::receiveRequest() {
if (!std::filesystem::exists(_fifoName))
return false;
switch (_status) {
case STATUS::MAX_OBJECTS_OF_TYPE:
case STATUS::MAX_TOTAL_OBJECTS:
_status = STATUS::NONE;
break;
default:
break;
}
try {
_request.erase(_request.cbegin(), _request.cend());
if (!Fifo::readStringBlock(_fifoName, _request))
return false;
if (processTask())
return sendResponse();
}
catch (const std::exception& e) {
Warn << e.what() << '\n';
}
return false;
}
bool FifoSession::sendResponse() {
if (!std::filesystem::exists(_fifoName))
return false;
std::string_view payload = buildReply(_status);
if (payload.empty())
return false;
return Fifo::sendMsg(_fifoName, payload);
}
void FifoSession::sendStatusToClient() {
auto lambda = [] (
const HEADER& header, std::string_view idStr, const CryptoPP::SecByteBlock& pubA) {
Fifo::sendMsg(ServerOptions::_acceptorName, header, idStr, pubA);
};
Session::sendStatusToClient(lambda, _status);
}
void FifoSession::displayCapacityCheck(std::atomic<unsigned>& totalNumberObjects) const {
Session::displayCapacityCheck(totalNumberObjects,
getNumberObjects(),
getNumberRunningByType(),
_maxNumberRunningByType,
_status);
}
} // end of namespace fifo