| 1 | /* |
| 2 | * SPDX-License-Identifier: GPL-2.0-only |
| 3 | * |
| 4 | * Copyright (C) 2020-2023 EfficiOS, inc. |
| 5 | */ |
| 6 | |
| 7 | #include <utility> |
| 8 | |
| 9 | #include "common/assert.h" |
| 10 | #include "cpp-common/bt2/component-class-dev.hpp" |
| 11 | #include "cpp-common/bt2/component-class.hpp" |
| 12 | #include "cpp-common/bt2/graph.hpp" |
| 13 | #include "cpp-common/bt2/plugin-load.hpp" |
| 14 | #include "cpp-common/bt2/plugin.hpp" |
| 15 | #include "cpp-common/bt2/query-executor.hpp" |
| 16 | #include "cpp-common/bt2c/call.hpp" |
| 17 | |
| 18 | #include "run-in.hpp" |
| 19 | |
| 20 | namespace { |
| 21 | |
| 22 | struct RunInData final |
| 23 | { |
| 24 | RunInCompClsQueryFunc compClsCtxFunc; |
| 25 | RunInCompClsInitFunc compCtxFunc; |
| 26 | RunInMsgIterClsInitFunc msgIterCtxFunc; |
| 27 | }; |
| 28 | |
| 29 | class RunInSource; |
| 30 | |
| 31 | class RunInSourceMsgIter final : public bt2::UserMessageIterator<RunInSourceMsgIter, RunInSource> |
| 32 | { |
| 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>(); |
| 40 | |
| 41 | if (data.msgIterCtxFunc) { |
| 42 | data.msgIterCtxFunc(self); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | void _next(bt2::ConstMessageArray&) |
| 47 | { |
| 48 | } |
| 49 | }; |
| 50 | |
| 51 | class RunInSource final : |
| 52 | public bt2::UserSourceComponent<RunInSource, RunInSourceMsgIter, const RunInData, |
| 53 | const RunInData> |
| 54 | { |
| 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); |
| 65 | |
| 66 | if (_mRunInData->compCtxFunc) { |
| 67 | _mRunInData->compCtxFunc(self); |
| 68 | } |
| 69 | } |
| 70 | |
| 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 | } |
| 78 | |
| 79 | return bt2::NullValue {}.shared(); |
| 80 | } |
| 81 | |
| 82 | private: |
| 83 | const RunInData *_mRunInData; |
| 84 | }; |
| 85 | |
| 86 | } /* namespace */ |
| 87 | |
| 88 | void runIn(RunInCompClsQueryFunc compClsCtxFunc, RunInCompClsInitFunc compCtxFunc, |
| 89 | RunInMsgIterClsInitFunc msgIterCtxFunc) |
| 90 | { |
| 91 | RunInData data {std::move(compClsCtxFunc), std::move(compCtxFunc), std::move(msgIterCtxFunc)}; |
| 92 | const auto srcCompCls = bt2::SourceComponentClass::create<RunInSource>(); |
| 93 | |
| 94 | /* Execute a query (executes `compClsCtxFunc`) */ |
| 95 | bt2::QueryExecutor::create(*srcCompCls, "object-name", data)->query(); |
| 96 | |
| 97 | /* Create graph */ |
| 98 | const auto graph = bt2::Graph::create(0); |
| 99 | |
| 100 | /* Add custom source component (executes `compCtxFunc`) */ |
| 101 | const auto srcComp = graph->addComponent(*srcCompCls, "the-source", data); |
| 102 | |
| 103 | /* Add dummy sink component */ |
| 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 | }); |
| 115 | |
| 116 | /* Connect ports */ |
| 117 | const auto outPort = srcComp.outputPorts()["out"]; |
| 118 | BT_ASSERT(outPort); |
| 119 | |
| 120 | const auto inPort = sinkComp.inputPorts()["in"]; |
| 121 | BT_ASSERT(inPort); |
| 122 | |
| 123 | graph->connectPorts(*outPort, *inPort); |
| 124 | |
| 125 | /* Run graph (executes `msgIterCtxFunc`) */ |
| 126 | graph->run(); |
| 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 | } |