Commit | Line | Data |
---|---|---|
484a3024 SM |
1 | /* |
2 | * SPDX-License-Identifier: GPL-2.0-only | |
3 | * | |
4 | * Copyright (C) 2020-2023 EfficiOS, inc. | |
5 | */ | |
6 | ||
7 | #include <utility> | |
8 | ||
484a3024 | 9 | #include "common/assert.h" |
402b595c SM |
10 | #include "cpp-common/bt2/component-class-dev.hpp" |
11 | #include "cpp-common/bt2/component-class.hpp" | |
50761a49 | 12 | #include "cpp-common/bt2/graph.hpp" |
0133a2ba SM |
13 | #include "cpp-common/bt2/plugin-load.hpp" |
14 | #include "cpp-common/bt2/plugin.hpp" | |
5ecc1dc9 | 15 | #include "cpp-common/bt2/query-executor.hpp" |
0133a2ba | 16 | #include "cpp-common/bt2c/call.hpp" |
484a3024 | 17 | |
c802cacb SM |
18 | #include "run-in.hpp" |
19 | ||
cc610c52 PP |
20 | namespace { |
21 | ||
484a3024 SM |
22 | struct RunInData final |
23 | { | |
24 | RunInCompClsQueryFunc compClsCtxFunc; | |
25 | RunInCompClsInitFunc compCtxFunc; | |
26 | RunInMsgIterClsInitFunc msgIterCtxFunc; | |
27 | }; | |
28 | ||
402b595c | 29 | class RunInSource; |
484a3024 | 30 | |
402b595c | 31 | class RunInSourceMsgIter final : public bt2::UserMessageIterator<RunInSourceMsgIter, RunInSource> |
484a3024 | 32 | { |
402b595c SM |
33 | public: |
34 | explicit RunInSourceMsgIter(const bt2::SelfMessageIterator self, | |
35 | bt2::SelfMessageIteratorConfiguration, | |
36 | const bt2::SelfComponentOutputPort port) : | |
37 | bt2::UserMessageIterator<RunInSourceMsgIter, RunInSource> {self, "RUN-IN-SRC-MSG-ITER"} | |
38 | { | |
39 | const auto& data = port.data<const RunInData>(); | |
484a3024 | 40 | |
402b595c SM |
41 | if (data.msgIterCtxFunc) { |
42 | data.msgIterCtxFunc(self); | |
43 | } | |
484a3024 SM |
44 | } |
45 | ||
402b595c SM |
46 | void _next(bt2::ConstMessageArray&) |
47 | { | |
484a3024 | 48 | } |
402b595c | 49 | }; |
484a3024 | 50 | |
402b595c SM |
51 | class RunInSource final : |
52 | public bt2::UserSourceComponent<RunInSource, RunInSourceMsgIter, const RunInData, | |
53 | const RunInData> | |
484a3024 | 54 | { |
402b595c SM |
55 | public: |
56 | static constexpr auto name = "run-in-src"; | |
57 | ||
58 | explicit RunInSource(const bt2::SelfSourceComponent self, bt2::ConstMapValue, | |
59 | const RunInData * const runInData) : | |
60 | bt2::UserSourceComponent<RunInSource, RunInSourceMsgIter, const RunInData, | |
61 | const RunInData> {self, "RUN-IN-SRC"}, | |
62 | _mRunInData {runInData} | |
63 | { | |
64 | this->_addOutputPort("out", *runInData); | |
484a3024 | 65 | |
402b595c SM |
66 | if (_mRunInData->compCtxFunc) { |
67 | _mRunInData->compCtxFunc(self); | |
68 | } | |
484a3024 SM |
69 | } |
70 | ||
402b595c SM |
71 | static bt2::Value::Shared _query(const bt2::SelfComponentClass self, bt2::PrivateQueryExecutor, |
72 | bt2c::CStringView, bt2::ConstValue, | |
73 | const RunInData * const data) | |
74 | { | |
75 | if (data->compClsCtxFunc) { | |
76 | data->compClsCtxFunc(self); | |
77 | } | |
484a3024 | 78 | |
402b595c SM |
79 | return bt2::NullValue {}.shared(); |
80 | } | |
484a3024 | 81 | |
402b595c SM |
82 | private: |
83 | const RunInData *_mRunInData; | |
08b4db41 SM |
84 | }; |
85 | ||
cc610c52 PP |
86 | } /* namespace */ |
87 | ||
484a3024 SM |
88 | void runIn(RunInCompClsQueryFunc compClsCtxFunc, RunInCompClsInitFunc compCtxFunc, |
89 | RunInMsgIterClsInitFunc msgIterCtxFunc) | |
90 | { | |
91 | RunInData data {std::move(compClsCtxFunc), std::move(compCtxFunc), std::move(msgIterCtxFunc)}; | |
402b595c | 92 | const auto srcCompCls = bt2::SourceComponentClass::create<RunInSource>(); |
484a3024 SM |
93 | |
94 | /* Execute a query (executes `compClsCtxFunc`) */ | |
5ecc1dc9 | 95 | bt2::QueryExecutor::create(*srcCompCls, "object-name", data)->query(); |
484a3024 SM |
96 | |
97 | /* Create graph */ | |
50761a49 | 98 | const auto graph = bt2::Graph::create(0); |
484a3024 SM |
99 | |
100 | /* Add custom source component (executes `compCtxFunc`) */ | |
50761a49 | 101 | const auto srcComp = graph->addComponent(*srcCompCls, "the-source", data); |
484a3024 | 102 | |
08b4db41 | 103 | /* Add dummy sink component */ |
0133a2ba SM |
104 | const auto sinkComp = bt2c::call([&] { |
105 | const auto utilsPlugin = bt2::findPlugin("utils"); | |
106 | ||
107 | BT_ASSERT(utilsPlugin); | |
108 | ||
109 | const auto dummySinkCompCls = utilsPlugin->sinkComponentClasses()["dummy"]; | |
110 | ||
111 | BT_ASSERT(dummySinkCompCls); | |
112 | ||
113 | return graph->addComponent(*dummySinkCompCls, "the-sink"); | |
114 | }); | |
484a3024 SM |
115 | |
116 | /* Connect ports */ | |
50761a49 SM |
117 | const auto outPort = srcComp.outputPorts()["out"]; |
118 | BT_ASSERT(outPort); | |
484a3024 | 119 | |
50761a49 SM |
120 | const auto inPort = sinkComp.inputPorts()["in"]; |
121 | BT_ASSERT(inPort); | |
484a3024 | 122 | |
50761a49 | 123 | graph->connectPorts(*outPort, *inPort); |
484a3024 SM |
124 | |
125 | /* Run graph (executes `msgIterCtxFunc`) */ | |
50761a49 | 126 | graph->run(); |
484a3024 SM |
127 | } |
128 | ||
129 | void runInCompClsQuery(RunInCompClsQueryFunc func) | |
130 | { | |
131 | runIn(std::move(func), nullptr, nullptr); | |
132 | } | |
133 | ||
134 | void runInCompClsInit(RunInCompClsInitFunc func) | |
135 | { | |
136 | runIn(nullptr, std::move(func), nullptr); | |
137 | } | |
138 | ||
139 | void runInMsgIterClsInit(RunInMsgIterClsInitFunc func) | |
140 | { | |
141 | runIn(nullptr, nullptr, std::move(func)); | |
142 | } |