2010-12-14 01:47:38 +00:00
|
|
|
"
|
2012-08-08 16:04:27 +00:00
|
|
|
(C) 2010-2012 by Holger Hans Peter Freyther
|
2010-12-14 01:47:38 +00:00
|
|
|
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/>.
|
|
|
|
"
|
2010-09-19 21:51:26 +00:00
|
|
|
|
2012-08-09 00:15:23 +00:00
|
|
|
PackageLoader
|
|
|
|
fileInPackage: 'Sockets';
|
|
|
|
fileInPackage: 'OsmoNetwork'.
|
2010-09-19 21:51:26 +00:00
|
|
|
|
2011-06-10 19:56:01 +00:00
|
|
|
Object subclass: MGCPCallAgentBase [
|
2012-08-09 00:15:23 +00:00
|
|
|
| net trunks sem addr port |
|
2013-03-30 15:46:01 +00:00
|
|
|
<category: 'OsmoMGCP-Callagent'>
|
2011-06-10 19:56:01 +00:00
|
|
|
<comment: 'I am responsible for the networking'>
|
2010-09-19 21:51:26 +00:00
|
|
|
|
2011-06-10 19:56:01 +00:00
|
|
|
MGCPCallAgentBase class >> startOn: anAddress [
|
2011-06-23 14:45:32 +00:00
|
|
|
<category: 'creation'>
|
2010-09-19 21:51:26 +00:00
|
|
|
^ (self new)
|
2011-06-23 14:45:32 +00:00
|
|
|
initialize: anAddress port: 2727;
|
|
|
|
yourself.
|
|
|
|
]
|
|
|
|
|
|
|
|
MGCPCallAgentBase class >> startOn: anAddress port: aPort [
|
|
|
|
<category: 'creation'>
|
|
|
|
^ self new
|
|
|
|
initialize: anAddress port: aPort;
|
2010-09-19 21:51:26 +00:00
|
|
|
yourself.
|
|
|
|
]
|
|
|
|
|
2011-06-23 14:45:32 +00:00
|
|
|
initialize: anAddress port: aPort [
|
2011-06-23 14:44:28 +00:00
|
|
|
<category: 'creation'>
|
2011-06-23 17:52:17 +00:00
|
|
|
sem := Semaphore forMutualExclusion.
|
2011-06-10 19:56:01 +00:00
|
|
|
trunks := OrderedCollection new.
|
2011-06-29 08:08:14 +00:00
|
|
|
addr := anAddress.
|
|
|
|
port := aPort.
|
2012-08-09 00:15:23 +00:00
|
|
|
|
|
|
|
net := Osmo.OsmoUDPSocket new
|
|
|
|
name: 'MGCPCallAgent';
|
|
|
|
onData: [:data | OsmoDispatcher dispatchBlock: [self handleData: data]];
|
|
|
|
yourself.
|
2010-09-19 21:51:26 +00:00
|
|
|
]
|
|
|
|
|
2011-06-10 19:56:01 +00:00
|
|
|
addTrunk: aTrunk [
|
2010-09-19 21:51:26 +00:00
|
|
|
<category: 'setup'>
|
2011-06-10 19:56:01 +00:00
|
|
|
sem critical: [
|
|
|
|
trunks add: aTrunk.
|
|
|
|
].
|
2010-09-19 21:51:26 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
handleData: aData [
|
|
|
|
<category: 'handling'>
|
2011-06-11 11:34:09 +00:00
|
|
|
^ self subclassResponsibility
|
2010-09-19 21:51:26 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
start [
|
2012-08-09 00:15:23 +00:00
|
|
|
| socket |
|
2010-09-19 21:51:26 +00:00
|
|
|
<category: 'handling'>
|
|
|
|
|
2011-06-29 08:08:14 +00:00
|
|
|
self stop.
|
2011-07-20 16:31:23 +00:00
|
|
|
socket := (Sockets.DatagramSocket local: addr port: port)
|
|
|
|
bufferSize: 2048;
|
|
|
|
yourself.
|
2011-06-29 08:08:14 +00:00
|
|
|
|
2012-08-09 00:15:23 +00:00
|
|
|
net start: socket.
|
2010-09-19 21:51:26 +00:00
|
|
|
]
|
2011-06-11 11:34:09 +00:00
|
|
|
|
2011-06-23 12:23:20 +00:00
|
|
|
stop [
|
2012-08-09 00:15:23 +00:00
|
|
|
<category: 'processing'>
|
|
|
|
net stop
|
2011-06-23 12:23:20 +00:00
|
|
|
]
|
|
|
|
|
2011-06-11 11:34:09 +00:00
|
|
|
queueData: aDatagram [
|
2012-08-09 00:15:23 +00:00
|
|
|
<category: 'sending'>
|
|
|
|
net queueData: aDatagram.
|
2011-06-11 11:34:09 +00:00
|
|
|
]
|
2010-09-19 21:51:26 +00:00
|
|
|
]
|
2011-06-10 19:56:01 +00:00
|
|
|
|
|
|
|
MGCPCallAgentBase subclass: MGCPCallAgent [
|
2011-06-24 19:04:54 +00:00
|
|
|
| transactions parser |
|
2013-03-30 15:46:01 +00:00
|
|
|
<category: 'OsmoMGCP-Callagent'>
|
2011-06-10 19:56:01 +00:00
|
|
|
<comment: 'I deal with transactions and timeouts'>
|
|
|
|
|
2011-06-23 14:45:32 +00:00
|
|
|
initialize: anAddress port: aPort [
|
2011-06-10 19:56:01 +00:00
|
|
|
<category: 'private'>
|
|
|
|
|
2011-06-23 14:45:32 +00:00
|
|
|
super initialize: anAddress port: aPort.
|
2011-06-10 19:56:01 +00:00
|
|
|
transactions := OrderedCollection new.
|
|
|
|
]
|
|
|
|
|
|
|
|
addTransaction: aTransaction [
|
|
|
|
<category: 'private'>
|
|
|
|
sem critical: [
|
|
|
|
aTransaction transactionId: self generateTransactionId.
|
|
|
|
transactions add: aTransaction.
|
|
|
|
]
|
|
|
|
]
|
|
|
|
|
2011-06-11 11:34:09 +00:00
|
|
|
removeTransactionInternal: aTransaction [
|
|
|
|
<category: 'private'>
|
2011-06-23 17:52:17 +00:00
|
|
|
sem critical: [transactions remove: aTransaction].
|
2011-06-10 19:56:01 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
transactionIdIsUsed: anId [
|
|
|
|
<category: 'private'>
|
|
|
|
^ transactions anySatisfy: [:each | each transactionId = anId]
|
|
|
|
]
|
|
|
|
|
|
|
|
generateTransactionId [
|
|
|
|
| ran |
|
|
|
|
<category: 'private'>
|
|
|
|
"I need to generate a transaction identifier. I assume proper locking"
|
|
|
|
|
|
|
|
"Check if the below could ever succeed"
|
|
|
|
(transactions size - 500000) > (999999999 - 100000000) ifTrue: [
|
|
|
|
^ self error: 'No free transaction ID.'.
|
|
|
|
].
|
|
|
|
|
|
|
|
|
|
|
|
[
|
|
|
|
ran := Random between: 100000000 and: 999999999.
|
|
|
|
self transactionIdIsUsed: ran
|
|
|
|
] whileTrue.
|
|
|
|
|
|
|
|
^ ran.
|
|
|
|
]
|
2011-06-11 11:34:09 +00:00
|
|
|
|
2011-06-24 19:04:54 +00:00
|
|
|
parser [
|
|
|
|
<category: 'private'>
|
|
|
|
^ parser ifNil: [parser := MGCPParser new]
|
|
|
|
]
|
|
|
|
|
2011-06-11 11:34:09 +00:00
|
|
|
handleData: aData [
|
|
|
|
[
|
2011-06-23 17:52:17 +00:00
|
|
|
| res data id trans |
|
2011-06-11 11:34:09 +00:00
|
|
|
data := aData data copyFrom: 1 to: aData size.
|
2011-07-20 16:31:23 +00:00
|
|
|
res := self parser parse: data asString onError: [
|
2011-09-22 18:06:41 +00:00
|
|
|
^ self error: 'Parse error ', data asByteArray printString.
|
2011-07-20 16:31:23 +00:00
|
|
|
].
|
|
|
|
|
2011-06-24 16:57:53 +00:00
|
|
|
id := res transactionId asInteger.
|
2011-06-11 11:34:09 +00:00
|
|
|
|
2011-06-23 17:52:17 +00:00
|
|
|
trans := sem critical: [transactions copy].
|
|
|
|
trans do: [:each |
|
2011-06-11 11:34:09 +00:00
|
|
|
each transactionId = id ifTrue: [
|
|
|
|
each response: res.
|
|
|
|
]
|
|
|
|
]
|
|
|
|
] on: Error do: [:e |
|
|
|
|
e logException: 'Incoming data ', e tag area: #mgcp.
|
|
|
|
]
|
|
|
|
]
|
2011-06-10 19:56:01 +00:00
|
|
|
]
|