UI newui(Session sess) { if (ui != null) ui.destroy(); ui = new UI(new Coord(w, h), sess); ui.root.guprof = uprof; ui.root.grprof = rprof; ui.root.ggprof = gprof; if (getParent() instanceof Console.Directory) ui.cons.add((Console.Directory) getParent()); ui.cons.add(this); if (glconf != null) ui.cons.add(glconf); return (ui); }
public void run() { try { Thread drawthread = new HackThread(drawfun, "Render thread"); drawthread.start(); synchronized (drawfun) { while (state == null) drawfun.wait(); } try { long now, then; long frames[] = new long[128]; int framep = 0, waited[] = new int[128]; while (true) { int fwaited = 0; Debug.cycle(); UI ui = this.ui; then = System.currentTimeMillis(); CPUProfile.Frame curf = null; if (Config.profile) curf = uprof.new Frame(); synchronized (ui) { if (ui.sess != null) ui.sess.glob.ctick(); dispatch(); ui.tick(); if ((ui.root.sz.x != w) || (ui.root.sz.y != h)) ui.root.resize(new Coord(w, h)); } if (curf != null) curf.tick("dsp"); BGL buf = new BGL(); GLState.Applier state = this.state; rootdraw(state, ui, buf); if (curf != null) curf.tick("draw"); synchronized (drawfun) { now = System.currentTimeMillis(); while (bufdraw != null) drawfun.wait(); bufdraw = new Frame(buf, state.cgl); drawfun.notifyAll(); fwaited += System.currentTimeMillis() - now; } ui.audio.cycle(); if (curf != null) curf.tick("aux"); now = System.currentTimeMillis(); long fd = bgmode ? this.bgfd : this.fd; if (now - then < fd) { synchronized (events) { events.wait(fd - (now - then)); } fwaited += System.currentTimeMillis() - now; } frames[framep] = now; waited[framep] = fwaited; for (int i = 0, ckf = framep, twait = 0; i < frames.length; i++) { ckf = (ckf - 1 + frames.length) % frames.length; twait += waited[ckf]; if (now - frames[ckf] > 1000) { fps = i; uidle = ((double) twait) / ((double) (now - frames[ckf])); break; } } framep = (framep + 1) % frames.length; if (curf != null) curf.tick("wait"); if (curf != null) curf.fin(); if (Thread.interrupted()) throw (new InterruptedException()); } } finally { drawthread.interrupt(); drawthread.join(); } } catch (InterruptedException e) { } finally { ui.destroy(); } }
public Session run(HavenPanel hp) throws InterruptedException { ui = hp.newui(null); ui.setreceiver(this); ui.bind(new LoginScreen(ui.root), 1); String username; boolean savepw = false; Utils.setpref("password", ""); byte[] token = null; if (Utils.getpref("savedtoken", "").length() == 64) token = Utils.hex2byte(Utils.getpref("savedtoken", null)); username = Utils.getpref("username", ""); String authserver = (Config.authserv == null) ? address : Config.authserv; retry: do { byte[] cookie; if (initcookie != null) { username = inituser; cookie = initcookie; initcookie = null; } else if (token != null) { savepw = true; ui.uimsg(1, "token", username); while (true) { Message msg; synchronized (msgs) { while ((msg = msgs.poll()) == null) msgs.wait(); } if (msg.id == 1) { if (msg.name == "login") { break; } else if (msg.name == "forget") { token = null; Utils.setpref("savedtoken", ""); continue retry; } } } ui.uimsg(1, "prg", "Authenticating..."); AuthClient auth = null; try { auth = new AuthClient(authserver, username); if (!auth.trytoken(token)) { auth.close(); token = null; Utils.setpref("savedtoken", ""); ui.uimsg(1, "error", "Invalid save"); continue retry; } cookie = auth.cookie; } catch (java.io.IOException e) { ui.uimsg(1, "error", e.getMessage()); continue retry; } finally { try { if (auth != null) auth.close(); } catch (java.io.IOException e) { } } } else { String password; ui.uimsg(1, "passwd", username, savepw); while (true) { Message msg; synchronized (msgs) { while ((msg = msgs.poll()) == null) msgs.wait(); } if (msg.id == 1) { if (msg.name == "login") { username = (String) msg.args[0]; password = (String) msg.args[1]; savepw = (Boolean) msg.args[2]; break; } } } ui.uimsg(1, "prg", "Authenticating..."); AuthClient auth = null; try { try { auth = new AuthClient(authserver, username); } catch (UnknownHostException e) { ui.uimsg(1, "error", "Could not locate server"); continue retry; } if (!auth.trypasswd(password)) { auth.close(); password = ""; ui.uimsg(1, "error", "Username or password incorrect"); continue retry; } cookie = auth.cookie; if (savepw) { if (auth.gettoken()) Utils.setpref("savedtoken", Utils.byte2hex(auth.token)); } } catch (java.io.IOException e) { ui.uimsg(1, "error", e.getMessage()); continue retry; } finally { try { if (auth != null) auth.close(); } catch (java.io.IOException e) { } } } ui.uimsg(1, "prg", "Connecting..."); try { sess = new Session(InetAddress.getByName(address), username, cookie); } catch (UnknownHostException e) { ui.uimsg(1, "error", "Could not locate server"); continue retry; } Thread.sleep(100); while (true) { if (sess.state == "") { Utils.setpref("username", username); ui.destroy(1); break retry; } else if (sess.connfailed != 0) { String error; switch (sess.connfailed) { case 1: error = "Invalid authentication token"; break; case 2: error = "Already logged in"; break; case 3: error = "Could not connect to server"; break; case 4: error = "This client is too old"; break; case 5: error = "Authentication token expired"; break; default: error = "Connection failed"; break; } ui.uimsg(1, "error", error); sess = null; continue retry; } synchronized (sess) { sess.wait(); } } } while (true); haven.error.ErrorHandler.setprop("usr", sess.username); return (sess); // (new RemoteUI(sess, ui)).start(); }