aboutsummaryrefslogtreecommitdiffstats
path: root/messagebus/src/main/java/com/yahoo/messagebus/NetworkMessageBus.java
blob: 6007ab8ed38529dfbe8b265b13bca4b9fb56e288 (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
// 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 java.util.concurrent.atomic.AtomicBoolean;

/**
 * The combination of a messagebus and a network over which it may send data.
 *
 * @author bratseth
 */
public class NetworkMessageBus {

    private final Network network;
    private final MessageBus messageBus;

    private final AtomicBoolean destroyed = new AtomicBoolean(false);

    public NetworkMessageBus(Network network, MessageBus messageBus) {
        this.network = network;
        this.messageBus = messageBus;
    }

    /** Returns the contained message bus object */
    public MessageBus getMessageBus() { return messageBus; }

    /** Returns the network of this as a Network */
    public Network getNetwork() { return network; }

    /**
     * Irreversibly destroys the content of this.
     *
     * @return whether this destroyed anything, or if it was already destroyed
     */
    public boolean destroy() {
        if ( destroyed.getAndSet(true)) return false;

        getMessageBus().destroy();
        return true;
    }

}