gcontini
2019-10-19 8bbef2865455754425a84b86680a89bff8aa7691
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
//============================================================================
// Name        : license-manager-cpp.cpp
// Author      :
// Version     :
// Copyright   : BSD
//============================================================================
 
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <cstring>
#include <iostream>
 
#include "api/license++.h"
 
#include "LicenseReader.hpp"
 
using namespace std;
void print_error(char out_buffer[256], LicenseInfo* licenseInfo) {
 
}
 
void identify_pc(IDENTIFICATION_STRATEGY pc_id_method, char chbuffer[PC_IDENTIFIER_SIZE + 1]) {
 
}
 
static void mergeLicenses(vector<license::FullLicenseInfo> licenses,
        LicenseInfo* license) {
    if (license != nullptr) {
        time_t curLicense_exp = 0;
        for (auto it = licenses.begin(); it != licenses.end(); it++) {
            //choose the license that expires later...
            if (!it->has_expiry) {
                it->toLicenseInfo(license);
                break;
            } else if (curLicense_exp < it->expires_on()) {
                curLicense_exp = it->expires_on();
                it->toLicenseInfo(license);
            }
        }
    }
}
 
EVENT_TYPE acquire_license(const char * product,
        const LicenseLocation* licenseLocation, LicenseInfo* licenseInfoOut) {
    license::LicenseReader lr = license::LicenseReader(licenseLocation);
    vector<license::FullLicenseInfo> licenses;
    license::EventRegistry er = lr.readLicenses(string(product), licenses);
    EVENT_TYPE result;
    if (licenses.size() > 0) {
        vector<license::FullLicenseInfo> licenses_with_errors;
        vector<license::FullLicenseInfo> licenses_ok;
        for (auto it = licenses.begin(); it != licenses.end(); it++) {
            bool valid = it->validate(0,er);
            if (valid) {
                licenses_ok.push_back(*it);
            } else {
                licenses_with_errors.push_back(*it);
            }
        }
        if (licenses_ok.size() > 0) {
            er.turnErrorsIntoWarnings();
            result = LICENSE_OK;
            mergeLicenses(licenses_ok, licenseInfoOut);
        } else {
            er.turnWarningsIntoErrors();
            result = er.getLastFailure()->event_type;
            mergeLicenses(licenses_with_errors, licenseInfoOut);
        }
 
    } else {
        er.turnWarningsIntoErrors();
        result = er.getLastFailure()->event_type;
    }
#ifdef _DEBUG
    cout << er <<endl;
#endif
    if (licenseInfoOut != nullptr) {
        er.exportLastEvents(licenseInfoOut->status, AUDIT_EVENT_NUM);
    }
    return result;
}
 
EVENT_TYPE confirm_license(char * product,
        LicenseLocation licenseLocation) {
    return LICENSE_OK;
}
 
EVENT_TYPE release_license(char * product,
        LicenseLocation licenseLocation) {
    return LICENSE_OK;
}