| 1 | /* |
| 2 | * SPDX-License-Identifier: MIT |
| 3 | * |
| 4 | * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 5 | * Copyright 2017-2023 Philippe Proulx <pproulx@efficios.com> |
| 6 | */ |
| 7 | |
| 8 | #ifndef BABELTRACE_PLUGINS_UTILS_MUXER_MSG_ITER_HPP |
| 9 | #define BABELTRACE_PLUGINS_UTILS_MUXER_MSG_ITER_HPP |
| 10 | |
| 11 | #include <vector> |
| 12 | |
| 13 | #include "cpp-common/bt2/component-class-dev.hpp" |
| 14 | #include "cpp-common/bt2/optional-borrowed-object.hpp" |
| 15 | #include "cpp-common/bt2/self-message-iterator-configuration.hpp" |
| 16 | #include "cpp-common/bt2c/prio-heap.hpp" |
| 17 | #include "cpp-common/bt2c/uuid.hpp" |
| 18 | #include "cpp-common/bt2s/optional.hpp" |
| 19 | |
| 20 | #include "upstream-msg-iter.hpp" |
| 21 | |
| 22 | namespace bt2mux { |
| 23 | |
| 24 | class Comp; |
| 25 | |
| 26 | class MsgIter final : public bt2::UserMessageIterator<MsgIter, Comp> |
| 27 | { |
| 28 | friend bt2::UserMessageIterator<MsgIter, Comp>; |
| 29 | |
| 30 | private: |
| 31 | /* Clock class nature expectation */ |
| 32 | enum class _ClkClsExpectation |
| 33 | { |
| 34 | ANY, |
| 35 | NONE, |
| 36 | ORIG_IS_UNIX_EPOCH, |
| 37 | ORIG_ISNT_UNIX_EPOCH_AND_SPEC_UUID, |
| 38 | ORIG_ISNT_UNIX_EPOCH_AND_NO_UUID, |
| 39 | }; |
| 40 | |
| 41 | /* Comparator for `_mHeap` with its own logger */ |
| 42 | class _HeapComparator final |
| 43 | { |
| 44 | public: |
| 45 | explicit _HeapComparator(const bt2c::Logger& logger); |
| 46 | |
| 47 | bool operator()(const UpstreamMsgIter *upstreamMsgIterA, |
| 48 | const UpstreamMsgIter *upstreamMsgIterB) const noexcept; |
| 49 | |
| 50 | private: |
| 51 | bt2c::Logger _mLogger; |
| 52 | }; |
| 53 | |
| 54 | public: |
| 55 | explicit MsgIter(bt2::SelfMessageIterator selfMsgIter, |
| 56 | bt2::SelfMessageIteratorConfiguration config, |
| 57 | bt2::SelfComponentOutputPort selfPort); |
| 58 | |
| 59 | private: |
| 60 | bool _canSeekBeginning(); |
| 61 | void _seekBeginning(); |
| 62 | void _next(bt2::ConstMessageArray& msgs); |
| 63 | |
| 64 | /* |
| 65 | * Makes sure `_mUpstreamMsgItersToReload` is empty so that `_mHeap` |
| 66 | * is ready for the next message selection. |
| 67 | * |
| 68 | * This may throw whatever UpstreamMsgIter::reload() may throw. |
| 69 | */ |
| 70 | void _ensureFullHeap(); |
| 71 | |
| 72 | /* |
| 73 | * Validates the clock class of the received message `msg`, setting |
| 74 | * the expectation if this is the first one. |
| 75 | * |
| 76 | * Throws `bt2::Error` on error. |
| 77 | */ |
| 78 | void _validateMsgClkCls(bt2::ConstMessage msg); |
| 79 | |
| 80 | /* |
| 81 | * Sets the clock class expectation (`_mClkClsExpectation` and |
| 82 | * `_mExpectedClkClsUuid`) according to `clkCls`. |
| 83 | */ |
| 84 | void _setClkClsExpectation(bt2::OptionalBorrowedObject<bt2::ConstClockClass> clkCls) noexcept; |
| 85 | |
| 86 | /* |
| 87 | * Checks that `clkCls` meets the current clock class expectation, |
| 88 | * throwing if it doesn't. |
| 89 | */ |
| 90 | void _makeSureClkClsIsExpected(bt2::ConstMessage msg, |
| 91 | bt2::OptionalBorrowedObject<bt2::ConstClockClass> clkCls) const; |
| 92 | |
| 93 | /* |
| 94 | * Container of all the upstream message iterators. |
| 95 | * |
| 96 | * The only purpose of this is to own them; where they are below |
| 97 | * indicates their state. |
| 98 | */ |
| 99 | std::vector<UpstreamMsgIter::UP> _mUpstreamMsgIters; |
| 100 | |
| 101 | /* |
| 102 | * Heap of ready-to-use upstream message iterators (pointers to |
| 103 | * owned objects in `_mUpstreamMsgIters` above). |
| 104 | */ |
| 105 | bt2c::PrioHeap<UpstreamMsgIter *, _HeapComparator> _mHeap; |
| 106 | |
| 107 | /* |
| 108 | * Current upstream message iterators to reload, on which we must |
| 109 | * call reload() before moving them to `_mHeap` or to |
| 110 | * `_mEndedUpstreamMsgIters`. |
| 111 | * |
| 112 | * Using `std::vector` instead of some linked list because the |
| 113 | * typical scenario is to add a single one and then remove it |
| 114 | * shortly after. |
| 115 | */ |
| 116 | std::vector<UpstreamMsgIter *> _mUpstreamMsgItersToReload; |
| 117 | |
| 118 | /* |
| 119 | * Which kind of clock class to expect from any incoming message. |
| 120 | * |
| 121 | * The very first received message determines this for all the |
| 122 | * following. |
| 123 | * |
| 124 | * For `ORIG_ISNT_UNIX_EPOCH_AND_SPEC_UUID`, `*_mExpectedClkClsUuid` |
| 125 | * is the expected specific UUID. |
| 126 | * |
| 127 | * For `ORIG_ISNT_UNIX_EPOCH_AND_NO_UUID`, `_mExpectedClkCls` is the |
| 128 | * expected clock class. |
| 129 | */ |
| 130 | _ClkClsExpectation _mClkClsExpectation = _ClkClsExpectation::ANY; |
| 131 | bt2s::optional<bt2c::Uuid> _mExpectedClkClsUuid; |
| 132 | bt2::ConstClockClass::Shared _mExpectedClkCls; |
| 133 | }; |
| 134 | |
| 135 | } /* namespace bt2mux */ |
| 136 | |
| 137 | #endif /* BABELTRACE_PLUGINS_UTILS_MUXER_MSG_ITER_HPP */ |