pars 0.2.1.99
Loading...
Searching...
No Matches
context_registry.h
Go to the documentation of this file.
1/*
2Copyright (c) 2025 Giuseppe Roberti.
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without modification,
6are permitted provided that the following conditions are met:
7
81. Redistributions of source code must retain the above copyright notice, this
9list of conditions and the following disclaimer.
10
112. Redistributions in binary form must reproduce the above copyright notice,
12this list of conditions and the following disclaimer in the documentation and/or
13other materials provided with the distribution.
14
153. Neither the name of the copyright holder nor the names of its contributors
16may be used to endorse or promote products derived from this software without
17specific prior written permission.
18
19THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29*/
30#pragma once
31
32#include "nngxx/ctx.h"
33
34#include "pars/net/context.h"
35#include "pars/net/socket.h"
36#include "pars/net/tool_view.h"
37
38#include <format>
39#include <tuple>
40#include <unordered_map>
41
42namespace pars::net
43{
44
46{
47public:
49 : router_m{r}
50 , sock_m{s}
51 {
52 }
53
54 void stop_all()
55 {
56 for (auto& c : ctx_map_m)
57 c.second.stop();
58 }
59
61 {
62 auto ctx = sock_m.make_ctx();
63
64 auto id = ctx.id();
65
66 auto res = ctx_map_m.try_emplace(id, router_m, std::move(ctx), sock_m);
67
68 if (!res.second)
69 throw std::runtime_error("Unable to emplace a context");
70
71 return res.first->second;
72 }
73
75 {
76 if (t.type() != typeid(nngxx::ctx_view))
77 throw std::runtime_error("We need a context here");
78
79 if (ctx_map_m.find(t.id()) == ctx_map_m.end())
80 throw std::runtime_error(std::format("Unknown context {}", t.id()));
81
82 return ctx_map_m.at(t.id());
83 }
84
85 void start_recv(int num_ctxs)
86 {
87 for (int i = 0; i < num_ctxs; ++i)
88 emplace().recv();
89 }
90
91private:
92 ev::enqueuer& router_m;
93 socket& sock_m;
94 std::unordered_map<int, context> ctx_map_m;
95};
96
97} // namespace pars::net
void start_recv(int num_ctxs)
context & of(const net::tool_view &t)
context_registry(ev::enqueuer &r, net::socket &s)
Represents an nng_socket.
Definition socket.h:71
Represents an nng_socket or nng_ctx view.
Definition tool_view.h:48
int id() const
The id of the underlying variant.
Definition tool_view.h:72
const std::type_info & type() const
Get the std::type_info of the underlying variant.
Definition tool_view.h:63
clev::iface< nng_ctx > ctx_view
Definition ctx.h:37