aboutsummaryrefslogtreecommitdiffstats
path: root/messagebus/src/vespa/messagebus/testlib/simpleprotocol.cpp
blob: 7eda0cd7f7fe30f2340ec4e513fda75a0dec3327 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "simpleprotocol.h"
#include "simplemessage.h"
#include "simplereply.h"
#include <vespa/messagebus/emptyreply.h>
#include <vespa/messagebus/routing/routingcontext.h>

namespace mbus {

const string SimpleProtocol::NAME("Simple");
const uint32_t SimpleProtocol::MESSAGE(1);
const uint32_t SimpleProtocol::REPLY(2);

class AllPolicy : public IRoutingPolicy {
public:
    void select(RoutingContext &ctx) override {
        std::vector<Route> recipients;
        ctx.getMatchedRecipients(recipients);
        ctx.addChildren(recipients);
    }

    void merge(RoutingContext &ctx) override {
        SimpleProtocol::simpleMerge(ctx);
    }
};

class AllPolicyFactory : public SimpleProtocol::IPolicyFactory {
public:
    IRoutingPolicy::UP create(const string &) override {
        return std::make_unique<AllPolicy>();
    }
    ~AllPolicyFactory() override;
};

AllPolicyFactory::~AllPolicyFactory() = default;

class HashPolicy : public IRoutingPolicy {
public:
    void select(RoutingContext &ctx) override {
        std::vector<Route> recipients;
        ctx.getMatchedRecipients(recipients);
        if (!recipients.empty()) {
            int i = dynamic_cast<const SimpleMessage&>(ctx.getMessage()).getHash();
            ctx.addChild(recipients[std::abs(i) % recipients.size()]);
        }
    }

    void merge(RoutingContext &ctx) override {
        SimpleProtocol::simpleMerge(ctx);
    }
};

class HashPolicyFactory : public SimpleProtocol::IPolicyFactory {
public:
    IRoutingPolicy::UP create(const string &) override {
        return std::make_unique<HashPolicy>();
    }
    ~HashPolicyFactory() override;
};

HashPolicyFactory::~HashPolicyFactory() = default;

SimpleProtocol::SimpleProtocol() :
    _policies()
{
    addPolicyFactory("All", std::make_unique<AllPolicyFactory>());
    addPolicyFactory("Hash", std::make_unique<HashPolicyFactory>());
}

SimpleProtocol::~SimpleProtocol() = default;

void
SimpleProtocol::addPolicyFactory(const string &name, IPolicyFactory::SP factory)
{
    _policies.emplace(name, std::move(factory));
}

const string &
SimpleProtocol::getName() const
{
    return NAME;
}

IRoutingPolicy::UP
SimpleProtocol::createPolicy(const string &name, const string &param) const
{
    auto it = _policies.find(name);
    if (it != _policies.end()) {
        return it->second->create(param);
    }
    return {};
}

Blob
SimpleProtocol::encode(const vespalib::Version &version, const Routable &routable) const
{
    (void)version;
    if (routable.getType() == MESSAGE) {
        string str = "M";
        str.append(dynamic_cast<const SimpleMessage&>(routable).getValue());
        Blob ret(str.size());
        memcpy(ret.data(), str.data(), str.size());
        return ret;
    } else if (routable.getType() == REPLY) {
        string str = "R";
        str.append(dynamic_cast<const SimpleReply&>(routable).getValue());
        Blob ret(str.size());
        memcpy(ret.data(), str.data(), str.size());
        return ret;
    } else {
        return Blob(0);
    }
}

Routable::UP
SimpleProtocol::decode(const vespalib::Version &version, BlobRef data) const
{
    (void)version;

    const char *d = data.data();
    uint32_t s = data.size();
    if (s < 1) {
        return {}; // too short
    }
    string str(d + 1, s - 1);
    if (*d == 'M') {
        return std::make_unique<SimpleMessage>(str);
    } else if (*d == 'R') {
        return std::make_unique<SimpleReply>(str);
    } else {
        return {}; // unknown type
    }
}

void
SimpleProtocol::simpleMerge(RoutingContext &ctx)
{
    auto ret = std::make_unique<EmptyReply>();
    for (RoutingNodeIterator it = ctx.getChildIterator();
         it.isValid(); it.next())
    {
        const Reply &reply = it.getReplyRef();
        for (uint32_t i = 0; i < reply.getNumErrors(); ++i) {
            ret->addError(reply.getError(i));
        }
    }
    ctx.setReply(std::move(ret));
}

} // namespace mbus