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
|
/* Copyright (C) 2022 The Qt Company Ltd.
*
* SPDX-License-Identifier: GPL-3.0-only WITH Qt-GPL-exception-1.0
*/
#pragma once
#include "clienthandler.h"
class SquishHandler : public ClientHandler {
public:
SquishHandler(const RequestInfo &request, const LicdSetup &settings)
: ClientHandler(request, settings)
{
m_updateInterval = utils::strToInt(m_settings.get("squish_report_interval"));
m_request.operation = OP_ADD_RESERVATION;
m_request.startTimestamp = utils::getTimestampNow();
}
bool isLicenseRequestDue() override { return true; }
bool isCachedReservationValid(std::string &reply) override { return true; }
void buildRequestJson() override
{
std::stringstream pay;
pay << "{";
pay<< "\"license_number\":" << "\"" << m_request.licenseId << "\",";
pay << "\"user_id\":" << "\"" << m_request.userId << "\",";
pay << "\"hw_id\":" << "\"" << m_settings.get("hw_id") << "\",";
pay << "\"operation\":" << "\"" << m_request.operation << "\",";
pay << "\"src\":" << "\"" << m_request.appName << "\",";
pay << "\"host_os\":" << "\"" << m_settings.get("host_os") << "\",";
pay << "\"src_version\":" << "\"" << m_request.appVersion << "\",";
pay << "\"email\":" << "\"" << m_request.email << "\"";
pay << "}";
m_request.payload = pay.str();
m_floatingLicense = true;
}
int parseAndSaveResponse(std::string &response) override
{
JsonHandler json(response);
// response JSON is converted to a reply string to the client from now on
std::stringstream ss;
m_license.status = (json.get("status") == "true") ? true : false;
if (m_license.status) {
ss << replyString[e_license_granted];
ss << " expiry_date=" << json.get("expiry_date");
ss << " license_id=" << json.get("license_number");
ss << " reservation_id=" << json.get("reservation_id");
response = ss.str();
} else {
if (json.get("message") == "License fully reserved") {
response = replyString[e_license_pool_full];
} else {
response = replyString[e_license_rejected];
}
// Do not save the response json, just return
return 1;
}
m_license.message = json.get("message");
m_license.license_id = json.get("license_number");
m_license.user_id = json.get("user_id");
m_license.reservation_id = json.get("reservation_id");
m_license.license_key = json.get("license_key");
m_license.parent_reservation_id = json.get("parent_reservvation_id");
m_license.operation = "add";
// Add timestamp into license JSON
struct timeval tp;
gettimeofday(&tp, NULL);
uint64_t timeNow = tp.tv_sec;
// Save the JSON, adding timestamp
json.add("last_timestamp", timeNow);
int result = utils::writeToFile(m_request.licenseFile, json.dump(4));
if (result != 0) {
std::cout << "ERROR saving license file: '" << m_request.licenseFile << "': " << strerror(result) << std::endl;
}
return 0;
}
void release() override
{
m_request.operation = OP_REMOVE_RESERVATION;
m_request.stopTimestamp = utils::getTimestampNow();
buildRequestJson();
}
};
|