/* */ package NET.worlds.scape; /* */ /* */ import NET.worlds.core.Hashtable; /* */ import NET.worlds.network.URL; /* */ import java.io.IOException; /* */ import java.net.InetAddress; /* */ import java.net.UnknownHostException; /* */ import java.util.NoSuchElementException; /* */ import java.util.StringTokenizer; /* */ import java.util.Vector; /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ public class CDDBLookup /* */ implements Runnable /* */ { /* */ public static final int STATUS_SERVERERROR = -3; /* */ public static final int STATUS_NOTUNIQUE = -2; /* */ public static final int STATUS_NOTFOUND = -1; /* */ public static final int STATUS_LOOKING = 0; /* */ public static final int STATUS_EXACTMATCH = 1; /* */ public static final int STATUS_CLOSEMATCH = 2; /* */ private static final boolean debug = false; /* */ private static final String lookupThreadName = "lookup"; /* */ private static final String siteThreadName = "site"; /* */ private static final String pkgName = "gamma"; /* */ private static final String pkgVersion = "1.0"; /* */ private static final int TIMEOUT = 3000; /* */ private static final String dbName = "home:cdcache.db"; /* 37 */ private static CDDBHost siteHost = new CDDBHost("cddb.cddb.com", 888); /* */ private static Vector sites; /* 39 */ private static Object siteMutex = new Object(); /* 40 */ private static Object knownDisksMutex = new Object(); /* */ /* 42 */ private static Hashtable threadActivity = new Hashtable(); /* */ private static Hashtable knownDisks; /* */ private static String userName; /* */ private static String hostName; /* */ private CDTrackInfo tracks; /* */ private String hash; /* */ private CDDBStatus status; /* */ private static final String discTitleKey = "DTITLE="; /* */ private static final String trackTitleKey = "TTITLE"; /* */ /* */ public CDDBLookup(CDTrackInfo tracks) /* */ { /* 54 */ if (userName == null) { /* 55 */ userName = System.getProperty("user.name").replace(' ', '_'); /* */ try { /* 57 */ hostName = InetAddress.getLocalHost().getHostName(); /* */ } catch (UnknownHostException e) { /* 59 */ hostName = "unknown"; /* */ } /* */ } /* 62 */ this.tracks = tracks; /* 63 */ this.hash = CDDBHash.hashString(tracks); /* 64 */ if ((this.status = dbLookup()) == null) { /* 65 */ runThread("lookup"); /* */ } /* */ } /* */ /* */ public String getHash() { /* 70 */ return this.hash; /* */ } /* */ /* */ public CDDiskInfo getDiskInfo() /* */ { /* 75 */ return getDiskInfo(false); /* */ } /* */ /* */ public synchronized CDDiskInfo getDiskInfo(boolean wait) /* */ { /* 80 */ if (wait) { /* 81 */ while (this.status == null) { /* */ try { /* 83 */ wait(); /* */ } catch (InterruptedException localInterruptedException) {} /* */ } /* */ } /* 87 */ if (this.status != null) /* 88 */ return this.status.getDiskInfo(); /* 89 */ return null; /* */ } /* */ /* */ public int getStatus() /* */ { /* 94 */ if (this.status != null) /* 95 */ return this.status.getStatus(); /* 96 */ return 0; /* */ } /* */ /* */ public void run() /* */ { /* 101 */ String name = Thread.currentThread().getName(); /* */ /* */ /* 104 */ if (name.equals("lookup")) { /* 105 */ maybeRunSiteThread(); /* 106 */ for (int i = 0; i < sites.size(); i++) { /* 107 */ synchronized (this) { /* 108 */ Thread t = runThread(i); /* */ try { /* */ do { /* 111 */ wait(3000L); /* 112 */ if (this.status != null) break; } while (recentlyActive(t)); /* */ } catch (InterruptedException localInterruptedException) {} /* 114 */ forgetThread(t); /* 115 */ if (this.status != null) /* */ break; /* 117 */ if (i == sites.size() - 1) { /* 118 */ this.status = new CDDBStatus(-3); /* 119 */ notifyAll(); /* */ } /* */ } /* */ } /* */ } else { /* 124 */ if (name.equals("site")) { /* 125 */ Vector v = getSiteList(); /* 126 */ synchronized (siteMutex) { /* 127 */ if (sites == null) /* 128 */ sites = v; /* 129 */ siteMutex.notifyAll(); /* */ } /* */ } /* */ /* 133 */ CDDBStatus newStatus = null; /* 134 */ int hostIndex = 0; /* */ try { /* 136 */ hostIndex = Integer.parseInt(name); /* */ } catch (NumberFormatException localNumberFormatException) {} /* 138 */ CDDBHost host = null; /* 139 */ synchronized (siteMutex) { /* 140 */ host = (CDDBHost)sites.elementAt(hostIndex); /* */ } /* 142 */ newStatus = findDiskInfo(host); /* 143 */ synchronized (this) { /* 144 */ if ((this.status == null) && (newStatus != null)) { /* 145 */ this.status = newStatus; /* 146 */ if (hostIndex != 0) { /* 147 */ synchronized (siteMutex) { /* 148 */ Object site = sites.elementAt(hostIndex); /* 149 */ sites.removeElementAt(hostIndex); /* 150 */ sites.insertElementAt(site, 0); /* */ } /* */ } /* 153 */ notifyAll(); /* */ } /* */ } /* */ } /* */ } /* */ /* */ /* */ /* */ private static void addSite(Vector v, String host, int port) /* */ { /* 163 */ v.addElement(new CDDBHost(host, port)); /* */ } /* */ /* */ /* */ /* */ private static void defaultSites() /* */ { /* 170 */ sites = new Vector(); /* 171 */ addSite(sites, "cddb.moonsoft.com", 888); /* 172 */ addSite(sites, "cddb.moonsoft.com", 8880); /* 173 */ addSite(sites, "cddb.celestial.com", 888); /* 174 */ addSite(sites, "cddb.sonic.net", 888); /* 175 */ addSite(sites, "sunsite.unc.edu", 888); /* 176 */ addSite(sites, "cddb.netads.com", 888); /* 177 */ addSite(sites, "cddb.mattdm.org", 888); /* 178 */ addSite(sites, "cddb.dartmouth.edu", 888); /* */ } /* */ /* */ private static Vector getSiteList() /* */ { /* 183 */ CDDBConnection con = null; /* */ /* */ try /* */ { /* 187 */ con = new CDDBConnection(siteHost, false); /* 188 */ con.command("sites").startsWith("210 "); /* */ /* 190 */ Vector v = new Vector(); /* 191 */ String line; while ((line = con.readBody()) != null) { String line; /* 192 */ StringTokenizer tok = new StringTokenizer(line, " "); /* 193 */ addSite(v, tok.nextToken(), /* 194 */ Integer.parseInt(tok.nextToken())); /* */ } /* 196 */ return v; /* */ /* */ } /* */ catch (IOException localIOException) {}catch (NumberFormatException localNumberFormatException) {}catch (NoSuchElementException localNoSuchElementException) {}finally /* */ { /* */ /* 202 */ if (con != null) /* 203 */ con.close(); /* */ } /* 205 */ return null; /* */ } /* */ /* */ private CDDBStatus findDiskInfo(CDDBHost host) /* */ { /* 210 */ CDDBConnection con = null; /* */ try { /* 212 */ con = new CDDBConnection(host, false); /* */ /* 214 */ if (con.command("cddb hello " + userName + " " + hostName + " " + "gamma" + " " + "1.0").startsWith("200 ")) { /* 215 */ String resp = con.command("cddb query " + /* 216 */ CDDBHash.lookupString(this.tracks)); /* 217 */ StringTokenizer t = new StringTokenizer(resp, " "); /* 218 */ String code = t.nextToken(); /* 219 */ CDDBStatus localCDDBStatus; if (code.equals("200")) /* 220 */ return dbAdd(new CDDBStatus(1, /* 221 */ getCDDBEntry(con, t))); /* 222 */ if (code.equals("211")) { /* 223 */ String line = con.readBody(); /* */ /* 225 */ for (int extra = 0; con.readBody() != null; extra++) {} /* */ /* 227 */ if (extra != 0) /* 228 */ return dbAdd(new CDDBStatus(-2)); /* 229 */ return dbAdd(new CDDBStatus(2, /* 230 */ getCDDBEntry(con, /* 231 */ new StringTokenizer(line, /* 232 */ " ")))); /* */ } /* 234 */ if (code.equals("202")) { /* 235 */ return new CDDBStatus(-1); /* */ } /* */ } /* */ } /* */ catch (IOException localIOException) {}catch (NoSuchElementException localNoSuchElementException) {}finally /* */ { /* 241 */ if (con != null) { /* 242 */ con.close(); /* */ } /* */ } /* 241 */ if (con != null) { /* 242 */ con.close(); /* */ } /* 244 */ return null; /* */ } /* */ /* */ private Thread runThread(String name) /* */ { /* 249 */ Thread t = new Thread(this, name); /* 250 */ t.setDaemon(true); /* 251 */ t.start(); /* 252 */ return t; /* */ } /* */ /* */ private void maybeRunSiteThread() /* */ { /* 257 */ synchronized (siteMutex) { /* 258 */ if (sites == null) { /* 259 */ Thread t = runThread("site"); /* */ try { /* */ do { /* 262 */ siteMutex.wait(3000L); /* 263 */ if (sites != null) break; } while (recentlyActive(t)); /* */ } catch (InterruptedException localInterruptedException) {} /* 265 */ forgetThread(t); /* 266 */ if (sites == null) { /* 267 */ defaultSites(); /* */ } /* */ } /* */ } /* */ } /* */ /* */ private CDDiskInfo getCDDBEntry(CDDBConnection con, StringTokenizer t) { /* */ try { /* 275 */ String cat = t.nextToken(); /* 276 */ String id = t.nextToken(); /* 277 */ if (con.command("cddb read " + cat + " " + id).startsWith("210 ")) { /* 278 */ return parseCDDBEntry(cat, con); /* */ } /* */ } /* */ catch (IOException localIOException) {}catch (NoSuchElementException localNoSuchElementException) {} /* 282 */ return null; /* */ } /* */ /* */ /* */ /* */ /* */ private CDDiskInfo parseCDDBEntry(String category, CDDBConnection con) /* */ throws IOException /* */ { /* 291 */ String artist = null; /* 292 */ String title = null; /* 293 */ String[] trackNames = new String[this.tracks.getNumTracks()]; /* 294 */ for (int i = 0; i < trackNames.length; i++) /* 295 */ trackNames[i] = ""; /* */ String line; /* 297 */ while ((line = con.readBody()) != null) { String line; /* 298 */ if (line.startsWith("DTITLE=")) { /* 299 */ line = line.substring("DTITLE=".length()); /* 300 */ int slashPos = line.indexOf('/'); /* 301 */ if (slashPos == -1) { /* 302 */ artist = title = line.trim(); /* */ } else { /* 304 */ artist = line.substring(0, slashPos).trim(); /* 305 */ title = line.substring(slashPos + 1).trim(); /* */ } /* */ } /* 308 */ else if (line.startsWith("TTITLE")) { /* 309 */ int eqIndex = line.indexOf('='); /* */ try { /* 311 */ int trackNum = /* 312 */ Integer.parseInt(line.substring( /* 313 */ "TTITLE".length(), /* 314 */ eqIndex)); int /* 315 */ tmp163_161 = trackNum; String[] tmp163_159 = trackNames;tmp163_159[tmp163_161] = (tmp163_159[tmp163_161] + line.substring(eqIndex + 1)); /* */ } /* */ catch (NumberFormatException localNumberFormatException) {}catch (IndexOutOfBoundsException localIndexOutOfBoundsException) {} /* */ } /* */ } /* */ /* 321 */ return new CDDiskInfo(artist, title, category, trackNames); /* */ } /* */ /* */ static void markActivity() /* */ { /* 326 */ threadActivity.put(Thread.currentThread(), /* 327 */ new Long(System.currentTimeMillis())); /* */ } /* */ /* */ private boolean recentlyActive(Thread t) /* */ { /* 332 */ Long l = (Long)threadActivity.get(t); /* 333 */ if (l != null) /* 334 */ return System.currentTimeMillis() - l.longValue() < 3000L; /* 335 */ return false; /* */ } /* */ /* */ private void forgetThread(Thread t) /* */ { /* 340 */ threadActivity.remove(t); /* */ } /* */ /* */ private CDDBStatus dbAdd(CDDBStatus status) /* */ { /* 345 */ synchronized (knownDisksMutex) { /* 346 */ if (knownDisks.get(this.hash) == null) { /* 347 */ knownDisks.put(this.hash, status); /* */ try { /* 349 */ new Saver(new URL("home:cdcache.db")).save(knownDisks); /* */ } catch (Exception localException) {} /* */ } /* */ } /* 353 */ return status; /* */ } /* */ /* */ /* */ private CDDBStatus dbLookup() /* */ { /* 359 */ synchronized (knownDisksMutex) { /* 360 */ if (knownDisks == null) { /* */ try { /* 362 */ knownDisks = /* 363 */ (Hashtable)new Restorer(new URL("home:cdcache.db")).restore(); /* */ } catch (Exception ex) { /* 365 */ knownDisks = new Hashtable(); /* */ } /* */ } /* 368 */ return (CDDBStatus)knownDisks.get(this.hash); /* */ } /* */ } /* */ } /* Location: C:\Program Files (x86)\Worlds Inc\WorldsPlayer - Win7\lib\worlds.jar!\NET\worlds\scape\CDDBLookup.class * Java compiler version: 6 (50.0) * JD-Core Version: 0.7.1 */