Home | History | Annotate | Download | only in dns
      1 /**
      2  * Copyright 2013 Florian Schmaus
      3  *
      4  * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *     http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package org.jivesoftware.smack.util.dns;
     17 
     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 
     45         if (priority < 0 || priority > 65535)
     46             throw new IllegalArgumentException(
     47                     "DNS SRV records priority must be a 16-bit unsiged integer (i.e. between 0-65535. Priority was: "
     48                             + priority);
     49 
     50         this.priority = priority;
     51         this.weight = weight;
     52 
     53     }
     54 
     55     public int getPriority() {
     56         return priority;
     57     }
     58 
     59     public int getWeight() {
     60         return weight;
     61     }
     62 
     63     @Override
     64     public int compareTo(SRVRecord other) {
     65         // According to RFC2782,
     66         // "[a] client MUST attempt to contact the target host with the lowest-numbered priority it can reach".
     67         // This means that a SRV record with a higher priority is 'less' then one with a lower.
     68         int res = other.priority - this.priority;
     69         if (res == 0) {
     70             res = this.weight - other.weight;
     71         }
     72         return res;
     73     }
     74 
     75     @Override
     76     public String toString() {
     77         return super.toString() + " prio:" + priority + ":w:" + weight;
     78     }
     79 }
     80