forked from ergo-services/ergo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rex.go
189 lines (163 loc) · 4.45 KB
/
rex.go
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package ergo
// https://github.com/erlang/otp/blob/master/lib/kernel/src/rpc.erl
import (
"fmt"
"github.com/halturin/ergo/etf"
"github.com/halturin/ergo/lib"
)
type rpcFunction func(...etf.Term) etf.Term
type modFun struct {
module string
function string
}
var (
allowedModFun = []string{
"observer_backend",
}
)
type rex struct {
GenServer
process *Process
methods map[modFun]rpcFunction
}
// Init initializes process state using arbitrary arguments
// Init(...) -> state
func (r *rex) Init(p *Process, args ...interface{}) (state interface{}) {
lib.Log("REX: Init: %#v", args)
r.process = p
r.methods = make(map[modFun]rpcFunction, 0)
for i := range allowedModFun {
mf := modFun{
allowedModFun[i],
"*",
}
r.methods[mf] = nil
}
return nil
}
// HandleCast -> ("noreply", state) - noreply
// ("stop", reason) - stop with reason
func (r *rex) HandleCast(message etf.Term, state interface{}) (string, interface{}) {
lib.Log("REX: HandleCast: %#v", message)
return "noreply", state
}
// HandleCall serves incoming messages sending via gen_server:call
// HandleCall -> ("reply", message, state) - reply
// ("noreply", _, state) - noreply
// ("stop", reason, _) - normal stop
func (r *rex) HandleCall(from etf.Tuple, message etf.Term, state interface{}) (string, etf.Term, interface{}) {
lib.Log("REX: HandleCall: %#v, From: %#v", message, from)
switch m := message.(type) {
case etf.Tuple:
//etf.Tuple{"call", "observer_backend", "sys_info",
// etf.List{}, etf.Pid{Node:"erl-examplenode@127.0.0.1", Id:0x46, Serial:0x0, Creation:0x2}}
switch m.Element(1) {
case etf.Atom("call"):
module := m.Element(2).(etf.Atom)
function := m.Element(3).(etf.Atom)
args := m.Element(4).(etf.List)
reply, state1 := r.handleRPC(module, function, args, state)
if reply != nil {
return "reply", reply, state1
}
to := etf.Tuple{string(module), r.process.Node.FullName}
m := etf.Tuple{m.Element(3), m.Element(4)}
reply, err := r.process.Call(to, m)
if err != nil {
reply = etf.Term(etf.Tuple{etf.Atom("error"), err})
}
return "reply", reply, state
case etf.Atom("$provide"):
module := m.Element(2).(etf.Atom)
function := m.Element(3).(etf.Atom)
fun := m.Element(4).(rpcFunction)
mf := modFun{
module: string(module),
function: string(function),
}
if _, ok := r.methods[mf]; ok {
return "reply", etf.Atom("taken"), state
}
r.methods[mf] = fun
return "reply", etf.Atom("ok"), state
case etf.Atom("$revoke"):
module := m.Element(2).(etf.Atom)
function := m.Element(3).(etf.Atom)
mf := modFun{
module: string(module),
function: string(function),
}
if _, ok := r.methods[mf]; ok {
delete(r.methods, mf)
return "reply", etf.Atom("ok"), state
}
return "reply", etf.Atom("unknown"), state
}
}
reply := etf.Term(etf.Tuple{etf.Atom("badrpc"), etf.Atom("unknown")})
return "reply", reply, state
}
// HandleInfo serves all another incoming messages (Pid ! message)
// HandleInfo -> ("noreply", state) - noreply
// ("stop", reason) - normal stop
func (r *rex) HandleInfo(message etf.Term, state interface{}) (string, interface{}) {
lib.Log("REX: HandleInfo: %#v", message)
return "noreply", state
}
// Terminate called when process died
func (r *rex) Terminate(reason string, state interface{}) {
lib.Log("REX: Terminate: %#v", reason)
}
func (r *rex) handleRPC(module, function etf.Atom, args etf.List, state interface{}) (reply, state1 interface{}) {
defer func() {
if x := recover(); x != nil {
err := fmt.Sprintf("panic reason: %s", x)
// recovered
reply = etf.Tuple{
etf.Atom("badrpc"),
etf.Tuple{
etf.Atom("EXIT"),
etf.Tuple{
etf.Atom("panic"),
etf.List{
etf.Tuple{module, function, args, etf.List{err}},
},
},
},
}
}
}()
state1 = state
mf := modFun{
module: string(module),
function: string(function),
}
// calling dynamically declared rpc method
if function, ok := r.methods[mf]; ok {
reply = function(args...)
return
}
// calling a local module if its been registered as a process)
if r.process.Node.GetProcessByName(mf.module) != nil {
return nil, state
}
// unknown request. return error
reply = etf.Tuple{
etf.Atom("badrpc"),
etf.Tuple{
etf.Atom("EXIT"),
etf.Tuple{
etf.Atom("undef"),
etf.List{
etf.Tuple{
module,
function,
args,
etf.List{},
},
},
},
},
}
return reply, state
}