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

HopSpec
RoutingTableSpec::removeHop(uint32_t i)
{
    HopSpec ret = _hops[i];
    _hops.erase(_hops.begin() + i);
    return ret;
}

RouteSpec
RoutingTableSpec::removeRoute(uint32_t i)
{
    RouteSpec ret = _routes[i];
    _routes.erase(_routes.begin() + i);
    return ret;
}

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