/
client.go
303 lines (263 loc) · 8.49 KB
/
client.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package transaction
import (
"github.com/discoviking/fsm"
"github.com/stefankopieczek/gossip/base"
"github.com/stefankopieczek/gossip/log"
"github.com/stefankopieczek/gossip/timing"
)
// SIP Client Transaction FSM
// Implements the behaviour described in RFC 3261 section 17.1
// FSM States
const (
client_state_calling = iota
client_state_proceeding
client_state_completed
client_state_terminated
)
// FSM Inputs
const (
client_input_1xx fsm.Input = iota
client_input_2xx
client_input_300_plus
client_input_timer_a
client_input_timer_b
client_input_timer_d
client_input_transport_err
client_input_delete
)
// Initialises the correct kind of FSM based on request method.
func (tx *ClientTransaction) initFSM() {
if tx.origin.Method == base.INVITE {
tx.initInviteFSM()
} else {
tx.initNonInviteFSM()
}
}
func (tx *ClientTransaction) initInviteFSM() {
log.Debug("Initialising client INVITE transaction FSM")
// Define Actions
// Resend the request.
act_resend := func() fsm.Input {
log.Debug("Client transaction %p, act_resend", tx)
tx.timer_a_time *= 2
tx.timer_a.Reset(tx.timer_a_time)
tx.resend()
return fsm.NO_INPUT
}
// Just pass up the latest response.
act_passup := func() fsm.Input {
log.Debug("Client transaction %p, act_passup", tx)
tx.passUp()
return fsm.NO_INPUT
}
// Handle 300+ responses.
// Pass up response and send ACK, start timer D.
act_300 := func() fsm.Input {
log.Debug("Client transaction %p, act_300", tx)
tx.passUp()
tx.Ack()
if tx.timer_d != nil {
tx.timer_d.Stop()
}
tx.timer_d = timing.AfterFunc(tx.timer_d_time, func() {
tx.fsm.Spin(client_input_timer_d)
})
return fsm.NO_INPUT
}
// Send an ACK.
act_ack := func() fsm.Input {
log.Debug("Client transaction %p, act_ack", tx)
tx.Ack()
return fsm.NO_INPUT
}
// Send up transport failure error.
act_trans_err := func() fsm.Input {
log.Debug("Client transaction %p, act_trans_err", tx)
tx.transportError()
return client_input_delete
}
// Send up timeout error.
act_timeout := func() fsm.Input {
log.Debug("Client transaction %p, act_timeout", tx)
tx.timeoutError()
return client_input_delete
}
// Pass up the response and delete the transaction.
act_passup_delete := func() fsm.Input {
log.Debug("Client transaction %p, act_passup_delete", tx)
tx.passUp()
tx.Delete()
return fsm.NO_INPUT
}
// Just delete the transaction.
act_delete := func() fsm.Input {
log.Debug("Client transaction %p, act_delete", tx)
tx.Delete()
return fsm.NO_INPUT
}
// Define States
// Calling
client_state_def_calling := fsm.State{
Index: client_state_calling,
Outcomes: map[fsm.Input]fsm.Outcome{
client_input_1xx: {client_state_proceeding, act_passup},
client_input_2xx: {client_state_terminated, act_passup_delete},
client_input_300_plus: {client_state_completed, act_300},
client_input_timer_a: {client_state_calling, act_resend},
client_input_timer_b: {client_state_terminated, act_timeout},
client_input_transport_err: {client_state_terminated, act_trans_err},
},
}
// Proceeding
client_state_def_proceeding := fsm.State{
Index: client_state_proceeding,
Outcomes: map[fsm.Input]fsm.Outcome{
client_input_1xx: {client_state_proceeding, act_passup},
client_input_2xx: {client_state_terminated, act_passup_delete},
client_input_300_plus: {client_state_completed, act_300},
client_input_timer_a: {client_state_proceeding, fsm.NO_ACTION},
client_input_timer_b: {client_state_proceeding, fsm.NO_ACTION},
},
}
// Completed
client_state_def_completed := fsm.State{
Index: client_state_completed,
Outcomes: map[fsm.Input]fsm.Outcome{
client_input_1xx: {client_state_completed, fsm.NO_ACTION},
client_input_2xx: {client_state_completed, fsm.NO_ACTION},
client_input_300_plus: {client_state_completed, act_ack},
client_input_timer_d: {client_state_terminated, act_delete},
client_input_transport_err: {client_state_terminated, act_trans_err},
client_input_timer_a: {client_state_completed, fsm.NO_ACTION},
client_input_timer_b: {client_state_completed, fsm.NO_ACTION},
},
}
// Terminated
client_state_def_terminated := fsm.State{
Index: client_state_terminated,
Outcomes: map[fsm.Input]fsm.Outcome{
client_input_1xx: {client_state_terminated, fsm.NO_ACTION},
client_input_2xx: {client_state_terminated, fsm.NO_ACTION},
client_input_300_plus: {client_state_terminated, fsm.NO_ACTION},
client_input_timer_a: {client_state_terminated, fsm.NO_ACTION},
client_input_timer_b: {client_state_terminated, fsm.NO_ACTION},
client_input_delete: {client_state_terminated, act_delete},
},
}
fsm, err := fsm.Define(
client_state_def_calling,
client_state_def_proceeding,
client_state_def_completed,
client_state_def_terminated,
)
if err != nil {
log.Severe("Failure to define INVITE client transaction fsm: %s", err.Error())
}
tx.fsm = fsm
}
func (tx *ClientTransaction) initNonInviteFSM() {
log.Debug("Initialising client non-INVITE transaction FSM")
// Define Actions
// Resend the request.
act_resend := func() fsm.Input {
tx.timer_a_time *= 2
// For non-INVITE, cap timer A at T2 seconds.
if tx.timer_a_time > T2 {
tx.timer_a_time = T2
}
tx.timer_a.Reset(tx.timer_a_time)
tx.resend()
return fsm.NO_INPUT
}
// Just pass up the latest response.
act_passup := func() fsm.Input {
tx.passUp()
return fsm.NO_INPUT
}
// Handle a final response.
act_final := func() fsm.Input {
tx.passUp()
if tx.timer_d != nil {
tx.timer_d.Stop()
}
tx.timer_d = timing.AfterFunc(tx.timer_d_time, func() {
tx.fsm.Spin(client_input_timer_d)
})
return fsm.NO_INPUT
}
// Send up transport failure error.
act_trans_err := func() fsm.Input {
tx.transportError()
return client_input_delete
}
// Send up timeout error.
act_timeout := func() fsm.Input {
tx.timeoutError()
return client_input_delete
}
// Just delete the transaction.
act_delete := func() fsm.Input {
tx.Delete()
return fsm.NO_INPUT
}
// Define States
// "Trying"
client_state_def_calling := fsm.State{
Index: client_state_calling,
Outcomes: map[fsm.Input]fsm.Outcome{
client_input_1xx: {client_state_proceeding, act_passup},
client_input_2xx: {client_state_completed, act_final},
client_input_300_plus: {client_state_completed, act_final},
client_input_timer_a: {client_state_calling, act_resend},
client_input_timer_b: {client_state_terminated, act_timeout},
client_input_transport_err: {client_state_terminated, act_trans_err},
},
}
// Proceeding
client_state_def_proceeding := fsm.State{
Index: client_state_proceeding,
Outcomes: map[fsm.Input]fsm.Outcome{
client_input_1xx: {client_state_proceeding, act_passup},
client_input_2xx: {client_state_completed, act_final},
client_input_300_plus: {client_state_completed, act_final},
client_input_timer_a: {client_state_proceeding, act_resend},
client_input_timer_b: {client_state_terminated, act_timeout},
client_input_transport_err: {client_state_terminated, act_trans_err},
},
}
// Completed
client_state_def_completed := fsm.State{
Index: client_state_completed,
Outcomes: map[fsm.Input]fsm.Outcome{
client_input_1xx: {client_state_completed, fsm.NO_ACTION},
client_input_2xx: {client_state_completed, fsm.NO_ACTION},
client_input_300_plus: {client_state_completed, fsm.NO_ACTION},
client_input_timer_d: {client_state_terminated, act_delete},
client_input_timer_a: {client_state_completed, fsm.NO_ACTION},
client_input_timer_b: {client_state_completed, fsm.NO_ACTION},
},
}
// Terminated
client_state_def_terminated := fsm.State{
Index: client_state_terminated,
Outcomes: map[fsm.Input]fsm.Outcome{
client_input_1xx: {client_state_terminated, fsm.NO_ACTION},
client_input_2xx: {client_state_terminated, fsm.NO_ACTION},
client_input_300_plus: {client_state_terminated, fsm.NO_ACTION},
client_input_timer_a: {client_state_terminated, fsm.NO_ACTION},
client_input_timer_b: {client_state_terminated, fsm.NO_ACTION},
client_input_timer_d: {client_state_terminated, fsm.NO_ACTION},
client_input_delete: {client_state_terminated, act_delete},
},
}
fsm, err := fsm.Define(
client_state_def_calling,
client_state_def_proceeding,
client_state_def_completed,
client_state_def_terminated,
)
if err != nil {
log.Severe("Failure to define INVITE client transaction fsm: %s", err.Error())
}
tx.fsm = fsm
}