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

#include "routingtable.h"
#include "hop.h"
#include "routingtablespec.h"

namespace mbus {

RoutingTable::HopIterator::HopIterator(const std::map<string, HopBlueprint> &hops) :
    _pos(hops.begin()),
    _end(hops.end())
{ }

RoutingTable::RouteIterator::RouteIterator(const std::map<string, Route> &routes) :
    _pos(routes.begin()),
    _end(routes.end())
{ }

RoutingTable::RoutingTable(const RoutingTableSpec &spec) :
    _name(spec.getProtocol()),
    _hops(),
    _routes()
{
    for (uint32_t i = 0; i < spec.getNumHops(); ++i) {
        const HopSpec& hopSpec = spec.getHop(i);
        _hops.emplace(hopSpec.getName(), HopBlueprint(hopSpec));
    }
    for (uint32_t i = 0; i < spec.getNumRoutes(); ++i) {
        Route route;
        const RouteSpec &routeSpec = spec.getRoute(i);
        for (uint32_t j = 0; j < routeSpec.getNumHops(); ++j) {
            route.addHop(Hop(routeSpec.getHop(j)));
        }
        _routes.emplace(routeSpec.getName(), std::move(route));
    }
}

bool
RoutingTable::hasHop(const string &name) const
{
    return _hops.find(name) != _hops.end();
}

const HopBlueprint *
RoutingTable::getHop(const string &name) const
{
    auto it = _hops.find(name);
    return it != _hops.end() ? &(it->second) : nullptr;
}

bool
RoutingTable::hasRoute(const string &name) const
{
    return _routes.find(name) != _routes.end();
}

const Route *
RoutingTable::getRoute(const string &name) const
{
    auto it = _routes.find(name);
    return it != _routes.end() ? &(it->second) : nullptr;
}

} // namespace mbus