changeset 786:fe63c508a177

add Http.reset_luan()
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 02 Sep 2016 17:43:29 -0600
parents d69d3c51c44e
children c49980cdece6
files src/luan/LuanState.java src/luan/modules/http/LuanHandler.java src/luan/modules/http/NotFound.java src/luan/modules/http/Server.luan
diffstat 4 files changed, 84 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
diff -r d69d3c51c44e -r fe63c508a177 src/luan/LuanState.java
--- a/src/luan/LuanState.java	Thu Sep 01 21:32:28 2016 -0600
+++ b/src/luan/LuanState.java	Fri Sep 02 17:43:29 2016 -0600
@@ -58,11 +58,11 @@
 		}
 		onClose.clear();
 	}
+
+	public final Object eval(String cmd,Object... args) throws LuanException {
+		return Luan.load(cmd,"eval").call(this,args);
+	}
 /*
-	public final Object eval(String cmd) throws LuanException {
-		return eval(cmd,new LuanTable());
-	}
-
 	public final Object eval(String cmd,LuanTable env) throws LuanException {
 		LuanFunction fn = BasicLuan.load(this,cmd,"eval",env,true);
 		return fn.call(this);
diff -r d69d3c51c44e -r fe63c508a177 src/luan/modules/http/LuanHandler.java
--- a/src/luan/modules/http/LuanHandler.java	Thu Sep 01 21:32:28 2016 -0600
+++ b/src/luan/modules/http/LuanHandler.java	Fri Sep 02 17:43:29 2016 -0600
@@ -1,6 +1,9 @@
 package luan.modules.http;
 
 import java.io.IOException;
+import java.lang.reflect.Method;
+import java.util.concurrent.locks.ReadWriteLock;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
@@ -9,24 +12,43 @@
 import org.slf4j.LoggerFactory;
 import org.eclipse.jetty.server.Request;
 import org.eclipse.jetty.server.handler.AbstractHandler;
+import luan.Luan;
 import luan.LuanState;
 import luan.LuanTable;
 import luan.LuanFunction;
+import luan.LuanJavaFunction;
 import luan.LuanCloner;
 import luan.LuanException;
 import luan.modules.PackageLuan;
 
 
 public class LuanHandler extends AbstractHandler {
-	private final LuanState luan;
+	private final LuanState luanInit;
 	private final Logger logger;
 	private String welcomeFile = "index.html";
+	private final ReadWriteLock lock = new ReentrantReadWriteLock();
+	private LuanState luan;
+
+	private static final Method resetLuanMethod;
+	static {
+		try {
+			resetLuanMethod = LuanHandler.class.getMethod("reset_luan");
+		} catch(NoSuchMethodException e) {
+			throw new RuntimeException(e);
+		}
+	}
 
 	public LuanHandler(LuanState luan,String loggerRoot) {
-		this.luan = luan;
+		this.luanInit = luan;
 		if( loggerRoot==null )
 			loggerRoot = "";
 		logger = LoggerFactory.getLogger(loggerRoot+LuanHandler.class.getName());
+		try {
+			LuanTable Http = (LuanTable)PackageLuan.require(luanInit,"luan:http/Http.luan");
+			Http.rawPut( "reset_luan", new LuanJavaFunction(resetLuanMethod,this) );
+		} catch(LuanException e) {
+			throw new RuntimeException(e);
+		}
 	}
 
 	@Override public void handle(String target,Request baseRequest,HttpServletRequest request,HttpServletResponse response) 
@@ -34,6 +56,7 @@
 	{
 		if( target.endsWith("/") )
 			target += welcomeFile;
+		lock.readLock().lock();
 		try {
 			if( !HttpServicer.service(luan,request,response,"site:"+target+".luan") )
 				return;
@@ -42,6 +65,8 @@
 			String err = e.getFullMessage();
 			logger.error(err);
 			response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,err);
+		} finally {
+			lock.readLock().unlock();
 		}
 		baseRequest.setHandled(true);
 	}
@@ -50,6 +75,13 @@
 		this.welcomeFile = welcomeFile;
 	}
 
+	@Override protected void doStart() throws Exception {
+//		Thread.dumpStack();
+//System.out.println("qqqqqqqqqqqqqqqqqqqq doStart "+this);
+		setLuan();
+		super.doStart();
+	}
+
 	@Override protected void doStop() throws Exception {
 		synchronized(luan) {
 			luan.close();
@@ -65,10 +97,7 @@
 */
 
 	public Object call_rpc(String fnName,Object... args) throws LuanException {
-		return callRpc(luan,fnName,args);
-	}
-
-	public static Object callRpc(LuanState luan,String fnName,Object... args) throws LuanException {
+		LuanState luan = this.luan;
 		synchronized(luan) {
 			LuanCloner cloner = new LuanCloner(LuanCloner.Type.COMPLETE);
 			luan = (LuanState)cloner.clone(luan);
@@ -81,4 +110,40 @@
 		return fn.call(luan,args);
 	}
 
+	public void reset_luan() {
+		new Thread() {
+			public void run() {
+				lock.writeLock().lock();
+				try {
+					synchronized(luan) {
+						luan.close();
+						setLuan();
+					}
+				} catch(IOException e) {
+					logger.error("reset_luan failed",e);
+				} finally {
+					lock.writeLock().unlock();
+				}
+			}
+		}.start();
+	}
+
+	private void setLuan() {
+		LuanCloner cloner = new LuanCloner(LuanCloner.Type.COMPLETE);
+		luan = (LuanState)cloner.clone(luanInit);
+		try {
+			PackageLuan.load(luan,"site:/init.luan");
+		} catch(LuanException e) {
+			String err = e.getFullMessage();
+			logger.error(err);
+		}
+	}
+
+	public Object runLuan(String sourceText,String sourceName) throws LuanException {
+		LuanFunction fn = Luan.load(sourceText,sourceName);
+		synchronized(luan) {
+			return fn.call(luan);
+		}
+	}
+
 }
diff -r d69d3c51c44e -r fe63c508a177 src/luan/modules/http/NotFound.java
--- a/src/luan/modules/http/NotFound.java	Thu Sep 01 21:32:28 2016 -0600
+++ b/src/luan/modules/http/NotFound.java	Fri Sep 02 17:43:29 2016 -0600
@@ -4,19 +4,20 @@
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import org.eclipse.jetty.server.Request;
-import luan.LuanState;
+import org.eclipse.jetty.server.handler.AbstractHandler;
 
 
-public class NotFound extends LuanHandler {
+public class NotFound extends AbstractHandler {
+	private final LuanHandler luanHandler;
 
-	public NotFound(LuanState luan,String loggerRoot) {
-		super(luan,loggerRoot);
+	public NotFound(LuanHandler luanHandler) {
+		this.luanHandler = luanHandler;
 	}
 
 	@Override public void handle(String target,Request baseRequest,HttpServletRequest request,HttpServletResponse response) 
 		throws IOException
 	{
-		super.handle("/not_found",baseRequest,request,response);
+		luanHandler.handle("/not_found",baseRequest,request,response);
 	}
 
 }
diff -r d69d3c51c44e -r fe63c508a177 src/luan/modules/http/Server.luan
--- a/src/luan/modules/http/Server.luan	Thu Sep 01 21:32:28 2016 -0600
+++ b/src/luan/modules/http/Server.luan	Fri Sep 02 17:43:29 2016 -0600
@@ -91,10 +91,10 @@
 	M.resource_handler.setResourceBase(Io.uri(base).to_string())
 	M.resource_handler.setWelcomeFiles {M.welcome_file}
 	M.luan_handler.setWelcomeFile(M.welcome_file)
-	M.handlers.addHandler(NotFound.new())
+	M.handlers.addHandler(NotFound.new(M.luan_handler))
 	M.server = Server.new(M.port)
 	M.server.setHandler(hc)
-	Package.load("site:/init.luan")
+--	Package.load("site:/init.luan")
 end
 
 function M.start()
@@ -111,8 +111,8 @@
 
 function M.serve(dir)
 	M.init(dir)
+	M.start_rpc()
 	M.start()
-	M.start_rpc()
 end
 
 return M