-
Notifications
You must be signed in to change notification settings - Fork 2
/
gr.lua
77 lines (66 loc) · 1.31 KB
/
gr.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
local log = require 'box.log'
local K = ...
local tcp = require( K..'.tcp' )
local udp = require( K..'.udp' )
local old = rawget(_G,K)
if old then
-- todo signal old to shutdown
for k,i in pairs(old) do
if type(i) == 'table' then
print("got key ",k," ",old[k])
if i._c and i._c.fihish then
i._c:finish()
end
end
end
end
local function new ( name )
return {
_name = name,
_c = false,
_warned = false,
}
end
local M = new('main')
-- M.__index = M
function M:config(host,port,proto)
assert(type(self) == 'table', "Static call prohibited")
if proto == 'tcp' then
local cnn = tcp(host,port)
self._c = cnn
elseif proto == 'udp' then
local cnn = udp(host,port)
self._c = cnn
else
error("Bad protocol "..proto, 2)
end
end
function M:send(key,value)
if not self._c then
if not self._warned then
self._warned = true
log._log( log.WARN, "Instance `%s' not configured prematurely", self._name )
end
return
end
self._c:send(key, value)
end
setmetatable(M,{
__index = function(t,k)
print("Creating new instance ",k)
local newobj = setmetatable(
new(k),
{
__index = t,
-- __newindex = function() error("",2) end
}
)
rawset(t,k,newobj)
return newobj
end,
__newindex = function(t,k,v)
print("new index ",k)
error("Prohibited",2)
end
})
rawset(_G,K,M)