aboutsummaryrefslogtreecommitdiffstats
path: root/messagebus/src/vespa/messagebus/routing/routingtablespec.cpp
blob: f62431a19f08d214e1252a116cd48ed14ac5f55f (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
// 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 {

RoutingTableSpec::RoutingTableSpec(const string &protocol) :
    _protocol(protocol),
    _hops(),
    _routes()
{ }

RoutingTableSpec::RoutingTableSpec(const RoutingTableSpec&) = default;

RoutingTableSpec::~RoutingTableSpec() = default;

RoutingTableSpec& RoutingTableSpec::operator=(const RoutingTableSpec&) = default;

RoutingTableSpec &
RoutingTableSpec::addHop(HopSpec && hop) & {
    _hops.emplace_back(std::move(hop));
    return *this;
}
RoutingTableSpec &&
RoutingTableSpec::addHop(HopSpec && hop) && {
    _hops.emplace_back(std::move(hop));
    return std::move(*this);
}

RoutingTableSpec &
RoutingTableSpec::setHop(uint32_t i, HopSpec &&hop) {
    _hops[i] = std::move(hop);
    return *this;
}

RoutingTableSpec &
RoutingTableSpec::addRoute(RouteSpec &&route) & {
    _routes.emplace_back(std::move(route));
    return *this;
}

RoutingTableSpec &&
RoutingTableSpec::addRoute(RouteSpec &&route) && {
    _routes.emplace_back(std::move(route));
    return std::move(*this);
}

void
RoutingTableSpec::toConfig(string &cfg, const string &prefix) const
{
    cfg.append(prefix).append("protocol ").append(RoutingSpec::toConfigString(_protocol)).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) {
            _hops[i].toConfig(cfg, make_string("%shop[%d].", prefix.c_str(), i));
        }
    }
    uint32_t numRoutes = _routes.size();
    if (numRoutes > 0) {
        cfg.append(prefix).append("route[").append(make_string("%d", numRoutes)).append("]\n");
        for (uint32_t i = 0; i < numRoutes; ++i) {
            _routes[i].toConfig(cfg, make_string("%sroute[%d].", prefix.c_str(), i));
        }
    }
}

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

bool
RoutingTableSpec::operator==(const RoutingTableSpec &rhs) const
{
    if (_protocol != rhs._protocol) {
        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;
        }
    }
    if (_routes.size() != rhs._routes.size()) {
        return false;
    }
    for (uint32_t i = 0, len = _routes.size(); i < len; ++i) {
        if (_routes[i] != rhs._routes[i]) {
            return false;
        }
    }
    return true;
}


} // namespace mbus