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

#pragma once

#include <vespa/messagebus/iprotocol.h>
#include <map>

namespace mbus {

class SimpleProtocol : public IProtocol {
public:
    /**
     * Defines a policy factory interface that tests can use to register arbitrary policies with this protocol.
     */
    class IPolicyFactory {
    public:
        /**
         * Convenience typedefs.
         */
        using SP = std::shared_ptr<IPolicyFactory>;

        /**
         * Required for inheritance.
         */
        virtual ~IPolicyFactory() { }

        /**
         * Creates a new isntance of the routing policy that this factory encapsulates.
         *
         * @param param The param for the policy constructor.
         * @return The routing policy created.
         */
        virtual IRoutingPolicy::UP create(const string &param) = 0;
    };

private:
    using FactoryMap = std::map<string, IPolicyFactory::SP>;
    FactoryMap _policies;

public:
    static const string NAME;
    static const uint32_t MESSAGE;
    static const uint32_t REPLY;

    /**
     * Constructs a new simple protocol. This registers policy factories for both {@link SimpleAllPolicy} and
     * {@link SimpleHashPolicy}.
     */
    SimpleProtocol();

    ~SimpleProtocol();

    /**
     * Registers a policy factory with this protocol under a given name. Whenever a policy is requested that
     * matches this name, the factory is invoked.
     *
     * @param name    The name of the policy.
     * @param factory The policy factory.
     */
    void addPolicyFactory(const string &name,
                          IPolicyFactory::SP factory);

    /**
     * Common merge logic that can be used for any simple policy. It all errors across all replies into
     * a new {@link EmptyReply}.
     *
     * @param ctx The routing context whose children to merge.
     */
    static void simpleMerge(RoutingContext &ctx);

    const string & getName() const override;
    IRoutingPolicy::UP createPolicy(const string &name, const string &param) const override;
    Blob encode(const vespalib::Version &version, const Routable &routable) const override;
    Routable::UP decode(const vespalib::Version &version, BlobRef data) const override;
};

} // namespace mbus