crawlserv++  [under development]
Application for crawling and analyzing textual content of websites.
App.hpp
Go to the documentation of this file.
1 /*
2  *
3  * ---
4  *
5  * Copyright (C) 2021 Anselm Schmidt (ans[ät]ohai.su)
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version in addition to the terms of any
11  * licences already herein identified.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <https://www.gnu.org/licenses/>.
20  *
21  * ---
22  *
23  * App.hpp
24  *
25  * The main application class that processes command line arguments, shows the initial header, loads the
26  * configuration from the argument-specified configuration file and creates as well as starts the server.
27  *
28  * Created on: Oct 26, 2018
29  * Author: ans
30  */
31 
32 #ifndef MAIN_APP_HPP_
33 #define MAIN_APP_HPP_
34 
35 #include "ConfigFile.hpp"
36 #include "Exception.hpp"
37 #include "Server.hpp"
38 #include "SignalHandler.hpp"
39 #include "Version.hpp"
40 
41 #include "../Helper/DateTime.hpp"
42 #include "../Helper/Portability/compiler.h"
43 #include "../Helper/Portability/getch.h"
44 #include "../Helper/Versions.hpp"
45 #include "../Struct/DatabaseSettings.hpp"
46 #include "../Struct/NetworkSettings.hpp"
47 #include "../Struct/ServerSettings.hpp"
48 
49 #include <atomic> // std::atomic
50 #include <csignal> // SIGINT, SIGTERM, std::sig_atomic_t
51 #include <exception> // std::exception
52 #include <iostream> // std::cout, std::endl, std::flush
53 #include <memory> // std::make_unique, std::unique_ptr
54 #include <string> // std::string
55 #include <string_view> // std::string_view_literals
56 #include <vector> // std::vector
57 
59 namespace crawlservpp::Main {
60 
61  using std::string_view_literals::operator""sv;
62 
63  /*
64  * CONSTANTS
65  */
66 
69 
71  inline constexpr auto argsRequired{2};
72 
74  inline constexpr auto pwPrompt1{"Enter password for "sv};
75 
77  inline constexpr auto pwPrompt2{"@"sv};
78 
80  inline constexpr auto pwPrompt3{":"sv};
81 
83  inline constexpr auto pwPrompt4{": "sv};
84 
86  inline constexpr auto doneMsg{"[DONE]"sv};
87 
89  inline constexpr auto inputBackspace{127};
90 
92  inline constexpr auto inputEof{-1};
93 
95  inline constexpr auto inputEtx{3};
96 
98  inline constexpr auto inputEsc{27};
99 
101  //NOLINTNEXTLINE(clang-diagnostic-string-plus-int, cppcoreguidelines-pro-bounds-pointer-arithmetic)
102  inline constexpr auto year{__DATE__ + 7};
103 
105  inline constexpr auto descName{"crawlserv++ Command-and-Control Server"sv};
106 
108  inline constexpr auto descVer{"Version "sv};
109 
111  inline constexpr auto descCopyrightHead{"Copyright (C) "sv};
112 
114  inline constexpr auto descCopyrightTail{" Anselm Schmidt (ans[ät]ohai.su)"sv};
115 
117  inline constexpr auto descLicense{
118  "This program is free software: you can redistribute it and/or modify\n"
119  "it under the terms of the GNU General Public License as published by\n"
120  "the Free Software Foundation, either version 3 of the License, or\n"
121  "(at your option) any later version.\n\n"
122  "This program is distributed in the hope that it will be useful,\n"
123  "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
124  "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
125  "GNU General Public License for more details.\n\n"
126  "You should have received a copy of the GNU General Public License\n"
127  "along with this program. If not, see <https://www.gnu.org/licenses/>."sv
128  };
129 
131  inline constexpr auto descUsing{"using"sv};
132 
134  inline constexpr auto descUsage{"USAGE: crawlserv <config_file> or crawlserv -v"};
135 
137 
138  /*
139  * DECLARATION
140  */
141 
143 
151  class App final : SignalHandler {
152  // for convenience
156 
157  public:
160 
161  explicit App(const std::vector<std::string>& args) noexcept;
162  virtual ~App();
163 
167 
168  int run() noexcept;
169 
171 
175 
178  App(App&) = delete;
179 
181  App& operator=(App&) = delete;
182 
184  App(App&&) = delete;
185 
187  App& operator=(App&&) = delete;
188 
190 
191  private:
192  std::atomic<bool> running{true};
193  std::unique_ptr<Server> server;
194  bool showVersionsOnly{false};
195 
196  // signal handling
197  void shutdown(std::sig_atomic_t signal);
198 
199  // helper functions
200  bool getPassword(DatabaseSettings& dbSettings);
201  bool inputLoop(DatabaseSettings& dbSettings, bool& isCancelTo);
202 
203  // static helper functions
204  static void outputHeader(bool showLibraryVersions);
205  static void checkArgumentNumber(int args);
206  static void loadConfig(
207  const std::string& fileName,
208  ServerSettings& serverSettings,
209  DatabaseSettings& dbSettings,
210  NetworkSettings& networkSettings
211  );
212  };
213 
214 } /* namespace crawlservpp::Main */
215 
216 #endif /* MAIN_APP_HPP_ */
constexpr auto descVer
The beginning of the version string.
Definition: App.hpp:108
constexpr auto year
The current year.
Definition: App.hpp:102
constexpr auto descName
The name of the application.
Definition: App.hpp:105
App(const std::vector< std::string > &args) noexcept
Constructor.
Definition: App.cpp:54
constexpr auto descUsing
The string before the used libraries.
Definition: App.hpp:131
constexpr auto argsRequired
Number of arguments required by the application.
Definition: App.hpp:71
Network settings containing the default proxy as well as host, port, and password of the TOR control ...
Definition: NetworkSettings.hpp:49
constexpr auto pwPrompt3
Third part of the password prompt.
Definition: App.hpp:80
constexpr auto pwPrompt4
Fourth part of the password prompt.
Definition: App.hpp:83
static void signal(int signalNumber)
Static signal handler.
Definition: SignalHandler.cpp:75
constexpr auto descCopyrightHead
The beginning of the copyright string.
Definition: App.hpp:111
constexpr auto pwPrompt1
First part of the password prompt.
Definition: App.hpp:74
Server settings containing its port, as well as allowed clients, origins, and actions.
Definition: ServerSettings.hpp:51
int run() noexcept
Runs the application.
Definition: App.cpp:150
constexpr auto descCopyrightTail
The actual copyrigt.
Definition: App.hpp:114
App & operator=(App &)=delete
Deleted copy assignment operator.
constexpr auto inputEsc
Code for the Escape key.
Definition: App.hpp:98
constexpr auto doneMsg
Message when done with the password input.
Definition: App.hpp:86
Database settings containing its host, port, user, password, schema, and compression.
Definition: DatabaseSettings.hpp:48
virtual ~App()
Destructor waiting for active threads before cleaning up the application.
Definition: App.cpp:104
constexpr auto descUsage
The usage string for the command line.
Definition: App.hpp:134
constexpr auto pwPrompt2
Second part of the password prompt.
Definition: App.hpp:77
Main application.
Definition: App.hpp:151
constexpr auto descLicense
The text of the license.
Definition: App.hpp:117
Definition: SignalHandler.hpp:38
Namespace for the main classes of the program.
Definition: App.cpp:34
constexpr auto inputEof
Code for the CTRL+C keys or the end of the file.
Definition: App.hpp:92
constexpr auto inputBackspace
Code for the backspace key.
Definition: App.hpp:89
constexpr auto inputEtx
Code for the CTRL+C keys or the end of the text.
Definition: App.hpp:95