-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnpnameserver.cpp
More file actions
93 lines (74 loc) · 2.78 KB
/
npnameserver.cpp
File metadata and controls
93 lines (74 loc) · 2.78 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
// Copyright (c) 2021-2025, Nikita Pennie <nikitapnn1@gmail.com>
// SPDX-License-Identifier: MIT
#include <iostream>
#include <memory>
#include <unordered_map>
#include <nprpc_nameserver.hpp>
#include <boost/asio/signal_set.hpp>
#include <boost/beast/core/error.hpp>
#include <boost/format.hpp>
#ifndef NPRPC_NAMESERVER_LOG
# define NPRPC_NAMESERVER_LOG 0
#endif
class NameserverImpl : public nprpc::common::INameserver_Servant
{
std::unordered_map<std::string, std::unique_ptr<nprpc::Object>> objects_;
public:
void Bind(nprpc::Object* obj, nprpc::flat::Span<char> name) override
{
if constexpr (NPRPC_NAMESERVER_LOG) {
std::cout << boost::format("Binding object: %1%\n") % obj->object_id();
std::cout << boost::format(" Object will be bound as: %1%\n") % (std::string_view)name;
}
auto const str = std::string((std::string_view)name);
objects_[str] = std::move(std::unique_ptr<nprpc::Object>(obj));
}
bool Resolve(nprpc::flat::Span<char> name,
nprpc::detail::flat::ObjectId_Direct obj) override
{
auto const str = std::string((std::string_view)name);
auto found = objects_.find(str);
if (found == objects_.end()) {
obj.object_id() = nprpc::invalid_object_id;
return false;
}
const auto& oid = found->second->get_data();
nprpc::detail::helpers::ObjectId::to_flat(obj, oid);
if constexpr (NPRPC_NAMESERVER_LOG) {
std::cout << boost::format("Resolved object: %1%\n") % obj.object_id();
std::cout << boost::format(" Object is resolved as: %1%\n") % str;
}
return true;
}
};
int main()
{
NameserverImpl server;
boost::asio::io_context ioc;
try {
auto rpc = nprpc::RpcBuilder()
.set_log_level(nprpc::LogLevel::error)
.with_hostname("localhost")
.with_tcp(15000)
.with_http(15001)
.allow_origins({"https://localhost:24443"})
.ssl("/home/nikita/projects/nprpc/certs/out/localhost.crt",
"/home/nikita/projects/nprpc/certs/out/localhost.key")
.build();
auto poa = nprpc::PoaBuilder(rpc)
.with_max_objects(1)
.with_object_id_policy( nprpc::PoaPolicy::ObjectIdPolicy::UserSupplied) .with_lifespan(nprpc::PoaPolicy::Lifespan::Persistent)
.build();
auto oid = poa->activate_object_with_id(
0, &server, nprpc::ObjectActivationFlags::all);
boost::asio::signal_set signals(rpc->ioc(), SIGINT, SIGTERM);
signals.async_wait(
[&](boost::beast::error_code const&, int) { rpc->ioc().stop(); });
rpc->run();
rpc->destroy();
} catch (const std::exception& e) {
std::cerr << boost::format("Nameserver failed: %1%\n") % e.what();
return 1;
}
return 0;
}