SRVRecord.java

  1. /**
  2.  *
  3.  * Copyright 2013-2014 Florian Schmaus
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smack.util.dns;

  18. /**
  19.  * @see <a href="http://tools.ietf.org/html/rfc2782">RFC 2782: A DNS RR for specifying the location of services (DNS
  20.  * SRV)</a>
  21.  * @author Florian Schmaus
  22.  *
  23.  */
  24. public class SRVRecord extends HostAddress implements Comparable<SRVRecord> {
  25.    
  26.     private int weight;
  27.     private int priority;
  28.    
  29.     /**
  30.      * Create a new SRVRecord
  31.      *
  32.      * @param fqdn Fully qualified domain name
  33.      * @param port The connection port
  34.      * @param priority Priority of the target host
  35.      * @param weight Relative weight for records with same priority
  36.      * @throws IllegalArgumentException fqdn is null or any other field is not in valid range (0-65535).
  37.      */
  38.     public SRVRecord(String fqdn, int port, int priority, int weight) {
  39.         super(fqdn, port);
  40.         if (weight < 0 || weight > 65535)
  41.             throw new IllegalArgumentException(
  42.                     "DNS SRV records weight must be a 16-bit unsiged integer (i.e. between 0-65535. Weight was: "
  43.                             + weight);

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

  48.         this.priority = priority;
  49.         this.weight = weight;

  50.     }
  51.    
  52.     public int getPriority() {
  53.         return priority;
  54.     }
  55.    
  56.     public int getWeight() {
  57.         return weight;
  58.     }

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

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