aboutsummaryrefslogtreecommitdiffstats
path: root/messagebus/src/vespa/messagebus/testlib/simpleprotocol.cpp
blob: e90cf0dd7a838a88f433f75b54bac1822ddcd961 (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
// Copyright Yahoo. 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 IRoutingPolicy::UP(new AllPolicy());
    }
};

class HashPolicy : public IRoutingPolicy {
public:
    void select(RoutingContext &ctx) override {
        std::vector<Route> recipients;
        ctx.getMatchedRecipients(recipients);
        if (!recipients.empty()) {
            int i = static_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 IRoutingPolicy::UP(new HashPolicy());
    }
};

SimpleProtocol::SimpleProtocol() :
    _policies()
{
    addPolicyFactory("All", IPolicyFactory::SP(new AllPolicyFactory));
    addPolicyFactory("Hash", IPolicyFactory::SP(new HashPolicyFactory));
}

SimpleProtocol::~SimpleProtocol()
{
    // empty
}

void
SimpleProtocol::addPolicyFactory(const string &name,
                                 IPolicyFactory::SP factory)
{
    _policies.insert(FactoryMap::value_type(name, factory));
}

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

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

Blob
SimpleProtocol::encode(const vespalib::Version &version, const Routable &routable) const
{
    (void)version;
    if (routable.getType() == MESSAGE) {
        string str = "M";
        str.append(static_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(static_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 Routable::UP(); // too short
    }
    string str(d + 1, s - 1);
    if (*d == 'M') {
        return Routable::UP(new SimpleMessage(str));
    } else if (*d == 'R') {
        return Routable::UP(new SimpleReply(str));
    } else {
        return Routable::UP(); // unknown type
    }
}

void
SimpleProtocol::simpleMerge(RoutingContext &ctx)
{
    Reply::UP ret(new 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