Joedb 9.5.0
The Journal-Only Embedded Database
Loading...
Searching...
No Matches
joedb_server.cpp
Go to the documentation of this file.
6
7#include <iostream>
8#include <cstring>
9#include <cstdlib>
10
11namespace joedb
12{
13 /////////////////////////////////////////////////////////////////////////////
14 static int joedb_server(int argc, char **argv)
15 /////////////////////////////////////////////////////////////////////////////
16 {
17 const Open_Mode default_open_mode = Open_Mode::write_existing_or_create_new;
18
19 Client_Parser client_parser(default_open_mode, Client_Parser::DB_Type::none);
20
21 if (argc <= 1)
22 {
23 std::cerr << "usage: " << argv[0];
24 std::cerr << " [--socket <endpoint_path>] [--timeout t]";
25 client_parser.print_help(std::cerr);
26 std::cerr << R"RRR(
27The timeout is the time (in seconds) during which a client lock is kept.
280 (the default) means there is no timeout, and the lock is kept until the
29client unlocks or is disconnected. A client that timed out is not disconnected,
30and can still push data: the push will succeed only if there is no conflict.
31)RRR";
32 return 1;
33 }
34
35 int32_t index = 1;
36
37 std::string_view endpoint_base("joedb");
38 for (int i = 1; i < argc; i++)
39 {
40 const std::string_view v(argv[i]);
41 if (v.size() > 6 && v.compare(v.size() - 6, 6, ".joedb") == 0)
42 {
43 endpoint_base = v;
44 endpoint_base.remove_suffix(6);
45 break;
46 }
47 }
48 std::string endpoint_path = std::string(endpoint_base) + ".sock";
49
50 if (index + 1 < argc && std::strcmp(argv[index], "--socket") == 0)
51 {
52 endpoint_path = argv[index + 1];
53 index += 2;
54 }
55
56 uint32_t timeout = 0;
57 if (index + 1 < argc && std::strcmp(argv[index], "--timeout") == 0)
58 {
59 timeout = uint32_t(std::atoi(argv[index + 1]));
60 index += 2;
61 }
62
63 Client &client = client_parser.parse(argc - index, argv + index);
64
65 IO_Context_Wrapper io_context_wrapper;
66
67 std::cout << "Creating server (endpoint_path = " << endpoint_path;
68 std::cout << "; timeout = " << timeout << ")\n";
69
70 Server server
71 (
72 client,
73 io_context_wrapper.io_context,
74 endpoint_path,
75 std::chrono::seconds(timeout),
76 &std::cerr
77 );
78
79 io_context_wrapper.run();
80
81 return 0;
82 }
83}
84
85/////////////////////////////////////////////////////////////////////////////
86int main(int argc, char **argv)
87/////////////////////////////////////////////////////////////////////////////
88{
89 return joedb::main_exception_catcher(joedb::joedb_server, argc, argv);
90}
int main()
Open_Mode
Definition Open_Mode.h:8
@ write_existing_or_create_new
either write_existing or create_new depending on whether the file exists. Racy in Posix,...
int main_exception_catcher(int(*main)(int, char **), int argc, char **argv)
Catch exception from main.
Definition Blob.h:7