aboutsummaryrefslogtreecommitdiffstats
path: root/messagebus/src/main/java/com/yahoo/messagebus/SendProxy.java
blob: a62dad67fd8a33a67edd5f7ec285bfbddef6145d (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.messagebus;

import com.yahoo.messagebus.network.Network;
import com.yahoo.messagebus.routing.Resender;
import com.yahoo.messagebus.routing.RoutingNode;
import java.util.logging.Level;

import java.util.logging.Logger;

/**
 * This class owns a message that is being sent by message bus. Once a reply is received, the message is attached to it
 * and returned to the application. This also implements the discard policy of {@link RoutingNode}.
 *
 * @author Simon Thoresen Hult
 */
public class SendProxy implements MessageHandler, ReplyHandler {

    private static final Logger log = Logger.getLogger(SendProxy.class.getName());
    private final MessageBus mbus;
    private final Network net;
    private final Resender resender;
    private Message msg = null;
    private boolean logTrace = false;

    /**
     * Constructs a new instance of this class to maintain sending of a single message.
     *
     * @param mbus     The message bus that owns this.
     * @param net      The network layer to transmit through.
     * @param resender The resender to use.
     */
    public SendProxy(MessageBus mbus, Network net, Resender resender) {
        this.mbus = mbus;
        this.net = net;
        this.resender = resender;
    }

    public void handleMessage(Message msg) {
        Trace trace = msg.getTrace();
        if (trace.getLevel() == 0) {
            if (log.isLoggable(Level.FINEST)) {
                trace.setLevel(9);
                logTrace = true;
            } else if (log.isLoggable(Level.FINE)) {
                trace.setLevel(6);
                logTrace = true;
            }
        }
        this.msg = msg;
        RoutingNode root = new RoutingNode(mbus, net, resender, this, msg);
        root.send();
    }

    public void handleReply(Reply reply) {
        if (reply == null) {
            msg.discard();
        } else {
            Trace trace = msg.getTrace();
            if (logTrace) {
                if (reply.hasErrors())
                    log.log(Level.FINE, () -> "Trace for reply with error(s):\n" + reply.getTrace());
                else
                    log.log(Level.FINEST, () -> "Trace for reply:\n" + reply.getTrace());
                Trace empty = new Trace();
                trace.swap(empty);
            } else if (trace.getLevel() > 0) {
                trace.getRoot().addChild(reply.getTrace().getRoot());
                trace.getRoot().normalize();
            }
            reply.swapState(msg);
            reply.setMessage(msg);

            ReplyHandler handler = reply.popHandler();
            handler.handleReply(reply);
        }
    }

}