diff options
Diffstat (limited to 'src/libs/qlicenseservice/licenser.cpp')
-rw-r--r-- | src/libs/qlicenseservice/licenser.cpp | 88 |
1 files changed, 46 insertions, 42 deletions
diff --git a/src/libs/qlicenseservice/licenser.cpp b/src/libs/qlicenseservice/licenser.cpp index 574fedd..f2364d6 100644 --- a/src/libs/qlicenseservice/licenser.cpp +++ b/src/libs/qlicenseservice/licenser.cpp @@ -52,8 +52,8 @@ Licenser::~Licenser() int Licenser::listen() { m_infoString = ""; - uint16_t socket = 0; // Placeholder for whatever socket gets active - std::string input = m_tcpServer->listenToClients(socket); + uint16_t socketId = 0; // Placeholder for whatever socket gets active + std::string input = m_tcpServer->listenToClients(socketId); input = utils::trimStr(input); if (input.empty()) { @@ -63,23 +63,23 @@ int Licenser::listen() return 0; //continue; } if (input == SOCKET_CLOSED_MSG) { - std::cout << "Client disconnected, socket id " << socket << std::endl; - removeReservation(socket); + std::cout << "Client disconnected, socket id " << socketId << std::endl; + removeReservation(socketId); return 0; } std::cout << "Got a request: " << input << std::endl; // Check if we already have this client in storage (floating one): - if (clientInStorage(socket)) { + if (clientInStorage(socketId)) { // do nothing - floating clients should only send request at startup - std::cout << "Floating client with ID " << socket << " sent something while working: ignoring\n"; - m_tcpServer->sendResponse(socket, "NOK\n"); + std::cout << "Floating client with ID " << socketId << " sent something while working: ignoring\n"; + m_tcpServer->sendResponse(socketId, "NOK\n"); return 0; } std::string reply = ""; // Holds server response first (if got any). After parsing, holds reply to the client bool removeRes = false; - if (parseInputAndCreateCLient(socket, input)) { + if (parseInputAndCreateCLient(socketId, input)) { if (m_currentClient->parseRequest() != e_bad_request) { RequestType reqType = m_currentClient->getRequestType(); if (reqType == RequestType::reservation_query) { @@ -87,8 +87,8 @@ int Licenser::listen() } else if (reqType == RequestType::daemon_version) { reply = getDaemonVersion(); } else { - std::cout << "Initiating a request to the server\n"; if (m_currentClient->isLicenseRequestDue()) { + std::cout << "Initiating a request to the server\n"; if (sendServerRequest(reply) == e_bad_connection) { // Bad server connection: Check the existing license file for expiry date if (!m_currentClient->isCachedReservationValid(reply)) { @@ -105,11 +105,15 @@ int Licenser::listen() } } else { reply = replyString[e_bad_request]; + removeRes = true; } } else { reply = replyString[e_bad_request]; + removeRes = true; } - if (m_currentClient->hasFloatingLicense() && !clientInStorage(socket)) { + if (m_currentClient->hasFloatingLicense() + && !clientInStorage(socketId) + && !removeRes) { // Store QA tool, Coco or Squish client (floating license) if not already stored if (m_currentClient->hasParent()) { if (!addClientToParent()) { @@ -118,7 +122,7 @@ int Licenser::listen() } } else { std::cout << "Storing client in main cache\n"; - m_floatingClients.push_back(std::move(m_currentClient)); + m_floatingClients[socketId] = std::move(m_currentClient); } } if (!m_infoString.empty()) { @@ -126,13 +130,14 @@ int Licenser::listen() reply += m_infoString; } reply +="\n"; - m_tcpServer->sendResponse(socket, reply); - std::cout << "Replied to socket " << socket << ": " << reply << std::endl; + m_tcpServer->sendResponse(socketId, reply); + std::cout << "Replied to socket " << socketId << ": " << reply << std::endl; if (removeRes) { // No license, remove reservation from cache (floating license) // or license file (site license) - removeReservation(socket); + removeReservation(socketId); + return 0; } if (m_floatingClients.size() > 0) { @@ -145,21 +150,19 @@ int Licenser::listen() int Licenser::checkTasksDue() { // Check if there's any floating clients which has tasks to be done - ClientHandler *client; - for (int i = 0 ; i < m_floatingClients.size(); i++) { - client = m_floatingClients[i]; + for (auto entry : m_floatingClients) { // First check if (parent) client has stopped and no children left - if (client->stopped() && !client->hasChildren()) { - removeReservation(client->getSocketId()); + if (entry.second->stopped() && !entry.second->hasChildren()) { + removeReservation(entry.first); return 0; } // Then check if there is any server pings due - if (client->isLicenseRequestDue()) { + if (entry.second->isLicenseRequestDue()) { std::string reply; if (sendServerRequest(reply) != e_bad_connection) { - std::cout << "Reported alive floating reservation with socket ID " << client->getSocketId() << std::endl; - std::cout << "Client has " << client->getNumberOfClients() << " sub-processes running\n"; - client->resetTime(); + std::cout << "Reported alive floating reservation with socket ID " << entry.first << std::endl; + std::cout << "Client has " << entry.second->getNumberOfClients() << " sub-processes running\n"; + entry.second->resetTime(); } } } @@ -237,12 +240,11 @@ void Licenser::doHmacHashSha256(const std::string &payload, const std::string &s for (uint8_t x : out) { ss_result << std::hex << std::setfill('0') << std::setw(2) << (int)x; } - authKey = ss_result.str(); JsonHandler pay(payload); - } + std::string Licenser::checkReservations() { // Open all license files @@ -270,12 +272,13 @@ std::string Licenser::checkReservations() return reply.str(); } + bool Licenser::addClientToParent() { bool found = false; - for (int i = 0; i < m_floatingClients.size(); i++) { - if (m_floatingClients.at(i)->getReservationId() == m_currentClient->getParentReservationId()) { - m_floatingClients.at(i)->addChildClient(m_currentClient); + for (auto parent : m_floatingClients) { + if (parent.second->getReservationId() == m_currentClient->getParentReservationId()) { + parent.second->addChildClient(m_currentClient); found = true; std::cout << "Stored client under reservation ID " << m_currentClient->getParentReservationId() << std::endl; @@ -292,19 +295,19 @@ bool Licenser::addClientToParent() void Licenser::removeReservation(int socketId) { - // Check if the client had a floating license, or is a child of one: - ClientHandler *client; - for (int i = 0; i < m_floatingClients.size(); i++) { - client = m_floatingClients[i]; - if (client->getSocketId() == socketId) { - // If parent has no running children left, we can release the reservation + // Close the socket + m_tcpServer->closeClientSocket(socketId); + // Check if the client had a floating license, or is a child of one. + // Need to loop through all of them because socketId can be under any parent + for (auto element : m_floatingClients) { + ClientHandler* client = element.second; + if (element.first == socketId) { if (!client->stopped()) { client->prepareRelease(); - m_tcpServer->closeClientSocket(socketId); // Don't remove parent yet, it might have child processes left } if (!client->hasChildren()) { - // No child processes: remove it + // If client has no running children left, we can release the reservation and remove client std::string reply; client->release(); client->buildRequestJson(); @@ -312,28 +315,28 @@ void Licenser::removeReservation(int socketId) { if (sendServerRequest(reply) != e_bad_connection) { // if connection succeeds, we can forget about this client std::cout << "Removing floating reservation with ID " << client->getReservationId() << std::endl; - m_floatingClients.erase(m_floatingClients.begin() + i); + m_floatingClients.erase(socketId); delete client; + } } return; } else if (client->removeChildClient(socketId)) { std::cout << "Removed child client under reservation ID " << client->getReservationId() << std::endl; - m_tcpServer->closeClientSocket(socketId); return; } } - m_tcpServer->closeClientSocket(socketId); return; } + bool Licenser::clientInStorage(uint16_t socketId) { for (auto client : m_floatingClients) { - if (client->getSocketId() == socketId) { + if (client.first == socketId) { return true; } - if (client->hasChildren()) { - if (client->clientInStorage(socketId)) { + if (client.second->hasChildren()) { + if (client.second->clientInStorage(socketId)) { return true; } } @@ -341,6 +344,7 @@ bool Licenser::clientInStorage(uint16_t socketId) { return false; } + std::string Licenser::getDaemonVersion() { std::string version = "Qt License Daemon (qtlicd) v"; |