smalltalk
/
osmo-st-sip
Archived
1
0
Fork 0
This repository has been archived on 2022-02-17. You can view files and clone it, but cannot push or open issues or pull requests.
osmo-st-sip/callagent/SIPCall.st

252 lines
8.0 KiB
Smalltalk

"
(C) 2011 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: SIPCall [
| initial_dialog dialog sdp_offer sdp_result invite state ua next_cseq |
<category: 'SIP-Call'>
<comment: 'I am a high level class to deal with transactions,
sessions and calls. Right now I do not support forking proxies and
will simply ignore everything but the first dialog.'>
SIPCall class >> stateInitial [ <category: 'states'> ^ #initial ]
SIPCall class >> stateInvite [ <category: 'states'> ^ #invite ]
SIPCall class >> stateSession [ <category: 'states'> ^ #session ]
SIPCall class >> stateTimeout [ <category: 'states'> ^ #timeout ]
SIPCall class >> stateCancel [ <category: 'states'> ^ #cancel ]
SIPCall class >> stateHangup [ <category: 'states'> ^ #hangup ]
SIPCall class >> stateRemoteHangup [ <category: 'states'> ^ #remoteHangup ]
SIPCall class >> stateFailed [ <category: 'states'> ^ #failed ]
LegalStates := nil.
SIPCall class >> legalStates [
<category: 'states'>
^ LegalStates ifNil: [
LegalStates := {
self stateInitial -> self stateInvite.
self stateInvite -> self stateSession.
self stateInvite -> self stateTimeout.
self stateInvite -> self stateCancel.
self stateInvite -> self stateFailed.
self stateSession -> self stateHangup.
self stateSession -> self stateRemoteHangup.
}
]
]
SIPCall class >> fromUser: aUser host: aHost port: aPort to: aTo on: aUseragent [
<category: 'creation'>
^ self new
useragent: aUseragent;
initialDialog: ((SIPDialog fromUser: aUser host: aHost port: aPort)
to: aTo; yourself);
yourself
]
state [
<category: 'states'>
^ state ifNil: [self class stateInitial]
]
moveToState: aState [
<category: 'states'>
"E.g. we get two remote hang ups as our ack has not arrived."
self state = aState ifTrue: [^true].
"Check if there is a state transition"
(self class legalStates includes: (self state -> aState)) ifFalse: [
self logError: 'SIPCall transition %1->%2 is not legal.'
% {self state. aState} area: #sip.
^ false
].
state := aState.
^ true
]
initialDialog: aDialog [
<category: 'creation'>
initial_dialog := aDialog.
initial_dialog contact: 'sip:osmo_st_sip@%1:%2'
% {ua transport address. ua transport port}.
]
useragent: aUseragent [
<category: 'creation'>
ua := aUseragent
]
createCall: aSDPOffer [
<category: 'call'>
(self moveToState: self class stateInvite) ifFalse: [
self logError: 'SIPCall failed to start.' area: #sip.
^ false
].
sdp_offer := aSDPOffer.
next_cseq := initial_dialog cseq + 1.
invite := (SIPInviteTransaction createWith: initial_dialog on: ua with: aSDPOffer cseq: initial_dialog cseq)
onTimeout: [self callTimedOut];
onSuccess: [:response :dialog | self callSuccess: response dialog: dialog];
onFailure: [:response :dialog | self callFailure: response dialog: dialog];
onNotification: [:response :dialog | self callNotification: response dialog: dialog];
start;
yourself
]
cancel [
<category: 'call'>
(self moveToState: self class stateCancel) ifTrue: [
invite cancel.
^ true
].
^ false
]
hangup [
<category: 'call'>
(self moveToState: self class stateHangup) ifTrue: [
(SIPByeTransaction createWith: dialog on: ua cseq: self nextCSeq)
onTimeout: [self hangupTimedOut];
onSuccess: [:resp :dlg | self hangupSuccess: resp dialog: dlg];
onFailure: [:resp :dlg | self hangupFailure: resp dialog: dlg];
start
].
]
check: aDialog [
<category: 'private'>
"I check if this enters a new confirmed dialog or if this is the
confirmed dialog."
"We have no confirmed dialog, accept it"
^ dialog isNil
ifTrue: [
aDialog isConfirmed ifTrue: [
dialog := aDialog.
self logError: 'SIPCall dialog is confirmed now.' area: #sip.
].
true]
ifFalse: [
"We could fork things here. For multi party call"
dialog to_tag = aDialog to_tag].
]
callTimedOut [
<category: 'call-result'>
self logError: 'SIPCall timed-out.' area: #sip.
(self moveToState: self class stateTimeout) ifFalse: [
^ self logError: 'SIPCall failed to move to timeout.' area: #sip.
].
]
callSuccess: aResponse dialog: aDialog [
<category: 'call-result'>
(self check: aDialog) ifFalse: [
self logError: 'SIPCall can only have one session. Ignoring.' area: #sip.
^ false
].
(self moveToState: self class stateSession) ifTrue: [
self logError: 'SIPCall session etsablished.' area: #sip.
sdp_result := aResponse sdp.
self newSession.
^ true
].
^ false
]
callFailure: aResponse dialog: aDialog [
<category: 'call-result'>
(self check: aDialog) ifFalse: [
self logError: 'SIPCall can only have one session. Ignoring failure.' area: #sip.
^ false
].
(self moveToState: self class stateFailed) ifTrue: [
self logError: 'SIPCall Failure.' area: #sip.
self newFailed.
].
]
callNotification: aResponse dialog: aDialog [
<category: 'call-result'>
(self check: aDialog) ifFalse: [
self logError: 'SIPCall can only have one session. Ignoring notification.' area: #sip.
^ false
].
self logError: 'SIPCall Notification.' area: #sip.
]
hangupTimedOut [
<category: 'hangup-result'>
self logError: 'Hang-Up timedout.' area: #sip.
]
hangupSuccess: aResponse dialog: aDialog [
<category: 'hangup-result'>
(self check: aDialog) ifFalse: [
self logError: 'SIPCall can only have one session. Ignoring failure.' area: #sip.
^ false
].
self logError: 'Hang-Up success.' area: #sip.
]
hangupFailure: aResponse dialog: aDialog [
<category: 'hangup-result'>
(self check: aDialog) ifFalse: [
self logError: 'SIPCall can only have one session. Ignoring failure.' area: #sip.
^ false
].
self logError: 'Hang-Up failure.' area: #sip.
]
terminate [
<category: 'public'>
"I try to finish up things."
self state = self class stateInvite ifTrue: [self cancel].
self state = self class stateSession ifTrue: [self hangup].
]
newSession [
<category: 'callback'>
]
newFailed [
<category: 'callback'>
]
nextCSeq [
| res |
<category: 'accessing'>
res := next_cseq.
next_cseq := next_cseq + 1.
^ res
]
]