home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2007 April / PCpro_2007_04.ISO / files / dsl / jNetTool.exe / org / xbill / DNS / ExtendedResolver$Resolution.class (.txt) < prev    next >
Encoding:
Java Class File  |  2005-06-05  |  4.5 KB  |  199 lines

  1. package org.xbill.DNS;
  2.  
  3. import java.io.IOException;
  4. import java.io.InterruptedIOException;
  5. import java.net.SocketException;
  6. import java.util.List;
  7.  
  8. class ExtendedResolver$Resolution implements ResolverListener {
  9.    Resolver[] resolvers;
  10.    int[] sent;
  11.    Object[] inprogress;
  12.    int retries;
  13.    int outstanding;
  14.    boolean done;
  15.    Message query;
  16.    Message response;
  17.    Throwable thrown;
  18.    ResolverListener listener;
  19.  
  20.    public ExtendedResolver$Resolution(ExtendedResolver eres, Message query) {
  21.       List l = ExtendedResolver.access$0(eres);
  22.       this.resolvers = (Resolver[])l.toArray(new Resolver[l.size()]);
  23.       if (ExtendedResolver.access$1(eres)) {
  24.          int nresolvers = this.resolvers.length;
  25.          int var10002 = ExtendedResolver.access$2(eres);
  26.          ExtendedResolver.access$3(eres, var10002 + 1);
  27.          int start = var10002 % nresolvers;
  28.          if (ExtendedResolver.access$2(eres) > nresolvers) {
  29.             ExtendedResolver.access$3(eres, ExtendedResolver.access$2(eres) % nresolvers);
  30.          }
  31.  
  32.          if (start > 0) {
  33.             Resolver[] shuffle = new Resolver[nresolvers];
  34.  
  35.             for(int i = 0; i < nresolvers; ++i) {
  36.                int pos = (i + start) % nresolvers;
  37.                shuffle[i] = this.resolvers[pos];
  38.             }
  39.  
  40.             this.resolvers = shuffle;
  41.          }
  42.       }
  43.  
  44.       this.sent = new int[this.resolvers.length];
  45.       this.inprogress = new Object[this.resolvers.length];
  46.       this.retries = ExtendedResolver.access$4(eres);
  47.       this.query = query;
  48.    }
  49.  
  50.    public void send(int n) {
  51.       int var10002 = this.sent[n]++;
  52.       ++this.outstanding;
  53.  
  54.       try {
  55.          this.inprogress[n] = this.resolvers[n].sendAsync(this.query, this);
  56.       } catch (Throwable t) {
  57.          this.thrown = t;
  58.          this.done = true;
  59.          if (this.listener == null) {
  60.             this.notifyAll();
  61.             return;
  62.          }
  63.       }
  64.  
  65.    }
  66.  
  67.    public Message start() throws IOException {
  68.       try {
  69.          int var10002 = this.sent[0]++;
  70.          ++this.outstanding;
  71.          this.inprogress[0] = new Object();
  72.          return this.resolvers[0].send(this.query);
  73.       } catch (Exception e) {
  74.          this.handleException(this.inprogress[0], e);
  75.          if (!this.done) {
  76.             synchronized(this) {
  77.                while(!this.done) {
  78.                   try {
  79.                      this.wait();
  80.                   } catch (InterruptedException var3) {
  81.                   }
  82.                }
  83.             }
  84.          }
  85.  
  86.          if (this.response != null) {
  87.             return this.response;
  88.          } else if (this.thrown instanceof IOException) {
  89.             throw (IOException)this.thrown;
  90.          } else if (this.thrown instanceof RuntimeException) {
  91.             throw (RuntimeException)this.thrown;
  92.          } else if (this.thrown instanceof Error) {
  93.             throw (Error)this.thrown;
  94.          } else {
  95.             throw new IllegalStateException("ExtendedResolver failure");
  96.          }
  97.       }
  98.    }
  99.  
  100.    public void startAsync(ResolverListener listener) {
  101.       this.listener = listener;
  102.       this.send(0);
  103.    }
  104.  
  105.    public void receiveMessage(Object id, Message m) {
  106.       if (Options.check("verbose")) {
  107.          System.err.println("ExtendedResolver: received message");
  108.       }
  109.  
  110.       synchronized(this) {
  111.          if (this.done) {
  112.             return;
  113.          }
  114.  
  115.          this.response = m;
  116.          this.done = true;
  117.          if (this.listener == null) {
  118.             this.notifyAll();
  119.             return;
  120.          }
  121.       }
  122.  
  123.       this.listener.receiveMessage(this, this.response);
  124.    }
  125.  
  126.    public void handleException(Object id, Exception e) {
  127.       if (Options.check("verbose")) {
  128.          System.err.println("ExtendedResolver: got " + e);
  129.       }
  130.  
  131.       synchronized(this) {
  132.          --this.outstanding;
  133.          if (this.done) {
  134.             return;
  135.          }
  136.  
  137.          int n;
  138.          for(n = 0; n < this.inprogress.length && this.inprogress[n] != id; ++n) {
  139.          }
  140.  
  141.          if (n == this.inprogress.length) {
  142.             return;
  143.          }
  144.  
  145.          boolean startnext = false;
  146.          boolean waiting = false;
  147.          if (this.sent[n] == 1 && n < this.resolvers.length - 1) {
  148.             startnext = true;
  149.          }
  150.  
  151.          if (e instanceof InterruptedIOException) {
  152.             if (this.sent[n] < this.retries) {
  153.                this.send(n);
  154.             }
  155.  
  156.             if (this.thrown == null) {
  157.                this.thrown = e;
  158.             }
  159.          } else if (e instanceof SocketException) {
  160.             if (this.thrown == null || this.thrown instanceof InterruptedIOException) {
  161.                this.thrown = e;
  162.             }
  163.          } else {
  164.             this.thrown = e;
  165.          }
  166.  
  167.          if (this.done) {
  168.             return;
  169.          }
  170.  
  171.          if (startnext) {
  172.             this.send(n + 1);
  173.          }
  174.  
  175.          if (this.done) {
  176.             return;
  177.          }
  178.  
  179.          if (this.outstanding == 0) {
  180.             this.done = true;
  181.             if (this.listener == null) {
  182.                this.notifyAll();
  183.                return;
  184.             }
  185.          }
  186.  
  187.          if (!this.done) {
  188.             return;
  189.          }
  190.       }
  191.  
  192.       if (!(this.thrown instanceof Exception)) {
  193.          this.thrown = new RuntimeException(this.thrown.getMessage());
  194.       }
  195.  
  196.       this.listener.handleException(this, (Exception)this.thrown);
  197.    }
  198. }
  199.