Joedb 10.4.3
The Journal-Only Embedded Database
Loading...
Searching...
No Matches
joedb_multi_server.cpp
Go to the documentation of this file.
6
7#include <iostream>
8#include <list>
9#include <memory>
10#include <cmath>
11
12namespace joedb
13{
14 ////////////////////////////////////////////////////////////////////////////
16 ////////////////////////////////////////////////////////////////////////////
17 {
18 private:
19 File file;
21 Server server;
22
23 public:
25 (
26 Logger &logger,
27 int log_level,
28 const std::string &file_name,
29 std::string endpoint_path,
30 std::chrono::milliseconds timeout
31 ):
32 file(file_name, Open_Mode::shared_write),
33 client(file),
34 server
35 (
36 logger,
37 log_level,
38 1,
39 std::move(endpoint_path),
40 client,
41 timeout
42 )
43 {
44 }
45
46 void join() {server.join();}
47 };
48
49 ////////////////////////////////////////////////////////////////////////////
50 static int multi_server(Arguments &arguments)
51 ////////////////////////////////////////////////////////////////////////////
52 {
53 Parsed_Logger logger(arguments);
54
55 const float timeout_seconds = arguments.get_option<float>
56 (
57 "timeout",
58 "seconds",
59 0.0f
60 );
61
62 const int log_level = arguments.get_option<int>("log_level", "level", 100);
63
64 arguments.add_parameter("<file.joedb>+");
65
66 if (arguments.get_remaining_count() == 0)
67 {
68 arguments.print_help(std::cerr);
69 return 1;
70 }
71
72 std::list<std::unique_ptr<Server_Data>> servers;
73
74 while (arguments.get_remaining_count())
75 {
76 const std::string file_name(arguments.get_next());
77 logger.get().log("creating server for: " + file_name);
78 servers.emplace_back
79 (
80 new Server_Data
81 (
82 logger.get(),
83 log_level,
84 file_name,
85 file_name + ".sock",
86 std::chrono::milliseconds(std::lround(timeout_seconds * 1000))
87 )
88 );
89 }
90
91 for (auto &server: servers)
92 server->join();
93
94 return 0;
95 }
96}
97
98/////////////////////////////////////////////////////////////////////////////
99int main(int argc, char **argv)
100/////////////////////////////////////////////////////////////////////////////
101{
102 return joedb::main_wrapper(joedb::multi_server, argc, argv);
103}
Server_Data(Logger &logger, int log_level, const std::string &file_name, std::string endpoint_path, std::chrono::milliseconds timeout)
int main()
Open_Mode
Definition Open_Mode.h:8
@ shared_write
like write_existing_or_create_new, but does not lock the file, and does not fail if locked
int main_wrapper(int(*main)(Arguments &), int argc, char **argv)
Process command-line arguments and catch exceptions from main.
JOEDB_FILE File
Definition File.h:23