aboutsummaryrefslogtreecommitdiffstats
path: root/messagebus/src/vespa/messagebus/routing/routingtable.h
blob: d3e656224cb710b2593cb4ce23c994e28c708943 (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once

#include <map>
#include <string>
#include "hopblueprint.h"
#include "route.h"

namespace mbus {

class RoutingTableSpec;
class IProtocol;
class INetwork;
class IReplyHandler;
class Message;

/**
 * At any time there may only ever be zero or one routing table registered in message bus for each protocol. This class
 * contains a list of named hops and routes that may be used to substitute references to these during route resolving.
 *
 * @author Simon Thoresen Hult
 * @version $Id$
 */
class RoutingTable {
private:
    string                         _name;
    std::map<string, HopBlueprint> _hops;
    std::map<string, Route>        _routes;

public:
    /**
     * Implements an iterator for the hops contained in this table.
     */
    class HopIterator {
    private:
        std::map<string, HopBlueprint>::const_iterator _pos, _end;

    public:
        HopIterator(const std::map<string, HopBlueprint> &hops);

        bool isValid()               { return _pos != _end; }
        void next()                  { ++_pos; }
        const string &getName() { return _pos->first; }
        const HopBlueprint &getHop() { return _pos->second; }
    };

    /**
     * Implements an iterator for the routes contained in this table.
     */
    class RouteIterator {
    private:
        std::map<string, Route>::const_iterator _pos, _end;

    public:
        RouteIterator(const std::map<string, Route> &hops);

        bool isValid()               { return _pos != _end; }
        void next()                  { ++_pos; }
        const string &getName() { return _pos->first; }
        const Route &getRoute()      { return _pos->second; }
    };

    /**
     * Convenience typedef for a shared pointer to a RoutingTable object.
     */
    using SP = std::shared_ptr<RoutingTable>;
    RoutingTable(const RoutingTable &) = delete;
    RoutingTable & operator = (const RoutingTable &) = delete;

    /**
     * Creates a new routing table based on a given specification. This also verifies the integrity of the table.
     *
     * @param spec The specification to use.
     */
    RoutingTable(const RoutingTableSpec &spec);

    /**
     * Returns whether or not there are any hops in this routing table.
     *
     * @return True if there is at least one hop.
     */
    bool hasHops() const { return !_hops.empty(); }

    /**
     * Returns the number of hops that are contained in this.
     *
     * @return The number of hops.
     */
    uint32_t getNumHops() const { return _hops.size(); }

    /**
     * Returns whether or not there exists a named hop in this.
     *
     * @param name The name of the hop to look for.
     * @return True if the named hop exists.
     */
    bool hasHop(const string &name) const;

    /**
     * Returns the named hop, may be null.
     *
     * @param name The name of the hop to return.
     * @return The hop implementation object.
     */
    const HopBlueprint *getHop(const string &name) const;

    /**
     * Returns an iterator for the hops of this.
     *
     * @return An iterator.
     */
    HopIterator getHopIterator() const { return HopIterator(_hops); }

    /**
     * Returns whether or not there are any routes in this routing table.
     *
     * @return True if there is at least one route.
     */
    bool hasRoutes() const { return !_routes.empty(); }

    /**
     * Returns the number of routes that are contained in this.
     *
     * @return The number of routes.
     */
    uint32_t getNumRoutes() const { return _routes.size(); }

    /**
     * Returns whether or not there exists a named route in this.
     *
     * @param name The name of the route to look for.
     * @return True if the named route exists.
     */
    bool hasRoute(const string &name) const;

    /**
     * Returns the named route, may be null.
     *
     * @param name The name of the route to return.
     * @return The route implementation object.
     */
    const Route *getRoute(const string &name) const;

    /**
     * Returns an iterator for the routes of this.
     *
     * @return An iterator.
     */
    RouteIterator getRouteIterator() const { return RouteIterator(_routes); }
};

} // namespace mbus