2 * SPDX-License-Identifier: MIT
4 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * Copyright 2017-2023 Philippe Proulx <pproulx@efficios.com>
8 #ifndef BABELTRACE_PLUGINS_UTILS_MUXER_MSG_ITER_HPP
9 #define BABELTRACE_PLUGINS_UTILS_MUXER_MSG_ITER_HPP
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"
20 #include "upstream-msg-iter.hpp"
26 class MsgIter final : public bt2::UserMessageIterator<MsgIter, Comp>
28 friend bt2::UserMessageIterator<MsgIter, Comp>;
31 /* Clock class nature expectation */
32 enum class _ClkClsExpectation
37 ORIG_ISNT_UNIX_EPOCH_AND_SPEC_UUID,
38 ORIG_ISNT_UNIX_EPOCH_AND_NO_UUID,
41 /* Comparator for `_mHeap` with its own logger */
42 class _HeapComparator final
45 explicit _HeapComparator(const bt2c::Logger& logger);
47 bool operator()(const UpstreamMsgIter *upstreamMsgIterA,
48 const UpstreamMsgIter *upstreamMsgIterB) const noexcept;
51 bt2c::Logger _mLogger;
55 explicit MsgIter(bt2::SelfMessageIterator selfMsgIter,
56 bt2::SelfMessageIteratorConfiguration config,
57 bt2::SelfComponentOutputPort selfPort);
60 bool _canSeekBeginning();
61 void _seekBeginning();
62 void _next(bt2::ConstMessageArray& msgs);
65 * Makes sure `_mUpstreamMsgItersToReload` is empty so that `_mHeap`
66 * is ready for the next message selection.
68 * This may throw whatever UpstreamMsgIter::reload() may throw.
70 void _ensureFullHeap();
73 * Validates the clock class of the received message `msg`, setting
74 * the expectation if this is the first one.
76 * Throws `bt2::Error` on error.
78 void _validateMsgClkCls(bt2::ConstMessage msg);
81 * Sets the clock class expectation (`_mClkClsExpectation` and
82 * `_mExpectedClkClsUuid`) according to `clkCls`.
84 void _setClkClsExpectation(bt2::OptionalBorrowedObject<bt2::ConstClockClass> clkCls) noexcept;
87 * Checks that `clkCls` meets the current clock class expectation,
88 * throwing if it doesn't.
90 void _makeSureClkClsIsExpected(bt2::ConstMessage msg,
91 bt2::OptionalBorrowedObject<bt2::ConstClockClass> clkCls) const;
94 * Container of all the upstream message iterators.
96 * The only purpose of this is to own them; where they are below
97 * indicates their state.
99 std::vector<UpstreamMsgIter::UP> _mUpstreamMsgIters;
102 * Heap of ready-to-use upstream message iterators (pointers to
103 * owned objects in `_mUpstreamMsgIters` above).
105 bt2c::PrioHeap<UpstreamMsgIter *, _HeapComparator> _mHeap;
108 * Current upstream message iterators to reload, on which we must
109 * call reload() before moving them to `_mHeap` or to
110 * `_mEndedUpstreamMsgIters`.
112 * Using `std::vector` instead of some linked list because the
113 * typical scenario is to add a single one and then remove it
116 std::vector<UpstreamMsgIter *> _mUpstreamMsgItersToReload;
119 * Which kind of clock class to expect from any incoming message.
121 * The very first received message determines this for all the
124 * For `ORIG_ISNT_UNIX_EPOCH_AND_SPEC_UUID`, `*_mExpectedClkClsUuid`
125 * is the expected specific UUID.
127 _ClkClsExpectation _mClkClsExpectation = _ClkClsExpectation::ANY;
128 bt2s::optional<bt2c::Uuid> _mExpectedClkClsUuid;
131 } /* namespace bt2mux */
133 #endif /* BABELTRACE_PLUGINS_UTILS_MUXER_MSG_ITER_HPP */