UpdateProtocol.java :  » Net » customdns » CustomDNS » Java Open Source

Java Open Source » Net » customdns 
customdns » CustomDNS » UpdateProtocol.java
package CustomDNS;

import java.io.Writer;
import java.io.IOException;


/*************************************************************************
 *  Constants and utility functions used by our update protocol.
 *************************************************************************
 *  @see CustomDNS.UpdateServer
 *  @see CustomDNS.UpdateClient
 */

public class UpdateProtocol {


    /** The default port for the update protocol. */
    public static final short DEFAULT_PORT = (short) 5768;

    /** The command completed successfully. */
    public static final int STATUS_OK = 200;
    /** The connection to the server was successful. */
    public static final int STATUS_WELCOME = 201;
    /** The AUTH command was accepted, but the authentication data has
     not been checked yet. */
    public static final int STATUS_AUTH_ACCEPTED = 202;

    /** A syntax error occurred. */
    public static final int STATUS_SYNTAX_ERROR = 500;
    /** The last command was unknown. */
    public static final int STATUS_UNKNOWN_COMMAND = 501;
    /** The last command referred to an unknown DNS zone. */
    public static final int STATUS_UNKNOWN_ZONE = 502;
    /** The last command cannot be handled in this server state. */
    public static final int STATUS_WRONG_STATE = 502;
    /** The last command included a malformed IP address. */
    public static final int STATUS_MALFORMED_ADDRESS = 504;
    /** The specified update could not be performed. */
    public static final int STATUS_NO_UPDATE = 510; 
    /** The AUTH command had an unknown authentication type. */
    public static final int STATUS_UNKNOWN_AUTH_TYPE = 520;
    /** The authentication could not be performed. */
    public static final int STATUS_COULD_NOT_AUTH = 521;
    /** The specified update could not be performed. */
    public static final int STATUS_NOT_ALLOWED = 522;
    

    /*********************************************************************
     *  Write a line in network format (CR/LF terminated).
     *********************************************************************
     *  We flush the writer immediately.
     *  @param out The output writer.
     *  @param line The string to write.
     *  @exception IOException An error occured while writing.
     */

    public static void writeLine(Writer out, String line)
  throws IOException
    {
  out.write(line + "\r\n");
  out.flush();  
    }
    

    /*********************************************************************
     *  An response returned by the update server.
     *********************************************************************
     *  This class respresents responses of the form:
     *  <pre>500 Error message</pre>
     *  This class is a subclass of Exception, so you can throw it.
     */

    static public class ServerResponse extends Exception {

  private int code;
  private String text;


  /*****************************************************************
   *  Create a new server response object.
   *****************************************************************
   *  @param code A three-digit response code.
   *  @param text The explanatory string returned by the server.
   */

  public ServerResponse (int code, String text) {
      super(code + " " + text);
      this.code = code;
      this.text = text;
  }


  /*****************************************************************
   *  Parse a server response string.
   *****************************************************************
   *  @param line The text returned by the server.
   */

  static public ServerResponse parse (String line)
      throws IOException
  {
      try {
    if (line.length() < 5 || line.charAt(3) != ' ')
        throwParseError();
    String digits = line.substring(0, 3);
    String text = line.substring(4);
    int code = Integer.parseInt(digits);
    return new ServerResponse(code, text);
      } catch (NumberFormatException e) {
    throwParseError();
    // We never get here.
    return null;
      }
  }

  // Give up on parsing and throw an error.
  static private void throwParseError ()
      throws IOException
  {
      throw new IOException("Error parsing response from UpdateServer.");
  }

  /** Get the server's response code. */
  public int getCode () {
      return this.code;
  }

  /** Get the server's response text. */
  public String getText () {
      return this.text;
  }

  /** Return true if the response represents an error. */
  public boolean isError () {
      if (this.code < 200 || this.code > 299)
    return true;
      return false;
  }
    }
        

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.