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 * @param inetAddresses list of addresses. 045 * @throws IllegalArgumentException fqdn is null or any other field is not in valid range (0-65535). 046 */ 047 public SRVRecord(String fqdn, int port, int priority, int weight, List<InetAddress> inetAddresses) { 048 super(fqdn, port, inetAddresses); 049 StringUtils.requireNotNullOrEmpty(fqdn, "The FQDN must not be null"); 050 if (weight < 0 || weight > 65535) 051 throw new IllegalArgumentException( 052 "DNS SRV records weight must be a 16-bit unsigned integer (i.e. between 0-65535. Weight was: " 053 + weight); 054 055 if (priority < 0 || priority > 65535) 056 throw new IllegalArgumentException( 057 "DNS SRV records priority must be a 16-bit unsigned integer (i.e. between 0-65535. Priority was: " 058 + priority); 059 060 this.priority = priority; 061 this.weight = weight; 062 063 } 064 065 public int getPriority() { 066 return priority; 067 } 068 069 public int getWeight() { 070 return weight; 071 } 072 073 @Override 074 public int compareTo(SRVRecord other) { 075 // According to RFC2782, 076 // "[a] client MUST attempt to contact the target host with the lowest-numbered priority it can reach". 077 // This means that a SRV record with a higher priority is 'less' then one with a lower. 078 int res = other.priority - this.priority; 079 if (res == 0) { 080 res = this.weight - other.weight; 081 } 082 return res; 083 } 084 085 @Override 086 public String toString() { 087 return super.toString() + " prio:" + priority + ":w:" + weight; 088 } 089}