Mercurial Hosting > luan
changeset 743:2c41f2aec92f
improve Rpc and implement rpc call for local webserver
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Wed, 13 Jul 2016 17:27:35 -0600 |
parents | 5578541125ea |
children | 4b8695f1cfc4 |
files | core/src/luan/modules/IoLuan.java core/src/luan/modules/Rpc.luan core/src/luan/modules/host/Hosting.luan http/src/luan/modules/http/LuanHandler.java http/src/luan/modules/http/Server.luan |
diffstat | 5 files changed, 84 insertions(+), 20 deletions(-) [+] |
line wrap: on
line diff
diff -r 5578541125ea -r 2c41f2aec92f core/src/luan/modules/IoLuan.java --- a/core/src/luan/modules/IoLuan.java Tue Jul 12 17:47:30 2016 -0600 +++ b/core/src/luan/modules/IoLuan.java Wed Jul 13 17:27:35 2016 -0600 @@ -618,11 +618,15 @@ public static final class LuanSocket extends LuanIO { private final Socket socket; - private LuanSocket(String host,int port) throws IOException { - this(new Socket(host,port)); + private LuanSocket(String host,int port) throws LuanException { + try { + this.socket = new Socket(host,port); + } catch(IOException e) { + throw new LuanException(e.toString()); + } } - private LuanSocket(Socket socket) throws IOException { + private LuanSocket(Socket socket) { this.socket = socket; }
diff -r 5578541125ea -r 2c41f2aec92f core/src/luan/modules/Rpc.luan --- a/core/src/luan/modules/Rpc.luan Tue Jul 12 17:47:30 2016 -0600 +++ b/core/src/luan/modules/Rpc.luan Wed Jul 13 17:27:35 2016 -0600 @@ -9,10 +9,17 @@ local M = {} +M.port = 9101 + M.call = RpcLuan.call -- Rpc.call(socket,fn_name,...) -M.respond = RpcLuan.respond -- Rpc.respond(socket,fns) + +M.functions = {} -function M.remote(socket_uri) +function M.respond(socket,fns) + RpcLuan.respond( socket, fns or M.functions ) +end + +function M.remote_socket(socket_uri) local mt = {} function mt.__index(_,key) return function(...) @@ -25,6 +32,19 @@ return t end +function M.remote(domain) + local socket = "socket:" .. domain .. ":" .. M.port + return M.remote_socket(socket) +end + +function M.responder(fns) + local server = Io.socket_server(M.port) + return function() + local socket = server() + M.respond(socket,fns) + end +end + --[[ function M.serve(port,fns) local server = Io.socket_server(port)
diff -r 5578541125ea -r 2c41f2aec92f core/src/luan/modules/host/Hosting.luan --- a/core/src/luan/modules/host/Hosting.luan Tue Jul 12 17:47:30 2016 -0600 +++ b/core/src/luan/modules/host/Hosting.luan Wed Jul 13 17:27:35 2016 -0600 @@ -14,14 +14,12 @@ local M = {} -M.port = 9101 function M.push(domain,password,dir) local my_dir = Io.uri("file:"..dir) my_dir.exists() or error("directory '"..dir.."' not found") my_dir.is_directory() or error("'"..dir.."' is not a directory") - local socket = "socket:" .. domain .. ":" .. M.port - local host = Rpc.remote(socket) + local host = Rpc.remote(domain) local tree = host.get(domain,password) if tree == nil then print("creating "..domain) @@ -64,32 +62,27 @@ end function M.delete(domain,password) - local socket = "socket:" .. domain .. ":" .. M.port - local host = Rpc.remote(socket) + local host = Rpc.remote(domain) host.delete(domain,password) end function M.exists(domain) - local socket = "socket:" .. domain .. ":" .. M.port - local host = Rpc.remote(socket) + local host = Rpc.remote(domain) return host.exists(domain) end function M.change_domain(old_domain,new_domain,password) - local socket = "socket:" .. new_domain .. ":" .. M.port - local host = Rpc.remote(socket) + local host = Rpc.remote(new_domain) return host.change_domain(old_domain,new_domain,password) end function M.change_password(domain,old_password,new_password) - local socket = "socket:" .. domain .. ":" .. M.port - local host = Rpc.remote(socket) + local host = Rpc.remote(domain) return host.change_password(domain,old_password,new_password) end function M.caller(domain) - local socket = "socket:" .. domain .. ":" .. M.port - local host = Rpc.remote(socket) + local host = Rpc.remote(domain) local mt = {} function mt.__index(_,key) return function(...)
diff -r 5578541125ea -r 2c41f2aec92f http/src/luan/modules/http/LuanHandler.java --- a/http/src/luan/modules/http/LuanHandler.java Tue Jul 12 17:47:30 2016 -0600 +++ b/http/src/luan/modules/http/LuanHandler.java Wed Jul 13 17:27:35 2016 -0600 @@ -10,7 +10,10 @@ import org.eclipse.jetty.server.Request; import org.eclipse.jetty.server.handler.AbstractHandler; import luan.LuanState; +import luan.LuanTable; +import luan.LuanFunction; import luan.LuanException; +import luan.modules.PackageLuan; public class LuanHandler extends AbstractHandler { @@ -58,4 +61,20 @@ super.destroy(); } */ + + 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 { + synchronized(luan) { + LuanTable rpc = (LuanTable)PackageLuan.require(luan,"luan:Rpc.luan"); + LuanTable fns = (LuanTable)rpc.get(luan,"functions"); + LuanFunction fn = (LuanFunction)fns.get(luan,fnName); + if( fn == null ) + throw new LuanException( "function not found: " + fnName ); + return fn.call(luan,args); + } + } + }
diff -r 5578541125ea -r 2c41f2aec92f http/src/luan/modules/http/Server.luan --- a/http/src/luan/modules/http/Server.luan Tue Jul 12 17:47:30 2016 -0600 +++ b/http/src/luan/modules/http/Server.luan Wed Jul 13 17:27:35 2016 -0600 @@ -1,10 +1,17 @@ +local Luan = require "luan:Luan.luan" +local error = Luan.error +local try = Luan.try or error() local String = require "luan:String.luan" -local gsub = String.gsub -local matches = String.matches +local gsub = String.gsub or error() +local matches = String.matches or error() local Io = require "luan:Io.luan" local Package = require "luan:Package.luan" +local Rpc = require "luan:Rpc.luan" +local Thread = require "luan:Thread.luan" local Http = require "luan:http/Http.luan" require "luan:logging/init.luan" -- initialize logging +local Logging = require "luan:logging/Logging.luan" +local logger = Logging.logger "http/Server" java() local Server = require "java:org.eclipse.jetty.server.Server" @@ -97,9 +104,30 @@ M.server.start() end +function M.start_rpc() + function Rpc.functions.call(domain,fn_name,...) + return M.luan_handler.call_rpc(fn_name,...) + end + + local function run_server() + local responder = Rpc.responder() + while true do + try { + responder + catch = function(e) + logger.warn(e) + end + } + end + end + + Thread.fork(run_server) +end + function M.serve(dir) M.init(dir) M.start() + M.start_rpc() end return M