aboutsummaryrefslogtreecommitdiffstats
path: root/Messages.st
blob: 5433cd1ee02fa7c43d4441ce80f4cd169df59583 (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
"
 (C) 2010 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/>.
"
"General IE based message handling"

Object subclass: IEBase [
    <comment: 'I am a base for IE types'>

    type [
        "Go through the elementId of the class"
        ^ self class elementId
    ]

    writeOnDirect: aMsg [
        "This should be implemented by the subclass"
        self subclassResponsibility
    ]

    writeOn: aMsg [
        aMsg putByte: self class elementId.
        self writeOnDirect: aMsg.
    ]
]

Object subclass: IEMessage [
    <category: 'osmo-messages'>
    | ies type |

    IEMessage class >> initWith: type [
        <category: 'creation'>
        ^ (self new)
            type: type;
            yourself
    ]

    IEMessage class >> findIE: data from: aIEBase on: aMsg [
        "TODO: This needs to move some basic dispatch class"
        "Find the IE that handles the type specified"
        | type |
        type := data at: 1.

        aIEBase allSubclassesDo: [:each |
            each elementId = type
                    ifTrue: [
                        | enc size |
                        size := each length: data.
                        enc := data copyFrom: 1 to: 1 + size.
                        aMsg addIe: (each parseFrom: enc).
                        ^ 1 + size
                ].
            ].

        ^ Exception signal: 'Unsupported IE type: ', type asString.
    ]

    IEMessage class >> decode: aByteArray with: aIEBase [
        | msg dat |
        msg := IEMessage initWith: (aByteArray at: 1).

        dat := aByteArray copyFrom: 2.
        [dat isEmpty not] whileTrue: [
            | consumed |
            consumed := self findIE: dat from: aIEBase on: msg.
            dat  := dat copyFrom: consumed + 1.
        ].

        ^ msg
    ]

    type: aType [
        <category: 'creation'>
        type := aType.
    ]

    type [
        ^ type
    ]

    addIe: aIe [
        <category: 'creation'>
        self ies add: aIe.
    ]

    ies [
        <category: 'access'>
        ies isNil ifTrue: [
            ies := OrderedCollection new.
        ].

        ^ ies
    ]

    findIE: type ifAbsent: block [
        "Find the IE with the type"
        self ies do: [:each |
            each type = type
                ifTrue: [
                    ^ each
            ].
        ].

        ^ block value.
    ]

    findIE: type ifPresent: block [
        "Find the IE with the type"
        self ies do: [:each |
            each type = type
                ifTrue: [
                    ^ block value: each
            ].
        ].

        ^ nil.
    ]

    writeOn: aMsg [
        <category: 'creation'>
        aMsg putByte: type.

        self ies do: [:each | each writeOn: aMsg ]
    ]
]

Object subclass: BCD [
    <category: 'osmo-message'>
    <comment: 'Class to deal with Binary Coded Decimals'>
    BCD class >> encode: aNumber [
        <category: 'access'>
        | col num |
        col := OrderedCollection new.

        num := aNumber.
        1 to: 3 do: [:each |
            col add: num \\ 10.
            num := num // 10.
        ].

        ^ col reverse asByteArray
    ]

    BCD class >> decode: aByteArray [
        <category: 'access'>
        | num cum |

        num := 0.
        cum := 1.
        aByteArray size to: 1 by: -1 do: [:each |
            | at |
            num := num + ((aByteArray at: each) * cum).
            cum := cum * 10.
        ].

        ^ num
    ]
]