From 408a344aaec19da05ee03115465a7ee4a3f656f9 Mon Sep 17 00:00:00 2001 From: aum Date: Wed, 20 Apr 2005 18:53:08 +0000 Subject: [PATCH] added QConsole --- apps/q/java/build.xml | 15 +- apps/q/java/src/HTML/Template.java | 1 + apps/q/java/src/net/i2p/aum/q/QConsole.java | 281 ++++++++++++++++++++ 3 files changed, 296 insertions(+), 1 deletion(-) create mode 100644 apps/q/java/src/net/i2p/aum/q/QConsole.java diff --git a/apps/q/java/build.xml b/apps/q/java/build.xml index f79680009..ea8055df3 100644 --- a/apps/q/java/build.xml +++ b/apps/q/java/build.xml @@ -17,6 +17,7 @@ + @@ -54,7 +55,19 @@ - + + + + + + + + + + + + + diff --git a/apps/q/java/src/HTML/Template.java b/apps/q/java/src/HTML/Template.java index 6c50e36e5..5baf9c425 100644 --- a/apps/q/java/src/HTML/Template.java +++ b/apps/q/java/src/HTML/Template.java @@ -36,6 +36,7 @@ package HTML; import java.util.*; import java.io.*; + import HTML.Tmpl.Element.*; import HTML.Tmpl.Parsers.*; import HTML.Tmpl.Util; diff --git a/apps/q/java/src/net/i2p/aum/q/QConsole.java b/apps/q/java/src/net/i2p/aum/q/QConsole.java new file mode 100644 index 000000000..ed3b80e18 --- /dev/null +++ b/apps/q/java/src/net/i2p/aum/q/QConsole.java @@ -0,0 +1,281 @@ +/* + * QConsole.java + * + * Created on 19 April 2005, 12:28 + */ + +package net.i2p.aum.q; + +import java.io.*; +import java.net.*; +import java.util.*; + +import javax.servlet.*; +import javax.servlet.http.*; + +import org.apache.xmlrpc.*; + +import HTML.*; + +/** + * + * @author david + * @version + */ +public class QConsole extends HttpServlet { + + XmlRpcClient nodeProxy; + + boolean nodeExists = false; + boolean nodeIsRunning = false; + String nodeDirStr; + File nodeDir; + String nodePrivKey; + String sep = File.separator; + String cpsep = File.pathSeparator; + Properties options; + + /** Initializes the servlet. + */ + public void init(ServletConfig config) throws ServletException { + super.init(config); + try { + nodeProxy = new XmlRpcClient("http://localhost:7651"); + } catch (Exception e) { + e.printStackTrace(); + } + + // load config, if any + options = new Properties(); + try { + FileInputStream f = new FileInputStream("qconsole.conf"); + options.load(f); + + } catch (Exception e) { + // no valid config + e.printStackTrace(); + System.out.println("BAD OR MISSING CONF"); + } + } + + /** Destroys the servlet. + */ + public void destroy() { + + } + + /** Processes requests for both HTTP GET and POST methods. + * @param request servlet request + * @param response servlet response + */ + protected void processRequest(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + + findNode(); + determineIfNodeIsRunning(); + + Hashtable vars = parseVars(request.getQueryString()); + + response.setContentType("text/html"); + PrintWriter out = response.getWriter(); + out.println(""); + out.println(""); + out.println("QConsole"); + out.println(""); + out.println(""); + + out.println("

Q Node Manager

"); + + //out.println("debug: vars='"+vars+"'

"); + + if (vars.containsKey("startnode") && !nodeIsRunning) { + startNode(); + if (!nodeIsRunning) { + out.println("Failed to start node :(

"); + } + + } else if (vars.containsKey("stopnode") && nodeIsRunning) { + stopNode(); + nodeIsRunning = false; + } + + if (nodeIsRunning) { + out.println("Q Node is running

"); + out.print("Node Console"); + out.print(" | "); + out.println("Stop Node"); + } else { + out.println("Q Node is not running

"); + out.println("Start Node"); + } + + out.println(""); + out.println(""); + /* */ + out.close(); + } + + /** Handles the HTTP GET method. + * @param request servlet request + * @param response servlet response + */ + protected void doGet(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + processRequest(request, response); + } + + /** Handles the HTTP POST method. + * @param request servlet request + * @param response servlet response + */ + protected void doPost(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + processRequest(request, response); + } + + /** Returns a short description of the servlet. + */ + public String getServletInfo() { + return "Short description"; + } + + /** try to find node */ + public void findNode() { + + try { + nodeDirStr = System.getProperties().getProperty("user.home") + + sep + ".quartermaster_client"; + + // yay, found a node (we hope), create an xmlrpc client for talking + // to that node + String propPath = nodeDirStr + sep + "node.conf"; + File propFile = new File(propPath); + FileInputStream propIn = new FileInputStream(propPath); + Properties prop = new Properties(); + prop.load(propIn); + + nodePrivKey = prop.getProperty("privKey"); + + // presence of private key indicates node exists + nodeExists = nodePrivKey != null; + + } catch (Exception e) { + // node doesn't exist + } + + } + + public void startNode() { + + int i; + String [] jars = { + "i2p", "mstreaming", "aum", + }; + + String cp = ""; + + String jarsDir = "lib"; + + for (i=0; i 0) { + cp += cpsep; + } + cp += jarsDir + sep + jars[i] + ".jar"; + } + + System.out.println("cp='"+cp+"'"); + + // build up args + int nopts = options.size(); + String args = ""; + args += "java"; + for (Enumeration e = options.propertyNames(); e.hasMoreElements();) { + String opt = (String)e.nextElement(); + String arg = "-D" + opt + "=" + options.getProperty(opt); + System.out.println(arg); + args += " " + arg; + } + + args += " -cp " + cp; + args += " net.i2p.aum.q.QMgr"; + args += " foreground"; + + Runtime runtime = Runtime.getRuntime(); + + // and spawn the start job + try { + //runtime.exec(startForegroundArgs, propLines); + System.out.println("args='"+args+"'"); + runtime.exec(args, null); + } catch (IOException e) { + e.printStackTrace(); + } + + // wait a bit + sleep(3); + + // try for 10s to contact node + for (i=0; i<10; i++) { + sleep(1); + determineIfNodeIsRunning(); + if (nodeIsRunning) { + break; + } + } + } + + public void stopNode() { + + Vector args = new Vector(); + args.addElement(nodePrivKey); + try { + System.out.println("stopping node..."); + nodeProxy.execute("i2p.q.shutdown", args); + } catch (Exception e) { + + } + System.out.println("node terminated"); + } + + /** returns true if node is up */ + public void determineIfNodeIsRunning() { + try { + nodeProxy.execute("i2p.q.ping", new Vector()); + nodeIsRunning = true; + } catch (Exception e) { + nodeIsRunning = false; + return; + } + } + + public void sleep(int n) { + try { + Thread.sleep(n * 1000); + } catch (Exception e) {} + } + + public Hashtable parseVars(String raw) { + Hashtable h = new Hashtable(); + + if (raw == null) { + return h; + } + + URLDecoder u = new URLDecoder(); + String [] items = raw.split("[&]"); + String dec; + for (int i=0; i