pars 0.2.1
Loading...
Searching...
No Matches
single.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/app/setup.h"
33#include "pars/ev/dispatcher.h"
34#include "pars/ev/enqueuer.h"
35#include "pars/ev/hf_registry.h"
36#include "pars/ev/runner.h"
37#include "pars/log.h"
38
39namespace pars::app
40{
41
42template<typename component_t>
44{
45public:
46 using component_type = component_t;
48
50 : runner_m{hf_registry_m}
51 , hf_registry_m{runner_m}
52 , dispatcher_m{runner_m}
53 , router_m{dispatcher_m, runner_m}
54 , component_m{hf_registry_m, router_m}
55 {
56 }
57
58 int exec(int argc, char** argv)
59 {
60 atexit(nng_fini);
61
62 setup();
63
64 startup(argc, argv);
65
66 return run();
67 }
68
69protected:
70 component_type& comp() { return component_m; }
71
72 ev::enqueuer& router() { return router_m; }
73
74 ev::hf_registry& hfs() { return hf_registry_m; }
75
76 virtual void startup(int argc, char** argv) = 0;
77
79 {
80 dispatcher_m.stop_running();
81
82 component_m.graceful_terminate();
83
84 dispatcher_m.terminate_now();
85 }
86
87 void terminate_now() { dispatcher_m.terminate_now(); }
88
89 void stop_job_thread(const int j_id) { runner_m.stop_thread(j_id); }
90
91 int run()
92 {
93 try
94 {
95 dispatcher_m.run();
96
97 return EXIT_SUCCESS;
98 }
99 catch (std::exception& e)
100 {
101 pars::err(SL, lf::app, "Error while running single application: {}",
102 e.what());
103
104 return EXIT_FAILURE;
105 }
106 }
107
108private:
109 ev::runner runner_m;
110 ev::hf_registry hf_registry_m;
111 ev::dispatcher dispatcher_m;
112 ev::enqueuer router_m;
113 component_type component_m;
114};
115
116} // namespace pars::app
single< component_type > self_type
Definition single.h:47
component_t component_type
Definition single.h:46
ev::enqueuer & router()
Definition single.h:72
virtual void startup(int argc, char **argv)=0
void stop_job_thread(const int j_id)
Definition single.h:89
ev::hf_registry & hfs()
Definition single.h:74
component_type & comp()
Definition single.h:70
void terminate_now()
Definition single.h:87
int exec(int argc, char **argv)
Definition single.h:58
void graceful_terminate()
Definition single.h:78
#define SL
Definition log.h:58
@ app
Definition flags.h:41
void err(spdlog::source_loc loc, pars::lf lf, spdlog::format_string_t< args_t... > fmt, args_t &&... args)
Definition log.h:211