forked from go-netty/go-netty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipeline.go
261 lines (203 loc) · 5.36 KB
/
pipeline.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
* Copyright 2019 the go-netty project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package netty
import (
"fmt"
"github.com/go-netty/go-netty/utils"
)
// Pipeline defines a message processing pipeline.
type Pipeline interface {
// AddFirst add a handler to the first.
AddFirst(handlers ...Handler) Pipeline
// AddLast add a handler to the last.
AddLast(handlers ...Handler) Pipeline
// AddHandler add handlers in position.
AddHandler(position int, handlers ...Handler) Pipeline
// IndexOf find fist index of handler.
IndexOf(func(Handler) bool) int
// LastIndexOf find last index of handler.
LastIndexOf(func(Handler) bool) int
// ContextAt get context by position.
ContextAt(position int) HandlerContext
// Size of handler
Size() int
// Channel get channel.
Channel() Channel
// ServeChannel serve the channel.
ServeChannel(channel Channel)
FireChannelActive()
FireChannelRead(message Message)
FireChannelWrite(message Message)
FireChannelException(ex Exception)
FireChannelInactive(ex Exception)
FireChannelEvent(event Event)
}
// NewPipeline create a pipeline.
func NewPipeline() Pipeline {
p := &pipeline{}
p.head = newHandlerContext(p, headHandler{}, nil, nil)
p.tail = newHandlerContext(p, tailHandler{}, nil, nil)
p.head.next = p.tail
p.tail.prev = p.head
// head + tail
p.size = 2
return p
}
// pipeline to implement Pipeline
type pipeline struct {
head *handlerContext
tail *handlerContext
channel Channel
size int
}
// AddFirst to add handlers at head
func (p *pipeline) AddFirst(handlers ...Handler) Pipeline {
// checking handler.
checkHandler(handlers...)
for _, h := range handlers {
p.addFirst(h)
}
return p
}
// AddLast to add handlers at tail
func (p *pipeline) AddLast(handlers ...Handler) Pipeline {
// checking handler.
checkHandler(handlers...)
for _, h := range handlers {
p.addLast(h)
}
return p
}
// AddHandler to insert handlers in position
func (p *pipeline) AddHandler(position int, handlers ...Handler) Pipeline {
// checking handler.
checkHandler(handlers...)
// checking position.
utils.AssertIf(position >= p.size, "invalid position: %d", position)
if -1 == position || position == p.size-1 {
return p.AddLast(handlers...)
}
curNode := p.head
for i := 0; i < position; i++ {
curNode = curNode.next
}
for _, h := range handlers {
oldNext := curNode.next
curNode.next = newHandlerContext(p, h, curNode, oldNext)
oldNext.prev = curNode.next
curNode = curNode.next
p.size++
}
return p
}
// IndexOf to find fist index of handler.
func (p *pipeline) IndexOf(comp func(Handler) bool) int {
head := p.head
for i := 0; ; i++ {
if comp(head.handler) {
return i
}
if head = head.next; head == nil {
break
}
}
return -1
}
// LastIndexOf to find last index of handler.
func (p *pipeline) LastIndexOf(comp func(Handler) bool) int {
tail := p.tail
for i := p.size - 1; ; i-- {
if comp(tail.handler) {
return i
}
if tail = tail.prev; tail == nil {
break
}
}
return -1
}
// ContextAt to access the context by position
func (p *pipeline) ContextAt(position int) HandlerContext {
if -1 == position || position >= p.size {
return nil
}
curNode := p.head
for i := 0; i < position; i++ {
curNode = curNode.next
}
return curNode
}
// Size of handlers
func (p *pipeline) Size() int {
return p.size
}
// addFirst to add handlers head
func (p *pipeline) addFirst(handler Handler) {
oldNext := p.head.next
p.head.next = newHandlerContext(p, handler, p.head, oldNext)
oldNext.prev = p.head.next
p.size++
}
// addLast to add handlers tail
func (p *pipeline) addLast(handler Handler) {
oldPrev := p.tail.prev
p.tail.prev = newHandlerContext(p, handler, oldPrev, p.tail)
oldPrev.next = p.tail.prev
p.size++
}
// Channel to get channel of Pipeline
func (p *pipeline) Channel() Channel {
return p.channel
}
// ServeChannel serveChannel to serve the channel
func (p *pipeline) ServeChannel(channel Channel) {
utils.AssertIf(nil != p.channel, "already attached channel")
p.channel = channel
p.channel.serveChannel()
}
func (p *pipeline) FireChannelActive() {
p.head.HandleActive()
}
func (p *pipeline) FireChannelRead(message Message) {
p.head.HandleRead(message)
}
func (p *pipeline) FireChannelWrite(message Message) {
p.tail.HandleWrite(message)
}
func (p *pipeline) FireChannelException(ex Exception) {
p.head.HandleException(ex)
}
func (p *pipeline) FireChannelInactive(ex Exception) {
p.head.HandleInactive(ex)
}
func (p *pipeline) FireChannelEvent(event Event) {
p.head.HandleEvent(event)
}
// checkHandler to checking handlers
func checkHandler(handlers ...Handler) {
for index, h := range handlers {
switch h.(type) {
case ActiveHandler:
case InboundHandler:
case OutboundHandler:
case ExceptionHandler:
case InactiveHandler:
case EventHandler:
default:
utils.Assert(fmt.Errorf("unrecognized Handler: %d:%T", index, h))
}
}
}