forked from jackburton79/ocs-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfiguration.cpp
171 lines (134 loc) · 3.12 KB
/
Configuration.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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
* Configuration.cpp
*
* Created on: 13/lug/2013
* Author: stefano
*/
#include "Configuration.h"
#include "Machine.h"
#include "NetworkInterface.h"
#include "NetworkRoster.h"
#include <algorithm>
#include <cerrno>
#include <ctime>
#include <fstream>
#include <assert.h>
#include <unistd.h>
const static char* kServer = "server";
const static char* kDeviceID = "deviceID";
static Configuration* sConfiguration;
Configuration::Configuration()
{
}
Configuration::~Configuration()
{
}
/* static */
Configuration*
Configuration::Get()
{
if (sConfiguration == NULL)
sConfiguration = new Configuration;
return sConfiguration;
}
bool
Configuration::Load(const char* fileName)
{
fConfigFileName = fileName;
try {
std::ifstream configFile(fileName);
std::string line;
while (std::getline(configFile, line) > 0) {
size_t pos = line.find("=");
if (pos != std::string::npos) {
fValues[line.substr(0, pos)] = line.substr(pos + 1, std::string::npos);
}
}
} catch (...) {
}
// Generate a DeviceID if we don't have one
std::map<std::string, std::string>::const_iterator i;
i = fValues.find(kDeviceID);
if (i == fValues.end())
_GenerateDeviceID();
return true;
}
bool
Configuration::Save(const char* fileName)
{
try {
std::ofstream configFile(fileName, std::ios_base::out);
std::map<std::string, std::string>::const_iterator i;
for (i = fValues.begin(); i != fValues.end(); i++) {
configFile << i->first << "=" << i->second << std::endl;
}
} catch (...) {
return false;
}
return true;
}
bool
Configuration::Save()
{
if (fConfigFileName == "")
return false;
return Save(fConfigFileName.c_str());
}
bool
Configuration::SetServer(const char* serverUrl)
{
fValues[kServer] = serverUrl;
return true;
}
std::string
Configuration::DeviceID() const
{
std::map<std::string, std::string>::const_iterator i;
i = fValues.find(kDeviceID);
if (i == fValues.end()) {
const_cast<Configuration*>(this)->_GenerateDeviceID();
i = fValues.find(kDeviceID);
}
assert(i->second != "");
return i->second;
}
std::string
Configuration::ServerURL() const
{
std::map<std::string, std::string>::const_iterator i;
i = fValues.find(kServer);
if (i != fValues.end())
return i->second;
return "";
}
bool
Configuration::LocalInventory() const
{
return fValues.find(kServer) == fValues.end();
}
void
Configuration::_GenerateDeviceID()
{
std::string deviceID = Machine::Get()->SystemSerialNumber();
if (deviceID == "") {
NetworkRoster roster;
NetworkInterface interface;
unsigned int cookie = 0;
while (roster.GetNextInterface(&cookie, interface) == 0) {
if (interface.Name() != "lo") {
deviceID = interface.HardwareAddress();
deviceID.erase(std::remove(deviceID.begin(), deviceID.end(), ':'),
deviceID.end());
break;
}
}
}
time_t rawtime = time(NULL);
struct tm* timeinfo = localtime(&rawtime);
char timeString[80];
strftime(timeString, sizeof(timeString), "-%Y-%m-%d-%H-%M-%S", timeinfo);
// DeviceID needs to have a date appended in this very format,
// otherwise OCSInventoryNG will not accept the inventory
deviceID.append(timeString);
fValues[kDeviceID] = deviceID;
}