aboutsummaryrefslogtreecommitdiffstats
path: root/messagebus/src/vespa/messagebus/routing/hop.cpp
blob: fbc6b1d3dae0889ca61d739763cd71a26fd3e8cd (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "hop.h"
#include "routeparser.h"

namespace mbus {

Hop::Hop() :
    _selector(),
    _ignoreResult(false)
{ }

Hop::Hop(const string &selector) :
    _selector(),
    _ignoreResult(false)
{
    Hop hop = parse(selector);
    _selector.swap(hop._selector);
    _ignoreResult = hop._ignoreResult;
}

Hop::Hop(std::vector<IHopDirective::SP> selector, bool ignoreResult) :
    _selector(std::move(selector)),
    _ignoreResult(ignoreResult)
{ }

Hop::Hop(const Hop &) = default;
Hop & Hop::operator = (const Hop &) = default;
Hop::~Hop() = default;

Hop &
Hop::addDirective(IHopDirective::SP dir)
{
    _selector.emplace_back(std::move(dir));
    return *this;
}

Hop &
Hop::setDirective(uint32_t i, IHopDirective::SP dir)
{
    _selector[i] = std::move(dir);
    return *this;
}

Hop
Hop::parse(const string &hop)
{
    return RouteParser::createHop(hop);
}

bool
Hop::matches(const Hop &hop) const
{
    if (_selector.size() != hop.getNumDirectives()) {
        return false;
    }
    for (uint32_t i = 0; i < hop.getNumDirectives(); ++i) {
        if (!_selector[i]->matches(hop.getDirective(i))) {
            return false;
        }
    }
    return true;
}

string
Hop::toDebugString() const
{
    string ret = "Hop(selector = { ";
    for (uint32_t i = 0; i < _selector.size(); ++i) {
        ret.append(_selector[i]->toDebugString());
        if (i < _selector.size() - 1) {
            ret.append(", ");
        }
    }
    ret.append(" }, ignoreResult = ");
    ret.append(_ignoreResult ? "true" : "false");
    ret.append(")");
    return ret;
}

string
Hop::toString() const
{
    string ret = _ignoreResult ? "?" : "";
    ret.append(toString(0, _selector.size()));
    return ret;
}

string
Hop::toString(uint32_t fromIncluding, uint32_t toNotIncluding) const
{
    string ret = "";
    for (uint32_t i = fromIncluding; i < toNotIncluding; ++i) {
        ret.append(_selector[i]->toString());
        if (i < toNotIncluding - 1) {
            ret.append("/");
        }
    }
    return ret;
}

string
Hop::getPrefix(uint32_t toNotIncluding) const
{
    if (toNotIncluding > 0) {
        return toString(0, toNotIncluding) + "/";
    }
    return "";
}

string
Hop::getSuffix(uint32_t fromNotIncluding) const
{
    if (fromNotIncluding < _selector.size() - 1) {
        string ret = "/";
        ret.append(toString(fromNotIncluding + 1, _selector.size()));
        return ret;
    }
    return "";
}

} // namespace mbus