summaryrefslogtreecommitdiffstats
path: root/callagent/useragent/SIPUserAgentBase.st
blob: 8eeae87cad2b100c60a8263b2d6eefa7d6e0fd53 (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
"
 (C) 2011-2014 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: SIPUserAgentBase [
    | transport name |
    <category: 'OsmoSIP-Callagent'>
    <comment: 'I am a user agent base'>

    SIPUserAgentBase class >> createOn: aTransport [
        <category: 'factory'>
        ^ self new
            transport: aTransport;
            yourself
    ]

    SIPUserAgentBase class >> branchStart [
        <category: 'ids'>
        "magic marker..."
        ^ 'z9hG4bK'
    ]

    SIPUserAgentBase class >> generateBranch [
        | data |
        data := '<1p>,<2p>' expandMacrosWithArguments:
                    {DateTime now asUTC asSeconds. Random between: 0 and: 99999}.
        ^ self branchStart, (SIPBase64 encode: data).
    ]

    SIPUserAgentBase class >> generateCSeq [
        <category: 'helper'>
        "CSeq must be < 2^31 but Random only allows 2^29"
        ^ Random between: 1 and: (2 raisedTo: 29).
    ]


    injectDefaults: aRequest [
        aRequest addParameter: 'Max-Forwards' value: '70'.
        aRequest addParameter: 'User-Agent' value: self name.
    ]

    generateVia: aBranch [
        <category: 'ids'>
        ^ (WriteStream on: String new)
                nextPutAll: 'SIP/2.0/';
                nextPutAll: transport type;
                nextPutAll: ' ';
                nextPutAll: transport address printString;
                nextPutAll: ':';
                nextPutAll: transport port asString;
                nextPutAll: ';branch=';
                nextPutAll: aBranch;
                nextPutAll: ';rport';
                contents.
    ]

    name [
        <category: 'accessing'>
        ^ name ifNil: ['OsmoST-SIP 0.34']
    ]

    transport: aTransport [
        transport ifNotNil: [transport handler: nil].

        transport := aTransport.
        transport handler: self.
    ]

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

    queueData: aData dialog: aDialog [
        | datagram |
        <category: 'output'>

        datagram := Sockets.Datagram
                                data: aData
                                address: (Sockets.SocketAddress
                                            byName: aDialog destIp)
                                port: aDialog destPort.
        transport queueDatagram: datagram.
    ]
]