示例#1
0
  /**
   * Connects this CDDB instance to the CDDB server running on the supplied host using the specified
   * port.
   *
   * <p><b>Note well:</b> you must close a CDDB connection once you are done with it, otherwise the
   * socket connection will remain open and pointlessly consume machine resources.
   *
   * @param hostname The host to which to connect.
   * @param port The port number on which to connect to the host.
   * @exception IOException Thrown if a network error occurs attempting to connect to the host.
   * @exception CDDBException Thrown if an error occurs after identifying ourselves to the host.
   * @return The message supplied with the succesful connection response.
   * @see #close
   */
  public String connect(String hostname, int port) throws IOException, CDDBException {
    // obtain the necessary information we'll need to identify
    // ourselves to the CDDB server
    String localhost = InetAddress.getLocalHost().getHostName();
    String username = System.getProperty("user.name");
    if (username == null) {
      username = "******";
    }

    // establish our socket connection and IO streams
    InetAddress addr = InetAddress.getByName(hostname);
    _sock = new Socket(addr, port);
    _in = new BufferedReader(new InputStreamReader(_sock.getInputStream()));
    _out = new PrintStream(new BufferedOutputStream(_sock.getOutputStream()));

    // first read (and discard) the banner string
    _in.readLine();

    // send a hello request
    StringBuilder req = new StringBuilder("cddb hello ");
    req.append(username).append(" ");
    req.append(localhost).append(" ");
    req.append(CLIENT_NAME).append(" ");
    req.append(CLIENT_VERSION);

    Response rsp = request(req.toString());

    // confirm that the response was a successful one
    if (CDDBProtocol.codeFamily(rsp.code) != CDDBProtocol.OK
        && rsp.code != 402 /* already shook hands */) {
      throw new CDDBException(rsp.code, rsp.message);
    }

    return rsp.message;
  }
示例#2
0
  /**
   * Issues a query to the CDDB server using the supplied CD identifying information.
   *
   * @param discid The disc identifier (information on how to compute the disc ID is available <a
   *     href="http://www.freedb.org/sections.php?op=viewarticle&artid=6">here</a>.
   * @param frameOffsets The frame offset of each track. The length of this array is assumed to be
   *     the number of tracks on the CD and is used in the query.
   * @param length The length (in seconds) of the CD.
   * @return If no entry matches the query, null is returned. Otherwise one or more entries is
   *     returned that matched the query parameters.
   */
  public Entry[] query(String discid, int[] frameOffsets, int length)
      throws IOException, CDDBException {
    // sanity check
    if (_sock == null) {
      throw new CDDBException(500, "Not connected");
    }

    // construct the query parameter
    StringBuilder req = new StringBuilder("cddb query ");
    req.append(discid).append(" ");
    req.append(frameOffsets.length).append(" ");
    for (int frameOffset : frameOffsets) {
      req.append(frameOffset).append(" ");
    }
    req.append(length);

    // make the request
    Response rsp = request(req.toString());
    Entry[] entries = null;

    // if this is an exact match, parse the entry and return it
    if (rsp.code == 200 /* exact match */) {
      entries = new Entry[1];
      entries[0] = new Entry();
      entries[0].parse(rsp.message);

    } else if (rsp.code == 211 /* inexact matches */) {
      // read the matches from the server
      ArrayList<Entry> list = new ArrayList<Entry>();
      String input = _in.readLine();
      while (input != null && !input.equals(CDDBProtocol.TERMINATOR)) {
        System.out.println("...: " + input);
        Entry e = new Entry();
        e.parse(input);
        list.add(e);
        input = _in.readLine();
      }
      entries = new Entry[list.size()];
      list.toArray(entries);

    } else if (CDDBProtocol.codeFamily(rsp.code) != CDDBProtocol.OK) {
      throw new CDDBException(rsp.code, rsp.message);
    }

    return entries;
  }