// {{{ handleClient() method private boolean handleClient(final Socket client, DataInputStream in) throws Exception { int key = in.readInt(); if (key != authKey) { Log.log( Log.ERROR, this, client + ": wrong" + " authorization key (got " + key + ", expected " + authKey + ")"); in.close(); client.close(); return false; } else { // Reset the timeout client.setSoTimeout(0); Log.log(Log.DEBUG, this, client + ": authenticated" + " successfully"); final String script = in.readUTF(); Log.log(Log.DEBUG, this, script); SwingUtilities.invokeLater( new Runnable() { public void run() { try { NameSpace ns = new NameSpace(BeanShell.getNameSpace(), "EditServer namespace"); ns.setVariable("socket", client); BeanShell.eval(null, ns, script); } catch (org.gjt.sp.jedit.bsh.UtilEvalError e) { Log.log(Log.ERROR, this, e); } finally { try { BeanShell.getNameSpace().setVariable("socket", null); } catch (org.gjt.sp.jedit.bsh.UtilEvalError e) { Log.log(Log.ERROR, this, e); } } } }); return true; } } // }}}
// {{{ run() method public void run() { for (; ; ) { if (abort) return; Socket client = null; try { client = socket.accept(); // Stop script kiddies from opening the edit // server port and just leaving it open, as a // DoS client.setSoTimeout(1000); Log.log(Log.MESSAGE, this, client + ": connected"); DataInputStream in = new DataInputStream(client.getInputStream()); if (!handleClient(client, in)) abort = true; } catch (Exception e) { if (!abort) Log.log(Log.ERROR, this, e); abort = true; } finally { /* if(client != null) { try { client.close(); } catch(Exception e) { Log.log(Log.ERROR,this,e); } client = null; } */ } } } // }}}