Joedb 10.5.0
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 server.start();
45 }
46
47 void join() {server.join();}
48
50 {
51 if (server.is_started())
52 try {server.stop();} catch (...) {}
53 }
54 };
55
56 ////////////////////////////////////////////////////////////////////////////
57 static int multi_server(Arguments &arguments)
58 ////////////////////////////////////////////////////////////////////////////
59 {
60 Parsed_Logger logger(arguments);
61
62 const float timeout_seconds = arguments.get_option<float>
63 (
64 "timeout",
65 "seconds",
66 0.0f
67 );
68
69 const int log_level = arguments.get_option<int>("log_level", "level", 100);
70
71 arguments.add_parameter("<file.joedb>+");
72
73 if (arguments.get_remaining_count() == 0)
74 {
75 arguments.print_help(std::cerr);
76 return 1;
77 }
78
79 std::list<std::unique_ptr<Server_Data>> servers;
80
81 while (arguments.get_remaining_count())
82 {
83 const std::string file_name(arguments.get_next());
84 logger.get().log("creating server for: " + file_name);
85 servers.emplace_back
86 (
87 new Server_Data
88 (
89 logger.get(),
90 log_level,
91 file_name,
92 file_name + ".sock",
93 std::chrono::milliseconds(std::lround(timeout_seconds * 1000))
94 )
95 );
96 }
97
98 for (auto &server: servers)
99 server->join();
100
101 return 0;
102 }
103}
104
105/////////////////////////////////////////////////////////////////////////////
106int main(int argc, char **argv)
107/////////////////////////////////////////////////////////////////////////////
108{
109 return joedb::main_wrapper(joedb::multi_server, argc, argv);
110}
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
Definition Open_Mode.h:13
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
Definition ids.h:50