aboutsummaryrefslogtreecommitdiffstats
path: root/m2ua/M2UAApplicationServerProcess.st
blob: e9fb1aaf364bb06f0eb9d351467836e74c76b533 (plain)
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
"
 (C) 2013 by Holger Hans Peter Freyther
 All Rights Reserved

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU Affero General Public License as
 published by the Free Software Foundation, either version 3 of the
 License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU Affero General Public License for more details.

 You should have received a copy of the GNU Affero General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
"

Object subclass: M2UAApplicationServerProcess [
    | socket asp_active_block asp_down_block asp_inactive_block asp_up_block error_block notify_block sctp_confirm_block sctp_released_block sctp_restarted_block sctp_status_block established state t_ack lastMsg on_state_change as_state |
    
    <category: 'OsmoNetwork-M2UA'>
    <comment: 'I am a M2UA Application Server Process.

I have an internal state machine and a state and will be used by the
M2UA Layer. I am written for the usage in a Media Gateway Controller
and will also keep information about the Application Server itself.

If I need to be used on a Signalling Gateway (SG) I will need a dedicated
M2UA Application Server class and state machine.

I can currently only manage a single interface. The specification allows
a single ASP to send one ASPActive for one interface at a time.'>

    M2UAApplicationServerProcess class >> initWith: aService [
	^self new
	    socketService: aService;
	    yourself
    ]

    M2UAApplicationServerProcess class >> new [
	^super new
	    initialize;
	    yourself
    ]

    onError: aBlock [
	"M-ERROR indication
	 Direction: M2UA -> LM
	 Purpose: ASP or SGP reports that it has received an ERROR
	 message from its peer."

	<category: 'Primitives-LayerManagement'>
	error_block := aBlock
    ]

    onNotify: aBlock [
	"M-NOTIFY indication
	 Direction: M2UA -> LM
	 Purpose: ASP reports that it has received a NOTIFY message
	 from its peer."

	<category: 'Primitives-LayerManagement'>
	notify_block := aBlock
    ]

    onSctpEstablished: aBlock [
	"M-SCTP_ESTABLISH confirm
	 Direction: M2UA -> LM
	 Purpose: ASP confirms to LM that it has established an SCTP association with an SGP."

	<category: 'Primitives-LayerManagement-SCTP'>
	sctp_confirm_block := aBlock
    ]

    onSctpReleased: aBlock [
	"M-SCTP_RELEASE confirm
	 Direction: M2UA -> LM
	 Purpose: ASP confirms to LM that it has released SCTP association with SGP."

	<category: 'Primitives-LayerManagement-SCTP'>
	sctp_released_block := aBlock
    ]

    onSctpRestarted: aBlock [
	"M-SCTP_RELEASE indication
	 Direction: M2UA -> LM
	 Purpose: SGP informs LM that ASP has released an SCTP association."

	<category: 'Primitives-LayerManagement-SCTP'>
	sctp_restarted_block := aBlock
    ]

    onSctpStatus: aBlock [
	"M-SCTP_STATUS indication
	 Direction: M2UA -> LM
	 Purpose: M2UA reports status of SCTP association."

	<category: 'Primitives-LayerManagement-SCTP'>
	sctp_status_block := aBlock
    ]

    sctpEstablish [
	"M-SCTP_ESTABLISH request
	 Direction: LM -> M2UA
	 Purpose: LM requests ASP to establish an SCTP association with an SGP."

	<category: 'Primitives-LayerManagement-SCTP'>
	established := false.
	socket stop.
	socket start
    ]

    sctpRelease [
	"M-SCTP_RELEASE request
	 Direction: LM -> M2UA
	 Purpose: LM requests ASP to release an SCTP association with SGP."

	<category: 'Primitives-LayerManagement-SCTP'>
	established := false.
	socket stop.
	t_ack ifNotNil: [t_ack cancel]
    ]

    sctpStatusRequest [
	"M-SCTP_STATUS request
	 Direction: LM -> M2UA
	 Purpose: LM requests M2UA to report status of SCTP association."

	<category: 'Primitives-LayerManagement-SCTP'>
	self notYetImplemented
    ]

    aspActive [
	<category: 'Primitives-LayerManagemennt-ASP'>
	"M-ASP_ACTIVE request
	 Direction: LM -> M2UA
	 Purpose: LM requests ASP to send an ASP ACTIVE message to the SGP."

	| msg |
	self checkNextState: M2UAAspStateActive.
	msg := M2UAMSG new
		    class: M2UAConstants clsASPTM;
		    msgType: M2UAConstants asptmActiv;
		    addTag: self createIdentIntTag;
		    addTag: self createInfoTag;
		    yourself.
	self send: msg
    ]

    aspDown [
	<category: 'Primitives-LayerManagemennt-ASP'>
	"M-ASP_DOWN request
	 Direction: LM -> M2UA
	 Purpose: LM requests ASP to stop its operation and send an ASP DOWN
	 message to the SGP."

	| msg |
	self checkNextState: M2UAAspStateDown.
	msg := M2UAMSG new
		    class: M2UAConstants clsASPSM;
		    msgType: M2UAConstants aspsmDown;
		    addTag: self createAspIdentTag;
		    addTag: self createInfoTag;
		    yourself.
	self send: msg
    ]

    aspInactive [
	<category: 'Primitives-LayerManagemennt-ASP'>
	"M-ASP_INACTIVE request
	 Direction: LM -> M2UA
	 Purpose: LM requests ASP to send an ASP INACTIVE message to the SGP."

	| msg |
	self checkNextState: M2UAAspStateInactive.
	msg := M2UAMSG new
		    class: M2UAConstants clsASPTM;
		    msgType: M2UAConstants asptmInactiv;
		    addTag: self createIdentIntTag;
		    addTag: self createInfoTag;
		    yourself.
	self send: msg
    ]

    aspUp [
	<category: 'Primitives-LayerManagemennt-ASP'>
	"M-ASP_UP request
	 Direction: LM -> M2UA
	 Purpose: LM requests ASP to start its operation and send an ASP UP
	 message to the SGP."

	| msg |
	self checkNextState: M2UAAspStateInactive.
	msg := M2UAMSG new
		    class: M2UAConstants clsASPSM;
		    msgType: M2UAConstants aspsmUp;
		    addTag: self createAspIdentTag;
		    addTag: self createInfoTag;
		    yourself.
	self send: msg
    ]

    onAspActive: aBlock [
	"M-ASP_ACTIVE confirm
	 Direction: M2UA -> LM
	 Purpose: ASP reports that is has received an ASP ACTIVE
	 Acknowledgment message from the SGP."

	<category: 'Primitives-LayerManagemennt-ASP'>
	asp_active_block := aBlock
    ]

    onAspDown: aBlock [
	"M-ASP_DOWN confirm
	 Direction: M2UA -> LM
	 Purpose: ASP reports that is has received an ASP DOWN Acknowledgment
	 message from the SGP."

	<category: 'Primitives-LayerManagemennt-ASP'>
	asp_down_block := aBlock
    ]

    onAspInactive: aBlock [
	"M-ASP_INACTIVE confirm
	 Direction: M2UA -> LM
	 Purpose: ASP reports that is has received an ASP INACTIVE
	 Acknowledgment message from the SGP."

	<category: 'Primitives-LayerManagemennt-ASP'>
	asp_inactive_block := aBlock
    ]

    onAspUp: aBlock [
	"M-ASP_UP confirm
	 Direction: M2UA -> LM
	 Purpose: ASP reports that it has received an ASP UP Acknowledgment
	 message from the SGP."

	<category: 'Primitives-LayerManagemennt-ASP'>
	asp_up_block := aBlock
    ]

    onStateChange: aBlock [
	"A generic callback for all state changes"

	<category: 'Primitives-LayerManagemennt-ASP'>
	on_state_change := aBlock
    ]

    deregisterLinkKey [
	"M-LINK_KEY_DEREG Request
	 Direction:  LM -> M2UA
	 Purpose:  LM requests ASP to de-register Link Key with SG by sending
	 DEREG REQ message."

	<category: 'Primitives-LayerManagement-LinkKey'>
	self notYetImplemented
    ]

    onLinkKeyDeregistered: aBlock [
	"M-LINK_KEY_DEREG Confirm
	 Direction:  M2UA -> LM
	 Purpose:  ASP reports to LM that it has successfully received a
	 DEREG RSP message from SG."

	<category: 'Primitives-LayerManagement-LinkKey'>
	self notYetImplemented
    ]

    onLinkKeyRegistered: aBlock [
	"M-LINK_KEY_REG Confirm
	 Direction:   M2UA -> LM
	 Purpose: ASP reports to LM that it has successfully received a REG
	 RSP message from SG."

	<category: 'Primitives-LayerManagement-LinkKey'>
	self notYetImplemented
    ]

    registerLinkKey [
	"M-LINK_KEY_REG Request
	 Direction:  LM -> M2UA
	 Purpose: LM requests ASP to register Link Key with SG by sending REG
	 REQ message."

	<category: 'Primitives-LayerManagement-LinkKey'>
	self notYetImplemented
    ]

    hostname: aHostname port: aPort [
	"Select the SCTP hostname/port for the SG to connect to"

	<category: 'configuration'>
	socket
	    hostname: aHostname;
	    port: aPort
    ]

    createAspIdentTag [
	<category: 'm2ua-tags'>
	^M2UATag initWith: M2UAConstants tagAspIdent data: #(1 2 3 4)
    ]

    createIdentIntTag [
	<category: 'm2ua-tags'>
	^M2UATag initWith: M2UAConstants tagIdentInt data: #(0 0 0 0)
    ]

    createInfoTag [
	<category: 'm2ua-tags'>
	^M2UATag initWith: M2UAConstants tagInfo
	    data: 'Hello from Smalltalk' asByteArray
    ]

    callNotification: aBlock [
	"Inform the generic method first, then all the others"

	<category: 'private'>
	on_state_change ifNotNil: [on_state_change value].
	aBlock ifNotNil: [aBlock value]
    ]

    checkNextState: nextState [
	"Check if nextState and state are compatible and if not
	 throw an exception. TODO:"

	<category: 'private'>
	self state = nextState 
	    ifTrue: 
		[^self error: ('M2UA ASP already in state <1p>' expandMacrosWith: state)].
	(self state nextPossibleStates includes: nextState) 
	    ifFalse: 
		[^self error: ('M2UA ASP illegal state transition from <1p> to <2p>.' 
			    expandMacrosWith: state
			    with: nextState)]
    ]

    dispatchData: aByteArray [
	<category: 'private'>
	| msg |
	msg := M2UAMSG parseToClass: aByteArray.
	msg dispatchOnAsp: self
    ]

    dispatchNotification: aBlock [
	<category: 'private'>
	aBlock value
    ]

    internalReset [
	<category: 'private'>
	self socketService: socket
    ]

    moveToState: newState [
	<category: 'private'>
	((state nextPossibleStates includes: newState) or: [state = newState]) 
	    ifFalse: 
		[^self error: ('M2UA ASP Illegal state transition from <1p> to <2p>' 
			    expandMacrosWith: state
			    with: newState)].

	"TODO: general on entry, on exit"
	state := newState
    ]

    sctpConnected [
	<category: 'private'>
	"The connect was issued."

	| wasEstablished |
	wasEstablished := established.
	established := true.
	state := M2UAAspStateDown.
	t_ack ifNotNil: [t_ack cancel].
	wasEstablished = true 
	    ifTrue: [sctp_confirm_block ifNotNil: [sctp_confirm_block value]]
	    ifFalse: [sctp_restarted_block ifNotNil: [sctp_restarted_block value]]
    ]

    sctpReleased [
	"The SCTP connection has been released."

	<category: 'private'>
	self moveToState: M2UAAspStateDown.
	established = true ifFalse: [^self].
	sctp_released_block ifNotNil: [sctp_released_block value]
    ]

    send: aMsg [
	"Forget about what we did before"

	<category: 'private'>
	t_ack ifNotNil: [t_ack cancel].
	t_ack := TimerScheduler instance scheduleInSeconds: 2
		    block: 
			["Re-send the message"

			self logNotice: ('<1p>:<2p> Sending message has timed out' 
				    expandMacrosWith: socket hostname
				    with: socket port)
			    area: #m2ua.
			self send: aMsg].
	socket nextPut: aMsg toMessage asByteArray
    ]

    initialize [
	<category: 'creation'>
	state := M2UAAspStateDown
    ]

    socketService: aService [
	<category: 'creation'>
	socket := aService.
	socket
	    onSctpConnect: [self sctpConnected];
	    onSctpReleased: [self sctpReleased];
	    onSctpData: 
		    [:stream :assoc :ppid :data | 
		    ppid = 2 
			ifFalse: 
			    [^self logNotice: 'M2UAApplicationServerProcess expecting PPID 2.'
				area: #m2ua].
		    self dispatchData: data]
    ]

    handleAspActiveAck: aMsg [
	<category: 'dispatch'>
	t_ack cancel.
	self moveToState: M2UAAspStateActive.
	self callNotification: asp_active_block
    ]

    handleAspDownAck: aMsg [
	<category: 'dispatch'>
	t_ack cancel.
	as_state := nil.
	self moveToState: M2UAAspStateDown.
	self callNotification: asp_down_block
    ]

    handleAspInactiveAck: aMsg [
	<category: 'dispatch'>
	t_ack cancel.
	as_state := nil.
	self moveToState: M2UAAspStateInactive.
	self callNotification: asp_inactive_block
    ]

    handleAspUpAck: aMsg [
	<category: 'dispatch'>
	t_ack cancel.
	self moveToState: M2UAAspStateInactive.
	self callNotification: asp_inactive_block
    ]

    handleError: aMsg [
	"Cancel pending operations.. because something went wrong"

	<category: 'dispatch'>
	t_ack cancel.
	error_block ifNotNil: [error_block value: aMsg]
    ]

    handleNotify: aMsg [
	<category: 'dispatch'>
	"Extract the status"

	| tag type ident |
	tag := aMsg findTag: M2UAConstants tagStatus.
	tag ifNil: [^self].
	type := (tag data ushortAt: 1) swap16.
	ident := (tag data ushortAt: 3) swap16.
	type = M2UAConstants ntfyKindStateChange ifTrue: [as_state := ident].

	"Inform our user about it"
	notify_block ifNotNil: [notify_block value: type value: ident]
    ]

    handleUnknownMessage: aMsg [
	"We got something we don't know. ignore it for now."

	<category: 'dispatch'>
	
    ]

    isASActive [
	<category: 'status'>
	^as_state = M2UAConstants ntfyStateASActive
    ]

    isASInactive [
	<category: 'status'>
	^as_state = M2UAConstants ntfyStateASInactive
    ]

    isASPending [
	<category: 'status'>
	^as_state = M2UAConstants ntfyStateASPending
    ]

    state [
	<category: 'accessing'>
	^state
    ]
]