-
Notifications
You must be signed in to change notification settings - Fork 21
/
collect.go
177 lines (163 loc) · 3.9 KB
/
collect.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
// SPDX-License-Identifier: MIT
//
// Copyright © 2018 Kent Gibson <[email protected]>.
package sms
import (
"fmt"
"sync"
"time"
"github.com/warthog618/sms/encoding/tpdu"
)
// Collector contains reassembly pipes that buffer concatenated TPDUs until a
// full set is available to be concatenated.
type Collector struct {
sync.Mutex // covers pipes and closing closed
pipes map[string]*pipe
closed bool
duration time.Duration
expiryHandler func([]*tpdu.TPDU)
}
// CollectorOption alters the behaviour of a Collector.
type CollectorOption interface {
ApplyCollectorOption(*Collector)
}
type reassemblyTimeoutOption struct {
d time.Duration
eh func([]*tpdu.TPDU)
}
func (o reassemblyTimeoutOption) ApplyCollectorOption(c *Collector) {
c.duration = o.d
c.expiryHandler = o.eh
}
// WithReassemblyTimeout limits the time allowed for a collection of TPDUs to
// be collected.
//
// If the timer expires before the collection is complete then the collected
// TPDUs are passed to the expiryHandler. The expiry handler can be nil in
// which case the collected TPDUs are simply discarded.
//
// A zero duration disables the timeout.
func WithReassemblyTimeout(d time.Duration, eh func([]*tpdu.TPDU)) CollectorOption {
return reassemblyTimeoutOption{d, eh}
}
// NewCollector creates a Collector.
func NewCollector(options ...CollectorOption) *Collector {
c := Collector{
pipes: make(map[string]*pipe),
}
for _, o := range options {
o.ApplyCollectorOption(&c)
}
return &c
}
// Close shuts down the Collector and all active pipes.
func (c *Collector) Close() {
c.Lock()
defer c.Unlock()
if c.closed {
return
}
c.closed = true
for _, p := range c.pipes {
if p.cleanup != nil {
p.cleanup.Stop()
}
}
}
// Pipes returns a snapshot of the reassembly pipes.
//
// This is intended for diagnostics.
func (c *Collector) Pipes() map[string][]*tpdu.TPDU {
c.Lock()
m := map[string][]*tpdu.TPDU{}
for k, v := range c.pipes {
m[k] = v.segments
}
c.Unlock()
return m
}
// Collect adds a TPDU to the collection.
//
// If all the components of a concatenated TPDU are available then they are
// returned.
func (c *Collector) Collect(pdu tpdu.TPDU) (d []*tpdu.TPDU, err error) {
c.Lock()
defer c.Unlock()
if c.closed {
return nil, ErrClosed
}
segments, seqno, concatRef, ok := pdu.ConcatInfo()
if !ok || segments < 2 {
// short circuit single segment - no need for a pipe
return []*tpdu.TPDU{&pdu}, nil
}
if seqno < 1 || seqno > segments {
return nil, ErrReassemblyInconsistency
}
key, err := pduKey(pdu, segments, concatRef)
p, ok := c.pipes[key]
if ok {
if p.segments[seqno-1] != nil {
return nil, ErrDuplicateSegment
}
if p.cleanup != nil && !p.cleanup.Stop() {
// timer has fired, but cleanup hasn't been performed yet - so need
// a new pipe
ok = false
}
}
if !ok {
p = &pipe{nil, make([]*tpdu.TPDU, segments), 0}
c.pipes[key] = p
}
p.segments[seqno-1] = &pdu
p.frags++
if p.frags == segments {
delete(c.pipes, key)
return p.segments, nil
}
if c.duration != 0 {
p.cleanup = time.AfterFunc(c.duration, func() {
c.Lock()
m := c.pipes[key]
if m == p {
delete(c.pipes, key)
}
c.Unlock()
if c.expiryHandler != nil {
c.expiryHandler(p.segments)
}
})
}
return nil, err
}
func pduKey(pdu tpdu.TPDU, segments, concatRef int) (string, error) {
st := pdu.SmsType()
var key string
switch st {
case tpdu.SmsSubmit:
key = fmt.Sprintf("%d:%02x:%s:%d:%d",
st,
pdu.DA.TOA,
pdu.DA.Addr,
concatRef,
segments)
case tpdu.SmsDeliver:
key = fmt.Sprintf("%d:%02x:%s:%d:%d",
st,
pdu.OA.TOA,
pdu.OA.Addr,
concatRef,
segments)
default:
return "", tpdu.ErrUnsupportedSmsType(st)
}
return key, nil
}
// pipe is a buffer that contains the individual TPDUs in a concatenation set
// until the complete set is available or the reassembly times out.
type pipe struct {
cleanup *time.Timer
segments []*tpdu.TPDU
frags int
}