-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathagent.go
153 lines (131 loc) · 2.83 KB
/
agent.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
package nest2go
import (
. "github.com/lawwong/nest2go/closable"
. "github.com/lawwong/nest2go/datapkg"
"github.com/lawwong/nest2go/uuid"
)
type agentResInfo struct {
req OpRequest
resCode int32
data []byte
}
type AgentBase struct {
session *uuid.Uuid
app *AppBase
serial int32 // only access in *AppBase.verifyAgent
lastApplyTime int64
peer *PeerBase
setpeer chan *PeerBase
opChan chan OpRequest
writeOp chan agentResInfo
Closable
}
func (a *AgentBase) OpChan() <-chan OpRequest {
if a != nil {
return a.opChan
}
return nil
}
func newAgentBase(session *uuid.Uuid, app *AppBase, peer *PeerBase) *AgentBase {
a := &AgentBase{
session: session,
app: app,
opChan: make(chan OpRequest),
setpeer: make(chan *PeerBase),
writeOp: make(chan agentResInfo),
Closable: MakeClosable(),
}
a.HandleClose(nil)
go a.run(peer)
return a
}
func (a *AgentBase) run(peer *PeerBase) {
// peer initial
encodeKey := peer.encodeKey
decodeKey := peer.decodeKey
eventChan := peer.EventChan()
opChan := peer.OpChan()
var resInfoCache agentResInfo
//var resMsgCache []byte
for {
select {
case newPeer := <-a.setpeer:
if newPeer != nil {
// peer replaced
newPeer.encodeKey = encodeKey
newPeer.decodeKey = decodeKey
eventChan = newPeer.EventChan()
opChan = newPeer.OpChan()
} else {
// peer removed
eventChan = nil
opChan = nil
}
if peer != nil {
peer.Close()
}
peer = newPeer
case _, ok := <-eventChan:
if !ok {
eventChan = nil
}
// not support
case opReq, ok := <-opChan:
if !ok {
opChan = nil
} else {
// read request from peer
opReq.SetResponseSender(a)
if opReq.IsEqual(resInfoCache.req) {
if peer != nil { // peer must not be nil?
peer.SendOpResponse(resInfoCache.req, resInfoCache.resCode, resInfoCache.data)
}
} else {
select {
case a.opChan <- opReq:
case <-a.CloseChan():
break
}
}
}
case resInfo := <-a.writeOp:
resInfoCache = resInfo
// TODO : serialized cache??
if peer != nil {
peer.SendOpResponse(resInfo.req, resInfo.resCode, resInfo.data)
}
case <-a.CloseChan():
break
}
}
}
// return replaced peer(closed)
func (a *AgentBase) setPeer(p *PeerBase) {
select {
case a.setpeer <- p:
case <-a.CloseChan():
}
}
func (a *AgentBase) SendOpResponse(req OpRequest, resCode int32, data []byte) error {
select {
case a.writeOp <- agentResInfo{
req,
resCode,
data,
}:
return nil
case <-a.CloseChan():
return ErrServiceClosed
}
}
//func (a *AgentBase) HandleClose(handler CloseHandler) {
// a.closable.HandleCloseFunc(func() {
// a.setPeer(nil)
// if handler != nil {
// handler.HandleClose()
// }
// })
//}
//func (a *AgentBase) HandleCloseFunc(handleFunc CloseHandlerFunc) {
// a.HandleClose(CloseHandler(handleFunc))
//}