aboutsummaryrefslogtreecommitdiffstats
path: root/TestPhone.st
blob: a9e21fb17db5b8c647be7d8849059311fbf0b640 (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
PackageLoader fileInPackage: 'OsmoNetwork'.

Object subclass: IPAConnection [
    | socket demuxer queue muxer dispatcher sccp ipa sem |
    IPAConnection class >> initWith: anAddr port: aPort token: aToken [
        ^ (self new)
            socket: (Sockets.Socket remote: anAddr port: aPort);
            setup: aToken;
            yourself
    ]

    socket: aSocket [
        socket := aSocket.
    ]

    setup: aToken [
        sem := Semaphore forMutualExclusion.

        demuxer := Osmo.IPADemuxer initOn: socket.
        queue := SharedQueue new.
        muxer := Osmo.IPAMuxer initOn: queue.

        dispatcher := Osmo.IPADispatcher new.
        dispatcher initialize.

        sccp := SCCPHandler new.
        sccp registerOn: dispatcher.
        sccp connection: self.

        ipa := Osmo.IPAProtoHandler new.
        ipa registerOn: dispatcher.
        ipa muxer: muxer.
        ipa token: aToken
    ]

    serve [
        [true] whileTrue: [
            [
                | data |
                data := demuxer next.
                dispatcher dispatch: data first with: data second.

                self drainSendQueue.
            ] on: SystemExceptions.EndOfStream do: [:e | ^ false ]
        ] 
    ]

    drainSendQueue [
        sem critical: [
            [queue isEmpty] whileFalse: [
                | msg |
                msg := queue next.
                socket nextPutAllFlush: msg.
            ]
        ]
    ]

    send: aMsg with: aType [
        muxer nextPut: aMsg with: aType.
        self drainSendQueue.
    ]

    sccpHandler [
        ^ sccp
    ]
]

Object subclass: IPAConfig [
    | socket addr port token connection sem |

    addr: anAddr port: aPort [
        addr := anAddr.
        port := aPort.
    ]

    token: aToken [
        token := aToken.
    ]

    connect [
        sem := Semaphore new.
        connection := IPAConnection initWith: addr port: port token: token.
    ]

    connection [
        ^ connection
    ] 

    serve [
        [
            [
                connection serve.
                'Connection disconnected' printNl.
            ] ensure: [
                connection := nil.
                sem signal.
            ]
        ] fork.
    ]

    isConnected [
        ^ connection isNil not
    ]

    semaphore [ ^ sem ]

    doLU: aPhone [
        ^ LUProcedure initWith: (connection sccpHandler) phone: aPhone.
    ]
    sendLU: aPhone [
        (self doLU: aPhone) execute.
    ]

    doCallNumber: aPhone nr: aNr [
        ^ CallProcedure initWith: (connection sccpHandler) phone: aPhone nr: aNr.
    ]

    callNumber: aPhone nr: aNumber [
        ^ (self doCallNumber: aPhone nr: aNumber) execute
    ]
]

Object subclass: PhoneConfig [
    | imsi auKey |

    <comment: 'I am the config of a phone. I do have an IMSI and such.'>

    PhoneConfig class >> initWith: aImsi auKey: anAuKey [
        ^ self new
            imsi: aImsi;
            auKey: anAuKey;
            yourself
    ]

    imsi: aImsi [
        imsi := aImsi.
    ]

    imsi [ ^ imsi ]
    auKey [ ^ auKey ]
    auKey: anAuKey [
        auKey := anAuKey.
    ]

    auKeyByteArray [
        ^ auKey isString
            ifTrue:  [
                | array |
                array := OrderedCollection new.
                1 to: auKey size by: 2 do: [:each |
                    array add: (Number readFrom:
                                    (auKey copyFrom: each to: each + 1) readStream
                                    radix: 16)
                ].

                array asByteArray.
            ]
            ifFalse: [auKey].
    ]
]