aboutsummaryrefslogtreecommitdiffstats
path: root/messagebus/src/vespa/messagebus/routing/routespec.cpp
blob: f9c0eb701b7b99d581e45e1174772ad7dd65c68a (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "routingspec.h"
#include <vespa/vespalib/util/stringfmt.h>

using vespalib::make_string;

namespace mbus {

RouteSpec::RouteSpec(const string &name) noexcept :
    _name(name),
    _hops()
{ }

RouteSpec::RouteSpec(const RouteSpec &) = default;
RouteSpec & RouteSpec::operator = (const RouteSpec &) = default;
RouteSpec::RouteSpec(RouteSpec &&) noexcept = default;
RouteSpec & RouteSpec::operator = (RouteSpec &&) noexcept = default;

RouteSpec::~RouteSpec() = default;

RouteSpec &
RouteSpec::addHop(const string &hop) & {
    _hops.push_back(hop);
    return *this;
}

RouteSpec &&
RouteSpec::addHop(const string &hop) && {
    _hops.push_back(hop);
    return std::move(*this);
}

RouteSpec &
RouteSpec::setHop(uint32_t i, const string &hop) {
    _hops[i] = hop;
    return *this;
}

void
RouteSpec::toConfig(string &cfg, const string &prefix) const
{
    cfg.append(prefix).append("name ").append(RoutingSpec::toConfigString(_name)).append("\n");
    uint32_t numHops = _hops.size();
    if (numHops > 0) {
        cfg.append(prefix).append("hop[").append(make_string("%d", numHops)).append("]\n");
        for (uint32_t i = 0; i < numHops; ++i) {
            cfg.append(prefix).append("hop[").append(make_string("%d", i)).append("] ");
            cfg.append(RoutingSpec::toConfigString(_hops[i])).append("\n");
        }
    }
}

string
RouteSpec::toString() const
{
    string ret = "";
    toConfig(ret, "");
    return ret;
}

bool
RouteSpec::operator==(const RouteSpec &rhs) const
{
    if (_name != rhs._name) {
        return false;
    }
    if (_hops.size() != rhs._hops.size()) {
        return false;
    }
    for (uint32_t i = 0, len = _hops.size(); i < len; ++i) {
        if (_hops[i] != rhs._hops[i]) {
            return false;
        }
    }
    return true;
}

} // namespace mbus