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

/**
 * This class represents a tcp directive within a {@link Hop}'s selector. This is a connection string used to establish
 * a direct connection to a host, bypassing service lookups through Slobrok.
 *
 * @author Simon Thoresen Hult
 */
public class TcpDirective implements HopDirective {

    private final String host;
    private final int port;
    private final String session;

    /**
     * Constructs a new directive to route directly to a tcp address.
     *
     * @param host    The host name to connect to.
     * @param port    The port to connect to.
     * @param session The session to route to.
     */
    public TcpDirective(String host, int port, String session) {
        this.host = host;
        this.port = port;
        this.session = session;
    }

    @Override
    public boolean matches(HopDirective dir) {
        if (!(dir instanceof TcpDirective)) {
            return false;
        }
        TcpDirective rhs = (TcpDirective)dir;
        return host.equals(rhs.host) && port == rhs.port && session.equals(rhs.session);
    }

    /**
     * Returns the host to connect to. This may be an ip address or a name.
     *
     * @return The host.
     */
    public String getHost() {
        return host;
    }

    /**
     * Returns the port to connect to on the remove host.
     *
     * @return The port number.
     */
    public int getPort() {
        return port;
    }

    /**
     * Returns the name of the session to route to.
     *
     * @return The session name.
     */
    public String getSession() {
        return session;
    }

    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof TcpDirective)) {
            return false;
        }
        TcpDirective rhs = (TcpDirective)obj;
        if (!host.equals(rhs.host)) {
            return false;
        }
        if (port != rhs.port) {
            return false;
        }
        if (!session.equals(rhs.session)) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "tcp/" + host + ":" + port + "/" + session;
    }

    @Override
    public String toDebugString() {
        return "TcpDirective(host = '" + host + "', port = " + port + ", session = '" + session + "')";
    }

    @Override
    public int hashCode() {
        int result = host != null ? host.hashCode() : 0;
        result = 31 * result + port;
        result = 31 * result + (session != null ? session.hashCode() : 0);
        return result;
    }
}