summaryrefslogtreecommitdiff
path: root/NET/worlds/scape/CDDBConnection.java
blob: c446d1d9f32a92cd50f72a0680427cde2779cdc6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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;
   }
}