forked from teddych/railcontrol
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRailControl.cpp
More file actions
189 lines (160 loc) · 5.56 KB
/
RailControl.cpp
File metadata and controls
189 lines (160 loc) · 5.56 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
RailControl - Model Railway Control Software
Copyright (c) 2017-2022 Dominik (Teddy) Mahrer - www.railcontrol.org
RailControl is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 3, or (at your option) any
later version.
RailControl is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with RailControl; see the file LICENCE. If not see
<http://www.gnu.org/licenses/>.
*/
#include <cstdio> //printf
#include <cstdlib> //exit(0);
#include <cstring> //memset
#include <iostream>
#include <signal.h>
#include <sstream>
#include <unistd.h> //close;
#include <vector>
#include "ArgumentHandler.h"
#include "Hardware/HardwareHandler.h"
#include "Languages.h"
#include "Logger/Logger.h"
#include "Manager.h"
#include "Network/Select.h"
#include "RailControl.h"
#include "Version.h"
#include "Utils/Utils.h"
using std::vector;
using std::string;
static volatile unsigned int runRailcontrol;
static volatile unsigned char stopSignalCounter;
static const unsigned char maxStopSignalCounter = 3;
void stopRailControlSignal(int signo)
{
Logger::Logger* logger = Logger::Logger::GetLogger("Main");
logger->Info(Languages::TextStoppingRequestedBySignal, signo);
runRailcontrol = false;
if (++stopSignalCounter < maxStopSignalCounter)
{
return;
}
logger->Info(Languages::TextReceivedSignalKill, maxStopSignalCounter);
exit(1);
}
void stopRailControlWebserver()
{
Logger::Logger::GetLogger("Main")->Info(Languages::TextStoppingRequestedByWebClient);
runRailcontrol = false;
}
int main (int argc, char* argv[])
{
std::map<std::string,char> argumentMap;
argumentMap["config"] = 'c';
argumentMap["daemonize"] = 'd';
argumentMap["logfile"] = 'l';
argumentMap["help"] = 'h';
argumentMap["silent"] = 's';
ArgumentHandler argumentHandler(argc, argv, argumentMap, 'c');
const bool help = argumentHandler.GetArgumentBool('h');
if (help == true)
{
std::cout << "Usage: " << argv[0] << " <options>" << std::endl;
std::cout << "Options:" << std::endl;
std::cout << " --config=ConfigFile Read config file with file name ConfigFile (default ConfigFile: railcontrol.conf)" << std::endl;
std::cout << "-d --daemonize Daemonize RailControl. Implies -s" << std::endl;
std::cout << " --logfile=LogFile Write a logfile to file LogFile (default LogFile: railcontrol.log)" << std::endl;
std::cout << "-h --help Show this help" << std::endl;
std::cout << "-s --silent Omit writing to console" << std::endl;
return EXIT_SUCCESS;
}
const bool daemonize = argumentHandler.GetArgumentBool('d');
if (daemonize == true)
{
pid_t pid = fork();
if (pid > 0)
{
return EXIT_SUCCESS;
}
close(STDERR_FILENO);
close(STDOUT_FILENO);
close(STDIN_FILENO);
}
stopSignalCounter = 0;
signal(SIGINT, stopRailControlSignal);
signal(SIGTERM, stopRailControlSignal);
const string RailControl = "RailControl";
Utils::Utils::SetThreadName(RailControl);
runRailcontrol = true;
Logger::Logger* logger = Logger::Logger::GetLogger("Main");
const bool silent = daemonize || argumentHandler.GetArgumentBool('s');
if (silent == false)
{
logger->AddConsoleLogger();
}
const string logFileName = argumentHandler.GetArgumentString('l', "railcontrol.log");
if (logFileName.length() > 0)
{
logger->AddFileLogger(logFileName);
}
logger->Info(Languages::TextStarting, RailControl);
logger->Info(Languages::TextVersion, GetVersionInfoRailControlVersion());
logger->Info(Languages::TextCompileDate, Utils::Utils::TimestampToDate(GetVersionInfoCompileTimestamp()));
logger->Info(Languages::TextGitHash, GetVersionInfoGitHash());
logger->Info(Languages::TextGitDate, Utils::Utils::TimestampToDate(GetVersionInfoGitTimestamp()));
unsigned int changedFiles = GetVersionInfoGitDirty();
if (changedFiles)
{
logger->Info(Languages::TextGitDirty, changedFiles);
}
logger->Info(Languages::TextStartArgument, argv[0]);
const string configFileDefaultName("railcontrol.conf");
string configFileName = argumentHandler.GetArgumentString('c', configFileDefaultName);
if (configFileName.compare("") == 0)
{
configFileName = configFileDefaultName;
}
if (!Utils::Utils::FileExists(configFileName))
{
logger->Warning(Languages::TextConfigFileNotFound, configFileName, configFileDefaultName);
configFileName = configFileDefaultName;
}
if (configFileName.compare(configFileDefaultName) == 0 && !Utils::Utils::FileExists(configFileDefaultName))
{
Utils::Utils::CopyFile(logger, "railcontrol.conf.dist", configFileDefaultName);
}
Config config(configFileName);
unsigned int logKeepBackups = config.getValue("logkeepbackups", 10);
Utils::Utils::RemoveOldBackupFiles(logger, logFileName, logKeepBackups);
Manager m(config);
// wait for q followed by \n or SIGINT or SIGTERM
char input = 0;
do
{
if (silent == true)
{
Utils::Utils::SleepForSeconds(1);
}
else
{
struct timeval tv;
tv.tv_sec = 1;
tv.tv_usec = 0;
fd_set set;
FD_ZERO(&set);
FD_SET(STDIN_FILENO, &set);
int ret = TEMP_FAILURE_RETRY(select(FD_SETSIZE, &set, NULL, NULL, &tv));
if (ret > 0 && FD_ISSET(STDIN_FILENO, &set))
{
__attribute__((unused)) size_t unused = read(STDIN_FILENO, &input, sizeof(input));
}
}
} while (input != 'q' && runRailcontrol);
logger->Info(Languages::TextStoppingRailControl);
return EXIT_SUCCESS;
}