forked from leeonsoft/zerver
/
component.go
239 lines (192 loc) · 4.85 KB
/
component.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package zerver
import (
"sync"
"github.com/cosiner/gohper/encoding"
"github.com/cosiner/gohper/errors"
log "github.com/cosiner/ygo/jsonlog"
)
// =============================================================================
// Component State
// =============================================================================
type compState uint8
const (
_UNINITIALIZE compState = iota
_WAITING
_INITIALIZED
)
func (s compState) String() string {
switch s {
case _UNINITIALIZE:
return "Uninitialize"
case _WAITING:
return "Initializing"
case _INITIALIZED:
return "Initialized"
}
panic("unexpected initial state")
}
// =============================================================================
// Component
// =============================================================================
type (
// Env is a server environment, real implementation is the Server itself.
Env interface {
Server() *Server
Filepath(path string) string
StartTask(path string, value interface{})
Component(name string) (interface{}, error)
Codec() encoding.Codec
Logger() *log.Logger
}
// Component is a Object which will automaticlly initial/destroyed by server
// if it's added to server, else it should init manually
Component interface {
Init(Env) error
Destroy()
}
NopComponent struct{}
)
func (NopComponent) Init(Env) error { return nil }
func (NopComponent) Destroy() {}
// =============================================================================
// Component Environment
// =============================================================================
type CompEnv struct {
comp Component
value interface{}
name string
Env
state compState
}
func newCompEnv(env Env, name string, c interface{}) *CompEnv {
e := &CompEnv{
name: name,
Env: env,
}
switch c := c.(type) {
case Component:
e.comp = c
e.state = _UNINITIALIZE
default:
e.value = c
e.state = _INITIALIZED
}
return e
}
func (e *CompEnv) Name() string {
return e.name
}
func (e *CompEnv) String() string {
return e.name + ":" + e.state.String()
}
func ComponentAttr(compName, attr string) string {
return compName + ":" + attr
}
func (e *CompEnv) Attr(name string) interface{} {
return e.Server().Attr(ComponentAttr(e.name, name))
}
func (e *CompEnv) SetAttr(name string, value interface{}) {
e.Server().SetAttr(ComponentAttr(e.name, name), value)
}
func (e *CompEnv) GetSetAttr(name string, val interface{}) interface{} {
return e.Server().GetSetAttr(ComponentAttr(e.name, name), val)
}
func (e *CompEnv) Init(Env) error {
if e.state == _INITIALIZED {
return nil
}
if e.state == _WAITING {
panic("Cycle dependence on " + e.name)
}
e.state = _WAITING
err := e.comp.Init(e)
e.state = _INITIALIZED
return err
}
func (e *CompEnv) Destroy() {
if e.value == nil && e.state == _INITIALIZED {
e.comp.Destroy()
}
}
func (e *CompEnv) underlay() interface{} {
if e.value != nil {
return e.value
}
return e.comp
}
// =============================================================================
// Component Manager
// =============================================================================
var ErrCompNotFound = errors.New("component not found")
type CompManager struct {
components map[string]*CompEnv
anonymous []Component
mu sync.RWMutex
}
func NewCompManager() CompManager {
return CompManager{
components: make(map[string]*CompEnv),
}
}
func (m *CompManager) Get(name string) (interface{}, error) {
m.mu.RLock()
e, has := m.components[name]
m.mu.RUnlock()
if !has {
return nil, ErrCompNotFound
}
if err := e.Init(e); err != nil { // only first time will execute
return nil, err
}
return e.underlay(), nil
}
// Register make a component managed
func (m *CompManager) Register(env Env, name string, comp interface{}) *CompEnv {
m.mu.Lock()
defer m.mu.Unlock()
if name == "" {
if c, is := comp.(Component); is {
m.anonymous = append(m.anonymous, c)
} else {
panic("non-component object shouldn't be add to manager anonymously")
}
return nil
}
cs := newCompEnv(env, name, comp)
m.components[name] = cs
return cs
}
// Remove will an component and Destroy it
func (m *CompManager) Remove(name string) {
m.mu.Lock()
defer m.mu.Unlock()
cs, has := m.components[name]
if has {
cs.Destroy()
delete(m.components, name)
}
}
func (m *CompManager) Init(e Env) error {
// initial named component first for anonymous may depend on them
for _, comp := range m.components {
if err := comp.Init(e); err != nil {
return err
}
}
for _, c := range m.anonymous {
if err := c.Init(e); err != nil {
return err
}
}
return nil
}
func (m *CompManager) Destroy() {
m.mu.Lock()
for _, cs := range m.components {
cs.Destroy()
}
for _, c := range m.anonymous {
c.Destroy()
}
m.mu.Unlock()
}