aboutsummaryrefslogtreecommitdiffstats
path: root/messagebus/src/vespa/messagebus/sendproxy.cpp
blob: fa2994a6b7ea2f4a08492b197745c3c829fb2be9 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "sendproxy.h"

#include <vespa/log/log.h>
LOG_SETUP(".sendproxy");

namespace mbus {

SendProxy::SendProxy(MessageBus &mbus, INetwork &net, Resender *resender) :
    _mbus(mbus),
    _net(net),
    _resender(resender),
    _msg(),
    _logTrace(false),
    _root()
{ }

void
SendProxy::handleMessage(Message::UP msg)
{
    Trace &trace = msg->getTrace();
    if (trace.getLevel() == 0) {
        if (LOG_WOULD_LOG(spam)) {
            trace.setLevel(9);
            _logTrace = true;
        } else if (LOG_WOULD_LOG(debug)) {
            trace.setLevel(6);
            _logTrace = true;
        }
    }
    _msg = std::move(msg);
    _root = std::make_unique<RoutingNode>(_mbus, _net, _resender, *this, *_msg, this);
    _root->send();
}

void
SendProxy::handleDiscard(Context)
{
    _msg->discard();
    delete this;
}

void
SendProxy::handleReply(Reply::UP reply)
{
    Trace &trace = _msg->getTrace();
    if (_logTrace) {
        if (reply->hasErrors()) {
            LOG(debug, "Trace for reply with error(s):\n%s", reply->getTrace().toString().c_str());
        } else if (LOG_WOULD_LOG(spam)) {
            LOG(spam, "Trace for reply:\n%s", reply->getTrace().toString().c_str());
        }
        trace.clear();
    } else if (trace.getLevel() > 0) {
        trace.addChild(reply->steal_trace());
        trace.normalize();
    }
    reply->swapState(*_msg);
    reply->setMessage(std::move(_msg));

    IReplyHandler &handler = reply->getCallStack().pop(*reply);
    handler.handleReply(std::move(reply));

    delete this;
}

} // namespace mbus