Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 948fd38ff8790ba575c278e8d6afee55b717d54e (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
/**
 * Copyright 2013 Florian Schmaus
 *
 * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jivesoftware.smack.util.dns;

/**
 * @see <a href="http://tools.ietf.org/html/rfc2782>RFC 2782: A DNS RR for specifying the location of services (DNS
 * SRV)<a>
 * @author Florian Schmaus
 * 
 */
public class SRVRecord extends HostAddress implements Comparable<SRVRecord> {
    
    private int weight;
    private int priority;
    
    /**
     * Create a new SRVRecord
     * 
     * @param fqdn Fully qualified domain name
     * @param port The connection port
     * @param priority Priority of the target host
     * @param weight Relative weight for records with same priority
     * @throws IllegalArgumentException fqdn is null or any other field is not in valid range (0-65535).
     */
    public SRVRecord(String fqdn, int port, int priority, int weight) {
        super(fqdn, port);
        if (weight < 0 || weight > 65535)
            throw new IllegalArgumentException(
                    "DNS SRV records weight must be a 16-bit unsiged integer (i.e. between 0-65535. Weight was: "
                            + weight);

        if (priority < 0 || priority > 65535)
            throw new IllegalArgumentException(
                    "DNS SRV records priority must be a 16-bit unsiged integer (i.e. between 0-65535. Priority was: "
                            + priority);

        this.priority = priority;
        this.weight = weight;

    }
    
    public int getPriority() {
        return priority;
    }
    
    public int getWeight() {
        return weight;
    }

    public int compareTo(SRVRecord other) {
        // According to RFC2782,
        // "[a] client MUST attempt to contact the target host with the lowest-numbered priority it can reach".
        // This means that a SRV record with a higher priority is 'less' then one with a lower.
        int res = other.priority - this.priority;
        if (res == 0) {
            res = this.weight - other.weight;
        }
        return res;
    }

    @Override
    public String toString() {
        return super.toString() + " prio:" + priority + ":w:" + weight;
    }
}

Back to the top