001/**
002 *
003 * Copyright 2013-2017 Florian Schmaus
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smack.util.dns;
018
019import java.net.InetAddress;
020import java.util.List;
021
022import org.jivesoftware.smack.util.StringUtils;
023
024/**
025 * A DNS SRV RR.
026 *
027 * @see <a href="http://tools.ietf.org/html/rfc2782">RFC 2782: A DNS RR for specifying the location of services (DNS
028 * SRV)</a>
029 * @author Florian Schmaus
030 * 
031 */
032public class SRVRecord extends HostAddress implements Comparable<SRVRecord> {
033
034    private int weight;
035    private int priority;
036
037    /**
038     * SRV Record constructor.
039     * 
040     * @param fqdn Fully qualified domain name
041     * @param port The connection port
042     * @param priority Priority of the target host
043     * @param weight Relative weight for records with same priority
044     * @throws IllegalArgumentException fqdn is null or any other field is not in valid range (0-65535).
045     */
046    public SRVRecord(String fqdn, int port, int priority, int weight, List<InetAddress> inetAddresses) {
047        super(fqdn, port, inetAddresses);
048        StringUtils.requireNotNullOrEmpty(fqdn, "The FQDN must not be null");
049        if (weight < 0 || weight > 65535)
050            throw new IllegalArgumentException(
051                    "DNS SRV records weight must be a 16-bit unsiged integer (i.e. between 0-65535. Weight was: "
052                            + weight);
053
054        if (priority < 0 || priority > 65535)
055            throw new IllegalArgumentException(
056                    "DNS SRV records priority must be a 16-bit unsiged integer (i.e. between 0-65535. Priority was: "
057                            + priority);
058
059        this.priority = priority;
060        this.weight = weight;
061
062    }
063
064    public int getPriority() {
065        return priority;
066    }
067
068    public int getWeight() {
069        return weight;
070    }
071
072    @Override
073    public int compareTo(SRVRecord other) {
074        // According to RFC2782,
075        // "[a] client MUST attempt to contact the target host with the lowest-numbered priority it can reach".
076        // This means that a SRV record with a higher priority is 'less' then one with a lower.
077        int res = other.priority - this.priority;
078        if (res == 0) {
079            res = this.weight - other.weight;
080        }
081        return res;
082    }
083
084    @Override
085    public String toString() {
086        return super.toString() + " prio:" + priority + ":w:" + weight;
087    }
088}