comparison src/luan/modules/Rpc.luan @ 775:1a68fc55a80c

simplify dir structure
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 26 Aug 2016 14:36:40 -0600
parents core/src/luan/modules/Rpc.luan@3f461f85243d
children bae2d0c2576c
comparison
equal deleted inserted replaced
774:3e30cf310e56 775:1a68fc55a80c
1 java()
2 local RpcLuan = require "java:luan.modules.RpcLuan"
3 local Luan = require "luan:Luan.luan"
4 local error = Luan.error
5 local set_metatable = Luan.set_metatable or error()
6 local try = Luan.try or error()
7 local Io = require "luan:Io.luan"
8 local Thread = require "luan:Thread.luan"
9 local Logging = require "luan:logging/Logging.luan" -- external dependency
10 local logger = Logging.logger "Rpc"
11
12
13 local M = {}
14
15 M.port = 9101
16
17 M.call = RpcLuan.call -- Rpc.call(socket,fn_name,...)
18
19 M.functions = {}
20
21 function M.respond(socket,fns)
22 RpcLuan.respond( socket, fns or M.functions )
23 end
24
25 function M.remote_socket(socket_uri)
26 local mt = {}
27 function mt.__index(_,key)
28 return function(...)
29 local socket = Io.uri(socket_uri)
30 return M.call(socket,key,...)
31 end
32 end
33 local t = {}
34 set_metatable(t,mt)
35 return t
36 end
37
38 function M.remote(domain)
39 local socket = "socket:" .. domain .. ":" .. M.port
40 return M.remote_socket(socket)
41 end
42
43 function M.serve(port,fns)
44 local server = Io.socket_server(port or M.port)
45 while true do
46 try {
47 function()
48 local socket = server()
49 local function respond()
50 try {
51 function()
52 M.respond(socket,fns)
53 end
54 catch = function(e)
55 logger.error(e)
56 end
57 }
58 end
59 Thread.fork(respond)
60 end
61 catch = function(e)
62 logger.error(e)
63 end
64 }
65 end
66 end
67
68 return M