aboutsummaryrefslogtreecommitdiffstats
path: root/messagebus/src/vespa/messagebus/routing/hopspec.cpp
blob: 64f7b8e01dcd83602e04e21dd6d40384704b2f54 (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
// Copyright Vespa.ai. 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 {

HopSpec::HopSpec(const string &name, const string &selector) :
    _name(name),
    _selector(selector),
    _recipients(),
    _ignoreResult(false)
{ }

HopSpec::HopSpec(const HopSpec & rhs) = default;
HopSpec & HopSpec::operator=(const HopSpec & rhs) = default;
HopSpec::HopSpec(HopSpec && rhs) noexcept = default;
HopSpec & HopSpec::operator=(HopSpec && rhs) noexcept = default;
HopSpec::~HopSpec() = default;

HopSpec &
HopSpec::addRecipient(const string &recipient) & {
    _recipients.push_back(recipient);
    return *this;
}

HopSpec &&
HopSpec::addRecipient(const string &recipient) && {
    _recipients.push_back(recipient);
    return std::move(*this);
}

void
HopSpec::toConfig(string &cfg, const string &prefix) const
{
    cfg.append(prefix).append("name ").append(RoutingSpec::toConfigString(_name)).append("\n");
    cfg.append(prefix).append("selector ").append(RoutingSpec::toConfigString(_selector)).append("\n");
    if (_ignoreResult) {
        cfg.append(prefix).append("ignoreresult true\n");
    }
    uint32_t numRecipients = _recipients.size();
    if (numRecipients > 0) {
        cfg.append(prefix).append("recipient[").append(make_string("%d", numRecipients)).append("]\n");
        for (uint32_t i = 0; i < numRecipients; ++i) {
            cfg.append(prefix).append("recipient[").append(make_string("%d", i)).append("] ");
            cfg.append(RoutingSpec::toConfigString(_recipients[i])).append("\n");
        }
    }
}

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

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


} // namespace mbus