-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathgen_server.go
111 lines (103 loc) · 2.77 KB
/
gen_server.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
package node
import (
"github.com/goerlang/etf"
"log"
)
// GenServer interface
type GenServer interface {
Init(args ...interface{})
HandleCast(message *etf.Term)
HandleCall(message *etf.Term, from *etf.Tuple) (reply *etf.Term)
HandleInfo(message *etf.Term)
Terminate(reason interface{})
}
// GenServerImpl is implementation of GenServer interface
type GenServerImpl struct {
Node *Node // current node of process
Self etf.Pid // Pid of process
}
// Options returns map of default process-related options
func (gs *GenServerImpl) Options() map[string]interface{} {
return map[string]interface{}{
"chan-size": 100, // size of channel for regular messages
"ctl-chan-size": 100, // size of channel for control messages
}
}
// ProcessLoop executes during whole time of process life.
// It receives incoming messages from channels and handle it using methods of behaviour implementation
func (gs *GenServerImpl) ProcessLoop(pcs procChannels, pd Process, args ...interface{}) {
pd.(GenServer).Init(args...)
pcs.init <- true
defer func() {
if r := recover(); r != nil {
// TODO: send message to parent process
log.Printf("GenServer recovered: %#v", r)
}
}()
for {
var message etf.Term
var fromPid etf.Pid
select {
case msg := <-pcs.in:
message = msg
case msgFrom := <-pcs.inFrom:
message = msgFrom[1]
fromPid = msgFrom[0].(etf.Pid)
case ctlMsg := <-pcs.ctl:
switch m := ctlMsg.(type) {
case etf.Tuple:
switch mtag := m[0].(type) {
case etf.Atom:
switch mtag {
case etf.Atom("$go_ctl"):
nLog("Control message: %#v", m)
default:
nLog("Unknown message: %#v", m)
}
default:
nLog("Unknown message: %#v", m)
}
default:
nLog("Unknown message: %#v", m)
}
continue
}
nLog("Message from %#v", fromPid)
switch m := message.(type) {
case etf.Tuple:
switch mtag := m[0].(type) {
case etf.Atom:
switch mtag {
case etf.Atom("$go_ctl"):
nLog("Control message: %#v", message)
case etf.Atom("$gen_call"):
fromTuple := m[1].(etf.Tuple)
reply := pd.(GenServer).HandleCall(&m[2], &fromTuple)
if reply != nil {
gs.Reply(&fromTuple, reply)
}
case etf.Atom("$gen_cast"):
pd.(GenServer).HandleCast(&m[1])
default:
pd.(GenServer).HandleInfo(&message)
}
default:
nLog("mtag: %#v", mtag)
pd.(GenServer).HandleInfo(&message)
}
default:
nLog("m: %#v", m)
pd.(GenServer).HandleInfo(&message)
}
}
}
// Reply sends delayed reply at incoming `gen_server:call/2`
func (gs *GenServerImpl) Reply(fromTuple *etf.Tuple, reply *etf.Term) {
gs.Node.Send((*fromTuple)[0].(etf.Pid), etf.Tuple{(*fromTuple)[1], *reply})
}
func (gs *GenServerImpl) setNode(node *Node) {
gs.Node = node
}
func (gs *GenServerImpl) setPid(pid etf.Pid) {
gs.Self = pid
}