| 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/query-executor.hpp" |
| 14 | |
| 15 | #include "run-in.hpp" |
| 16 | |
| 17 | namespace { |
| 18 | |
| 19 | struct RunInData final |
| 20 | { |
| 21 | RunInCompClsQueryFunc compClsCtxFunc; |
| 22 | RunInCompClsInitFunc compCtxFunc; |
| 23 | RunInMsgIterClsInitFunc msgIterCtxFunc; |
| 24 | }; |
| 25 | |
| 26 | class RunInSource; |
| 27 | |
| 28 | class RunInSourceMsgIter final : public bt2::UserMessageIterator<RunInSourceMsgIter, RunInSource> |
| 29 | { |
| 30 | public: |
| 31 | explicit RunInSourceMsgIter(const bt2::SelfMessageIterator self, |
| 32 | bt2::SelfMessageIteratorConfiguration, |
| 33 | const bt2::SelfComponentOutputPort port) : |
| 34 | bt2::UserMessageIterator<RunInSourceMsgIter, RunInSource> {self, "RUN-IN-SRC-MSG-ITER"} |
| 35 | { |
| 36 | const auto& data = port.data<const RunInData>(); |
| 37 | |
| 38 | if (data.msgIterCtxFunc) { |
| 39 | data.msgIterCtxFunc(self); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | void _next(bt2::ConstMessageArray&) |
| 44 | { |
| 45 | } |
| 46 | }; |
| 47 | |
| 48 | class RunInSource final : |
| 49 | public bt2::UserSourceComponent<RunInSource, RunInSourceMsgIter, const RunInData, |
| 50 | const RunInData> |
| 51 | { |
| 52 | public: |
| 53 | static constexpr auto name = "run-in-src"; |
| 54 | |
| 55 | explicit RunInSource(const bt2::SelfSourceComponent self, bt2::ConstMapValue, |
| 56 | const RunInData * const runInData) : |
| 57 | bt2::UserSourceComponent<RunInSource, RunInSourceMsgIter, const RunInData, |
| 58 | const RunInData> {self, "RUN-IN-SRC"}, |
| 59 | _mRunInData {runInData} |
| 60 | { |
| 61 | this->_addOutputPort("out", *runInData); |
| 62 | |
| 63 | if (_mRunInData->compCtxFunc) { |
| 64 | _mRunInData->compCtxFunc(self); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | static bt2::Value::Shared _query(const bt2::SelfComponentClass self, bt2::PrivateQueryExecutor, |
| 69 | bt2c::CStringView, bt2::ConstValue, |
| 70 | const RunInData * const data) |
| 71 | { |
| 72 | if (data->compClsCtxFunc) { |
| 73 | data->compClsCtxFunc(self); |
| 74 | } |
| 75 | |
| 76 | return bt2::NullValue {}.shared(); |
| 77 | } |
| 78 | |
| 79 | private: |
| 80 | const RunInData *_mRunInData; |
| 81 | }; |
| 82 | |
| 83 | class DummySink : public bt2::UserSinkComponent<DummySink> |
| 84 | { |
| 85 | public: |
| 86 | static constexpr auto name = "dummy"; |
| 87 | |
| 88 | explicit DummySink(const bt2::SelfSinkComponent self, bt2::ConstMapValue, void *) : |
| 89 | bt2::UserSinkComponent<DummySink>(self, "DUMMY-SINK") |
| 90 | { |
| 91 | this->_addInputPort("in"); |
| 92 | } |
| 93 | |
| 94 | void _graphIsConfigured() |
| 95 | { |
| 96 | _mMsgIter = this->_createMessageIterator(this->_inputPorts()["in"]); |
| 97 | } |
| 98 | |
| 99 | bool _consume() |
| 100 | { |
| 101 | return _mMsgIter->next().has_value(); |
| 102 | } |
| 103 | |
| 104 | private: |
| 105 | bt2::MessageIterator::Shared _mMsgIter; |
| 106 | }; |
| 107 | |
| 108 | } /* namespace */ |
| 109 | |
| 110 | void runIn(RunInCompClsQueryFunc compClsCtxFunc, RunInCompClsInitFunc compCtxFunc, |
| 111 | RunInMsgIterClsInitFunc msgIterCtxFunc) |
| 112 | { |
| 113 | RunInData data {std::move(compClsCtxFunc), std::move(compCtxFunc), std::move(msgIterCtxFunc)}; |
| 114 | const auto srcCompCls = bt2::SourceComponentClass::create<RunInSource>(); |
| 115 | |
| 116 | /* Execute a query (executes `compClsCtxFunc`) */ |
| 117 | bt2::QueryExecutor::create(*srcCompCls, "object-name", data)->query(); |
| 118 | |
| 119 | /* Create graph */ |
| 120 | const auto graph = bt2::Graph::create(0); |
| 121 | |
| 122 | /* Add custom source component (executes `compCtxFunc`) */ |
| 123 | const auto srcComp = graph->addComponent(*srcCompCls, "the-source", data); |
| 124 | |
| 125 | /* Add dummy sink component */ |
| 126 | const auto sinkComp = |
| 127 | graph->addComponent(*bt2::SinkComponentClass::create<DummySink>(), "the-sink"); |
| 128 | |
| 129 | /* Connect ports */ |
| 130 | const auto outPort = srcComp.outputPorts()["out"]; |
| 131 | BT_ASSERT(outPort); |
| 132 | |
| 133 | const auto inPort = sinkComp.inputPorts()["in"]; |
| 134 | BT_ASSERT(inPort); |
| 135 | |
| 136 | graph->connectPorts(*outPort, *inPort); |
| 137 | |
| 138 | /* Run graph (executes `msgIterCtxFunc`) */ |
| 139 | graph->run(); |
| 140 | } |
| 141 | |
| 142 | void runInCompClsQuery(RunInCompClsQueryFunc func) |
| 143 | { |
| 144 | runIn(std::move(func), nullptr, nullptr); |
| 145 | } |
| 146 | |
| 147 | void runInCompClsInit(RunInCompClsInitFunc func) |
| 148 | { |
| 149 | runIn(nullptr, std::move(func), nullptr); |
| 150 | } |
| 151 | |
| 152 | void runInMsgIterClsInit(RunInMsgIterClsInitFunc func) |
| 153 | { |
| 154 | runIn(nullptr, nullptr, std::move(func)); |
| 155 | } |