comparison src/luan/modules/http/LuanHandler.java @ 1333:25746915a241

merge Luan and LuanState
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 12 Feb 2019 22:33:40 -0700
parents 11b7e11f9ed5
children e0cf0d108a77
comparison
equal deleted inserted replaced
1332:11b7e11f9ed5 1333:25746915a241
18 import luan.webserver.Status; 18 import luan.webserver.Status;
19 import luan.webserver.Server; 19 import luan.webserver.Server;
20 import luan.webserver.Handler; 20 import luan.webserver.Handler;
21 import luan.webserver.ResponseOutputStream; 21 import luan.webserver.ResponseOutputStream;
22 import luan.Luan; 22 import luan.Luan;
23 import luan.LuanState;
24 import luan.LuanTable; 23 import luan.LuanTable;
25 import luan.LuanFunction; 24 import luan.LuanFunction;
26 import luan.LuanJavaFunction; 25 import luan.LuanJavaFunction;
27 import luan.LuanCloner; 26 import luan.LuanCloner;
28 import luan.LuanException; 27 import luan.LuanException;
29 import luan.modules.PackageLuan; 28 import luan.modules.PackageLuan;
30 import luan.modules.BasicLuan; 29 import luan.modules.BasicLuan;
31 import luan.modules.logging.LuanLogger; 30 import luan.modules.logging.LuanLogger;
32 31
33 32
34 public final class LuanHandler implements Handler, LuanState.OnClose { 33 public final class LuanHandler implements Handler, Luan.OnClose {
35 private final LuanState luanInit; 34 private final Luan luanInit;
36 private final Logger logger; 35 private final Logger logger;
37 private final ReadWriteLock lock = new ReentrantReadWriteLock(); 36 private final ReadWriteLock lock = new ReentrantReadWriteLock();
38 private final List<Reference<Closeable>> onClose = new ArrayList<Reference<Closeable>>(); 37 private final List<Reference<Closeable>> onClose = new ArrayList<Reference<Closeable>>();
39 private volatile LuanState currentLuan; 38 private volatile Luan currentLuan;
40 private volatile boolean isDisabled = false; 39 private volatile boolean isDisabled = false;
41 40
42 private static final Method resetLuanMethod; 41 private static final Method resetLuanMethod;
43 private static final Method evalInRootMethod; 42 private static final Method evalInRootMethod;
44 private static final Method disableLuanMethod; 43 private static final Method disableLuanMethod;
50 } catch(NoSuchMethodException e) { 49 } catch(NoSuchMethodException e) {
51 throw new RuntimeException(e); 50 throw new RuntimeException(e);
52 } 51 }
53 } 52 }
54 53
55 public LuanHandler(LuanState luanInit) { 54 public LuanHandler(Luan luanInit) {
56 this.luanInit = luanInit; 55 this.luanInit = luanInit;
57 this.logger = LuanLogger.getLogger(luanInit,LuanHandler.class.getName()); 56 this.logger = LuanLogger.getLogger(luanInit,LuanHandler.class.getName());
58 try { 57 try {
59 LuanTable Http = (LuanTable)luanInit.require("luan:http/Http.luan"); 58 LuanTable Http = (LuanTable)luanInit.require("luan:http/Http.luan");
60 Http.put( "reset_luan", new LuanJavaFunction(resetLuanMethod,this) ); 59 Http.put( "reset_luan", new LuanJavaFunction(resetLuanMethod,this) );
64 throw new RuntimeException(e); 63 throw new RuntimeException(e);
65 } 64 }
66 currentLuan = newLuan(); 65 currentLuan = newLuan();
67 } 66 }
68 67
69 private LuanState newLuan() { 68 private Luan newLuan() {
70 LuanCloner cloner = new LuanCloner(LuanCloner.Type.COMPLETE); 69 LuanCloner cloner = new LuanCloner(LuanCloner.Type.COMPLETE);
71 LuanState luan = (LuanState)cloner.clone(luanInit); 70 Luan luan = (Luan)cloner.clone(luanInit);
72 luan.onClose = this; 71 luan.onClose = this;
73 try { 72 try {
74 PackageLuan.load(luan,"site:/init.luan"); 73 PackageLuan.load(luan,"site:/init.luan");
75 } catch(LuanException e) { 74 } catch(LuanException e) {
76 //e.printStackTrace(); 75 //e.printStackTrace();
79 } 78 }
80 return luan; 79 return luan;
81 } 80 }
82 81
83 /* 82 /*
84 public LuanState getLuan() { 83 public Luan getLuan() {
85 return luan; 84 return luan;
86 } 85 }
87 */ 86 */
88 @Override public Response handle(Request request) { 87 @Override public Response handle(Request request) {
89 if( isDisabled ) 88 if( isDisabled )
136 135
137 public Object call_rpc(String fnName,Object... args) throws LuanException { 136 public Object call_rpc(String fnName,Object... args) throws LuanException {
138 lock.readLock().lock(); 137 lock.readLock().lock();
139 try { 138 try {
140 LuanFunction fn; 139 LuanFunction fn;
141 LuanState luan = currentLuan; 140 Luan luan = currentLuan;
142 synchronized(luan) { 141 synchronized(luan) {
143 PackageLuan.enableLoad(luan,"luan:Rpc.luan"); 142 PackageLuan.enableLoad(luan,"luan:Rpc.luan");
144 LuanTable rpc = (LuanTable)luan.require("luan:Rpc.luan"); 143 LuanTable rpc = (LuanTable)luan.require("luan:Rpc.luan");
145 LuanTable fns = (LuanTable)rpc.get("functions"); 144 LuanTable fns = (LuanTable)rpc.get("functions");
146 fn = (LuanFunction)fns.get(fnName); 145 fn = (LuanFunction)fns.get(fnName);
147 if( fn == null ) 146 if( fn == null )
148 throw new LuanException( "function not found: " + fnName ); 147 throw new LuanException( "function not found: " + fnName );
149 LuanCloner cloner = new LuanCloner(LuanCloner.Type.INCREMENTAL); 148 LuanCloner cloner = new LuanCloner(LuanCloner.Type.INCREMENTAL);
150 luan = (LuanState)cloner.clone(luan); 149 luan = (Luan)cloner.clone(luan);
151 fn = (LuanFunction)cloner.get(fn); 150 fn = (LuanFunction)cloner.get(fn);
152 } 151 }
153 return fn.call(luan,args); 152 return fn.call(luan,args);
154 } finally { 153 } finally {
155 lock.readLock().unlock(); 154 lock.readLock().unlock();
176 175
177 public Object runLuan(String sourceText,String sourceName) throws LuanException { 176 public Object runLuan(String sourceText,String sourceName) throws LuanException {
178 lock.readLock().lock(); 177 lock.readLock().lock();
179 try { 178 try {
180 LuanFunction fn = Luan.load(sourceText,sourceName); 179 LuanFunction fn = Luan.load(sourceText,sourceName);
181 LuanState luan = currentLuan; 180 Luan luan = currentLuan;
182 synchronized(luan) { 181 synchronized(luan) {
183 LuanCloner cloner = new LuanCloner(LuanCloner.Type.INCREMENTAL); 182 LuanCloner cloner = new LuanCloner(LuanCloner.Type.INCREMENTAL);
184 LuanState luan2 = (LuanState)cloner.clone(luan); 183 Luan luan2 = (Luan)cloner.clone(luan);
185 return fn.call(luan2); 184 return fn.call(luan2);
186 } 185 }
187 } finally { 186 } finally {
188 lock.readLock().unlock(); 187 lock.readLock().unlock();
189 } 188 }
190 } 189 }
191 190
192 public void eval_in_root(String text) throws LuanException { 191 public void eval_in_root(String text) throws LuanException {
193 LuanState oldLuan = currentLuan; 192 Luan oldLuan = currentLuan;
194 LuanState luan; 193 Luan luan;
195 synchronized(oldLuan) { 194 synchronized(oldLuan) {
196 LuanCloner cloner = new LuanCloner(LuanCloner.Type.COMPLETE); 195 LuanCloner cloner = new LuanCloner(LuanCloner.Type.COMPLETE);
197 luan = (LuanState)cloner.clone(oldLuan); 196 luan = (Luan)cloner.clone(oldLuan);
198 } 197 }
199 luan.onClose = this; 198 luan.onClose = this;
200 BasicLuan.load(text,"<eval_in_root>",null).call(luan); 199 BasicLuan.load(text,"<eval_in_root>",null).call(luan);
201 currentLuan = luan; 200 currentLuan = luan;
202 } 201 }