aboutsummaryrefslogtreecommitdiffstats
path: root/messagebus/src/vespa/messagebus/routing/route.h
blob: be64b33901fc2fbc0d1f30e0ec19cc5552e6f288 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once

#include "hop.h"

namespace mbus {

class Hop;

/**
 * A route is a list of {@link Hop hops} that are resolved from first to last as a routable moves from source to
 * destination. A route may be changed at any time be either application logic or an invoked {@link RoutingPolicy}, so
 * no guarantees on actual path can be given without the full knowledge of all that logic.
 *
 * To construct a route you may either use the factory method {@link this#parse(String)} to produce a route instance
 * from a string representation, or you may build one programatically through the hop accessors.
 */
class Route {
private:
    std::vector<Hop> _hops;

public:
    /**
     * Convenience typedef for an auto-pointer to a route.
     */
    using UP = std::unique_ptr<Route>;

    /**
     * Parses the given string as a list of space-separated hops. The {@link this#toString()} method is compatible with
     * this parser.
     *
     * @param route The string to parse.
     * @return A route that corresponds to the string.
     */
    static Route parse(vespalib::stringref route);

    /**
     * Create a Route that contains no hops
     */
    Route();
    Route(const Route &) = default;
    Route & operator = (const Route &) = default;
    Route(Route &&) noexcept = default;
    Route & operator = (Route && ) noexcept = default;
    ~Route();

    /**
     * Constructs a route that contains the given hops.
     *
     * @param hops The hops to instantiate with.
     */
    explicit Route(std::vector<Hop> hops);

    /**
     * Returns whether or not there are any hops in this route.
     *
     * @return True if there is at least one hop.
     */
    [[nodiscard]] bool hasHops() const { return !_hops.empty(); }

    /**
     * Returns the number of hops that make up this route.
     *
     * @return The number of hops.
     */
    [[nodiscard]] uint32_t getNumHops() const { return _hops.size(); }

    /**
     * Returns the hop at the given index.
     *
     * @param i The index of the hop to return.
     * @return The hop.
     */
    Hop &getHop(uint32_t i) { return _hops[i]; }

    /**
     * Returns a const reference to the hop at the given index.
     *
     * @param i The index of the hop to return.
     * @return The hop.
     */
    [[nodiscard]] const Hop &getHop(uint32_t i) const { return _hops[i]; }

    /**
     * Adds a hop to the list of hops that make up this route.
     *
     * @param hop The hop to add.
     * @return This, to allow chaining.
     */
    Route &addHop(Hop hop);

    /**
     * Sets the hop at a given index.
     *
     * @param i   The index at which to set the hop.
     * @param hop The hop to set.
     * @return This, to allow chaining.
     */
    Route &setHop(uint32_t i, Hop hop);

    /**
     * Removes the hop at a given index.
     *
     * @param i The index of the hop to remove.
     * @return The hop removed.
     */
    Hop removeHop(uint32_t i);

    /**
     * Returns a string representation of this route.
     *
     * @return A string representation.
     */
    [[nodiscard]] string toString() const;

    /**
     * Returns a string representation of this that can be debugged but not parsed.
     *
     * @return The debug string.
     */
    [[nodiscard]] string toDebugString() const;
};

} // namespace mbus