diff options
Diffstat (limited to 'NET/worlds/scape/CDDBConnection.java')
| -rw-r--r-- | NET/worlds/scape/CDDBConnection.java | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/NET/worlds/scape/CDDBConnection.java b/NET/worlds/scape/CDDBConnection.java new file mode 100644 index 0000000..c446d1d --- /dev/null +++ b/NET/worlds/scape/CDDBConnection.java @@ -0,0 +1,75 @@ +package NET.worlds.scape; + +import NET.worlds.network.DNSLookup; +import java.io.DataInputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.net.Socket; + +class CDDBConnection { + private Socket sock; + private PrintStream sockOut; + private DataInputStream sockIn; + private boolean readOnly; + private boolean debug; + + public CDDBConnection(CDDBHost host, boolean debug) throws IOException { + this.debug = debug; + if (debug) { + System.out.println("Contacting host " + host); + } + + this.sock = new Socket(DNSLookup.lookup(host.getHost()), host.getPort()); + CDDBLookup.markActivity(); + this.sockOut = new PrintStream(this.sock.getOutputStream(), true); + this.sockIn = new DataInputStream(this.sock.getInputStream()); + String msg = this.recv(); + if (msg.startsWith("200 ")) { + this.readOnly = false; + } else { + if (!msg.startsWith("201 ")) { + throw new IOException("Can't connect to " + host + ": " + msg); + } + + this.readOnly = true; + } + } + + public String command(String command) throws IOException { + this.send(command); + return this.recv(); + } + + public String readBody() throws IOException { + String str = this.recv(); + return str.equals(".") ? null : str; + } + + public void close() { + try { + this.send("quit"); + this.recv(); + this.sock.close(); + } catch (IOException var2) { + } + } + + private void send(String msg) throws IOException { + if (this.debug) { + System.out.println("--> " + msg); + } + + this.sockOut.println(msg); + CDDBLookup.markActivity(); + } + + private String recv() throws IOException { + String msg = this.sockIn.readLine(); + CDDBLookup.markActivity(); + if (this.debug) { + System.out.println("<-- " + msg); + } + + return msg; + } +} |