james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From n...@apache.org
Subject cvs commit: jakarta-james/src/java/org/apache/james/dnsserver DNSServer.java
Date Thu, 19 Jun 2003 06:13:24 GMT
noel        2003/06/18 23:13:23

  Modified:    src/java/org/apache/james/dnsserver DNSServer.java
  Log:
  Added randomization for MX records
  
  Revision  Changes    Path
  1.21      +22 -6     jakarta-james/src/java/org/apache/james/dnsserver/DNSServer.java
  
  Index: DNSServer.java
  ===================================================================
  RCS file: /home/cvs/jakarta-james/src/java/org/apache/james/dnsserver/DNSServer.java,v
  retrieving revision 1.20
  retrieving revision 1.21
  diff -u -r1.20 -r1.21
  --- DNSServer.java	17 Jun 2003 22:06:48 -0000	1.20
  +++ DNSServer.java	19 Jun 2003 06:13:23 -0000	1.21
  @@ -355,13 +355,29 @@
           return answers;
       }
   
  -    private static class MXRecordComparator
  -        implements Comparator {
  -
  +    /* RFC 2821 section 5 requires that we sort the MX records by their
  +     * preference, and introduce a randomization.  This Comparator does
  +     * comparisons as normal unless the values are equal, in which case
  +     * it "tosses a coin", randomly speaking.
  +     *
  +     * This way MX record w/preference 0 appears before MX record
  +     * w/preference 1, but a bunch of MX records with the same preference
  +     * would appear in different orders each time.
  +     *
  +     * Reminder for maintainers: the return value on a Comparator can
  +     * be counter-intuitive for those who aren't used to the old C
  +     * strcmp function:
  +     *
  +     * < 0 ==> a < b
  +     * = 0 ==> a = b
  +     * > 0 ==> a > b
  +     */
  +    private static class MXRecordComparator implements Comparator {
  +        private final static Random random = new Random();
           public int compare (Object a, Object b) {
  -            MXRecord ma = (MXRecord)a;
  -            MXRecord mb = (MXRecord)b;
  -            return ma.getPriority () - mb.getPriority ();
  +            int pa = ((MXRecord)a).getPriority();
  +            int pb = ((MXRecord)b).getPriority();
  +            return (pa == pb) ? (512 - random.nextInt(1024)) : pa - pb;
           }
       }
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: james-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: james-dev-help@jakarta.apache.org


Mime
View raw message