pars 0.2.1
Loading...
Searching...
No Matches
enqueuer.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 "pars/concept/event.h"
33#include "pars/concept/net.h"
34#include "pars/ev/dispatcher.h"
35#include "pars/ev/event.h"
36#include "pars/ev/hf_registry.h"
37#include "pars/ev/runner.h"
38
39namespace pars::ev
40{
41
43{
44public:
46 : dispatcher_m{d}
47 , runner_m{r}
48 {
49 }
50
51 template<internal_event_c event_t>
52 void queue_fire(event_t ev)
53 {
54 dispatcher_m.queue_back(fired{std::move(ev), {}});
55 }
56
57 template<template<typename> typename kind_of, network_event_c event_t,
58 network_event_c event2_t>
59 requires kind_c<kind_of>
61 {
62 queue_fire(ev, md.pipe().socket_id(), md.tool(), md.pipe());
63 }
64
65 template<network_event_c event_t, net::tool_c tool_t>
66 void queue_fire(event_t ev, const int s_id, tool_t& t, const net::pipe& p)
67 {
68 if constexpr (std::is_same_v<event_t, creating_pipe> ||
69 std::is_same_v<event_t, pipe_created> ||
70 std::is_same_v<event_t, pipe_removed>)
71 {
72 if (dispatcher_m.terminating())
73 {
74 p.close().or_abort();
75
76 return;
77 }
78
79 if constexpr (std::is_same_v<event_t, creating_pipe>)
80 runner_m.add_pipe(p);
81 else if constexpr (std::is_same_v<event_t, pipe_removed>)
82 runner_m.remove_pipe(p);
83
84 if (!runner_m.can_exec(s_id, spec<fired<event_t>>::hash))
85 return;
86 }
87
88 dispatcher_m.queue_back(fired{std::move(ev), {s_id, t, p}});
89 }
90
91 template<network_event_c event_t, net::tool_c tool_t>
92 void queue_sent(event_t ev, int s_id, tool_t& t, net::pipe p)
93 {
94 dispatcher_m.queue_back(sent{std::move(ev), {s_id, t, p}});
95 }
96
97 template<net::tool_c tool_t>
98 void queue_received(nngxx::msg m, int s_id, tool_t& t, net::pipe p)
99 {
100 dispatcher_m.queue_back(received{std::move(m), {s_id, t, p}});
101 }
102
103private:
104 dispatcher& dispatcher_m;
105 runner& runner_m;
106};
107
108} // namespace pars::ev
void queue_fire(event_t ev)
Definition enqueuer.h:52
void queue_fire(event_t ev, ev::metadata< kind_of, event2_t > md)
Definition enqueuer.h:60
void queue_sent(event_t ev, int s_id, tool_t &t, net::pipe p)
Definition enqueuer.h:92
void queue_received(nngxx::msg m, int s_id, tool_t &t, net::pipe p)
Definition enqueuer.h:98
void queue_fire(event_t ev, const int s_id, tool_t &t, const net::pipe &p)
Definition enqueuer.h:66
enqueuer(dispatcher &d, runner &r)
Definition enqueuer.h:45
clev::own< nng_msg * > msg
Definition msg.h:39
constexpr void or_abort() noexcept(clev_exception_disabled_v)
Definition err.h:106