-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
143 lines (125 loc) · 4.97 KB
/
main.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
/*NOTE: PFACES_BUILD_FOR_INTERFACES is defined in the project ! */
#include "pfaces-agent.h"
#include "pfaces-agent-utils.h"
#include "Logger.h"
#include "pfacesConfiguration.h"
#include "pfacesIO.h"
#include <csignal>
#include <chrono>
#include <thread>
// OS-specific includes
#ifdef _MSC_VER // VS on Windows ?
#include "pfaces-service.h"
#define pFacesAgentManager pFacesServiceManager
#else
#include "pfaces-daemon.h"
#define pFacesAgentManager pFacesDaemonManager
#endif // _MSC_VER
#define AGENT_MAIN "pfaces-agent/main"
#define TEST_MODE
#ifdef TEST_MODE
std::shared_ptr<pFacesAgent> spSingleAgent;
void exitSignalHandler(int signum) {
Logger::log("TEST_MODE/main", "Got CTRL-C signal. Exiting .... ");
spSingleAgent->kill();
exit(0);
}
int main(int argc, char* argv[]) {
LaunchModes launch_event;
launch_event = LaunchModes::RUN;
AgentConfigs agent_config;
agent_config.id = "agent_0";
agent_config.device_mask = "CG";
agent_config.listen_port = 8000;
agent_config.user_data_directory = "C:\\pFacesAgent0_data\\";
agent_config.build_command = "'C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\MSBuild\\Current\\Bin\\MSBuild.exe' %PROJECT_PATH%\\%PROJECT_NAME%.sln /property:Configuration=Release /property:Platform=x64";
spSingleAgent = std::make_shared<pFacesAgent>(agent_config);
Logger::log("TEST_MODE/main", "launching a single agent !");
int ret = spSingleAgent->run();
if(ret == 0)
Logger::log("TEST_MODE/main", "The single agent is launched successfully !");
else
Logger::log("TEST_MODE/main", "The single agent failed to launched !");
Logger::log("TEST_MODE/main", "waiting for CTRL-C signal ... ");
signal(SIGINT, exitSignalHandler);
while (true)std::this_thread::sleep_for(std::chrono::milliseconds(100));;
}
#else
std::vector<AgentConfigs> fetchConfigs(std::string config_file) {
pfacesConfigurationReader config_reader = pfacesConfigurationReader(config_file, "");
try {
config_reader.parse(nullptr, nullptr);
} catch (std::exception ex) {
Logger::log("fetchConfigs",
std::string("Configuration parse error: ") +
std::string(ex.what())
);
return {};
}
int num_agents = config_reader.readConfigValueInt("num_agents");
if (num_agents < 1) {
Logger::log("fetchConfigs", "Configuration file error: num_agents must be >= 1.");
return {};
}
std::vector<AgentConfigs> configs(num_agents);
for (size_t i = 0; i < num_agents; i++) {
std::string config_path = std::string("Agent") + std::to_string(i) + std::string(".");
configs[i].id = atoll(config_reader.readConfigValueString(config_path + std::string("id")).c_str());
configs[i].device_mask = config_reader.readConfigValueString(config_path + std::string("device_mask"));
configs[i].listen_port = atoll(config_reader.readConfigValueString(config_path + std::string("listen_port")).c_str());
configs[i].device_abuse = (config_reader.readConfigValueString(config_path + std::string("device_abuse")) == std::string("true"));
configs[i].user_data_directory = config_reader.readConfigValueString(config_path + std::string("user_data_directory"));
configs[i].build_command = config_reader.readConfigValueString(config_path + std::string("build_command"));
}
return configs;
}
int main(int argc, char* argv[]) {
// what command ?
std::string cmd;
if (argc == 2)
cmd = std::string(argv[1]);
else
cmd = "-run";
// prepare
LaunchModes launch_event;
std::vector<AgentConfigs> configs;
if (cmd == std::string("-install")) {
launch_event = LaunchModes::INSTALL;
Logger::log(AGENT_MAIN, "Launch event set to install.");
}
else if (cmd == std::string("-uninstall")) {
launch_event = LaunchModes::UNINSTALL;
Logger::log(AGENT_MAIN, "Launch event set to uninstall.");
}
else if (cmd == std::string("-run")) {
launch_event = LaunchModes::RUN;
Logger::log(AGENT_MAIN, "Launch event set to run.");
// reading launch configurations
std::string config_file = pfacesAgentUtils::getApplicationDirectory() + "agent.conf";
if (!pfacesFileIO::isFileExist(config_file)) {
Logger::log(AGENT_MAIN, "Configuration file not found. Existing ... !");
return -1;
}
Logger::log(AGENT_MAIN, "Reading the agents configs.");
configs = fetchConfigs(config_file);
if (configs.size() == 0) {
Logger::log(AGENT_MAIN, "Failed to fetch the agent configurations. Existing ... !");
return -1;
}
}
else {
Logger::log(AGENT_MAIN,
std::string("Invalid launch command (") + cmd +
std::string("). pfaces-agent.exe must recieve one of the following launch commands: -install, -uninstall, or -run. Exiting ... !")
);
return -1;
}
// launch
Logger::log(AGENT_MAIN, "Creating the service/daemon and calling its launch method.");
pFacesAgentManager agentManager(configs);
agentManager.launch(launch_event);
// Maybe later:
// here we can report to a centeral server that some agents are installed, uninstalled, ran or shut-down
return 0;
}
#endif