aboutsummaryrefslogtreecommitdiffstats
path: root/messagebus/src/vespa/messagebus/reply.h
blob: c71c0843ffe5c68ad6e2e1809d3d45b2b6593616 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once

#include "routable.h"
#include <vector>

namespace mbus {

class Message;
class Error;

/**
 * A reply is a response to a message that has been sent throught the
 * messagebus. No reply will ever exist without a corresponding message. There
 * are no error-replies defined, as errors can instead piggyback any reply by
 * the {@link #errors} member variable.
 */
class Reply : public Routable {
private:
    using MessageUP = std::unique_ptr<Message>;
    std::vector<Error>  _errors;     // A list of errors that have occured during the lifetime of this reply.
    MessageUP           _msg;        // The message to which this is a reply.
    double              _retryDelay; // How to perform resending of this.

public:
    /**
     * Convenience typedef for an auto pointer to a Reply object.
     */
    using UP = std::unique_ptr<Reply>;

    /**
     * Constructs a new instance of this class. This object is useless until the
     * state of a real message is swapped with this using {@link
     * #swapState(Routable)}.
     */
    Reply();

    /**
     * If a reply is deleted with elements on the callstack, this destructor
     * will log an error and generate an auto-reply to avoid having the sender
     * wait indefinetly for a reply.
     */
    ~Reply() override;

    void swapState(Routable &rhs) override;
    bool isReply() const override { return true; }

    /**
     * Add an Error to this Reply
     *
     * @param error the error to add
     */
    void addError(const Error &error);

    /**
     * Returns whether or not this reply contains at least one error.
     *
     * @return True if this contains errors.
     */
    bool hasErrors() const { return ! _errors.empty(); }

    /**
     * Returns whether or not this reply contains any fatal errors.
     *
     * @return True if it contains fatal errors.
     */
    bool hasFatalErrors() const;

    /**
     * Returns the error at the given position.
     *
     * @param i The index of the error to return.
     * @return The error at the given index.
     */
    const Error &getError(uint32_t i) const;

    /**
     * Returns the number of errors that this reply contains.
     *
     * @return The number of replies.
     */
    uint32_t getNumErrors() const;

    /**
     * Attach a Message to this Reply. If a Reply contains errors, messagebus
     * will attach the original Message to the Reply before giving it to the
     * application.
     *
     * @param msg the Message to attach
     */
    void setMessage(MessageUP msg);

    /**
     * Detach the Message attached to this Reply. If a Reply contains errors,
     * messagebus will attach the original Message to the Reply before giving it
     * to the application.
     *
     * @return the detached Message
     */
    MessageUP getMessage();

    /**
     * Returns the retry request of this reply. This can be set using {@link
     * #setRetryDelay} and is an instruction to the resender logic of message
     * bus on how to perform the retry. If this value is anything other than a
     * negative number, it instructs the resender to disregard all configured
     * resending attributes and instead act according to this value.
     *
     * @return The retry request.
     */
    double getRetryDelay() const { return _retryDelay; }

    /**
     * Sets the retry delay request of this reply. If this is a negative number,
     * it will use the defaults configured in the source session.
     *
     * @param retryDelay The retry request.
     */
    void setRetryDelay(double retryDelay) { _retryDelay = retryDelay; }
};

} // namespace mbus